From c633a68afb66ebaf5742cde3689ba8b08bc349d4 Mon Sep 17 00:00:00 2001 From: Kenneth Jenkins <51246568+kenjenkins@users.noreply.github.com> Date: Tue, 15 Oct 2024 15:32:29 -0700 Subject: [PATCH] inmemory: add a test for the ListTypes race fix (#5327) Add a test that interleaves calls to Put() (with different type strings) and ListTypes(). At least on my machine, this appears to reliably detect the data race fixed in commit 2f8743522d7b60a6b3773fff5a13d6b0b76924ae when run with the Go race detector. (The 'make test' and 'make cover' targets run with the Go race detector enabled.) --- pkg/storage/inmemory/backend_test.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/pkg/storage/inmemory/backend_test.go b/pkg/storage/inmemory/backend_test.go index ebbdc7033..f47026a06 100644 --- a/pkg/storage/inmemory/backend_test.go +++ b/pkg/storage/inmemory/backend_test.go @@ -252,3 +252,17 @@ func TestLease(t *testing.T) { assert.True(t, ok, "expected b to to acquire the lease") } } + +// Concurrent calls to Put() and ListTypes() should not cause a data race. +func TestListTypes_concurrent(_ *testing.T) { + ctx := context.Background() + backend := New() + for i := 0; i < 10; i++ { + t := fmt.Sprintf("Type-%02d", i) + go backend.Put(ctx, []*databroker.Record{{ + Id: "1", + Type: t, + }}) + go backend.ListTypes(ctx) + } +}