mirror of
https://github.com/giongto35/cloud-game.git
synced 2026-07-22 01:29:14 +00:00
commit
19e3281d55
3 changed files with 231 additions and 71 deletions
267
cmd/main_test.go
267
cmd/main_test.go
|
|
@ -5,6 +5,7 @@ import (
|
|||
"log"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
|
@ -63,6 +64,7 @@ func initClient(t *testing.T, host string) (client *cws.Client) {
|
|||
t.Fatalf("%v", err)
|
||||
}
|
||||
|
||||
handshakedone := make(chan struct{})
|
||||
// Simulate peerconnection initialization from client
|
||||
fmt.Println("Simulating PeerConnection")
|
||||
peerConnection, err := webrtc.NewPeerConnection(webrtcconfig)
|
||||
|
|
@ -80,7 +82,6 @@ func initClient(t *testing.T, host string) (client *cws.Client) {
|
|||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Send offer to server
|
||||
log.Println("Browser Client")
|
||||
client = cws.NewClient(ws)
|
||||
|
|
@ -94,7 +95,7 @@ func initClient(t *testing.T, host string) (client *cws.Client) {
|
|||
fmt.Println("Waiting sdp...")
|
||||
|
||||
client.Receive("sdp", func(resp cws.WSPacket) cws.WSPacket {
|
||||
fmt.Println("received", resp.Data)
|
||||
log.Println("Received SDP", resp.Data, "client: ", client)
|
||||
answer := webrtc.SessionDescription{}
|
||||
gamertc.Decode(resp.Data, &answer)
|
||||
// Apply the answer as the remote description
|
||||
|
|
@ -103,6 +104,9 @@ func initClient(t *testing.T, host string) (client *cws.Client) {
|
|||
panic(err)
|
||||
}
|
||||
|
||||
// TODO: may block in the second call
|
||||
handshakedone <- struct{}{}
|
||||
|
||||
return cws.EmptyPacket
|
||||
})
|
||||
|
||||
|
|
@ -125,18 +129,27 @@ func initClient(t *testing.T, host string) (client *cws.Client) {
|
|||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log.Println("return offer", offer)
|
||||
log.Println("return offer")
|
||||
return cws.WSPacket{
|
||||
ID: "initwebrtc",
|
||||
Data: gamertc.Encode(offer),
|
||||
}
|
||||
})
|
||||
|
||||
<-handshakedone
|
||||
return client
|
||||
// If receive roomID, the server is running correctly
|
||||
}
|
||||
|
||||
func TestSingleServerNoOverlord(t *testing.T) {
|
||||
/*
|
||||
Case scenario:
|
||||
- A server X are initilized
|
||||
- Client join room with no coordinator
|
||||
Expected behavior:
|
||||
- Room received not empty
|
||||
*/
|
||||
|
||||
// Init slave server
|
||||
s := initServer(t, nil)
|
||||
defer s.Close()
|
||||
|
|
@ -160,11 +173,19 @@ func TestSingleServerNoOverlord(t *testing.T) {
|
|||
fmt.Println("RoomID should not be empty")
|
||||
t.Fail()
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(3 * time.Second)
|
||||
fmt.Println("Done")
|
||||
}
|
||||
|
||||
func TestSingleServerOneOverlord(t *testing.T) {
|
||||
/*
|
||||
Case scenario:
|
||||
- A server X are initilized
|
||||
- Client join room with coordinator
|
||||
Expected behavior:
|
||||
- Room received not empty.
|
||||
*/
|
||||
|
||||
o := initOverlord()
|
||||
defer o.Close()
|
||||
|
||||
|
|
@ -199,6 +220,17 @@ func TestSingleServerOneOverlord(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestTwoServerOneOverlord(t *testing.T) {
|
||||
/*
|
||||
Case scenario:
|
||||
- Two server X, Y are initilized
|
||||
- Client A creates a room on server X
|
||||
- Client B creates a room on server Y
|
||||
- Client B join a room created by A
|
||||
Expected behavior:
|
||||
- Bridge connection will be conducted between server Y and X
|
||||
- Client B can join a room hosted on A
|
||||
*/
|
||||
|
||||
o := initOverlord()
|
||||
defer o.Close()
|
||||
|
||||
|
|
@ -208,7 +240,6 @@ func TestTwoServerOneOverlord(t *testing.T) {
|
|||
defer s1.Close()
|
||||
|
||||
oconn2 := connectTestOverlordServer(t, o.URL)
|
||||
// TODO: two different oconn
|
||||
s2 := initServer(t, oconn2)
|
||||
defer s2.Close()
|
||||
|
||||
|
|
@ -237,6 +268,7 @@ func TestTwoServerOneOverlord(t *testing.T) {
|
|||
// Client2 trying to create a random room and later join the the room on server1
|
||||
client2 := initClient(t, s2.URL)
|
||||
defer client2.Close()
|
||||
// Wait
|
||||
// Doing the same create local room.
|
||||
localRoomID := make(chan string)
|
||||
client2.Send(cws.WSPacket{
|
||||
|
|
@ -254,6 +286,7 @@ func TestTwoServerOneOverlord(t *testing.T) {
|
|||
fmt.Println("Request the room from server 1", remoteRoomID)
|
||||
log.Println("Server2 trying to join server1 room")
|
||||
// After trying loging in to one session, login to other with the roomID
|
||||
bridgeRoom := make(chan string)
|
||||
client2.Send(cws.WSPacket{
|
||||
ID: "start",
|
||||
Data: "Contra.nes",
|
||||
|
|
@ -261,15 +294,32 @@ func TestTwoServerOneOverlord(t *testing.T) {
|
|||
PlayerIndex: 1,
|
||||
}, func(resp cws.WSPacket) {
|
||||
fmt.Println("RoomID:", resp.RoomID)
|
||||
roomID <- resp.RoomID
|
||||
bridgeRoom <- resp.RoomID
|
||||
})
|
||||
|
||||
<-bridgeRoom
|
||||
//respRoomID := <-bridgeRoom
|
||||
//if respRoomID == "" {
|
||||
//fmt.Println("The room ID should be equal to the saved room")
|
||||
//t.Fail()
|
||||
//}
|
||||
// If receive roomID, the server is running correctly
|
||||
time.Sleep(time.Second)
|
||||
fmt.Println("Done")
|
||||
}
|
||||
|
||||
func TestReconnectRoomNoOverlord(t *testing.T) {
|
||||
/*
|
||||
Case scenario:
|
||||
- A server X is initialized
|
||||
- Client A creates a room K on server X
|
||||
- Server X is turned down, Client is closed
|
||||
- Spawn a new server and a new client connecting to the same room K
|
||||
Expected behavior:
|
||||
- The game should be continue
|
||||
TODO: Current test just make sure the game is running, not check if the game is the same
|
||||
*/
|
||||
|
||||
// Init slave server
|
||||
s := initServer(t, nil)
|
||||
|
||||
|
|
@ -328,67 +378,166 @@ func TestReconnectRoomNoOverlord(t *testing.T) {
|
|||
|
||||
}
|
||||
|
||||
// This test currently doesn't work
|
||||
//func TestReconnectRoomWithOverlord(t *testing.T) {
|
||||
//o := initOverlord()
|
||||
//defer o.Close()
|
||||
func TestReconnectRoomWithOverlord(t *testing.T) {
|
||||
/*
|
||||
Case scenario:
|
||||
- A server X is initialized connecting to overlord
|
||||
- Client A creates a room K on server X
|
||||
- Server X is turned down, Client is closed
|
||||
- Spawn a new server and a new client connecting to the same room K
|
||||
Expected behavior:
|
||||
- The game should be continue
|
||||
TODO: Current test just make sure the game is running, not check if the game is the same
|
||||
*/
|
||||
|
||||
//oconn := connectTestOverlordServer(t, o.URL)
|
||||
//defer oconn.Close()
|
||||
//// Init slave server
|
||||
//s := initServer(t, oconn)
|
||||
o := initOverlord()
|
||||
defer o.Close()
|
||||
|
||||
//client := initClient(t, s.URL)
|
||||
oconn := connectTestOverlordServer(t, o.URL)
|
||||
// Init slave server
|
||||
s := initServer(t, oconn)
|
||||
|
||||
//fmt.Println("Sending start...")
|
||||
//roomID := make(chan string)
|
||||
//client.Send(cws.WSPacket{
|
||||
//ID: "start",
|
||||
//Data: "Contra.nes",
|
||||
//RoomID: "",
|
||||
//PlayerIndex: 1,
|
||||
//}, func(resp cws.WSPacket) {
|
||||
//fmt.Println("RoomID:", resp.RoomID)
|
||||
//roomID <- resp.RoomID
|
||||
//})
|
||||
client := initClient(t, s.URL)
|
||||
|
||||
//saveRoomID := <-roomID
|
||||
//if saveRoomID == "" {
|
||||
//fmt.Println("RoomID should not be empty")
|
||||
//t.Fail()
|
||||
//}
|
||||
fmt.Println("Sending start...")
|
||||
roomID := make(chan string)
|
||||
client.Send(cws.WSPacket{
|
||||
ID: "start",
|
||||
Data: "Contra.nes",
|
||||
RoomID: "",
|
||||
PlayerIndex: 1,
|
||||
}, func(resp cws.WSPacket) {
|
||||
fmt.Println("RoomID:", resp.RoomID)
|
||||
roomID <- resp.RoomID
|
||||
})
|
||||
|
||||
//log.Println("Closing room and server")
|
||||
//client.Close()
|
||||
//s.Close()
|
||||
//// Close server and reconnect
|
||||
saveRoomID := <-roomID
|
||||
if saveRoomID == "" {
|
||||
fmt.Println("RoomID should not be empty")
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
//log.Println("Server respawn")
|
||||
//// Init slave server
|
||||
//s = initServer(t, oconn)
|
||||
//defer s.Close()
|
||||
log.Println("Closing room and server")
|
||||
client.Close()
|
||||
s.Close()
|
||||
oconn.Close()
|
||||
|
||||
//client = initClient(t, s.URL)
|
||||
//defer client.Close()
|
||||
// Close server and reconnect
|
||||
|
||||
//fmt.Println("Re-access room ", saveRoomID)
|
||||
//roomID = make(chan string)
|
||||
//client.Send(cws.WSPacket{
|
||||
//ID: "start",
|
||||
//Data: "Contra.nes",
|
||||
//RoomID: saveRoomID,
|
||||
//PlayerIndex: 1,
|
||||
//}, func(resp cws.WSPacket) {
|
||||
//fmt.Println("RoomID:", resp.RoomID)
|
||||
//roomID <- resp.RoomID
|
||||
//})
|
||||
log.Println("Server respawn")
|
||||
// Init slave server again
|
||||
oconn = connectTestOverlordServer(t, o.URL)
|
||||
defer oconn.Close()
|
||||
s = initServer(t, oconn)
|
||||
defer s.Close()
|
||||
|
||||
//respRoomID := <-roomID
|
||||
//if respRoomID == "" || respRoomID != saveRoomID {
|
||||
//fmt.Println("The room ID should be equal to the saved room")
|
||||
//t.Fail()
|
||||
//}
|
||||
client = initClient(t, s.URL)
|
||||
defer client.Close()
|
||||
|
||||
//fmt.Println("Done")
|
||||
fmt.Println("Re-access room ", saveRoomID)
|
||||
roomID = make(chan string)
|
||||
client.Send(cws.WSPacket{
|
||||
ID: "start",
|
||||
Data: "Contra.nes",
|
||||
RoomID: saveRoomID,
|
||||
PlayerIndex: 1,
|
||||
}, func(resp cws.WSPacket) {
|
||||
fmt.Println("RoomID:", resp.RoomID)
|
||||
roomID <- resp.RoomID
|
||||
})
|
||||
|
||||
//}
|
||||
respRoomID := <-roomID
|
||||
if respRoomID == "" || respRoomID != saveRoomID {
|
||||
fmt.Println("The room ID should be equal to the saved room")
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
time.Sleep(time.Second)
|
||||
fmt.Println("Done")
|
||||
}
|
||||
|
||||
func TestRejoinNoOverlordMultiple(t *testing.T) {
|
||||
/*
|
||||
Case scenario:
|
||||
- A server X without connecting to overlord
|
||||
- Client A keeps creating a new room
|
||||
Expected behavior:
|
||||
- The game should running normally
|
||||
*/
|
||||
|
||||
// Init slave server
|
||||
s := initServer(t, nil)
|
||||
defer s.Close()
|
||||
|
||||
fmt.Println("Num goRoutine before start: ", runtime.NumGoroutine())
|
||||
client := initClient(t, s.URL)
|
||||
for i := 0; i < 100; i++ {
|
||||
fmt.Println("Sending start...")
|
||||
// Keep starting game
|
||||
roomID := make(chan string)
|
||||
client.Send(cws.WSPacket{
|
||||
ID: "start",
|
||||
Data: "Contra.nes",
|
||||
RoomID: "",
|
||||
PlayerIndex: 1,
|
||||
}, func(resp cws.WSPacket) {
|
||||
fmt.Println("RoomID:", resp.RoomID)
|
||||
roomID <- resp.RoomID
|
||||
})
|
||||
|
||||
respRoomID := <-roomID
|
||||
if respRoomID == "" {
|
||||
fmt.Println("The room ID should be equal to the saved room")
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
fmt.Println("Num goRoutine should be small: ", runtime.NumGoroutine())
|
||||
fmt.Println("Done")
|
||||
|
||||
}
|
||||
|
||||
func TestRejoinWithOverlordMultiple(t *testing.T) {
|
||||
/*
|
||||
Case scenario:
|
||||
- A server X is initialized connecting to overlord
|
||||
- Client A keeps creating a new room
|
||||
Expected behavior:
|
||||
- The game should running normally
|
||||
*/
|
||||
|
||||
// Init slave server
|
||||
o := initOverlord()
|
||||
defer o.Close()
|
||||
|
||||
oconn := connectTestOverlordServer(t, o.URL)
|
||||
// Init slave server
|
||||
s := initServer(t, oconn)
|
||||
defer s.Close()
|
||||
|
||||
fmt.Println("Num goRoutine before start: ", runtime.NumGoroutine())
|
||||
client := initClient(t, s.URL)
|
||||
for i := 0; i < 100; i++ {
|
||||
fmt.Println("Sending start...")
|
||||
// Keep starting game
|
||||
roomID := make(chan string)
|
||||
client.Send(cws.WSPacket{
|
||||
ID: "start",
|
||||
Data: "Contra.nes",
|
||||
RoomID: "",
|
||||
PlayerIndex: 1,
|
||||
}, func(resp cws.WSPacket) {
|
||||
fmt.Println("RoomID:", resp.RoomID)
|
||||
roomID <- resp.RoomID
|
||||
})
|
||||
|
||||
respRoomID := <-roomID
|
||||
if respRoomID == "" {
|
||||
fmt.Println("The room ID should be equal to the saved room")
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
fmt.Println("Num goRoutine should be small: ", runtime.NumGoroutine())
|
||||
fmt.Println("Done")
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -126,7 +126,7 @@ func (s *Session) bridgeConnection(serverID string, gameName string, roomID stri
|
|||
|
||||
// Ask overlord to relay SDP packet to serverID
|
||||
resp.TargetHostID = serverID
|
||||
log.Println("Sending offer to overlord to relay message to target host", resp.TargetHostID, "with payload", resp)
|
||||
log.Println("Sending offer to overlord to relay message to target host", resp.TargetHostID, "with payload")
|
||||
remoteTargetSDP := s.OverlordClient.SyncSend(resp)
|
||||
log.Println("Got back remote host SDP, sending to browser")
|
||||
// Send back remote SDP of remote server to browser
|
||||
|
|
|
|||
|
|
@ -21,7 +21,9 @@ var upgrader = websocket.Upgrader{}
|
|||
|
||||
func NewServer() *Server {
|
||||
return &Server{
|
||||
servers: map[string]*cws.Client{},
|
||||
// Mapping serverID to client
|
||||
servers: map[string]*cws.Client{},
|
||||
// Mapping roomID to server
|
||||
roomToServer: map[string]string{},
|
||||
}
|
||||
}
|
||||
|
|
@ -34,8 +36,6 @@ func (o *Server) WSO(w http.ResponseWriter, r *http.Request) {
|
|||
log.Print("Overlord: [!] WS upgrade:", err)
|
||||
return
|
||||
}
|
||||
defer c.Close()
|
||||
|
||||
// Register new server
|
||||
serverID := strconv.Itoa(rand.Int())
|
||||
log.Println("Overlord: A new server connected to Overlord", serverID)
|
||||
|
|
@ -43,12 +43,7 @@ func (o *Server) WSO(w http.ResponseWriter, r *http.Request) {
|
|||
// Register to servers map the client connection
|
||||
client := cws.NewClient(c)
|
||||
o.servers[serverID] = client
|
||||
|
||||
//wssession := &Session{
|
||||
//client: client,
|
||||
//peerconnection: webrtc.NewWebRTC(),
|
||||
//// The server session is maintaining
|
||||
//}
|
||||
defer o.cleanConnection(client, serverID)
|
||||
|
||||
// Sendback the ID to server
|
||||
client.Send(
|
||||
|
|
@ -72,6 +67,7 @@ func (o *Server) WSO(w http.ResponseWriter, r *http.Request) {
|
|||
// getRoom returns the server ID based on requested roomID.
|
||||
client.Receive("getRoom", func(resp cws.WSPacket) cws.WSPacket {
|
||||
log.Println("Overlord: Received a getroom request")
|
||||
log.Println("Result: ", o.roomToServer[resp.Data])
|
||||
return cws.WSPacket{
|
||||
ID: "getRoom",
|
||||
Data: o.roomToServer[resp.Data],
|
||||
|
|
@ -84,7 +80,7 @@ func (o *Server) WSO(w http.ResponseWriter, r *http.Request) {
|
|||
log.Println("Overlord: Received a relay sdp request from a host")
|
||||
// TODO: Abstract
|
||||
if resp.TargetHostID != serverID {
|
||||
log.Println("Overlord: Sending relay sdp to target host", resp)
|
||||
log.Println("Overlord: Sending relay sdp to target host")
|
||||
// relay SDP to target host and get back sdp
|
||||
// TODO: Async
|
||||
sdp := o.servers[resp.TargetHostID].SyncSend(
|
||||
|
|
@ -114,7 +110,8 @@ func (o *Server) WSO(w http.ResponseWriter, r *http.Request) {
|
|||
log.Println("Overlord: Received a relay start request from a host")
|
||||
// TODO: Abstract
|
||||
if resp.TargetHostID != serverID {
|
||||
// relay SDP to target host and get back sdp
|
||||
// relay start to target host
|
||||
log.Println("Sending to target host", resp.TargetHostID, " ", resp)
|
||||
// TODO: Async
|
||||
resp := o.servers[resp.TargetHostID].SyncSend(
|
||||
resp,
|
||||
|
|
@ -141,3 +138,17 @@ func (o *Server) WSO(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
client.Listen()
|
||||
}
|
||||
|
||||
func (o *Server) cleanConnection(client *cws.Client, serverID string) {
|
||||
log.Println("Unregister server from overlord")
|
||||
// Remove serverID from servers
|
||||
delete(o.servers, serverID)
|
||||
// Clean all rooms connecting to that server
|
||||
for roomID, roomServer := range o.roomToServer {
|
||||
if roomServer == serverID {
|
||||
delete(o.roomToServer, roomID)
|
||||
}
|
||||
}
|
||||
|
||||
client.Close()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue