File size: 7,629 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package ctxlock_test

import (
	"context"
	"sync/atomic"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"github.com/target/goalert/ctxlock"
)

func TestIDLocker_NoQueue(t *testing.T) {
	assert.Panics(t, func() {
		ctxlock.NewIDLocker[string](ctxlock.Config{MaxHeld: -1})
	}, "MaxHeld must be >= 0")

	l := ctxlock.NewIDLocker[string](ctxlock.Config{})

	ctx := context.Background()

	err := l.Lock(ctx, "foo")
	require.NoError(t, err, "first lock should work")

	err = l.Lock(ctx, "foo")
	require.ErrorIs(t, err, ctxlock.ErrQueueFull, "second lock should fail with ErrQueueFull")

	err = l.Lock(ctx, "bar")
	require.NoError(t, err, "lock for different id should work")

	l.Unlock("foo")
	err = l.Lock(ctx, "foo")
	require.NoError(t, err, "third lock should work")

	l.Unlock("foo")
	require.Panics(t, func() {
		l.Unlock("foo") // too many unlocks
	})
}

func TestIDLocker_Context(t *testing.T) {
	l := ctxlock.NewIDLocker[string](ctxlock.Config{MaxWait: 1})

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	err := l.Lock(ctx, "foo")
	require.NoError(t, err, "first lock should work")

	ch := make(chan error, 2)
	go func() { ch <- l.Lock(ctx, "foo") }()
	go func() { ch <- l.Lock(ctx, "foo") }()

	err = <-ch
	require.ErrorIs(t, err, ctxlock.ErrQueueFull, "second lock should fail with ErrQueueFull")
	l.Unlock("foo")

	err = <-ch
	require.NoError(t, err, "third lock should work")

	cancel()
	err = l.Lock(ctx, "foo")
	require.ErrorIs(t, err, context.Canceled, "lock should fail with context canceled")
}

func TestIDLocker_Timeout(t *testing.T) {
	l := ctxlock.NewIDLocker[string](ctxlock.Config{MaxWait: 1, Timeout: time.Millisecond})

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	err := l.Lock(ctx, "foo")
	require.NoError(t, err, "first lock should work")

	ch := make(chan error, 1)
	go func() { ch <- l.Lock(ctx, "foo") }()

	err = <-ch
	require.ErrorIs(t, err, ctxlock.ErrTimeout, "third lock should fail with ErrTimeout")
}

func TestIDLocker_CancelQueue(t *testing.T) {
	l := ctxlock.NewIDLocker[string](ctxlock.Config{MaxWait: 10, Timeout: time.Second})

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	err := l.Lock(ctx, "foo")
	require.NoError(t, err, "first lock should work")

	ch := make(chan error)
	nnn, nCancel := context.WithCancel(ctx)
	// We need to test that canceling items in the queue works.
	// Our nnn will be the one we cancel. We're going to fill the queue
	// with these, then Unlock a few times to make new space, fill it with
	// ones waiting on ctx, then do again, then cancel nnn.
	//
	// Our queue will look like this:
	// [nnn, nnn, nnn, ctx, ctx, nnn, ctx, ctx, ctx, ctx]
	//
	// The only way we can be sure the queue is full, is to push items until
	// we get ErrQueueFull. Doing so will allow us to test the logic of the
	// concurrent queue, without relying on timing.
	//
	// Note: all the `go func() { ... }` calls can run in any order, so we
	// use the ErrQueueFull to guarantee that we've filled/processed all the
	// items we want to test before pushing more.
	for i := 0; i < 11; i++ {
		go func() { ch <- l.Lock(nnn, "foo") }()
	}
	require.ErrorIs(t, <-ch, ctxlock.ErrQueueFull) // queue overflow
	// Queue now looks like this (all nnn):
	// [nnn, nnn, nnn, nnn, nnn, nnn, nnn, nnn, nnn, nnn, nnn]

	// We need to make space for the two ctx items (position 3 and 4).
	l.Unlock("foo")
	l.Unlock("foo")
	require.NoError(t, <-ch)
	require.NoError(t, <-ch)

	// Attempt to push 3 ctx items.
	go func() { ch <- l.Lock(ctx, "foo") }()
	go func() { ch <- l.Lock(ctx, "foo") }()
	go func() { ch <- l.Lock(ctx, "foo") }()
	require.ErrorIs(t, <-ch, ctxlock.ErrQueueFull) // queue overflow on the last
	// Queue now should look like this:
	// [nnn, nnn, nnn, nnn, nnn, nnn, nnn, nnn, ctx, ctx]

	// Make one space, push two nnn items.
	// One of them will randomly end up in the last spot, and the other
	// will be rejected with ErrQueueFull.
	l.Unlock("foo")
	require.NoError(t, <-ch)
	go func() { ch <- l.Lock(nnn, "foo") }()
	go func() { ch <- l.Lock(nnn, "foo") }()
	require.ErrorIs(t, <-ch, ctxlock.ErrQueueFull) // queue overflow
	// Queue now looks like this:
	// [nnn, nnn, nnn, nnn, nnn, nnn, nnn, ctx, ctx, nnn]

	// We want that last nnn to move from position 9 to position 5.

	l.Unlock("foo")
	l.Unlock("foo")
	l.Unlock("foo")
	l.Unlock("foo")
	require.NoError(t, <-ch)
	require.NoError(t, <-ch)
	require.NoError(t, <-ch)
	require.NoError(t, <-ch)

	// Queue now looks like this:
	// [nnn, nnn, nnn, ctx, ctx, nnn]
	for i := 0; i < 5; i++ {
		go func() { ch <- l.Lock(ctx, "foo") }()
	}
	require.ErrorIs(t, <-ch, ctxlock.ErrQueueFull) // queue overflow
	// Queue now looks like this:
	// [nnn, nnn, nnn, ctx, ctx, nnn, ctx, ctx, ctx, ctx]

	// Now we can cancel nnn, and ensure we get our 4 canceled items.
	nCancel()
	for i := 0; i < 4; i++ {
		require.ErrorIs(t, <-ch, context.Canceled)
	}

	// At this point, we should have 6 items in the queue.
	// We can now unlock 6 times, and ensure we get 6 items.
	for i := 0; i < 6; i++ {
		l.Unlock("foo")
		require.NoError(t, <-ch)
	}

	l.Unlock("foo") // original lock

	// We should now have an empty queue, and no held lock.
	require.Panics(t, func() { l.Unlock("foo") }, "unlocking an empty queue should panic")
}

