-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailbox_test.go
65 lines (53 loc) · 1.34 KB
/
mailbox_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package broadway
import (
"testing"
"time"
)
/* case ThrowOnOverflow, DropOnOverflow:
select {
case m.mailbox <- envelope:
default:
if m.overflowPolicy == ThrowOnOverflow {
panic(errors.New("Mailbox overflowed")) // Supervisor can handle this
}
}
case BlockOnOverflow:
m.mailbox <- envelope */
func TestMailbox(t *testing.T) {
m := NewMailbox(MailboxConfig{})
ch := make(chan struct{})
go func() {
m.Dequeue()
ch <- struct{}{}
}()
m.Enqueue(Envelope{})
select {
case <-ch:
case <-time.After(time.Second):
t.Error("Failed to dequeue")
}
}
func TestMailbox_DropOnOverflow(t *testing.T) {
m := NewMailbox(MailboxConfig{OverflowPolicy: DropOnOverflow})
m.Enqueue(Envelope{})
m.Enqueue(Envelope{}) // This will be dropped and should not deadlock
}
func TestMailbox_ThrowOnOverflow(t *testing.T) {
defer func() {
if r := recover(); r == nil || r != OverflowError {
t.Error("Unexpected error", r)
}
}()
m := NewMailbox(MailboxConfig{OverflowPolicy: PanicOnOverflow})
m.Enqueue(Envelope{})
m.Enqueue(Envelope{}) // This will error
}
func TestMailbox_BlockOnOverflow(t *testing.T) {
m := NewMailbox(MailboxConfig{OverflowPolicy: BlockOnOverflow})
go func() {
m.Enqueue(Envelope{})
m.Enqueue(Envelope{}) // This block
t.Error("Enqueue did not block")
}()
<-time.After(time.Second) // Give it time to block
}