From be482aecc44d81583e1cf07646c4417e6a87f6c6 Mon Sep 17 00:00:00 2001 From: jsvisa Date: Fri, 27 Sep 2024 11:00:09 +0000 Subject: [PATCH] live: add testcase Signed-off-by: jsvisa --- eth/tracers/live/filter_api_test.go | 89 +++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 eth/tracers/live/filter_api_test.go diff --git a/eth/tracers/live/filter_api_test.go b/eth/tracers/live/filter_api_test.go new file mode 100644 index 0000000000..6f5c403242 --- /dev/null +++ b/eth/tracers/live/filter_api_test.go @@ -0,0 +1,89 @@ +package live + +import ( + "testing" +) + +func testGen(blknum uint64) ([]interface{}, error) { + traces := make([]interface{}, 5) + for i := range traces { + traces[i] = int(blknum*5) + i + } + return traces, nil +} + +func TestExportLimitedTraces(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + fromBlock uint64 + toBlock uint64 + count uint64 + after uint64 + want []int + }{ + { + name: "Basic Test", + fromBlock: 0, + toBlock: 2, + count: 10, + after: 0, + want: []int{ + 0, 1, 2, 3, 4, // the first block + 5, 6, 7, 8, 9, // the second block + }, + }, + { + name: "With After", + fromBlock: 0, + toBlock: 0, + count: 3, + after: 2, + want: []int{ + 2, // the first block left + 3, 4, // the second block + }, + }, + { + name: "With After", + fromBlock: 0, + toBlock: 0, + count: 3, + after: 2, + want: []int{ + 2, // the first block left + 3, 4, // the second block + }, + }, + + { + name: "Not Enough Traces", + fromBlock: 0, + toBlock: 0, + count: 10, + after: 5, + want: nil, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + have, err := exportLimitedTraces(testGen, tt.fromBlock, tt.toBlock, tt.count, tt.after) + if err != nil { + t.Error(err) + return + } + + if hl, wl := len(have), len(tt.want); hl != wl { + t.Errorf("test(%s) exported len not equal, have %d, want %d", tt.name, hl, wl) + return + } + for i := range have { + if h, w := have[i].(int), tt.want[i]; h != w { + t.Errorf("test(%s) result not equal have %v want %v at index %d", tt.name, h, w, i) + } + } + }) + } +}