chore(queue): returns the numbers of workers has been created (#611)

* chore(queue): returns the numbers of workers has been created

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>

* chore: enable logger.

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu
2021-07-24 10:51:26 +08:00
committed by GitHub
parent 6ebbbe5026
commit 6db9768758
2 changed files with 44 additions and 16 deletions

View File

@@ -20,10 +20,6 @@ func (m mockMessage) Bytes() []byte {
return []byte(m.msg)
}
func TestMain(m *testing.M) {
m.Run()
}
func TestQueueUsage(t *testing.T) {
w := NewWorker()
assert.Equal(t, runtime.NumCPU()<<1, w.Capacity())
@@ -96,3 +92,27 @@ func TestShutDonwPanic(t *testing.T) {
q.Shutdown()
q.Wait()
}
func TestWorkersNum(t *testing.T) {
w := NewWorker(
WithRunFunc(func(msg queue.QueuedMessage) error {
logx.LogAccess.Infof("get message: %s", msg.Bytes())
time.Sleep(100 * time.Millisecond)
return nil
}),
)
q, err := queue.NewQueue(
queue.WithWorker(w),
queue.WithWorkerCount(2),
)
assert.NoError(t, err)
q.Start()
q.Start()
q.Start()
q.Start()
time.Sleep(50 * time.Millisecond)
assert.Equal(t, 8, q.Workers())
q.Shutdown()
q.Wait()
assert.Equal(t, 0, q.Workers())
}