-
Notifications
You must be signed in to change notification settings - Fork 3
/
lifecycle_test.go
60 lines (46 loc) · 1.48 KB
/
lifecycle_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
package lifecycle_test
import (
"context"
"testing"
"time"
example "github.com/boz/go-lifecycle/_example"
)
func TestLifecycle_shutdown(t *testing.T) {
cache := example.NewCache(context.Background())
runTestWithShutdown(t, cache, func() { cache.Shutdown(nil) }, "cache.Shutdown()")
}
func TestLifecycle_ctx_cancel(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cache := example.NewCache(ctx)
runTestWithShutdown(t, cache, cancel, "context.Cancel()")
}
func TestLifecycle_shutdownAsync(t *testing.T) {
cache := example.NewCache(context.Background())
runTestWithShutdown(t, cache, func() { cache.ShutdownAsync(nil) }, "cache.ShutdownAsync()")
}
func runTestWithShutdown(t *testing.T, cache example.Cache, stopfn func(), msg string) {
if err := cache.Put("foo", "bar"); err != nil {
t.Errorf("%v: unable to put before shutdown: %v", msg, err)
}
if _, err := cache.Get("foo"); err != nil {
t.Errorf("%v: unable to get before shutdown: %v", msg, err)
}
select {
case <-cache.Done():
t.Errorf("%v: done readable before shutdown", msg)
default:
}
stopfn()
select {
case <-cache.Done():
case <-time.After(time.Millisecond * 10):
t.Error("shutdown not completed after 10ms")
}
if err := cache.Put("foo", "bar"); err != example.ErrNotRunning {
t.Errorf("%v: invalid err after shutdown: %v", msg, err)
}
if _, err := cache.Get("foo"); err != example.ErrNotRunning {
t.Errorf("%v: invalid err after shutdown: %v", msg, err)
}
}