-
Notifications
You must be signed in to change notification settings - Fork 179
/
event_retriever_live_test.go
104 lines (75 loc) · 2.3 KB
/
event_retriever_live_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//go:build live
package retriever
import (
"log"
"os"
"strconv"
"sync"
"testing"
gsrpc "github.com/centrifuge/go-substrate-rpc-client/v4"
"github.com/centrifuge/go-substrate-rpc-client/v4/registry/state"
)
var (
eventTestURLs = []string{
"wss://fullnode.parachain.centrifuge.io",
"wss://rpc.polkadot.io",
"wss://acala-rpc-0.aca-api.network",
"wss://wss.api.moonbeam.network",
}
)
func TestLive_EventRetriever_GetEvents(t *testing.T) {
t.Parallel()
var wg sync.WaitGroup
eventsThreshold, err := strconv.Atoi(os.Getenv("GSRPC_LIVE_TEST_EVENTS_THRESHOLD"))
if err != nil {
eventsThreshold = 50
log.Printf("Env Var GSRPC_LIVE_TEST_EVENTS_THRESHOLD not set, defaulting to %d", eventsThreshold)
}
for _, testURL := range eventTestURLs {
testURL := testURL
wg.Add(1)
go func() {
defer wg.Done()
api, err := gsrpc.NewSubstrateAPI(testURL)
if err != nil {
log.Printf("Couldn't connect to '%s': %s\n", testURL, err)
return
}
retriever, err := NewDefaultEventRetriever(state.NewEventProvider(api.RPC.State), api.RPC.State)
if err != nil {
log.Printf("Couldn't create event retriever: %s", err)
return
}
header, err := api.RPC.Chain.GetHeaderLatest()
if err != nil {
log.Printf("Couldn't get latest header for '%s': %s\n", testURL, err)
return
}
eventsCount := 0
for {
blockHash, err := api.RPC.Chain.GetBlockHash(uint64(header.Number))
if err != nil {
log.Printf("Couldn't retrieve blockHash for '%s', block number %d: %s\n", testURL, header.Number, err)
return
}
events, err := retriever.GetEvents(blockHash)
if err != nil {
log.Printf("Couldn't retrieve events for '%s', block number %d: %s\n", testURL, header.Number, err)
return
}
log.Printf("Found %d events for '%s', at block number %d.\n", len(events), testURL, header.Number)
eventsCount += len(events)
if eventsCount > eventsThreshold {
log.Printf("Retrieved a total of %d events for '%s', last block number %d. Stopping now.\n", eventsCount, testURL, header.Number)
return
}
header, err = api.RPC.Chain.GetHeader(header.ParentHash)
if err != nil {
log.Printf("Couldn't retrieve header for block number '%d' for '%s': %s\n", header.Number, testURL, err)
return
}
}
}()
}
wg.Wait()
}