func TestIDLocker_Unlock_Abandoned(t *testing.T) {
	l := ctxlock.NewIDLocker[string](ctxlock.Config{
		MaxWait: 1,
		Timeout: time.Second, // in case of bug, we don't want to wait forever
	})

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	err := l.Lock(ctx, "foo")
	require.NoError(t, err, "first lock should work")

	test := func() {
		nnn, cancel := context.WithCancel(ctx)
		ch := make(chan error, 1)
		go func() { ch <- l.Lock(nnn, "foo") }()
		go func() { ch <- l.Lock(nnn, "foo") }()
		require.ErrorIs(t, <-ch, ctxlock.ErrQueueFull, "queue should be full")

		cancel()
		l.Unlock("foo")

		if <-ch != nil {
			// lost the race, re-lock foo
			err := l.Lock(ctx, "foo")
			require.NoError(t, err, "re-lock should work")
		}
	}

	for i := 0; i < 1000; i++ {
		test()
	}

	l.Unlock("foo") // original lock
	assert.Panics(t, func() { l.Unlock("foo") }, "unlocking an empty queue should panic")
}

func BenchmarkIDLocker_Sequential(b *testing.B) {
	l := ctxlock.NewIDLocker[struct{}](ctxlock.Config{})
	ctx := context.Background()
	for i := 0; i < b.N; i++ {
		err := l.Lock(ctx, struct{}{})
		if err != nil {
			b.Fatal(err)
		}
		l.Unlock(struct{}{})
	}
}

func BenchmarkIDLocker_Sequential_Cardinality(b *testing.B) {
	l := ctxlock.NewIDLocker[int64](ctxlock.Config{})
	ctx := context.Background()
	var n int64
	for i := 0; i < b.N; i++ {
		err := l.Lock(ctx, n)
		if err != nil {
			b.Fatal(err)
		}
		n++
		if n > 100 {
			l.Unlock(n - 100)
		}
	}
}

func BenchmarkIDLocker_Concurrent(b *testing.B) {
	l := ctxlock.NewIDLocker[struct{}](ctxlock.Config{MaxWait: -1})
	ctx := context.Background()

	b.SetParallelism(1000)
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			err := l.Lock(ctx, struct{}{})
			require.NoError(b, err)
			l.Unlock(struct{}{})
		}
	})
}

func BenchmarkIDLocker_Concurrent_Cardinality(b *testing.B) {
	l := ctxlock.NewIDLocker[int64](ctxlock.Config{MaxWait: 1})
	ctx := context.Background()

	b.SetParallelism(1000)
	var n int64
	b.RunParallel(func(pb *testing.PB) {
		id := atomic.AddInt64(&n, 1)
		ch := make(chan error, 1)
		for pb.Next() {
			err := l.Lock(ctx, id)
			require.NoError(b, err)
			go func() { ch <- l.Lock(ctx, id) }()
			l.Unlock(id)
			require.NoError(b, <-ch)
			l.Unlock(id)
		}
	})
}