Skip to content

Commit 6527048

Browse files
committed
Use non pointer mutex for lobby
We get a warning when trying to copy the lobby anyway. And this way we don't have to manually initialise it.
1 parent 9420893 commit 6527048

File tree

5 files changed

+13
-22
lines changed

5 files changed

+13
-22
lines changed

internal/game/data.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ type Lobby struct {
8787
// it is empty.
8888
LastPlayerDisconnectTime *time.Time
8989

90-
mutex *sync.Mutex
90+
mutex sync.Mutex
9191

9292
WriteObject func(*Player, easyjson.Marshaler) error
9393
WritePreparedMessage func(*Player, *gws.Broadcaster) error

internal/game/data_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
package game
22

33
import (
4-
"sync"
54
"testing"
65
"time"
76
)
87

98
func TestOccupiedPlayerCount(t *testing.T) {
109
t.Parallel()
1110

12-
lobby := &Lobby{
13-
mutex: &sync.Mutex{},
14-
}
11+
lobby := &Lobby{}
1512
if lobby.GetOccupiedPlayerSlots() != 0 {
1613
t.Errorf("Occupied player count expected to be 0, but was %d", lobby.GetOccupiedPlayerSlots())
1714
}

internal/game/lobby.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"math/rand/v2"
99
"sort"
1010
"strings"
11-
"sync"
1211
"time"
1312
"unicode/utf8"
1413

@@ -988,7 +987,6 @@ func CreateLobby(
988987
CustomWords: customWords,
989988
currentDrawing: make([]any, 0),
990989
State: Unstarted,
991-
mutex: &sync.Mutex{},
992990
}
993991

994992
if len(customWords) > 1 {

internal/game/lobby_test.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package game
33
import (
44
"encoding/json"
55
"reflect"
6-
"sync"
76
"testing"
87
"unsafe"
98

@@ -19,7 +18,6 @@ func createLobbyWithDemoPlayers(playercount int) *Lobby {
1918
lobby := &Lobby{
2019
Owner: owner,
2120
creator: owner,
22-
mutex: &sync.Mutex{},
2321
}
2422
for i := 0; i < playercount; i++ {
2523
lobby.players = append(lobby.players, &Player{
@@ -38,6 +36,16 @@ func noOpWritePreparedMessage(_ *Player, _ *gws.Broadcaster) error {
3836
return nil
3937
}
4038

39+
func Test_Locking(t *testing.T) {
40+
t.Parallel()
41+
42+
lobby := &Lobby{}
43+
lobby.mutex.Lock()
44+
if lobby.mutex.TryLock() {
45+
t.Error("Mutex shouldn't be aqcuiredable at this point")
46+
}
47+
}
48+
4149
func Test_CalculateVotesNeededToKick(t *testing.T) {
4250
t.Parallel()
4351

@@ -138,9 +146,7 @@ func Test_simplifyText(t *testing.T) {
138146
func Test_recalculateRanks(t *testing.T) {
139147
t.Parallel()
140148

141-
lobby := &Lobby{
142-
mutex: &sync.Mutex{},
143-
}
149+
lobby := &Lobby{}
144150
lobby.players = append(lobby.players, &Player{
145151
ID: uuid.Must(uuid.NewV4()),
146152
Score: 1,
@@ -228,7 +234,6 @@ func Test_wordSelectionEvent(t *testing.T) {
228234
t.Parallel()
229235

230236
lobby := &Lobby{
231-
mutex: &sync.Mutex{},
232237
EditableLobbySettings: EditableLobbySettings{
233238
DrawingTime: 10,
234239
Rounds: 10,
@@ -326,7 +331,6 @@ func Test_kickDrawer(t *testing.T) {
326331
t.Parallel()
327332

328333
lobby := &Lobby{
329-
mutex: &sync.Mutex{},
330334
EditableLobbySettings: EditableLobbySettings{
331335
DrawingTime: 10,
332336
Rounds: 10,

internal/game/words_test.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"fmt"
66
"strings"
7-
"sync"
87
"testing"
98

109
"github.com/stretchr/testify/assert"
@@ -90,7 +89,6 @@ func Test_getRandomWords(t *testing.T) {
9089
CustomWordsPerTurn: 0,
9190
},
9291
words: []string{"a", "b", "c"},
93-
mutex: &sync.Mutex{},
9492
}
9593

9694
randomWords := GetRandomWords(3, lobby)
@@ -112,7 +110,6 @@ func Test_getRandomWords(t *testing.T) {
112110
},
113111

114112
CustomWords: []string{"d", "e", "f"},
115-
mutex: &sync.Mutex{},
116113
}
117114

118115
randomWords := GetRandomWords(3, lobby)
@@ -133,7 +130,6 @@ func Test_getRandomWords(t *testing.T) {
133130
CustomWordsPerTurn: 3,
134131
},
135132
CustomWords: nil,
136-
mutex: &sync.Mutex{},
137133
}
138134

139135
randomWords := GetRandomWords(3, lobby)
@@ -154,7 +150,6 @@ func Test_getRandomWords(t *testing.T) {
154150
CustomWordsPerTurn: 3,
155151
},
156152
CustomWords: []string{"d", "e", "f"},
157-
mutex: &sync.Mutex{},
158153
}
159154

160155
randomWords := GetRandomWords(3, lobby)
@@ -182,7 +177,6 @@ func Test_getRandomWordsReloading(t *testing.T) {
182177
CustomWordsPerTurn: 0,
183178
},
184179
CustomWords: nil,
185-
mutex: &sync.Mutex{},
186180
}
187181

188182
// Running this 10 times, expecting it to get 3 words each time, even
@@ -204,7 +198,6 @@ func Test_getRandomWordsReloading(t *testing.T) {
204198
CustomWordsPerTurn: 3,
205199
},
206200
CustomWords: nil,
207-
mutex: &sync.Mutex{},
208201
}
209202

210203
// Running this 10 times, expecting it to get 3 words each time, even
@@ -226,7 +219,6 @@ func Test_getRandomWordsReloading(t *testing.T) {
226219
CustomWordsPerTurn: 3,
227220
},
228221
CustomWords: []string{"a"},
229-
mutex: &sync.Mutex{},
230222
}
231223

232224
// Running this 10 times, expecting it to get 3 words each time, even

0 commit comments

Comments
 (0)