-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeter_reader_test.go
67 lines (58 loc) · 1.14 KB
/
meter_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
// 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 TestMeterReader_test1(t *testing.T) {
t.Parallel()
src := randdata.New(randdata.Binary, 0, 500000)
r := speedio.NewMeterReader(src)
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, 20000)
sum := 0
r.Start()
ticker := time.NewTicker(time.Second / 5)
for {
<-ticker.C
n, err := io.ReadFull(r, buf)
if 0 < n {
sum += n
}
if err != nil {
if err != io.EOF && err != io.ErrUnexpectedEOF {
t.Error(err)
}
break
}
if n < 1 {
t.Errorf("unexpected n: %d", n)
break
}
}
ticker.Stop()
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)
}