Use atomic.Bool rather than bool in tests to fix race detector issues

This commit is contained in:
Kegan Dougal 2024-03-11 10:22:18 +00:00
parent cfff8bccb7
commit c7dd361ca6
2 changed files with 12 additions and 10 deletions

View File

@ -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

View File

@ -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