From e7f20f8f4061e50f16b8aef5856963d547a4fd9b Mon Sep 17 00:00:00 2001 From: welkin22 Date: Thu, 31 Oct 2024 22:01:56 +0800 Subject: [PATCH] fix unit test --- core/txdag_reader.go | 8 +-- core/txdag_reader_test.go | 97 +++++++++++++++++++++++++++--------- core/txdag_test_case.csv | 100 -------------------------------------- 3 files changed, 77 insertions(+), 128 deletions(-) delete mode 100644 core/txdag_test_case.csv diff --git a/core/txdag_reader.go b/core/txdag_reader.go index aa67909aa..957f9d60b 100644 --- a/core/txdag_reader.go +++ b/core/txdag_reader.go @@ -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 { @@ -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() } @@ -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) @@ -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) diff --git a/core/txdag_reader_test.go b/core/txdag_reader_test.go index 660b72955..66e66872e 100644 --- a/core/txdag_reader_test.go +++ b/core/txdag_reader_test.go @@ -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) @@ -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) diff --git a/core/txdag_test_case.csv b/core/txdag_test_case.csv deleted file mode 100644 index 381cfdfac..000000000 --- a/core/txdag_test_case.csv +++ /dev/null @@ -1,100 +0,0 @@ -1,01c4c3c2c002 -2,01c4c3c2c002 -3,01c4c3c2c002 -4,01c4c3c2c002 -5,01c4c3c2c002 -6,01c4c3c2c002 -7,01c4c3c2c002 -8,01c4c3c2c002 -9,01c4c3c2c002 -10,01c4c3c2c002 -11,01c4c3c2c002 -12,01c4c3c2c002 -13,01c4c3c2c002 -14,01c4c3c2c002 -15,01c4c3c2c002 -16,01c4c3c2c002 -17,01c4c3c2c002 -18,01c4c3c2c002 -19,01c4c3c2c002 -20,01dbdac2c002c1c0c2c101c2c102c2c103c2c104c2c105c2c106c2c107 -21,01c4c3c2c002 -22,01c4c3c2c002 -23,01c4c3c2c002 -24,01c4c3c2c002 -25,01c4c3c2c002 -26,01c4c3c2c002 -27,01c4c3c2c002 -28,01c4c3c2c002 -29,01c4c3c2c002 -30,01c4c3c2c002 -31,01c4c3c2c002 -32,01c4c3c2c002 -33,01c4c3c2c002 -34,01c4c3c2c002 -35,01c4c3c2c002 -36,01c4c3c2c002 -37,01c4c3c2c002 -38,01c4c3c2c002 -39,01c4c3c2c002 -40,01c4c3c2c002 -41,01c4c3c2c002 -42,01c4c3c2c002 -43,01c4c3c2c002 -44,01c4c3c2c002 -45,01c4c3c2c002 -46,01c4c3c2c002 -47,01c4c3c2c002 -48,01c4c3c2c002 -49,01c4c3c2c002 -50,01c4c3c2c002 -51,01c4c3c2c002 -52,01c4c3c2c002 -53,01c4c3c2c002 -54,01c4c3c2c002 -55,01c4c3c2c002 -56,01c4c3c2c002 -57,01c4c3c2c002 -58,01c4c3c2c002 -59,01c4c3c2c002 -60,01c4c3c2c002 -61,01c4c3c2c002 -62,01c4c3c2c002 -63,01c4c3c2c002 -64,01c4c3c2c002 -65,01c4c3c2c002 -66,01c4c3c2c002 -67,01c4c3c2c002 -68,01c4c3c2c002 -69,01c4c3c2c002 -70,01c4c3c2c002 -71,01c4c3c2c002 -72,01c4c3c2c002 -73,01c4c3c2c002 -74,01c4c3c2c002 -75,01c4c3c2c002 -76,01c4c3c2c002 -77,01c4c3c2c002 -78,01c4c3c2c002 -79,01c4c3c2c002 -80,01c4c3c2c002 -81,01c4c3c2c002 -82,01c4c3c2c002 -83,01c4c3c2c002 -84,01c4c3c2c002 -85,01c4c3c2c002 -86,01c4c3c2c002 -87,01c4c3c2c002 -88,01c4c3c2c002 -89,01c4c3c2c002 -90,01c4c3c2c002 -91,01c4c3c2c002 -92,01c4c3c2c002 -93,01c4c3c2c002 -94,01c4c3c2c002 -95,01c4c3c2c002 -96,01c4c3c2c002 -97,01c4c3c2c002 -98,01c4c3c2c002 -99,01c4c3c2c002 -100,01c4c3c2c002