-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathnodeClient_test.go
119 lines (100 loc) · 2.83 KB
/
nodeClient_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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package aptos
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestPollForTransaction(t *testing.T) {
// this doesn't need to actually have an aptos-node!
// API error on every GET is fine, poll for a few milliseconds then return error
client, err := NewClient(LocalnetConfig)
assert.NoError(t, err)
start := time.Now()
err = client.PollForTransactions([]string{"alice", "bob"}, PollTimeout(10*time.Millisecond), PollPeriod(2*time.Millisecond))
dt := time.Now().Sub(start)
assert.GreaterOrEqual(t, dt, 9*time.Millisecond)
assert.Less(t, dt, 20*time.Millisecond)
assert.Error(t, err)
}
func TestEventsByHandle(t *testing.T) {
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
// handle initial request from client
w.WriteHeader(http.StatusOK)
return
}
assert.Equal(t, "/accounts/0x0/events/0x2/transfer", r.URL.Path)
start := r.URL.Query().Get("start")
limit := r.URL.Query().Get("limit")
startInt, _ := strconv.ParseUint(start, 10, 64)
limitInt, _ := strconv.ParseUint(limit, 10, 64)
events := make([]map[string]interface{}, 0, limitInt)
for i := uint64(0); i < limitInt; i++ {
events = append(events, map[string]interface{}{
"type": "0x1::coin::TransferEvent",
"guid": map[string]interface{}{
"creation_number": "1",
"account_address": AccountZero.String(),
},
"sequence_number": strconv.FormatUint(startInt+i, 10),
"data": map[string]interface{}{
"amount": fmt.Sprintf("%d", (startInt+i)*100),
},
})
}
json.NewEncoder(w).Encode(events)
}))
defer mockServer.Close()
client, err := NewClient(NetworkConfig{
Name: "mocknet",
NodeUrl: mockServer.URL,
})
assert.NoError(t, err)
t.Run("pagination with concurrent fetching", func(t *testing.T) {
start := uint64(0)
limit := uint64(150)
events, err := client.EventsByHandle(
AccountZero,
"0x2",
"transfer",
&start,
&limit,
)
assert.NoError(t, err)
assert.Len(t, events, 150)
})
t.Run("default page size when limit not provided", func(t *testing.T) {
events, err := client.EventsByHandle(
AccountZero,
"0x2",
"transfer",
nil,
nil,
)
assert.NoError(t, err)
assert.Len(t, events, 100)
assert.Equal(t, uint64(99), events[99].SequenceNumber)
})
t.Run("single page fetch", func(t *testing.T) {
start := uint64(50)
limit := uint64(5)
events, err := client.EventsByHandle(
AccountZero,
"0x2",
"transfer",
&start,
&limit,
)
jsonBytes, _ := json.MarshalIndent(events, "", " ")
t.Logf("JSON Response: %s", string(jsonBytes))
assert.NoError(t, err)
assert.Len(t, events, 5)
assert.Equal(t, uint64(50), events[0].SequenceNumber)
assert.Equal(t, uint64(54), events[4].SequenceNumber)
})
}