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