agones.dev/agones@v1.54.0/sdks/go/alpha_test.go (about) 1 // Copyright 2020 Google LLC All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package sdk 16 17 import ( 18 "context" 19 "testing" 20 21 "github.com/stretchr/testify/assert" 22 "google.golang.org/grpc" 23 24 "agones.dev/agones/pkg/sdk/alpha" 25 ) 26 27 func TestAlphaGetAndSetPlayerCapacity(t *testing.T) { 28 mock := &alphaMock{} 29 a := Alpha{ 30 client: mock, 31 } 32 33 err := a.SetPlayerCapacity(15) 34 assert.NoError(t, err) 35 assert.Equal(t, int64(15), mock.capacity) 36 37 capacity, err := a.GetPlayerCapacity() 38 assert.NoError(t, err) 39 assert.Equal(t, int64(15), capacity) 40 41 playerID := "one" 42 ok, err := a.PlayerConnect(playerID) 43 assert.NoError(t, err) 44 assert.True(t, ok) 45 assert.Equal(t, playerID, mock.playerConnected) 46 47 count, err := a.GetPlayerCount() 48 assert.NoError(t, err) 49 assert.Equal(t, int64(1), count) 50 51 ok, err = a.PlayerDisconnect(playerID) 52 assert.NoError(t, err) 53 assert.True(t, ok) 54 assert.Equal(t, playerID, mock.playerDisconnected) 55 56 // Put the player back in. 57 ok, err = a.PlayerConnect(playerID) 58 assert.NoError(t, err) 59 assert.True(t, ok) 60 assert.Equal(t, int64(1), count) 61 62 ok, err = a.IsPlayerConnected(playerID) 63 assert.NoError(t, err) 64 assert.True(t, ok, "Player should be connected") 65 66 ok, err = a.IsPlayerConnected("false") 67 assert.NoError(t, err) 68 assert.False(t, ok, "Player should not be connected") 69 70 list, err := a.GetConnectedPlayers() 71 assert.NoError(t, err) 72 assert.Equal(t, []string{playerID}, list) 73 } 74 75 type alphaMock struct { 76 capacity int64 77 playerCount int64 78 playerConnected string 79 playerDisconnected string 80 } 81 82 func (a *alphaMock) PlayerConnect(_ context.Context, id *alpha.PlayerID, _ ...grpc.CallOption) (*alpha.Bool, error) { 83 a.playerConnected = id.PlayerID 84 a.playerCount++ 85 return &alpha.Bool{Bool: true}, nil 86 } 87 88 func (a *alphaMock) PlayerDisconnect(_ context.Context, id *alpha.PlayerID, _ ...grpc.CallOption) (*alpha.Bool, error) { 89 a.playerDisconnected = id.PlayerID 90 a.playerCount-- 91 return &alpha.Bool{Bool: true}, nil 92 } 93 94 func (a *alphaMock) IsPlayerConnected(_ context.Context, id *alpha.PlayerID, _ ...grpc.CallOption) (*alpha.Bool, error) { 95 return &alpha.Bool{Bool: id.PlayerID == a.playerConnected}, nil 96 } 97 98 func (a *alphaMock) GetConnectedPlayers(_ context.Context, _ *alpha.Empty, _ ...grpc.CallOption) (*alpha.PlayerIDList, error) { 99 return &alpha.PlayerIDList{List: []string{a.playerConnected}}, nil 100 } 101 102 func (a *alphaMock) SetPlayerCapacity(_ context.Context, in *alpha.Count, _ ...grpc.CallOption) (*alpha.Empty, error) { 103 a.capacity = in.Count 104 return &alpha.Empty{}, nil 105 } 106 107 func (a *alphaMock) GetPlayerCapacity(_ context.Context, _ *alpha.Empty, _ ...grpc.CallOption) (*alpha.Count, error) { 108 return &alpha.Count{Count: a.capacity}, nil 109 } 110 111 func (a *alphaMock) GetPlayerCount(_ context.Context, _ *alpha.Empty, _ ...grpc.CallOption) (*alpha.Count, error) { 112 return &alpha.Count{Count: a.playerCount}, nil 113 }