-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader_test.go
70 lines (61 loc) · 1.16 KB
/
reader_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
// Copyright (c) 2020 Hirotsuna Mizuno. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
package speedio_test
import (
"io"
"testing"
"time"
"github.com/tunabay/go-randdata"
"github.com/tunabay/go-speedio"
)
//
func TestReader_test1(t *testing.T) {
// t.Parallel()
src := randdata.New(randdata.Binary, 0, 500000)
r, err := speedio.NewReader(src, 800000)
if err != nil {
t.Error(err)
return
}
t.Log("set rate:", r.LimitingBitRate())
done := make(chan struct{})
go func() {
ticker := time.NewTicker(time.Second / 2)
defer ticker.Stop()
for {
select {
case <-ticker.C:
t.Log("bitrate:", r.BitRate())
case <-done:
return
}
}
}()
buf := make([]byte, 8192)
sum := 0
r.Start()
time.Sleep(time.Second) // limiter's resolution
for {
n, err := r.Read(buf)
if 0 < n {
sum += n
}
if err != nil {
if err != io.EOF {
t.Error(err)
}
break
}
if n < 1 {
t.Errorf("unexpected n=%d", n)
break
}
}
if err := r.Close(); err != nil {
t.Error(err)
}
close(done)
bc, et, br := r.Total()
t.Logf("total(n=%d): %v, %v, %v", sum, bc, et, br)
}