Skip to content

Commit

Permalink
SDK Conformance test - fix parallel recordRequests (googleforgames#1456)
Browse files Browse the repository at this point in the history
Add Unit Test to cover the race condition.

Co-authored-by: Mark Mandel <markmandel@google.com>
  • Loading branch information
2 people authored and ilkercelikyilmaz committed Oct 23, 2020
1 parent 117e48a commit 779388c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/sdkserver/localsdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ type LocalSDKServer struct {
gs *sdk.GameServer
update chan struct{}
updateObservers sync.Map
testMutex sync.Mutex
requestSequence []string
expectedSequence []string
gsState agonesv1.GameServerState
Expand All @@ -90,6 +91,7 @@ func NewLocalSDKServer(filePath string) (*LocalSDKServer, error) {
gs: defaultGs,
update: make(chan struct{}),
updateObservers: sync.Map{},
testMutex: sync.Mutex{},
requestSequence: make([]string, 0),
testMode: false,
gsState: agonesv1.GameServerStateScheduled,
Expand Down Expand Up @@ -164,6 +166,8 @@ func (l *LocalSDKServer) SetExpectedSequence(sequence []string) {
// recordRequest append request name to slice
func (l *LocalSDKServer) recordRequest(request string) {
if l.testMode {
l.testMutex.Lock()
defer l.testMutex.Unlock()
l.requestSequence = append(l.requestSequence, request)
}
}
Expand All @@ -185,6 +189,8 @@ func (l *LocalSDKServer) recordRequestWithValue(request string, value string, ob
}

if value == fieldVal {
l.testMutex.Lock()
defer l.testMutex.Unlock()
l.requestSequence = append(l.requestSequence, request)
} else {
fmt.Printf("Error: we expected to receive '%s' as value for '%s' request but received '%s'. \n", fieldVal, request, value)
Expand Down
37 changes: 37 additions & 0 deletions pkg/sdkserver/localsdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package sdkserver
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"sync"
Expand Down Expand Up @@ -380,3 +381,39 @@ func TestLocalSDKServerStateUpdates(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, gs.Status.State, string(agonesv1.GameServerStateShutdown))
}

// TestSDKConformanceFunctionality - run a number of record requests in parallel
func TestSDKConformanceFunctionality(t *testing.T) {
t.Parallel()

l, err := NewLocalSDKServer("")
assert.Nil(t, err)
l.testMode = true
l.recordRequest("")
l.gs = &sdk.GameServer{ObjectMeta: &sdk.GameServer_ObjectMeta{Name: "empty"}}
exampleUID := "052fb0f4-3d50-11e5-b066-42010af0d7b6"
// field which is tested
setAnnotation := "setannotation"
l.gs.ObjectMeta.Uid = exampleUID

expected := []string{}
expected = append(expected, "", setAnnotation)

wg := sync.WaitGroup{}
for i := 0; i < 20; i++ {
wg.Add(1)
str := fmt.Sprintf("%d", i)
expected = append(expected, str)

go func() {
l.recordRequest(str)
l.recordRequestWithValue(setAnnotation, exampleUID, "UID")
wg.Done()
}()
}
wg.Wait()

l.SetExpectedSequence(expected)
b := EqualSets(l.expectedSequence, l.requestSequence)
assert.True(t, b, "we should receive strings from all go routines %v %v", l.expectedSequence, l.requestSequence)
}

0 comments on commit 779388c

Please sign in to comment.