11package goob_test
22
33import (
4+ "context"
45 "math/rand"
56 "reflect"
67 "runtime"
@@ -10,10 +11,11 @@ import (
1011 "time"
1112
1213 "github.com/ysmood/goob"
14+ "github.com/ysmood/gotrace"
1315)
1416
1517func checkLeak (t * testing.T ) {
16- // testleak.Check (t, 0)
18+ gotrace . CheckLeak (t , 0 )
1719}
1820
1921type null struct {}
@@ -27,9 +29,11 @@ func eq(t *testing.T, expected, actual interface{}) {
2729func TestNew (t * testing.T ) {
2830 checkLeak (t )
2931
30- ob := goob .New ()
31- defer ob .Close ()
32- s := ob .Subscribe ()
32+ ctx , cancel := context .WithCancel (context .Background ())
33+ defer t .Cleanup (cancel )
34+
35+ ob := goob .New (ctx )
36+ s := ob .Subscribe (ctx )
3337 size := 1000
3438
3539 expected := []int {}
@@ -51,31 +55,31 @@ func TestNew(t *testing.T) {
5155 eq (t , expected , result )
5256}
5357
54- func TestUnsubscribe (t * testing.T ) {
58+ func TestCancel (t * testing.T ) {
5559 checkLeak (t )
5660
57- ob := goob .New ()
58-
59- s := ob .Subscribe ()
60- ob .Unsubscribe (s )
61+ ob := goob .New (context .Background ())
6162
63+ ctx , cancel := context .WithCancel (context .Background ())
64+ ob .Subscribe (ctx )
65+ cancel ()
6266 time .Sleep (10 * time .Millisecond )
63-
6467 eq (t , ob .Len (), 0 )
6568}
6669
6770func TestClosed (t * testing.T ) {
6871 checkLeak (t )
6972
70- ob := goob .New ()
71- ob .Subscribe ()
72- ob .Close ()
73+ ctx , cancel := context .WithCancel (context .Background ())
7374
74- s := ob .Subscribe ()
75+ ob := goob .New (ctx )
76+ ob .Subscribe (ctx )
77+ cancel ()
78+
79+ s := ob .Subscribe (context .Background ())
7580 _ , ok := <- s
7681
7782 ob .Publish (1 )
78- ob .Unsubscribe (s )
7983
8084 eq (t , ok , false )
8185 eq (t , ob .Len (), 0 )
@@ -84,11 +88,13 @@ func TestClosed(t *testing.T) {
8488func TestMultipleConsumers (t * testing.T ) {
8589 checkLeak (t )
8690
87- ob := goob .New ()
88- defer ob .Close ()
89- s1 := ob .Subscribe ()
90- s2 := ob .Subscribe ()
91- s3 := ob .Subscribe ()
91+ ctx , cancel := context .WithCancel (context .Background ())
92+ defer t .Cleanup (cancel )
93+
94+ ob := goob .New (ctx )
95+ s1 := ob .Subscribe (ctx )
96+ s2 := ob .Subscribe (ctx )
97+ s3 := ob .Subscribe (ctx )
9298 size := 1000
9399
94100 expected := []int {}
@@ -136,9 +142,11 @@ func TestMultipleConsumers(t *testing.T) {
136142func TestSlowConsumer (t * testing.T ) {
137143 checkLeak (t )
138144
139- ob := goob .New ()
140- defer ob .Close ()
141- s := ob .Subscribe ()
145+ ctx , cancel := context .WithCancel (context .Background ())
146+ defer t .Cleanup (cancel )
147+
148+ ob := goob .New (ctx )
149+ s := ob .Subscribe (ctx )
142150
143151 ob .Publish (1 )
144152
@@ -160,9 +168,11 @@ func TestMonkey(t *testing.T) {
160168 run := func () {
161169 defer wg .Done ()
162170
163- ob := goob .New ()
164- defer ob .Close ()
165- s := ob .Subscribe ()
171+ ctx , cancel := context .WithCancel (context .Background ())
172+ defer cancel ()
173+
174+ ob := goob .New (ctx )
175+ s := ob .Subscribe (ctx )
166176
167177 go func () {
168178 for range make ([]null , size ) {
@@ -198,9 +208,11 @@ func TestMonkey(t *testing.T) {
198208}
199209
200210func BenchmarkPublish (b * testing.B ) {
201- ob := goob .New ()
202- defer ob .Close ()
203- s := ob .Subscribe ()
211+ ctx , cancel := context .WithCancel (context .Background ())
212+ defer b .Cleanup (cancel )
213+
214+ ob := goob .New (ctx )
215+ s := ob .Subscribe (ctx )
204216
205217 for i := 0 ; i < runtime .NumCPU (); i ++ {
206218 go func () {
@@ -219,9 +231,11 @@ func BenchmarkPublish(b *testing.B) {
219231}
220232
221233func BenchmarkConsume (b * testing.B ) {
222- ob := goob .New ()
223- defer ob .Close ()
224- s := ob .Subscribe ()
234+ ctx , cancel := context .WithCancel (context .Background ())
235+ defer b .Cleanup (cancel )
236+
237+ ob := goob .New (ctx )
238+ s := ob .Subscribe (ctx )
225239
226240 for i := 0 ; i < b .N ; i ++ {
227241 ob .Publish (nil )
0 commit comments