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" "fmt"
"reflect" "reflect"
"sort" "sort"
"sync/atomic"
"testing" "testing"
"time" "time"
@ -201,15 +202,15 @@ func assertDestroyedConns(t *testing.T, cidToConn map[ConnID]*Conn, isDestroyedF
t.Helper() t.Helper()
for cid, conn := range cidToConn { for cid, conn := range cidToConn {
if isDestroyedFn(cid) { 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 { } 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 { type mockConnHandler struct {
isDestroyed bool isDestroyed atomic.Bool
cancel context.CancelFunc 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) OnUpdate(ctx context.Context, update caches.Update) {}
func (c *mockConnHandler) PublishEventsUpTo(roomID string, nid int64) {} func (c *mockConnHandler) PublishEventsUpTo(roomID string, nid int64) {}
func (c *mockConnHandler) Destroy() { func (c *mockConnHandler) Destroy() {
c.isDestroyed = true c.isDestroyed.Store(true)
} }
func (c *mockConnHandler) Alive() bool { func (c *mockConnHandler) Alive() bool {
return true // buffer never fills up return true // buffer never fills up

View File

@ -4,14 +4,15 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/jmoiron/sqlx"
"github.com/matrix-org/sliding-sync/sqlutil"
"net/http" "net/http"
"os" "os"
"sync/atomic" "sync/atomic"
"testing" "testing"
"time" "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/sync2"
"github.com/matrix-org/sliding-sync/sync3" "github.com/matrix-org/sliding-sync/sync3"
"github.com/matrix-org/sliding-sync/sync3/extensions" "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 // now sync with device B, and check we send the filter up
deviceBToken := "DEVICE_B_TOKEN" deviceBToken := "DEVICE_B_TOKEN"
v2.addAccountWithDeviceID(alice, "B", deviceBToken) v2.addAccountWithDeviceID(alice, "B", deviceBToken)
seenInitialRequest := false var seenInitialRequest atomic.Bool
v2.SetCheckRequest(func(token string, req *http.Request) { v2.SetCheckRequest(func(token string, req *http.Request) {
if token != deviceBToken { if token != deviceBToken {
return return
@ -62,7 +63,7 @@ func TestSecondPollerFiltersToDevice(t *testing.T) {
timelineLimit := filterJSON.Get("room.timeline.limit").Int() timelineLimit := filterJSON.Get("room.timeline.limit").Int()
roomsFilter := filterJSON.Get("room.rooms") roomsFilter := filterJSON.Get("room.rooms")
if !seenInitialRequest { if !seenInitialRequest.Load() {
// First poll: should be an initial sync, limit 1, excluding all room timelines. // First poll: should be an initial sync, limit 1, excluding all room timelines.
if since != "" { if since != "" {
t.Errorf("Expected no since token on first poll, but got %v", 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"}}`) 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") 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 // the first request will not wait for the response before returning due to device A. Poll again