-
Notifications
You must be signed in to change notification settings - Fork 0
/
parallelwriter_test.go
80 lines (64 loc) · 1.65 KB
/
parallelwriter_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
package parallelwriter
import (
"bytes"
"math/rand"
"testing"
"time"
)
func TestParallelWriter(t *testing.T) {
b1 := &bytes.Buffer{}
b2 := &bytes.Buffer{}
pw := ParallelWriter(b1, b2)
if _, ok := pw.(*parallelWriter); !ok {
t.Error("ParallelWriter return is not a parallelWriter!")
}
}
func TestWrite(t *testing.T) {
b1 := &bytes.Buffer{}
b2 := &bytes.Buffer{}
pw := ParallelWriter(b1, b2)
data := []byte("foobar")
n, err := pw.Write(data)
if err != nil {
t.Error("Error on write:", err)
}
if n != len(data) {
t.Error("Written count is not equal to data length:", len(data), "!=", n)
}
if b := b1.Bytes(); !bytes.Equal(b, data) {
t.Error("b1 content is incorrect. Expected:", data, "Got:", b)
}
if b := b2.Bytes(); !bytes.Equal(b, data) {
t.Error("b2 content is incorrect. Expected:", data, "Got:", b)
}
}
type slowBuffer struct {
buf bytes.Buffer
}
func (s *slowBuffer) Write(data []byte) (int, error) {
rand.Seed(time.Now().UTC().UnixNano())
<-time.After(time.Millisecond * time.Duration(rand.Int()%1000))
return s.buf.Write(data)
}
func (s *slowBuffer) Bytes() []byte {
return s.buf.Bytes()
}
func TestSlowWrite(t *testing.T) {
b1 := &slowBuffer{}
b2 := &slowBuffer{}
pw := ParallelWriter(b1, b2)
data := []byte("foobar")
n, err := pw.Write(data)
if err != nil {
t.Error("Error on write:", err)
}
if n != len(data) {
t.Error("Written count is not equal to data length:", len(data), "!=", n)
}
if b := b1.Bytes(); !bytes.Equal(b, data) {
t.Error("b1 content is incorrect. Expected:", data, "Got:", b)
}
if b := b2.Bytes(); !bytes.Equal(b, data) {
t.Error("b2 content is incorrect. Expected:", data, "Got:", b)
}
}