Skip to content

Commit 7abd8e0

Browse files
committed
test concurrent
1 parent 018c65c commit 7abd8e0

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

queue_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package nbchanqueue
22

33
import (
4+
"cmp"
45
"context"
56
"errors"
7+
"sync"
68
"testing"
79
"time"
810

11+
"github.com/google/go-cmp/cmp/cmpopts"
912
"gotest.tools/v3/assert"
13+
cmp2 "gotest.tools/v3/assert/cmp"
1014
)
1115

1216
func TestQueue(t *testing.T) {
@@ -62,6 +66,46 @@ func TestQueueCtxTimeout(t *testing.T) {
6266
assert.DeepEqual(t, expected, items)
6367
}
6468

69+
func TestQueueConcurrency(t *testing.T) {
70+
var wgGet sync.WaitGroup
71+
var wgPut sync.WaitGroup
72+
73+
q := New[int]()
74+
75+
var getItems []int
76+
var putItems []int
77+
var putLock sync.Mutex
78+
79+
wgGet.Add(1)
80+
go func() {
81+
defer wgGet.Done()
82+
for v := range q.Get() {
83+
getItems = append(getItems, v)
84+
}
85+
}()
86+
87+
for i := 0; i < 10; i++ {
88+
wgPut.Add(1)
89+
go func() {
90+
defer wgPut.Done()
91+
for j := 0; j < 10; j++ {
92+
v := i*10 + j
93+
q.Put(v)
94+
putLock.Lock()
95+
putItems = append(putItems, v)
96+
putLock.Unlock()
97+
}
98+
}()
99+
}
100+
wgPut.Wait()
101+
q.Close()
102+
wgGet.Wait()
103+
104+
assert.Assert(t, cmp2.Len(putItems, 10*10))
105+
assert.Assert(t, cmp2.Len(getItems, 10*10))
106+
assert.DeepEqual(t, putItems, getItems, cmpopts.SortSlices(cmp.Less[int]))
107+
}
108+
65109
func assertItems[E any](t *testing.T, q *Queue[E], expected []E) {
66110
items, err := readNWithTimeout(q, len(expected))
67111
assert.NilError(t, err)

0 commit comments

Comments
 (0)