|
| 1 | +package rpc |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/NethermindEth/starknet.go/client" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | +) |
| 11 | + |
| 12 | +func TestSubscribeNewHeads(t *testing.T) { |
| 13 | + if testEnv != "testnet" { |
| 14 | + t.Skip("Skipping test as it requires a testnet environment") |
| 15 | + } |
| 16 | + |
| 17 | + testConfig := beforeEach(t) |
| 18 | + require.NotNil(t, testConfig.wsBase, "wsProvider base is not set") |
| 19 | + |
| 20 | + type testSetType struct { |
| 21 | + headers chan *BlockHeader |
| 22 | + blockID []BlockID |
| 23 | + counter int |
| 24 | + isErrorExpected bool |
| 25 | + } |
| 26 | + |
| 27 | + provider := testConfig.provider |
| 28 | + blockNumber, err := provider.BlockNumber(context.Background()) |
| 29 | + require.NoError(t, err) |
| 30 | + |
| 31 | + latestBlockNumbers := []uint64{blockNumber, blockNumber + 1} // for the case the latest block number is updated |
| 32 | + |
| 33 | + testSet := map[string][]testSetType{ |
| 34 | + "testnet": { |
| 35 | + { // normal |
| 36 | + headers: make(chan *BlockHeader), |
| 37 | + isErrorExpected: false, |
| 38 | + }, |
| 39 | + { // with tag latest |
| 40 | + headers: make(chan *BlockHeader), |
| 41 | + blockID: []BlockID{WithBlockTag("latest")}, |
| 42 | + isErrorExpected: false, |
| 43 | + }, |
| 44 | + { // with tag pending |
| 45 | + headers: make(chan *BlockHeader), |
| 46 | + blockID: []BlockID{WithBlockTag("pending")}, |
| 47 | + isErrorExpected: true, |
| 48 | + }, |
| 49 | + { // with block number within the range of 1024 blocks |
| 50 | + headers: make(chan *BlockHeader), |
| 51 | + blockID: []BlockID{WithBlockNumber(blockNumber - 100)}, |
| 52 | + counter: 100, |
| 53 | + isErrorExpected: false, |
| 54 | + }, |
| 55 | + { // invalid, with block number out of the range of 1024 blocks |
| 56 | + headers: make(chan *BlockHeader), |
| 57 | + blockID: []BlockID{WithBlockNumber(blockNumber - 1025)}, |
| 58 | + isErrorExpected: true, |
| 59 | + }, |
| 60 | + { // invalid, more than one blockID parameter |
| 61 | + headers: make(chan *BlockHeader), |
| 62 | + blockID: []BlockID{WithBlockTag("latest"), WithBlockTag("latest")}, |
| 63 | + isErrorExpected: true, |
| 64 | + }, |
| 65 | + }, |
| 66 | + }[testEnv] |
| 67 | + |
| 68 | + for index, test := range testSet { |
| 69 | + t.Run(fmt.Sprintf("test %d", index+1), func(t *testing.T) { |
| 70 | + |
| 71 | + wsProvider, err := NewWebsocketProvider(testConfig.wsBase) |
| 72 | + require.NoError(t, err) |
| 73 | + defer wsProvider.Close() |
| 74 | + |
| 75 | + var sub *client.ClientSubscription |
| 76 | + if len(test.blockID) == 0 { |
| 77 | + sub, err = wsProvider.SubscribeNewHeads(context.Background(), test.headers) |
| 78 | + } else { |
| 79 | + sub, err = wsProvider.SubscribeNewHeads(context.Background(), test.headers, test.blockID...) |
| 80 | + } |
| 81 | + |
| 82 | + if test.isErrorExpected { |
| 83 | + require.Error(t, err) |
| 84 | + return |
| 85 | + } else { |
| 86 | + require.NoError(t, err) |
| 87 | + } |
| 88 | + |
| 89 | + require.NotNil(t, sub) |
| 90 | + defer sub.Unsubscribe() |
| 91 | + |
| 92 | + for { |
| 93 | + select { |
| 94 | + case resp := <-test.headers: |
| 95 | + require.IsType(t, &BlockHeader{}, resp) |
| 96 | + |
| 97 | + if test.counter != 0 { |
| 98 | + if test.counter == 1 { |
| 99 | + require.Contains(t, latestBlockNumbers, resp.BlockNumber+1) |
| 100 | + return |
| 101 | + } else { |
| 102 | + test.counter-- |
| 103 | + } |
| 104 | + } else { |
| 105 | + return |
| 106 | + } |
| 107 | + case err := <-sub.Err(): |
| 108 | + require.NoError(t, err) |
| 109 | + } |
| 110 | + } |
| 111 | + }) |
| 112 | + } |
| 113 | +} |
0 commit comments