-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoincap_test.go
101 lines (93 loc) · 2.07 KB
/
coincap_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
// Copyright 2017 Aleksandr Demakin. All rights reserved.
package coincap
import (
"testing"
"time"
)
func TestGlobal(t *testing.T) {
client := New()
gl, err := client.Global()
if err != nil {
t.Error(err)
return
}
if val, err := gl.AltCap.Float64(); err != nil {
t.Error(err)
} else if val == 0 {
t.Error("null value")
}
if val, err := gl.VolumeTotal.Float64(); err != nil {
t.Error(err)
} else if val == 0 {
t.Error("null value")
}
}
func TestCoins(t *testing.T) {
client := New()
if coins, err := client.Coins(); err != nil {
t.Error(err)
} else if len(coins) == 0 {
t.Error("empty coins")
}
}
func TestFrontXCP(t *testing.T) {
client := New()
if coins, err := client.CoinsXCP(); err != nil {
t.Error(err)
} else if len(coins) == 0 {
t.Error("empty coins")
}
}
func TestPageCoin(t *testing.T) {
client := New()
if page, err := client.Page("ETH"); err != nil {
t.Error(err)
} else if len(page.ID) == 0 {
t.Error("empty reply")
}
}
func TestHistory(t *testing.T) {
client := New()
if hist, err := client.History("BTC", HistoryIntervalAll); err != nil {
t.Error(err)
} else if len(hist.MarketCap)*len(hist.Price)*len(hist.Volume) == 0 {
t.Error("empty reply")
}
}
func TestSubscribeTrades(t *testing.T) {
client := New()
tradeChan, stopChan := make(chan *Trade), make(chan bool)
go func() {
var received int
afterChan := time.After(time.Second * 4)
for loop := true; loop && received < 10; {
select {
case <-tradeChan:
if received == 0 { // re-subscribe after first message. after that we should receive some more.
stopChan <- false
}
received++
case <-afterChan:
loop = false
}
}
if received < 2 {
t.Error("less than 2 messages in 3 sec")
} else {
t.Logf("%d messages received", received)
}
stopChan <- true
}()
doneChan := make(chan struct{})
go func() {
if err := client.SubscribeTrades(tradeChan, stopChan); err != nil {
t.Error(err)
}
doneChan <- struct{}{}
}()
select {
case <-time.After(time.Second * 4):
t.Error("subscribe did not return in 4 sec")
case <-doneChan:
}
}