mirror of
https://github.com/matrix-org/sliding-sync.git
synced 2025-03-10 13:37:11 +00:00
Don't race when generating new event IDs in tests
This commit is contained in:
parent
d7b70d0afe
commit
2ffa81ede2
@ -3,14 +3,24 @@ package testutils
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var eventIDCounter = 0
|
||||
var (
|
||||
eventIDCounter = 0
|
||||
eventIDMu sync.Mutex
|
||||
)
|
||||
|
||||
func generateEventID() string {
|
||||
eventIDMu.Lock()
|
||||
defer eventIDMu.Unlock()
|
||||
eventIDCounter++
|
||||
return fmt.Sprintf("$event_%d", eventIDCounter)
|
||||
}
|
||||
|
||||
func NewStateEvent(t *testing.T, evType, stateKey, sender string, content interface{}) json.RawMessage {
|
||||
t.Helper()
|
||||
eventIDCounter++
|
||||
e := struct {
|
||||
Type string `json:"type"`
|
||||
StateKey string `json:"state_key"`
|
||||
@ -22,7 +32,27 @@ func NewStateEvent(t *testing.T, evType, stateKey, sender string, content interf
|
||||
StateKey: stateKey,
|
||||
Sender: sender,
|
||||
Content: content,
|
||||
EventID: fmt.Sprintf("$event_%d", eventIDCounter),
|
||||
EventID: generateEventID(),
|
||||
}
|
||||
j, err := json.Marshal(&e)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to make event JSON: %s", err)
|
||||
}
|
||||
return j
|
||||
}
|
||||
|
||||
func NewEvent(t *testing.T, evType, sender string, content interface{}) json.RawMessage {
|
||||
t.Helper()
|
||||
e := struct {
|
||||
Type string `json:"type"`
|
||||
Sender string `json:"sender"`
|
||||
Content interface{} `json:"content"`
|
||||
EventID string `json:"event_id"`
|
||||
}{
|
||||
Type: evType,
|
||||
Sender: sender,
|
||||
Content: content,
|
||||
EventID: generateEventID(),
|
||||
}
|
||||
j, err := json.Marshal(&e)
|
||||
if err != nil {
|
||||
|
Loading…
x
Reference in New Issue
Block a user