Skip to content

Commit a1906b3

Browse files
committed
[dev] draft for e2e test
The rate limit is not working as intended.
1 parent fc3f3ca commit a1906b3

File tree

2 files changed

+193
-0
lines changed

2 files changed

+193
-0
lines changed

account_e2e_test.go

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Copyright (c) 2018 LI Zhennan
3+
*
4+
* Use of this work is governed by a MIT License.
5+
* You may find a license copy in project root.
6+
*/
7+
8+
package etherscan
9+
10+
import (
11+
"encoding/json"
12+
"fmt"
13+
"math/big"
14+
"testing"
15+
)
16+
17+
func TestClient_AccountBalance(t *testing.T) {
18+
balance, err := api.AccountBalance("0x281055afc982d96fab65b3a49cac8b878184cb16")
19+
noError(t, err, "api.AccountBalance")
20+
21+
if balance.Int().Cmp(big.NewInt(0)) != 1 {
22+
t.Fatalf("rich man is no longer rich")
23+
}
24+
}
25+
26+
func TestClient_MultiAccountBalance(t *testing.T) {
27+
balances, err := api.MultiAccountBalance(
28+
"0x281055afc982d96fab65b3a49cac8b878184cb16",
29+
"0x6f46cf5569aefa1acc1009290c8e043747172d89",
30+
"0x90e63c3d53e0ea496845b7a03ec7548b70014a91",
31+
"0x53d284357ec70ce289d6d64134dfac8e511c8a3d")
32+
noError(t, err, "api.MultiAccountBalance")
33+
34+
for i, item := range balances {
35+
if item.Account == "" {
36+
t.Errorf("bound error on index %v", i)
37+
}
38+
if item.Balance.Int().Cmp(big.NewInt(0)) != 1 {
39+
t.Errorf("rich man %s at index %v is no longer rich.", item.Account, i)
40+
}
41+
}
42+
}
43+
44+
func TestClient_NormalTxByAddress(t *testing.T) {
45+
const wantLen = 19
46+
47+
var a, b = 54092, 79728
48+
txs, err := api.NormalTxByAddress("0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae", &a, &b, 1, 500, false)
49+
noError(t, err, "api.NormalTxByAddress")
50+
51+
//j, _ := json.MarshalIndent(txs, "", " ")
52+
//fmt.Printf("%s\n", j)
53+
54+
if len(txs) != wantLen {
55+
t.Errorf("got txs length %v, want %v", len(txs), wantLen)
56+
}
57+
}
58+
59+
func TestClient_InternalTxByAddress(t *testing.T) {
60+
const wantLen = 66
61+
62+
var a, b = 0, 2702578
63+
txs, err := api.InternalTxByAddress("0x2c1ba59d6f58433fb1eaee7d20b26ed83bda51a3", &a, &b, 1, 500, false)
64+
noError(t, err, "api.InternalTxByAddress")
65+
66+
//j, _ := json.MarshalIndent(txs, "", " ")
67+
//fmt.Printf("%s\n", j)
68+
69+
if len(txs) != wantLen {
70+
t.Errorf("got txs length %v, want %v", len(txs), wantLen)
71+
}
72+
}
73+
74+
func TestClient_ERC20TransferTx(t *testing.T) {
75+
const wantLen = 3
76+
77+
var a, b = 3273004, 3328071
78+
var contract, address = "0xe0b7927c4af23765cb51314a0e0521a9645f0e2a", "0x4e83362442b8d1bec281594cea3050c8eb01311c"
79+
txs, err := api.ERC20TransferTx(&contract, &address, &a, &b, 1, 500)
80+
noError(t, err, "api.ERC20TransferTx")
81+
82+
//j, _ := json.MarshalIndent(txs, "", " ")
83+
//fmt.Printf("%s\n", j)
84+
85+
if len(txs) != wantLen {
86+
t.Errorf("got txs length %v, want %v", len(txs), wantLen)
87+
}
88+
}
89+
90+
func TestClient_BlocksMinedByAddress(t *testing.T) {
91+
const wantLen = 10
92+
93+
blocks, err := api.BlocksMinedByAddress("0x9dd134d14d1e65f84b706d6f205cd5b1cd03a46b", 1, wantLen)
94+
noError(t, err, "api.BlocksMinedByAddress")
95+
96+
//j, _ := json.MarshalIndent(blocks, "", " ")
97+
//fmt.Printf("%s\n", j)
98+
99+
if len(blocks) != wantLen {
100+
t.Errorf("got txs length %v, want %v", len(blocks), wantLen)
101+
}
102+
}
103+
104+
func TestClient_UnclesMinedByAddress(t *testing.T) {
105+
const wantLen = 10
106+
107+
blocks, err := api.UnclesMinedByAddress("0x9dd134d14d1e65f84b706d6f205cd5b1cd03a46b", 1, wantLen)
108+
noError(t, err, "api.UnclesMinedByAddress")
109+
110+
j, _ := json.MarshalIndent(blocks, "", " ")
111+
fmt.Printf("%s\n", j)
112+
113+
if len(blocks) != wantLen {
114+
t.Errorf("got txs length %v, want %v", len(blocks), wantLen)
115+
}
116+
}

setup_e2e_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright (c) 2018 LI Zhennan
3+
*
4+
* Use of this work is governed by a MIT License.
5+
* You may find a license copy in project root.
6+
*/
7+
8+
package etherscan
9+
10+
import (
11+
"testing"
12+
"time"
13+
)
14+
15+
var (
16+
// api test client for many test cases
17+
api *Client
18+
// bucket default rate limiter
19+
bucket *Bucket
20+
)
21+
22+
func init() {
23+
bucket = NewBucket(5, 200*time.Microsecond)
24+
25+
api = New(Mainnet, "etherscan-api-e2e-test")
26+
api.Verbose = true
27+
api.BeforeRequest = func(module string, action string, param map[string]interface{}) error {
28+
bucket.Take()
29+
return nil
30+
}
31+
}
32+
33+
// Bucket is a simple and easy rate limiter
34+
// Use NewBucket() to construct one.
35+
type Bucket struct {
36+
bucket chan bool
37+
refillTime time.Duration
38+
}
39+
40+
// NewBucket factory of Bucket
41+
func NewBucket(cap int, refillTime time.Duration) (b *Bucket) {
42+
b = &Bucket{
43+
bucket: make(chan bool, cap-1),
44+
refillTime: refillTime,
45+
}
46+
47+
go b.fillRoutine()
48+
49+
return
50+
}
51+
52+
// Take a action token from bucket,
53+
// blocks if there is currently no token left.
54+
func (b *Bucket) Take() {
55+
b.bucket <- true
56+
}
57+
58+
// fill a action token into bucket,
59+
// blocks if the bucket is currently full
60+
func (b *Bucket) fill() {
61+
<-b.bucket
62+
}
63+
64+
func (b *Bucket) fillRoutine() {
65+
ticker := time.NewTicker(b.refillTime)
66+
67+
for range ticker.C {
68+
b.fill()
69+
}
70+
}
71+
72+
// noError checks for testing error
73+
func noError(t *testing.T, err error, msg string) {
74+
if err != nil {
75+
t.Fatalf("%s: %v", msg, err)
76+
}
77+
}

0 commit comments

Comments
 (0)