Debian Codegraph @

pgconn/ctxwatch/context_watcher_test.go

raw
1 package ctxwatch_test
2
3 import (
4 "context"
5 "sync/atomic"
6 "testing"
7 "time"
8
9 "github.com/jackc/pgx/v5/pgconn/ctxwatch"
10 "github.com/stretchr/testify/require"
11 )
12
13 type testHandler struct {
14 handleCancel func(context.Context)
15 handleUnwatchAfterCancel func()
16 }
17
18 func (h *testHandler) HandleCancel(ctx context.Context) {
19 h.handleCancel(ctx)
20 }
21
22 func (h *testHandler) HandleUnwatchAfterCancel() {
23 h.handleUnwatchAfterCancel()
24 }
25
26 func TestContextWatcherContextCancelled(t *testing.T) {
27 canceledChan := make(chan struct{})
28 cleanupCalled := false
29 cw := ctxwatch.NewContextWatcher(&testHandler{
30 handleCancel: func(context.Context) {
31 canceledChan <- struct{}{}
32 }, handleUnwatchAfterCancel: func() {
33 cleanupCalled = true
34 },
35 })
36
37 ctx, cancel := context.WithCancel(context.Background())
38 cw.Watch(ctx)
39 cancel()
40
41 select {
42 case <-canceledChan:
43 case <-time.NewTimer(time.Second).C:
44 t.Fatal("Timed out waiting for cancel func to be called")
45 }
46
47 cw.Unwatch()
48
49 require.True(t, cleanupCalled, "Cleanup func was not called")
50 }
51
52 func TestContextWatcherUnwatchedBeforeContextCancelled(t *testing.T) {
53 cw := ctxwatch.NewContextWatcher(&testHandler{
54 handleCancel: func(context.Context) {
55 t.Error("cancel func should not have been called")
56 }, handleUnwatchAfterCancel: func() {
57 t.Error("cleanup func should not have been called")
58 },
59 })
60
61 ctx, cancel := context.WithCancel(context.Background())
62 cw.Watch(ctx)
63 cw.Unwatch()
64 cancel()
65 }
66
67 func TestContextWatcherMultipleWatchPanics(t *testing.T) {
68 cw := ctxwatch.NewContextWatcher(&testHandler{handleCancel: func(context.Context) {}, handleUnwatchAfterCancel: func() {}})
69
70 ctx, cancel := context.WithCancel(t.Context())
71 defer cancel()
72 cw.Watch(ctx)
73 defer cw.Unwatch()
74
75 ctx2, cancel2 := context.WithCancel(t.Context())
76 defer cancel2()
77 require.Panics(t, func() { cw.Watch(ctx2) }, "Expected panic when Watch called multiple times")
78 }
79
80 func TestContextWatcherUnwatchWhenNotWatchingIsSafe(t *testing.T) {
81 cw := ctxwatch.NewContextWatcher(&testHandler{handleCancel: func(context.Context) {}, handleUnwatchAfterCancel: func() {}})
82 cw.Unwatch() // unwatch when not / never watching
83
84 ctx := t.Context()
85 cw.Watch(ctx)
86 cw.Unwatch()
87 cw.Unwatch() // double unwatch
88 }
89
90 func TestContextWatcherUnwatchIsConcurrencySafe(t *testing.T) {
91 cw := ctxwatch.NewContextWatcher(&testHandler{handleCancel: func(context.Context) {}, handleUnwatchAfterCancel: func() {}})
92
93 ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
94 defer cancel()
95 cw.Watch(ctx)
96
97 go cw.Unwatch()
98 go cw.Unwatch()
99
100 <-ctx.Done()
101 }
102
103 func TestContextWatcherStress(t *testing.T) {
104 var cancelFuncCalls int64
105 var cleanupFuncCalls int64
106
107 cw := ctxwatch.NewContextWatcher(&testHandler{
108 handleCancel: func(context.Context) {
109 atomic.AddInt64(&cancelFuncCalls, 1)
110 }, handleUnwatchAfterCancel: func() {
111 atomic.AddInt64(&cleanupFuncCalls, 1)
112 },
113 })
114
115 cycleCount := 100_000
116
117 for i := range cycleCount {
118 ctx, cancel := context.WithCancel(context.Background())
119 defer cancel() // satisfy linter; cancel is idempotent
120 cw.Watch(ctx)
121 if i%2 == 0 {
122 cancel()
123 }
124
125 // Without time.Sleep, cw.Unwatch will almost always run before the cancel func which means cancel will never happen. This gives us a better mix.
126 if i%333 == 0 {
127 // on Windows Sleep takes more time than expected so we try to get here less frequently to avoid
128 // the CI takes a long time
129 time.Sleep(time.Nanosecond)
130 }
131
132 cw.Unwatch()
133 if i%2 == 1 {
134 cancel()
135 }
136 }
137
138 actualCancelFuncCalls := atomic.LoadInt64(&cancelFuncCalls)
139 actualCleanupFuncCalls := atomic.LoadInt64(&cleanupFuncCalls)
140
141 if actualCancelFuncCalls == 0 {
142 t.Fatal("actualCancelFuncCalls == 0")
143 }
144
145 maxCancelFuncCalls := int64(cycleCount) / 2
146 if actualCancelFuncCalls > maxCancelFuncCalls {
147 t.Errorf("cancel func calls should be no more than %d but was %d", actualCancelFuncCalls, maxCancelFuncCalls)
148 }
149
150 if actualCancelFuncCalls != actualCleanupFuncCalls {
151 t.Errorf("cancel func calls (%d) should be equal to cleanup func calls (%d) but was not", actualCancelFuncCalls, actualCleanupFuncCalls)
152 }
153 }
154
155 func BenchmarkContextWatcherUncancellable(b *testing.B) {
156 cw := ctxwatch.NewContextWatcher(&testHandler{handleCancel: func(context.Context) {}, handleUnwatchAfterCancel: func() {}})
157
158 for b.Loop() {
159 cw.Watch(context.Background())
160 cw.Unwatch()
161 }
162 }
163
164 func BenchmarkContextWatcherCancelled(b *testing.B) {
165 cw := ctxwatch.NewContextWatcher(&testHandler{handleCancel: func(context.Context) {}, handleUnwatchAfterCancel: func() {}})
166
167 for b.Loop() {
168 ctx, cancel := context.WithCancel(context.Background())
169 cw.Watch(ctx)
170 cancel()
171 cw.Unwatch()
172 }
173 }
174
175 func BenchmarkContextWatcherCancellable(b *testing.B) {
176 cw := ctxwatch.NewContextWatcher(&testHandler{handleCancel: func(context.Context) {}, handleUnwatchAfterCancel: func() {}})
177
178 ctx := b.Context()
179
180 for b.Loop() {
181 cw.Watch(ctx)
182 cw.Unwatch()
183 }
184 }