eth/filters: more testcase

Signed-off-by: jsvisa <delweng@gmail.com>
This commit is contained in:
jsvisa 2023-06-27 00:47:59 +08:00
parent a374b1b6ea
commit 223f05ff76

View file

@ -781,35 +781,60 @@ func TestLogsSubscription(t *testing.T) {
{Topics: []common.Hash{topic, common.HexToHash(addr.Hex()), i2h(4)}, Data: i2h(14).Bytes(), BlockNumber: 4}, {Topics: []common.Hash{topic, common.HexToHash(addr.Hex()), i2h(4)}, Data: i2h(14).Bytes(), BlockNumber: 4},
} }
pendingBlockNumber := big.NewInt(rpc.PendingBlockNumber.Int64())
testCases := []struct { testCases := []struct {
crit FilterCriteria crit FilterCriteria
expected []*types.Log expected []*types.Log
notifier *mockNotifier notifier *mockNotifier
sub *rpc.Subscription sub *rpc.Subscription
err chan error expectErr error
err chan error
}{ }{
// from 0 to latest // from 0 to latest
{ {
FilterCriteria{FromBlock: big.NewInt(0), ToBlock: big.NewInt(5)}, FilterCriteria{FromBlock: big.NewInt(0)},
allLogs, newMockNotifier(), &rpc.Subscription{ID: rpc.NewID()}, nil, allLogs, newMockNotifier(), &rpc.Subscription{ID: rpc.NewID()}, nil, nil,
}, },
// from 1 to latest // from 1 to latest
{ {
FilterCriteria{FromBlock: big.NewInt(1), ToBlock: pendingBlockNumber}, FilterCriteria{FromBlock: big.NewInt(1), ToBlock: big.NewInt(rpc.LatestBlockNumber.Int64())},
allLogs, newMockNotifier(), &rpc.Subscription{ID: rpc.NewID()}, nil, allLogs, newMockNotifier(), &rpc.Subscription{ID: rpc.NewID()}, nil, nil,
},
// from 2 to latest
{
FilterCriteria{FromBlock: big.NewInt(2)},
allLogs[1:], newMockNotifier(), &rpc.Subscription{ID: rpc.NewID()}, nil, nil,
},
// from latest to latest
{
FilterCriteria{},
nil, newMockNotifier(), &rpc.Subscription{ID: rpc.NewID()}, nil, nil,
},
// from 1 to 3
{
FilterCriteria{FromBlock: big.NewInt(1), ToBlock: big.NewInt(3)},
nil, newMockNotifier(), &rpc.Subscription{ID: rpc.NewID()}, errInvalidToBlock, nil,
},
// from -101 to latest
{
FilterCriteria{FromBlock: big.NewInt(-101)},
nil, newMockNotifier(), &rpc.Subscription{ID: rpc.NewID()}, errInvalidFromBlock, nil,
}, },
// // from 1 to 3
// {
// FilterCriteria{FromBlock: big.NewInt(1), ToBlock: big.NewInt(3)},
// allLogs[0:3], newMockNotifier(), &rpc.Subscription{ID: rpc.NewID()}, nil,
// },
} }
// subscribe logs // subscribe logs
for i, tc := range testCases { for i, tc := range testCases {
testCases[i].err = make(chan error) testCases[i].err = make(chan error)
err := api.logs(context.Background(), tc.notifier, tc.sub, tc.crit) err := api.logs(context.Background(), tc.notifier, tc.sub, tc.crit)
if tc.expectErr != nil {
if err == nil {
t.Errorf("test %d: want error %v, have nothing", i, tc.expectErr)
continue
}
if !errors.Is(err, tc.expectErr) {
t.Errorf("test %d: error mismatch, want %v, have %v", i, tc.expectErr, err)
}
continue
}
if err != nil { if err != nil {
t.Fatalf("SubscribeLogs %d failed: %v\n", i, err) t.Fatalf("SubscribeLogs %d failed: %v\n", i, err)
} }