-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathfast_test.go
127 lines (94 loc) · 1.62 KB
/
fast_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
120
121
122
123
124
125
126
127
package fast
import (
"fmt"
"testing"
"time"
"github.com/ddo/go-spin"
)
var f *Fast
var testing_urls []string
func TestNew(t *testing.T) {
f = New()
if f.client == nil {
t.Error()
}
}
func TestInit(t *testing.T) {
err := f.Init()
if err != nil {
t.Error()
return
}
if f.url == "" {
t.Error()
}
if f.token == "" {
t.Error()
}
if f.urlCount == 0 {
t.Error()
}
}
func TestGetUrls(t *testing.T) {
urls, err := f.GetUrls()
if err != nil {
t.Error()
return
}
if len(urls) != f.urlCount {
t.Error()
}
testing_urls = urls
}
func TestDownload(t *testing.T) {
byteLenChan := make(chan int64)
done := make(chan struct{})
spinner := spin.New("")
go func() {
for range byteLenChan {
fmt.Printf(" \r %s", spinner.Spin())
}
}()
err := f.download(testing_urls[0], byteLenChan, done)
if err != nil {
t.Error()
return
}
fmt.Println("done")
}
// stop after 5s
func TestDownloadStop(t *testing.T) {
byteLenChan := make(chan int64)
done := make(chan struct{})
spinner := spin.New("")
go func() {
for range byteLenChan {
fmt.Printf(" \r %s", spinner.Spin())
}
}()
go func() {
<-time.After(2 * time.Second)
close(done)
}()
err := f.download(testing_urls[0], byteLenChan, done)
if err != nil {
t.Error()
return
}
fmt.Println("done")
}
func TestMeasure(t *testing.T) {
KbpsChan := make(chan float64)
spinner := spin.New("")
go func() {
for Kbps := range KbpsChan {
fmt.Printf(" \r %s %.2f Kbps %.2f Mbps", spinner.Spin(), Kbps, Kbps/1000)
}
}()
err := f.Measure(testing_urls, KbpsChan)
if err != nil {
t.Error()
return
}
fmt.Println("done")
}