Skip to content

Commit

Permalink
fix unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
welkin22 committed Oct 31, 2024
1 parent f197994 commit e7f20f8
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 128 deletions.
8 changes: 4 additions & 4 deletions core/txdag_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (t *TxDAGFileReader) initAndStartReading(startBlockNum uint64) error {
if startBlockNum > 0 {
//We move the scanner to the position of startBlockNum-1 so that we can start reading data from startBlockNum next.
startBlockNum = startBlockNum - 1
for t.scanner.Scan() {
for t.scanner != nil && t.scanner.Scan() {
text := t.scanner.Text()
blockNum, err := readTxDAGBlockNumFromLine(text)
if err != nil {
Expand All @@ -104,7 +104,7 @@ func (t *TxDAGFileReader) initAndStartReading(startBlockNum uint64) error {
t.latest = blockNum
break
}
if t.scanner.Err() != nil {
if t.scanner != nil && t.scanner.Err() != nil {
log.Error("TxDAG reader init, scan TxDAG file got err", "err", t.scanner.Err(), "startBlockNum", startBlockNum, "latest", t.latest)
return t.scanner.Err()
}
Expand All @@ -118,7 +118,7 @@ func (t *TxDAGFileReader) initAndStartReading(startBlockNum uint64) error {
func (t *TxDAGFileReader) loopReadDAGIntoChan() {
start := time.Now()

for t.scanner.Scan() {
for t.scanner != nil && t.scanner.Scan() {
select {
case <-t.closeChan:
close(t.dagChan)
Expand All @@ -143,7 +143,7 @@ func (t *TxDAGFileReader) loopReadDAGIntoChan() {
}
}
}
if t.scanner.Err() != nil {
if t.scanner != nil && t.scanner.Err() != nil {
log.Error("scan TxDAG file got err", "latest", t.latest, "err", t.scanner.Err())
} else {
log.Info("TxDAG reader done. Exiting...", "latest", t.latest)
Expand Down
97 changes: 73 additions & 24 deletions core/txdag_reader_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,46 @@
package core

import (
"os"
"path/filepath"
"testing"
"time"

"github.com/ethereum/go-ethereum/core/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestNewTxDAGFileReader(t *testing.T) {
reader, err := NewTxDAGFileReader("./txdag_test_case.csv")
path := filepath.Join(os.TempDir(), "test.csv")
defer func() {
os.Remove(path)
}()
except := map[uint64]types.TxDAG{
0: types.NewEmptyTxDAG(),
1: makeEmptyPlainTxDAG(1),
2: makeEmptyPlainTxDAG(2, types.NonDependentRelFlag),
3: types.NewEmptyTxDAG(),
4: makeEmptyPlainTxDAG(4, types.NonDependentRelFlag, types.ExcludedTxFlag),
5: makeEmptyPlainTxDAG(5, types.NonDependentRelFlag, types.ExcludedTxFlag),
}
writeFile, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
require.NoError(t, err)
for i := uint64(0); i < 6; i++ {
require.NoError(t, writeTxDAGToFile(writeFile, TxDAGOutputItem{blockNumber: i, txDAG: except[i]}))
}
writeFile.Sync()
writeFile.Close()

reader, err := NewTxDAGFileReader(path)
if err != nil {
t.Error("newReaderErr", "err", err)
return
}
err = reader.InitAndStartReadingLock(10)
err = reader.InitAndStartReadingLock(2)
if err != nil {
t.Error("newReaderErr", "err", err)
return
}
//Check the initialization status
assert.Equal(t, true, reader.isInit)
Expand All @@ -23,51 +49,74 @@ func TestNewTxDAGFileReader(t *testing.T) {
for reader.chanFirstBlockNumber == -1 {
time.Sleep(10 * time.Millisecond)
}
//The starting point is 10, so 9 should not exist, 10 should exist, and txCount==1
dag9 := reader.TxDAG(9)
assert.Nil(t, dag9)
dag10 := reader.TxDAG(10)
assert.NotNil(t, dag10)
assert.Equal(t, 1, dag10.TxCount())
//Waiting to process to 20
for reader.latest < 20 {
//The starting point is 2, so 1 should not exist, 2 should exist, and txCount==2
dag1 := reader.TxDAG(1)
assert.Nil(t, dag1)
dag2 := reader.TxDAG(2)
assert.NotNil(t, dag2)
assert.Equal(t, 2, dag2.TxCount())
//Waiting to process to 5
for reader.latest < 5 {
time.Sleep(10 * time.Millisecond)
}
//There are 9 transactions in 20
dag20 := reader.TxDAG(20)
assert.NotNil(t, dag20)
assert.Equal(t, 9, dag20.TxCount())
//Already read 20, data less than 20 cannot be read.
dag11 := reader.TxDAG(11)
assert.Nil(t, dag11)
err = reader.Reset(10)
dag5 := reader.TxDAG(5)
assert.NotNil(t, dag5)
assert.Equal(t, 5, dag5.TxCount())
//Already read 5, data less than 5 cannot be read.
dag3 := reader.TxDAG(3)
assert.Nil(t, dag3)
err = reader.Reset(2)
if err != nil {
t.Error("resetErr", "err", err)
return
}
//Check the initialization status again after reset
assert.Equal(t, true, reader.isInit)
assert.NotNil(t, reader.scanner)
//Waiting for 11 to be read
for reader.latest < 11 {
//Waiting for 3 to be read
for reader.latest < 3 {
time.Sleep(10 * time.Millisecond)
}
//11 should no longer be nil, because after reset, we haven't read it yet.
dag11 = reader.TxDAG(11)
assert.NotNil(t, dag11)
assert.Equal(t, 1, dag11.TxCount())
dag3 = reader.TxDAG(3)
assert.NotNil(t, dag3)
assert.Equal(t, 0, dag3.TxCount())
//1000 should not exist
dag1000 := reader.TxDAG(1000)
assert.Nil(t, dag1000)
}

func TestTxDAGFileReader_Close(t *testing.T) {
reader, err := NewTxDAGFileReader("./txdag_test_case.csv")
path := filepath.Join(os.TempDir(), "test.csv")
defer func() {
os.Remove(path)
}()
except := map[uint64]types.TxDAG{
0: types.NewEmptyTxDAG(),
1: makeEmptyPlainTxDAG(1),
2: makeEmptyPlainTxDAG(2, types.NonDependentRelFlag),
3: types.NewEmptyTxDAG(),
4: makeEmptyPlainTxDAG(4, types.NonDependentRelFlag, types.ExcludedTxFlag),
5: makeEmptyPlainTxDAG(5, types.NonDependentRelFlag, types.ExcludedTxFlag),
}
writeFile, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
require.NoError(t, err)
for i := uint64(0); i < 6; i++ {
require.NoError(t, writeTxDAGToFile(writeFile, TxDAGOutputItem{blockNumber: i, txDAG: except[i]}))
}
writeFile.Sync()
writeFile.Close()

reader, err := NewTxDAGFileReader(path)
if err != nil {
t.Error("newReaderErr", "err", err)
return
}
err = reader.InitAndStartReadingLock(10)
err = reader.InitAndStartReadingLock(2)
if err != nil {
t.Error("newReaderErr", "err", err)
return
}
assert.Equal(t, true, reader.isInit)
assert.NotNil(t, reader.scanner)
Expand Down
100 changes: 0 additions & 100 deletions core/txdag_test_case.csv

This file was deleted.

0 comments on commit e7f20f8

Please sign in to comment.