File: bucket_test.go

package info (click to toggle)
golang-github-tsenart-tb 0.0~git20151208.0.19f4c3d-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 92 kB
  • ctags: 51
  • sloc: makefile: 2
file content (191 lines) | stat: -rw-r--r-- 4,160 bytes parent folder | download
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package tb

import (
	"fmt"
	"runtime"
	"testing"
	"time"
)

func TestNewBucket(t *testing.T) {
	t.Parallel()

	b := NewBucket(10, -1)
	b.Take(10)
	time.Sleep(100 * time.Millisecond)
	if w, g := int64(0), b.Take(1); w != g {
		t.Fatal("Expected no filling when freq == -1")
	}
}

func TestBucket_Take_single(t *testing.T) {
	t.Parallel()

	b := NewBucket(10, 0)
	defer b.Close()

	ex := [...]int64{5, 5, 1, 1, 5, 4, 1, 0}
	for i := 0; i < len(ex)-1; i += 2 {
		if got, want := b.Take(ex[i]), ex[i+1]; got != want {
			t.Errorf("Want: %d, Got: %d", want, got)
		}
	}
}

func TestBucket_Put_single(t *testing.T) {
	t.Parallel()

	b := NewBucket(10, 0)
	defer b.Close()

	b.Take(10)

	ex := [...]int64{5, 5, 10, 5, 15, 0}
	for i := 0; i < len(ex)-1; i += 2 {
		if got, want := b.Put(ex[i]), ex[i+1]; got != want {
			t.Errorf("Want: %d, Got: %d", want, got)
		}
	}
}

func TestBucket_Take_multi(t *testing.T) {
	t.Parallel()

	b := NewBucket(10, 0)
	defer b.Close()

	exs := [2][]int64{{4, 4, 2, 2}, {2, 2, 1, 1}}
	for i := 0; i < 2; i++ {
		go func(i int) {
			for j := 0; j < len(exs[i])-1; j += 2 {
				if got, want := b.Take(exs[i][j]), exs[i][j+1]; got != want {
					t.Errorf("Want: %d, Got: %d", want, got)
				}
			}
		}(i)
	}
}

func TestBucket_Put_multi(t *testing.T) {
	t.Parallel()

	b := NewBucket(10, 0)
	defer b.Close()

	b.Take(10)

	exs := [2][]int64{{4, 4, 2, 2}, {2, 2, 1, 1}}
	for i := 0; i < 2; i++ {
		go func(i int) {
			for j := 0; j < len(exs[i])-1; j += 2 {
				if got, want := b.Put(exs[i][j]), exs[i][j+1]; got != want {
					t.Errorf("Want: %d, Got: %d", want, got)
				}
			}
		}(i)
	}
}

func TestBucket_Take_throughput(t *testing.T) {
	t.Parallel()

	if testing.Short() {
		t.Skip("Skipping test in short mode")
	}

	runtime.GOMAXPROCS(2)

	b := NewBucket(1000, 0)
	defer b.Close()

	b.Take(1000)

	var (
		out   int64
		began = time.Now()
	)
	for out < 1000 {
		out += b.Take(1000 - out)
	}

	ended := time.Since(began)
	if int(ended.Seconds()) != 1 {
		t.Errorf("Want 1000 tokens to take 1s. Got: %d", int(ended.Seconds()))
	}
}

func BenchmarkBucket_Take_sequential(b *testing.B) {
	bucket := NewBucket(int64(b.N), 0)
	defer bucket.Close()

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		bucket.Take(8)
	}
}

func BenchmarkBucket_Put_sequential(b *testing.B) {
	bucket := NewBucket(int64(b.N), 0)
	defer bucket.Close()

	bucket.Take(int64(b.N))

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		bucket.Put(8)
	}
}

func TestBucket_Wait(t *testing.T) {
	t.Parallel()

	cases := map[*Bucket]time.Duration{
		NewBucket(250, 100*time.Millisecond): 7 * time.Second,
		NewBucket(500, 100*time.Millisecond): 3 * time.Second,
		NewBucket(1e3, 500*time.Millisecond): 1 * time.Second,
		NewBucket(1e3, 20*time.Millisecond):  1 * time.Second,
		NewBucket(1e3, 1*time.Millisecond):   1 * time.Second,
		NewBucket(1e3, 0):                    1 * time.Second,
		NewBucket(2e3, 0):                    0,
		NewBucket(3e3, 0):                    0,
	}
	errors := make(chan error, len(cases))

	for bucket, wait := range cases {
		go func(bucket *Bucket, wait time.Duration) {
			defer bucket.Close()

			start := time.Now()
			got := bucket.Wait(2000)
			took := time.Since(start)
			if int(wait.Seconds()) != int(got.Seconds()) {
				errors <- fmt.Errorf("bucket.Wait(2000) with cap=%d, freq=%s: Want: %s, Got %s",
					bucket.capacity, bucket.freq, wait, got)
			} else if took < wait-time.Second || took > wait+time.Second {
				// took is the actual time the bucket.Wait() took
				// wait is the time we expected it to take
				// if took is more than 1 second different from wait, then return an error
				errors <- fmt.Errorf("bucket.Wait(2000) with cap=%d, freq=%s: Waited for %v which isn't within 1 second of %v",
					bucket.capacity, bucket.freq, wait, took)
			} else {
				errors <- nil
			}
		}(bucket, wait)
	}

	for i := 0; i < cap(errors); i++ {
		if err := <-errors; err != nil {
			t.Error(err)
		}
	}
}

func TestBucket_Close(t *testing.T) {
	b := NewBucket(10000, 0)
	b.Close()
	b.Take(10000)
	time.Sleep(10 * time.Millisecond)
	if want, got := int64(0), b.Take(1); want != got {
		t.Errorf("Want: %d Got: %d", want, got)
	}
}