-
Notifications
You must be signed in to change notification settings - Fork 8
/
subscription_test.go
58 lines (44 loc) · 1.33 KB
/
subscription_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
package kcache
import (
"context"
"testing"
logutil "github.com/boz/go-logutil"
"github.com/boz/kcache/filter"
"github.com/boz/kcache/testutil"
"github.com/stretchr/testify/assert"
)
func TestSubscription(t *testing.T) {
testDoTestSubscription(t, "close", func(s subscription, _ chan struct{}) { s.Close() })
testDoTestSubscription(t, "stopch", func(_ subscription, stopch chan struct{}) { close(stopch) })
}
func testDoTestSubscription(t *testing.T, name string, stopfn func(subscription, chan struct{})) {
log := logutil.Default()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
readych := make(chan struct{})
stopch := make(chan struct{})
cache := newCache(ctx, log, stopch, filter.Null())
sub := newSubscription(log, stopch, readych, cache)
defer sub.Close()
testutil.AssertNotDone(t, name, sub)
testutil.AssertNotReady(t, name, sub)
evt := testGenEvent(EventTypeCreate, "a", "b", "1")
sub.send(evt)
select {
case ev, ok := <-sub.Events():
assert.True(t, ok, name)
assert.Equal(t, evt, ev, name)
case <-testutil.AsyncWaitch(ctx):
assert.Fail(t, name)
}
stopfn(sub, stopch)
testutil.AssertDone(t, name, sub)
testutil.AssertNotReady(t, name, sub)
sub.send(evt)
select {
case _, ok := <-sub.Events():
assert.False(t, ok, name)
case <-testutil.AsyncWaitch(ctx):
assert.Fail(t, name)
}
}