github.com/blend/go-sdk@v1.20220411.3/async/queue_test.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package async 9 10 import ( 11 "context" 12 "fmt" 13 "sync" 14 "testing" 15 16 "github.com/blend/go-sdk/assert" 17 ) 18 19 func Test_Queue_Start_Close(t *testing.T) { 20 its := assert.New(t) 21 22 wg := sync.WaitGroup{} 23 wg.Add(8) 24 q := NewQueue(func(_ context.Context, obj interface{}) error { 25 defer wg.Done() 26 return nil 27 }) 28 29 go func() { _ = q.Start() }() 30 <-q.Latch.NotifyStarted() 31 32 its.True(q.Latch.IsStarted()) 33 34 for x := 0; x < 8; x++ { 35 q.Enqueue(fmt.Sprint(x)) 36 } 37 38 wg.Wait() 39 q.Close() 40 its.False(q.Latch.IsStarted()) 41 } 42 43 func Test_Queue_Start_Stop(t *testing.T) { 44 its := assert.New(t) 45 46 workCount := 128 47 48 wg := sync.WaitGroup{} 49 wg.Add(workCount) 50 q := NewQueue(func(_ context.Context, obj interface{}) error { 51 defer wg.Done() 52 return nil 53 }, OptQueueMaxWork(workCount+1)) 54 55 go func() { _ = q.Start() }() 56 <-q.Latch.NotifyStarted() 57 its.True(q.Latch.IsStarted()) 58 59 for x := 0; x < workCount; x++ { 60 q.Enqueue(fmt.Sprint(x)) 61 } 62 63 its.Nil(q.Stop()) 64 its.False(q.Latch.IsStarted()) 65 wg.Wait() 66 } 67 68 func Test_Queue_Start_Stop_Start(t *testing.T) { 69 its := assert.New(t) 70 71 workCount := 10 72 73 wg := sync.WaitGroup{} 74 wg.Add(workCount) 75 q := NewQueue(func(_ context.Context, obj interface{}) error { 76 defer wg.Done() 77 return nil 78 }, OptQueueMaxWork(workCount)) 79 80 go func() { _ = q.Start() }() 81 <-q.Latch.NotifyStarted() 82 83 its.True(q.Latch.IsStarted()) 84 85 for x := 0; x < workCount; x++ { 86 q.Enqueue(fmt.Sprint(x)) 87 } 88 its.Nil(q.Stop()) 89 its.False(q.Latch.IsStarted()) 90 wg.Wait() 91 92 wg.Add(workCount) 93 94 go func() { _ = q.Start() }() 95 <-q.Latch.NotifyStarted() 96 97 its.True(q.Latch.IsStarted()) 98 99 for x := 0; x < workCount; x++ { 100 q.Enqueue(fmt.Sprint(x)) 101 } 102 its.Nil(q.Stop()) 103 its.False(q.Latch.IsStarted()) 104 wg.Wait() 105 }