MCPcopy
hub / github.com/lxzan/gws / TestTaskQueue

Function TestTaskQueue

task_test.go:254–316  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

252}
253
254func TestTaskQueue(t *testing.T) {
255 var as = assert.New(t)
256
257 t.Run("", func(t *testing.T) {
258 var mu = &sync.Mutex{}
259 var listA []int
260 var listB []int
261
262 var count = 1000
263 var wg = &sync.WaitGroup{}
264 wg.Add(count)
265 var q = newWorkerQueue(8)
266 for i := 0; i < count; i++ {
267 listA = append(listA, i)
268
269 v := i
270 q.Push(func() {
271 defer wg.Done()
272 var latency = time.Duration(internal.AlphabetNumeric.Intn(100)) * time.Microsecond
273 time.Sleep(latency)
274 mu.Lock()
275 listB = append(listB, v)
276 mu.Unlock()
277 })
278 }
279 wg.Wait()
280 as.ElementsMatch(listA, listB)
281 })
282
283 t.Run("", func(t *testing.T) {
284 sum := int64(0)
285 w := newWorkerQueue(8)
286 var wg = &sync.WaitGroup{}
287 wg.Add(1000)
288 for i := int64(1); i <= 1000; i++ {
289 var tmp = i
290 w.Push(func() {
291 time.Sleep(time.Millisecond)
292 atomic.AddInt64(&sum, tmp)
293 wg.Done()
294 })
295 }
296 wg.Wait()
297 as.Equal(sum, int64(500500))
298 })
299
300 t.Run("", func(t *testing.T) {
301 sum := int64(0)
302 w := newWorkerQueue(1)
303 var wg = &sync.WaitGroup{}
304 wg.Add(1000)
305 for i := int64(1); i <= 1000; i++ {
306 var tmp = i
307 w.Push(func() {
308 time.Sleep(time.Millisecond)
309 atomic.AddInt64(&sum, tmp)
310 wg.Done()
311 })

Callers

nothing calls this directly

Calls 4

newWorkerQueueFunction · 0.85
RunMethod · 0.80
IntnMethod · 0.80
PushMethod · 0.45

Tested by

no test coverage detected