Skip to content

Commit

Permalink
Adds go conformance tests for Lists (#3524)
Browse files Browse the repository at this point in the history
  • Loading branch information
igooch committed Dec 1, 2023
1 parent fc192db commit 6fd270d
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 15 deletions.
2 changes: 1 addition & 1 deletion build/includes/sdk.mk
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ SDK_IMAGE_TAG=$(build_sdk_prefix)$(SDK_FOLDER):$(build_sdk_version)
DEFAULT_CONFORMANCE_TESTS = ready,allocate,setlabel,setannotation,gameserver,health,shutdown,watch,reserve
ALPHA_CONFORMANCE_TESTS = getplayercapacity,setplayercapacity,playerconnect,playerdisconnect,getplayercount,isplayerconnected,getconnectedplayers
# TODO: Move Counter and List tests into ALPHA_CONFORMANCE_TESTS once the they are written for all SDKs
COUNTS_AND_LISTS_TESTS = getcounter,updatecounter,setcountcounter,setcapacitycounter
COUNTS_AND_LISTS_TESTS = getcounter,updatecounter,setcountcounter,setcapacitycounter,getlist,updatelist,addlistvalue,removelistvalue

.PHONY: test-sdks test-sdk build-sdks build-sdk gen-all-sdk-grpc gen-sdk-grpc run-all-sdk-command run-sdk-command build-example

Expand Down
13 changes: 9 additions & 4 deletions pkg/sdkserver/localsdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,14 @@ func NewLocalSDKServer(filePath string, testSdkName string) (*LocalSDKServer, er
l.gs.Status.Players = &sdk.GameServer_Status_PlayerStatus{}
}
if runtime.FeatureEnabled(runtime.FeatureCountsAndLists) {
// Adding test Counter for the conformance test (Counters is not nil for LocalSDKServer tests)
// Adding test Counter and List for the conformance tests (not nil for LocalSDKServer tests)
if l.gs.Status.Counters == nil {
l.gs.Status.Counters = map[string]*sdk.GameServer_Status_CounterStatus{"conformanceTestCounter": {Count: 1, Capacity: 10}}
l.gs.Status.Counters = map[string]*sdk.GameServer_Status_CounterStatus{
"conformanceTestCounter": {Count: 1, Capacity: 10}}
}
if l.gs.Status.Lists == nil {
l.gs.Status.Lists = map[string]*sdk.GameServer_Status_ListStatus{
"conformanceTestList": {Values: []string{"test0", "test1", "test2"}, Capacity: 100}}
}
}

Expand Down Expand Up @@ -739,9 +744,9 @@ func (l *LocalSDKServer) UpdateList(ctx context.Context, in *alpha.UpdateListReq
fmutils.Prune(tmpList, in.UpdateMask.Paths)
// Due due filtering and pruning all gameserver object field(s) contained in the FieldMask are overwritten by the request object field(s).
proto.Merge(tmpList, in.List)
// Verify that Capacity >= len(tmpList.values)
// Silently truncate list values if Capacity < len(Values)
if tmpList.Capacity < int64(len(tmpList.Values)) {
return nil, errors.Errorf("out of range. Capacity must be great than or equal to the size of the List of values. Found Capacity: %d, List Size: %d", tmpList.Capacity, len(tmpList.Values))
tmpList.Values = append([]string{}, tmpList.Values[:tmpList.Capacity]...)
}
// Write newly updated List to gameserverstatus.
l.gs.Status.Lists[name].Capacity = tmpList.Capacity
Expand Down
6 changes: 5 additions & 1 deletion pkg/sdkserver/localsdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1082,7 +1082,11 @@ func TestLocalSDKServerUpdateList(t *testing.T) {
},
UpdateMask: &fieldmaskpb.FieldMask{Paths: []string{"capacity"}},
},
wantErr: errors.Errorf("out of range. Capacity must be great than or equal to the size of the List of values. Found Capacity: %d, List Size: %d", 1, 2),
want: &alpha.List{
Name: "models",
Capacity: int64(1),
Values: []string{"model1"},
},
},
}

Expand Down
68 changes: 59 additions & 9 deletions test/sdk/go/sdk-client-test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
pkgSdk "agones.dev/agones/pkg/sdk"
"agones.dev/agones/pkg/util/runtime"
goSdk "agones.dev/agones/sdks/go"
"github.com/google/go-cmp/cmp"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -99,7 +100,8 @@ func main() {
}

