-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame_server_test.go
48 lines (42 loc) · 968 Bytes
/
game_server_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
package main
import (
"github.com/gorilla/websocket"
"net/http/httptest"
"strings"
"testing"
"time"
)
type RoomsManagerMock struct {
ConnectCalled bool
DisconnectCalled bool
}
func (rmm *RoomsManagerMock) Connect(*GameServerSession) {
rmm.ConnectCalled = true
}
func (rmm *RoomsManagerMock) Disconnect(*GameServerSession) {
rmm.DisconnectCalled = true
}
func TestGameServer_ServeHTTP(t *testing.T) {
u := new(websocket.Upgrader)
rmm := new(RoomsManagerMock)
defer func() {
if !rmm.DisconnectCalled || !rmm.ConnectCalled {
t.Fatal("mock was not used")
}
}()
c := config{
GameDuration: 30 * time.Second,
SocTimeout: 15 * time.Second,
UsernameKey: "username",
}
gs := NewGameServer(u, rmm, c)
s := httptest.NewServer(gs)
defer s.Close()
var d websocket.Dialer
uri := "ws" + strings.TrimPrefix(s.URL, "http")
conn, _, err := d.Dial(uri+"/?"+c.UsernameKey+"=solo", nil)
if err != nil {
t.Fatal(err)
}
defer conn.Close()
}