-
Notifications
You must be signed in to change notification settings - Fork 2
/
data_test.go
141 lines (112 loc) · 2.74 KB
/
data_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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"bytes"
"testing"
"time"
)
func TestRandomInt(t *testing.T) {
seed := int64(99)
rnd1 := NewRandom(seed)
rnd2 := NewRandom(seed)
for i := 0; i < 100; i++ {
x := i
y := i + 100
v1 := rnd1.Int(x, y)
v2 := rnd2.Int(x, y)
// given the same seed, two Random should produce
// the same values
if v1 != v2 {
t.Errorf("rnd1 and rnd2 produced different values for (%d, %d): %d vs %d",
x, y, v1, v2)
}
// v1 should be greater than or equal to x
if v1 < x {
t.Errorf("outside min range: (%d, %d): %d", x, y, v1)
}
// v1 should be less than or equal to y
if v1 > y {
t.Errorf("outside max range: (%d, %d): %d", x, y, v1)
}
}
x, y := 100, 100
if n := rnd1.Int(x, y); n != 100 {
t.Errorf("unexpected result for (%d, %d): wanted %d, got %d",
x, y, x, n)
}
}
func TestRandomWriteSend(t *testing.T) {
seed := int64(99)
rnd1 := NewRandom(seed)
buf := &bytes.Buffer{}
for i := 0; i < 10; i++ {
n := i
b0 := i + 10
b1 := i + 100
k0 := i + 10
k1 := i + 100
v0 := i + 10
v1 := i + 100
err := rnd1.Write(buf, n, b0, b1, k0, k1, v0, v1)
if err != nil {
t.Error(i, err)
continue
}
ch := make(chan []*Row, n)
d0 := 10 * time.Millisecond
d1 := 2 * d0
go func(ch chan []*Row, d0, d1 time.Duration, n, b0, b1, k0, k1, v0, v1 int) {
t0 := time.Now()
i := 0
for rows := range ch {
t1 := time.Now()
delay := t1.Sub(t0)
t0 = t1
if i > 0 {
if delay.Nanoseconds() < d0.Nanoseconds() {
t.Errorf("rows[%d] arrived after %s, expected it to be >= %s",
i, delay, d0)
}
if n := delay.Nanoseconds() - d1.Nanoseconds(); n > 5e6 {
t.Errorf("rows[%d] arrived after %s, expected it to be <= %s",
i, delay, d1)
}
}
var j int
var row *Row
for j, row = range rows {
if x := len(row.Key.b); x < k0 {
t.Errorf("rows[%d][%d].Key.b was %d, expected it to be >= %d",
i, j, x, k0)
}
if x := len(row.Key.b); x > k1 {
t.Errorf("rows[%d][%d].Key.b was %d, expected it to be <= %d",
i, j, x, k1)
}
if x := len(row.Value.b); x < v0 {
t.Errorf("rows[%d][%d].Value.b was %d, expected it to be >= %d",
i, j, x, v0)
}
if x := len(row.Value.b); x > v1 {
t.Errorf("rows[%d][%d].Value.b was %d, expected it to be <= %d",
i, j, x, v1)
}
}
if j < b0 {
t.Errorf("rows[%d] contained %d items, expected it to be >= %d",
i, j, b0)
}
if j > b1 {
t.Errorf("rows[%d] contained %d items, expected it to be <= %d",
i, j, b1)
}
i++
}
if i != n {
t.Errorf("expected %d row sets, got %d", n, i)
}
}(ch, d0, d1, n, b0, b1, k0, k1, v0, v1)
rnd1.Send(ch, buf, d0, d1)
close(ch)
buf.Reset()
}
}