File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change 11package nbchanqueue
22
33import (
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
1216func 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+
65109func assertItems [E any ](t * testing.T , q * Queue [E ], expected []E ) {
66110 items , err := readNWithTimeout (q , len (expected ))
67111 assert .NilError (t , err )
You can’t perform that action at this time.
0 commit comments