From c7dd361ca64b1f84472c4842eb550f9dc83a09b2 Mon Sep 17 00:00:00 2001 From: Kegan Dougal <7190048+kegsay@users.noreply.github.com> Date: Mon, 11 Mar 2024 10:22:18 +0000 Subject: [PATCH] Use atomic.Bool rather than bool in tests to fix race detector issues --- sync3/connmap_test.go | 9 +++++---- tests-integration/poller_test.go | 13 +++++++------ 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/sync3/connmap_test.go b/sync3/connmap_test.go index 8fbcf8cd..adf3aaf4 100644 --- a/sync3/connmap_test.go +++ b/sync3/connmap_test.go @@ -5,6 +5,7 @@ import ( "fmt" "reflect" "sort" + "sync/atomic" "testing" "time" @@ -201,15 +202,15 @@ func assertDestroyedConns(t *testing.T, cidToConn map[ConnID]*Conn, isDestroyedF t.Helper() for cid, conn := range cidToConn { if isDestroyedFn(cid) { - mustEqual(t, conn.handler.(*mockConnHandler).isDestroyed, true, fmt.Sprintf("conn %+v was not destroyed", cid)) + mustEqual(t, conn.handler.(*mockConnHandler).isDestroyed.Load(), true, fmt.Sprintf("conn %+v was not destroyed", cid)) } else { - mustEqual(t, conn.handler.(*mockConnHandler).isDestroyed, false, fmt.Sprintf("conn %+v was destroyed", cid)) + mustEqual(t, conn.handler.(*mockConnHandler).isDestroyed.Load(), false, fmt.Sprintf("conn %+v was destroyed", cid)) } } } type mockConnHandler struct { - isDestroyed bool + isDestroyed atomic.Bool cancel context.CancelFunc } @@ -219,7 +220,7 @@ func (c *mockConnHandler) OnIncomingRequest(ctx context.Context, cid ConnID, req func (c *mockConnHandler) OnUpdate(ctx context.Context, update caches.Update) {} func (c *mockConnHandler) PublishEventsUpTo(roomID string, nid int64) {} func (c *mockConnHandler) Destroy() { - c.isDestroyed = true + c.isDestroyed.Store(true) } func (c *mockConnHandler) Alive() bool { return true // buffer never fills up diff --git a/tests-integration/poller_test.go b/tests-integration/poller_test.go index 5ce3b4dd..3c3fae06 100644 --- a/tests-integration/poller_test.go +++ b/tests-integration/poller_test.go @@ -4,14 +4,15 @@ import ( "context" "encoding/json" "fmt" - "github.com/jmoiron/sqlx" - "github.com/matrix-org/sliding-sync/sqlutil" "net/http" "os" "sync/atomic" "testing" "time" + "github.com/jmoiron/sqlx" + "github.com/matrix-org/sliding-sync/sqlutil" + "github.com/matrix-org/sliding-sync/sync2" "github.com/matrix-org/sliding-sync/sync3" "github.com/matrix-org/sliding-sync/sync3/extensions" @@ -45,7 +46,7 @@ func TestSecondPollerFiltersToDevice(t *testing.T) { // now sync with device B, and check we send the filter up deviceBToken := "DEVICE_B_TOKEN" v2.addAccountWithDeviceID(alice, "B", deviceBToken) - seenInitialRequest := false + var seenInitialRequest atomic.Bool v2.SetCheckRequest(func(token string, req *http.Request) { if token != deviceBToken { return @@ -62,7 +63,7 @@ func TestSecondPollerFiltersToDevice(t *testing.T) { timelineLimit := filterJSON.Get("room.timeline.limit").Int() roomsFilter := filterJSON.Get("room.rooms") - if !seenInitialRequest { + if !seenInitialRequest.Load() { // First poll: should be an initial sync, limit 1, excluding all room timelines. if since != "" { t.Errorf("Expected no since token on first poll, but got %v", since) @@ -89,7 +90,7 @@ func TestSecondPollerFiltersToDevice(t *testing.T) { } } - seenInitialRequest = true + seenInitialRequest.Store(true) }) wantMsg := json.RawMessage(`{"type":"f","content":{"f":"b"}}`) @@ -110,7 +111,7 @@ func TestSecondPollerFiltersToDevice(t *testing.T) { }, }) - if !seenInitialRequest { + if !seenInitialRequest.Load() { t.Fatalf("did not see initial request for 2nd device") } // the first request will not wait for the response before returning due to device A. Poll again