-
Notifications
You must be signed in to change notification settings - Fork 222
Expand file tree
/
Copy pathnum_queue_test.go
More file actions
55 lines (45 loc) · 830 Bytes
/
Copy pathnum_queue_test.go
File metadata and controls
55 lines (45 loc) · 830 Bytes
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
package utils
import (
"math/rand"
"sync"
"testing"
"github.com/stretchr/testify/require"
)
func TestNumQueue(t *testing.T) {
t.Run("Simple", func(t *testing.T) {
N := uint64(100)
q := NewNumQueue(0)
for i := uint64(1); i <= N; i++ {
var iter sync.WaitGroup
iter.Add(1)
go func(i uint64) {
defer iter.Done()
q.WaitFor(i)
}(i)
q.Done(i)
iter.Wait()
}
})
t.Run("Random", func(t *testing.T) {
require := require.New(t)
N := 100
q := NewNumQueue(0)
output := make(chan uint64, 10)
nums := rand.Perm(N)
for _, n := range nums {
go func(n uint64) {
q.WaitFor(n - 1)
output <- n
if n == uint64(N) {
close(output)
}
q.Done(n)
}(uint64(n + 1))
}
var prev uint64
for got := range output {
require.Less(prev, got)
prev = got
}
})
}