Skip to content

Commit 1ae6644

Browse files
committed
chore: testing panic in worker
Signed-off-by: Bo-Yi Wu <[email protected]>
1 parent 286596a commit 1ae6644

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

queue.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ func (q *Queue) work() {
121121
atomic.AddInt32(&q.runningWorkers, -1)
122122
if err := recover(); err != nil {
123123
q.logger.Error(err)
124+
q.logger.Infof("restart the new worker: %d", num)
124125
go q.work()
125126
}
126127
}()

queue_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,31 @@ func TestWorkerStatus(t *testing.T) {
9393
q.Shutdown()
9494
q.Wait()
9595
}
96+
97+
func TestWorkerPanic(t *testing.T) {
98+
w := &queueWorker{
99+
messages: make(chan QueuedMessage, 10),
100+
}
101+
q, err := NewQueue(
102+
WithWorker(w),
103+
WithWorkerCount(2),
104+
)
105+
assert.NoError(t, err)
106+
assert.NotNil(t, q)
107+
108+
q.Queue(mockMessage{
109+
message: "foobar",
110+
})
111+
q.Queue(mockMessage{
112+
message: "foobar",
113+
})
114+
q.Queue(mockMessage{
115+
message: "panic",
116+
})
117+
q.Start()
118+
time.Sleep(30 * time.Millisecond)
119+
assert.Equal(t, 2, q.Workers())
120+
q.Shutdown()
121+
q.Wait()
122+
assert.Equal(t, 0, q.Workers())
123+
}

worker.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ func (w *queueWorker) AfterRun() error { return nil }
4444
func (w *queueWorker) Run(chan struct{}) error {
4545
for msg := range w.messages {
4646
log.Println("got message", msg)
47+
if string(msg.Bytes()) == "panic" {
48+
panic("show panic")
49+
}
4750
time.Sleep(100 * time.Millisecond)
4851
}
4952
return nil

0 commit comments

Comments
 (0)