Skip to content

Commit

Permalink
Use atomic.Bool rather than bool in tests to fix race detector issues
Browse files Browse the repository at this point in the history
  • Loading branch information
kegsay committed Mar 11, 2024
1 parent cfff8bc commit c7dd361
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
9 changes: 5 additions & 4 deletions sync3/connmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"reflect"
"sort"
"sync/atomic"
"testing"
"time"

Expand Down Expand Up @@ -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
}

Expand All @@ -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
Expand Down
13 changes: 7 additions & 6 deletions tests-integration/poller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -89,7 +90,7 @@ func TestSecondPollerFiltersToDevice(t *testing.T) {
}
}

seenInitialRequest = true
seenInitialRequest.Store(true)
})

wantMsg := json.RawMessage(`{"type":"f","content":{"f":"b"}}`)
Expand All @@ -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
Expand Down

0 comments on commit c7dd361

Please sign in to comment.