| 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") |
| }) |
| } |
|
|
| 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) |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| for i := 0; i < 11; i++ { |
| go func() { ch <- l.Lock(nnn, "foo") }() |
| } |
| require.ErrorIs(t, <-ch, ctxlock.ErrQueueFull) |
| |
| |
|
|
| |
| l.Unlock("foo") |
| l.Unlock("foo") |
| require.NoError(t, <-ch) |
| require.NoError(t, <-ch) |
|
|
| |
| 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) |
| |
| |
|
|
| |
| |
| |
| 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) |
| |
| |
|
|
| |
|
|
| 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) |
|
|
| |
| |
| for i := 0; i < 5; i++ { |
| go func() { ch <- l.Lock(ctx, "foo") }() |
| } |
| require.ErrorIs(t, <-ch, ctxlock.ErrQueueFull) |
| |
| |
|
|
| |
| nCancel() |
| for i := 0; i < 4; i++ { |
| require.ErrorIs(t, <-ch, context.Canceled) |
| } |
|
|
| |
| |
| for i := 0; i < 6; i++ { |
| l.Unlock("foo") |
| require.NoError(t, <-ch) |
| } |
|
|
| l.Unlock("foo") |
|
|
| |
| 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, |
| }) |
|
|
| 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 { |
| |
| err := l.Lock(ctx, "foo") |
| require.NoError(t, err, "re-lock should work") |
| } |
| } |
|
|
| for i := 0; i < 1000; i++ { |
| test() |
| } |
|
|
| l.Unlock("foo") |
| 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) |
| } |
| }) |
| } |
|
|