if runtime.FeatureEnabled(runtime.FeatureCountsAndLists) {
testCountsAndLists(sdk)
testCounts(sdk)
testLists(sdk)
}

err = sdk.Shutdown()
Expand Down Expand Up @@ -154,39 +156,87 @@ func testPlayerTracking(sdk *goSdk.SDK) {
}
}

func testCountsAndLists(sdk *goSdk.SDK) {
func testCounts(sdk *goSdk.SDK) {
// LocalSDKServer starting "conformanceTestCounter": {Count: 1, Capacity: 10}
count, err := sdk.Alpha().GetCounterCount("conformanceTestCounter")
counter := "conformanceTestCounter"
count, err := sdk.Alpha().GetCounterCount(counter)
if err != nil {
log.Fatalf("Error getting Counter count: %s", err)
} else if count != int64(1) {
log.Fatalf("Counter count should be 1, but is %d", count)
}

inc, err := sdk.Alpha().IncrementCounter("conformanceTestCounter", 9)
inc, err := sdk.Alpha().IncrementCounter(counter, 9)
if !inc {
log.Fatalf("Error incrementing Counter: %s", err)
}

dec, err := sdk.Alpha().DecrementCounter("conformanceTestCounter", 10)
dec, err := sdk.Alpha().DecrementCounter(counter, 10)
if !dec {
log.Fatalf("Error decrementing Counter: %s", err)
}

setCount, err := sdk.Alpha().SetCounterCount("conformanceTestCounter", 10)
setCount, err := sdk.Alpha().SetCounterCount(counter, 10)
if !setCount {
log.Fatalf("Error setting Counter count: %s", err)
}

capacity, err := sdk.Alpha().GetCounterCapacity("conformanceTestCounter")
capacity, err := sdk.Alpha().GetCounterCapacity(counter)
if err != nil {
log.Fatalf("Error getting Counter capacity: %s", err)
} else if capacity != int64(10) {
log.Fatalf("Counter capacity should be 10, but is %d", count)
log.Fatalf("Counter capacity should be 10, but is %d", capacity)
}

setCapacity, err := sdk.Alpha().SetCounterCapacity("conformanceTestCounter", 1)
setCapacity, err := sdk.Alpha().SetCounterCapacity(counter, 1)
if !setCapacity {
log.Fatalf("Error setting Counter capacity: %s", err)
}
}

func testLists(sdk *goSdk.SDK) {
// LocalSDKServer starting "conformanceTestList": {Values: []string{"test0", "test1", "test2"}, Capacity: 100}}
list := "conformanceTestList"
vals := []string{"test0", "test1", "test2"}

contains, err := sdk.Alpha().ListContains(list, "test1")
if !contains {
log.Fatalf("List should contain value \"test1\" err: %s", err)
}

length, err := sdk.Alpha().GetListLength(list)
if err != nil {
log.Fatalf("Error getting List length: %s", err)
} else if int64(length) != 3 {
log.Fatalf("List length should be 3, but is %d", length)
}

values, err := sdk.Alpha().GetListValues(list)
if err != nil {
log.Fatalf("Error getting List values: %s", err)
} else if !cmp.Equal(vals, values) {
log.Fatalf("List values should be %v, but is %v", vals, values)
}

appendValue, err := sdk.Alpha().AppendListValue(list, "test3")
if !appendValue {
log.Fatalf("Unable to append value \"test3\" err: %s", err)
}

deleteValue, err := sdk.Alpha().DeleteListValue(list, "test2")
if !deleteValue {
log.Fatalf("Unable to delete value \"test2\" err: %s", err)
}

capacity, err := sdk.Alpha().GetListCapacity(list)
if err != nil {
log.Fatalf("Error getting List capacity: %s", err)
} else if capacity != int64(100) {
log.Fatalf("List capacity should be 100, but is %d", capacity)
}

setCapacity, err := sdk.Alpha().SetListCapacity(list, 2)
if !setCapacity {
log.Fatalf("Error setting List capacity: %s", err)
}
}

0 comments on commit 6fd270d

Please sign in to comment.