This repository has been archived by the owner on May 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailbox_test.go
112 lines (102 loc) · 1.93 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package mailbox
import (
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
var _ Storage = &SliceStorage{}
var _ Mailbox = &mailbox{}
func TestSmoke(t *testing.T) {
assert := assert.New(t)
mbox := New(&SliceStorage{})
go func() { mbox.Send(1) }()
v, _ := mbox.Receive()
assert.Equal(1, v)
}
func TestCount(t *testing.T) {
assert := assert.New(t)
store := SliceStorage{}
mbox := New(&store)
N := 1000
go func() {
for i := 0; i < N; i++ {
mbox.Send(1)
}
mbox.Close()
}()
total := 0
for {
v, ok := mbox.Receive()
if !ok {
break
}
assert.Equal(1, v)
total++
}
assert.Equal(N, total)
}
func TestItems(t *testing.T) {
assert := assert.New(t)
mbox := New(&SliceStorage{})
N := 1000
go func() {
for i := 1; i <= N; i++ {
i := i
mbox.Send(i)
}
mbox.Close()
}()
total := 0
for {
v, ok := mbox.Receive()
if !ok {
break
}
total += v.(int)
}
assert.Equal(500500, total)
}
func TestSendClose(t *testing.T) {
assert := assert.New(t)
mbox := New(&SliceStorage{})
assert.True(mbox.Send(1))
mbox.Close()
<-time.After(time.Millisecond * 30)
assert.False(mbox.Send(1, time.Millisecond*10))
}
func TestRcvdClose(t *testing.T) {
assert := assert.New(t)
mbox := New(&SliceStorage{})
assert.True(mbox.Send(1))
v, ok := mbox.Receive()
assert.True(ok)
assert.Equal(1, v)
mbox.Close()
<-time.After(time.Millisecond * 30)
assert.False(mbox.Send(1, time.Millisecond*10))
v, ok = mbox.Receive()
assert.False(ok)
assert.Nil(v)
v, ok = mbox.Receive(time.Millisecond * 10)
assert.False(ok)
assert.Nil(v)
}
func TestRcvdCloseTimeout(t *testing.T) {
assert := assert.New(t)
mbox := New(&SliceStorage{})
assert.True(mbox.Send(1))
v, ok := mbox.Receive()
assert.True(ok)
assert.Equal(1, v)
v, ok = mbox.Receive(time.Millisecond * 10)
assert.False(ok)
assert.Nil(v)
}
func Example() {
mbox := New(&SliceStorage{})
mbox.Send("VAL")
v, _ := mbox.Receive()
fmt.Println(v)
// Output: VAL
}