TEST: Testing single block

This commit is contained in:
David Zhou 2025-05-13 12:46:52 -04:00
parent 04ffb08423
commit dbee17b27d
2 changed files with 15 additions and 38 deletions

View file

@ -195,6 +195,7 @@ type Firehose struct {
blockPrintQueue chan *blockPrintJob blockPrintQueue chan *blockPrintJob
blockOutputQueue chan *blockOutput blockOutputQueue chan *blockOutput
flushDone sync.WaitGroup flushDone sync.WaitGroup
flushOutputDone sync.WaitGroup
closeOnce sync.Once closeOnce sync.Once
} }
@ -270,24 +271,8 @@ func NewFirehose(config *FirehoseConfig) *Firehose {
} }
// Output channel to order the flushing linearly // Output channel to order the flushing linearly
go func() { firehose.flushOutputDone.Add(1)
expected := uint64(1000) // TODO: We will need to determine at which block it starts
buffer := make(map[uint64][]byte)
for result := range firehose.blockOutputQueue {
buffer[result.blockNum] = result.data
for {
data, ok := buffer[expected]
if !ok {
break
}
firehose.flushToFirehose(data)
delete(buffer, expected)
expected++ // TODO: Assuming block numbers are linear
}
}
}()
} }
return firehose return firehose
@ -2878,6 +2863,9 @@ func (f *Firehose) CloseBlockPrintQueue() {
f.closeOnce.Do(func() { f.closeOnce.Do(func() {
close(f.blockPrintQueue) close(f.blockPrintQueue)
f.flushDone.Wait() f.flushDone.Wait()
close(f.blockOutputQueue)
f.flushOutputDone.Wait()
}) })
} }
} }

View file

@ -10,7 +10,6 @@ import (
"strconv" "strconv"
"strings" "strings"
"testing" "testing"
"time"
) )
func TestFirehose_BlockPrintsToFirehose_SingleBlock(t *testing.T) { func TestFirehose_BlockPrintsToFirehose_SingleBlock(t *testing.T) {
@ -25,7 +24,7 @@ func TestFirehose_BlockPrintsToFirehose_SingleBlock(t *testing.T) {
f.OnBlockchainInit(params.AllEthashProtocolChanges) f.OnBlockchainInit(params.AllEthashProtocolChanges)
blockNumbers := []uint64{123, 124, 125} blockNumbers := []uint64{0}
for i, blockNum := range blockNumbers { for i, blockNum := range blockNumbers {
f.OnBlockStart(blockEvent(blockNum)) f.OnBlockStart(blockEvent(blockNum))
@ -41,30 +40,22 @@ func TestFirehose_BlockPrintsToFirehose_SingleBlock(t *testing.T) {
f.OnClose() f.OnClose()
output := f.InternalTestingBuffer().String() lines := strings.Split(strings.TrimSpace(f.InternalTestingBuffer().String()), "\n")
require.Len(t, lines, 2)
outNumber := make([]string, 0) require.Equal(t, "FIRE INIT 3.0 geth 1.15.10", lines[0])
for i, line := range strings.Split(output, "\n") {
if i == 0 {
require.Equal(t, "FIRE INIT 3.0 geth 1.15.10", line)
continue
}
fields := strings.SplitN(line, " ", 4) fields := strings.SplitN(lines[1], " ", 4)
if len(fields) >= 3 { require.GreaterOrEqual(t, len(fields), 3)
require.Equal(t, "FIRE", fields[0]) require.Equal(t, "FIRE", fields[0])
require.Equal(t, "BLOCK", fields[1]) require.Equal(t, "BLOCK", fields[1])
outNumber = append(outNumber, fields[2]) require.Equal(t, "0", fields[2])
}
}
require.Equal(t, []string{"123", "124", "125"}, outNumber)
} }
func TestFirehose_BlocksPrintToFirehose_MultipleBlocksInOrder(t *testing.T) { func TestFirehose_BlocksPrintToFirehose_MultipleBlocksInOrder(t *testing.T) {
const blockCount = 10 const blockCount = 10
const baseBlockNum = 1000 const baseBlockNum = 0
f := NewFirehose(&FirehoseConfig{ f := NewFirehose(&FirehoseConfig{
ConcurrentBlockFlushing: true, ConcurrentBlockFlushing: true,
@ -93,8 +84,6 @@ func TestFirehose_BlocksPrintToFirehose_MultipleBlocksInOrder(t *testing.T) {
f.OnBlockEnd(nil) f.OnBlockEnd(nil)
} }
time.Sleep(5 * time.Second)
f.OnClose() f.OnClose()
output := f.InternalTestingBuffer().String() output := f.InternalTestingBuffer().String()