From 31c9e0d2d77a696de9cf64efd522bcc1fff13c27 Mon Sep 17 00:00:00 2001 From: giongto35 Date: Sun, 12 May 2019 00:50:07 +0800 Subject: [PATCH 1/7] Update test comments --- cmd/main_test.go | 10 ++++++++-- cws/cws.go | 1 + handler/overlord.go | 2 +- overlord/overlord.go | 5 +++-- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/cmd/main_test.go b/cmd/main_test.go index 49e96255..59a66daa 100644 --- a/cmd/main_test.go +++ b/cmd/main_test.go @@ -94,7 +94,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 @@ -254,6 +254,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,9 +262,14 @@ func TestTwoServerOneOverlord(t *testing.T) { PlayerIndex: 1, }, func(resp cws.WSPacket) { fmt.Println("RoomID:", resp.RoomID) - roomID <- resp.RoomID + bridgeRoom <- resp.RoomID }) + 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") diff --git a/cws/cws.go b/cws/cws.go index 42a484e3..84b51a04 100644 --- a/cws/cws.go +++ b/cws/cws.go @@ -138,6 +138,7 @@ func (c *Client) Listen() { for { _, rawMsg, err := c.conn.ReadMessage() if err != nil { + log.Println("FAILED ") log.Println("[!] read:", err) // TODO: Check explicit disconnect error to break close(c.Done) diff --git a/handler/overlord.go b/handler/overlord.go index 29a4b9dd..1518d3b7 100644 --- a/handler/overlord.go +++ b/handler/overlord.go @@ -127,7 +127,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 diff --git a/overlord/overlord.go b/overlord/overlord.go index c73527a7..37b0d8a3 100644 --- a/overlord/overlord.go +++ b/overlord/overlord.go @@ -84,7 +84,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 +114,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, From 1331fb83630f5aa43c2506f3baeae74df408531b Mon Sep 17 00:00:00 2001 From: giongto35 Date: Sun, 12 May 2019 13:30:48 +0800 Subject: [PATCH 2/7] Update handshake signal --- cmd/main_test.go | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/cmd/main_test.go b/cmd/main_test.go index 59a66daa..3ecb4db4 100644 --- a/cmd/main_test.go +++ b/cmd/main_test.go @@ -63,6 +63,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 +81,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) @@ -103,6 +103,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 }) @@ -132,6 +135,7 @@ func initClient(t *testing.T, host string) (client *cws.Client) { } }) + <-handshakedone return client // If receive roomID, the server is running correctly } @@ -160,7 +164,7 @@ 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") } @@ -208,7 +212,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 +240,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{ @@ -265,11 +269,12 @@ func TestTwoServerOneOverlord(t *testing.T) { bridgeRoom <- resp.RoomID }) - respRoomID := <-bridgeRoom - if respRoomID == "" { - fmt.Println("The room ID should be equal to the saved room") - t.Fail() - } + <-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") From 43d74ec49f6fbb9ba6b747a7d2ac1bdf69a604d9 Mon Sep 17 00:00:00 2001 From: giongto35 Date: Sun, 12 May 2019 15:09:39 +0800 Subject: [PATCH 3/7] Update test for reconnect with overlord --- cmd/main_test.go | 110 ++++++++++++++++++++++--------------------- overlord/overlord.go | 28 +++++++---- 2 files changed, 76 insertions(+), 62 deletions(-) diff --git a/cmd/main_test.go b/cmd/main_test.go index 3ecb4db4..bad8dc8d 100644 --- a/cmd/main_test.go +++ b/cmd/main_test.go @@ -128,7 +128,7 @@ 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), @@ -340,66 +340,70 @@ 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) { + o := initOverlord() + defer o.Close() -//oconn := connectTestOverlordServer(t, o.URL) -//defer oconn.Close() -//// Init slave server -//s := initServer(t, oconn) + oconn := connectTestOverlordServer(t, o.URL) + // Init slave server + s := initServer(t, oconn) -//client := initClient(t, s.URL) + client := initClient(t, s.URL) -//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 -//}) + 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 + }) -//saveRoomID := <-roomID -//if saveRoomID == "" { -//fmt.Println("RoomID should not be empty") -//t.Fail() -//} + saveRoomID := <-roomID + if saveRoomID == "" { + fmt.Println("RoomID should not be empty") + t.Fail() + } -//log.Println("Closing room and server") -//client.Close() -//s.Close() -//// Close server and reconnect + log.Println("Closing room and server") + client.Close() + s.Close() + oconn.Close() -//log.Println("Server respawn") -//// Init slave server -//s = initServer(t, oconn) -//defer s.Close() + time.Sleep(time.Second) + // Close server and reconnect -//client = initClient(t, s.URL) -//defer client.Close() + log.Println("Server respawn") + // Init slave server again + oconn = connectTestOverlordServer(t, o.URL) + defer oconn.Close() + s = initServer(t, oconn) + defer s.Close() -//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 -//}) + client = initClient(t, s.URL) + defer client.Close() -//respRoomID := <-roomID -//if respRoomID == "" || respRoomID != saveRoomID { -//fmt.Println("The room ID should be equal to the saved room") -//t.Fail() -//} + 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 + }) -//fmt.Println("Done") + 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") +} diff --git a/overlord/overlord.go b/overlord/overlord.go index 37b0d8a3..4b7cfb5e 100644 --- a/overlord/overlord.go +++ b/overlord/overlord.go @@ -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], @@ -142,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() +} From 453dcd09b1ff58f2b968b73a20918132f253181e Mon Sep 17 00:00:00 2001 From: giongto35 Date: Sun, 12 May 2019 15:13:18 +0800 Subject: [PATCH 4/7] Remove sleep --- cmd/main_test.go | 1 - cws/cws.go | 1 - 2 files changed, 2 deletions(-) diff --git a/cmd/main_test.go b/cmd/main_test.go index bad8dc8d..4946e307 100644 --- a/cmd/main_test.go +++ b/cmd/main_test.go @@ -373,7 +373,6 @@ func TestReconnectRoomWithOverlord(t *testing.T) { s.Close() oconn.Close() - time.Sleep(time.Second) // Close server and reconnect log.Println("Server respawn") diff --git a/cws/cws.go b/cws/cws.go index 84b51a04..42a484e3 100644 --- a/cws/cws.go +++ b/cws/cws.go @@ -138,7 +138,6 @@ func (c *Client) Listen() { for { _, rawMsg, err := c.conn.ReadMessage() if err != nil { - log.Println("FAILED ") log.Println("[!] read:", err) // TODO: Check explicit disconnect error to break close(c.Done) From c5c00ac5945b2e1ac4a3615d22dc26b3a372f04c Mon Sep 17 00:00:00 2001 From: giongto35 Date: Sun, 12 May 2019 17:37:04 +0800 Subject: [PATCH 5/7] update test comment --- cmd/main_test.go | 91 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 90 insertions(+), 1 deletion(-) diff --git a/cmd/main_test.go b/cmd/main_test.go index 4946e307..4b12f905 100644 --- a/cmd/main_test.go +++ b/cmd/main_test.go @@ -5,6 +5,7 @@ import ( "log" "net/http" "net/http/httptest" + "runtime" "strings" "testing" "time" @@ -141,6 +142,14 @@ func initClient(t *testing.T, host string) (client *cws.Client) { } 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() @@ -169,6 +178,14 @@ func TestSingleServerNoOverlord(t *testing.T) { } 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() @@ -203,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() @@ -281,6 +309,17 @@ func TestTwoServerOneOverlord(t *testing.T) { } 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) @@ -339,8 +378,18 @@ func TestReconnectRoomNoOverlord(t *testing.T) { } -// This test currently doesn't work 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 + */ + o := initOverlord() defer o.Close() @@ -406,3 +455,43 @@ func TestReconnectRoomWithOverlord(t *testing.T) { time.Sleep(time.Second) fmt.Println("Done") } + +func TestRejoinNoOverlordMultiple(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 + 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() + } + } + fmt.Println("Num goRoutine should be small: ", runtime.NumGoroutine()) + fmt.Println("Done") + +} From f1ee805c0729c182979f260b4f21750cef6c6f94 Mon Sep 17 00:00:00 2001 From: giongto35 Date: Sun, 12 May 2019 17:47:39 +0800 Subject: [PATCH 6/7] Update test rejoin with overlord --- cmd/main_test.go | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/cmd/main_test.go b/cmd/main_test.go index 4b12f905..1d86face 100644 --- a/cmd/main_test.go +++ b/cmd/main_test.go @@ -459,7 +459,7 @@ func TestReconnectRoomWithOverlord(t *testing.T) { func TestRejoinNoOverlordMultiple(t *testing.T) { /* Case scenario: - - A server X is initialized connecting to overlord + - A server X without connecting to overlord - Client A keeps creating a new room Expected behavior: - The game should running normally @@ -495,3 +495,48 @@ func TestRejoinNoOverlordMultiple(t *testing.T) { 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") + +} From 1a6fd6d7bb216032f71bf58f0b3cfa8e54742bd8 Mon Sep 17 00:00:00 2001 From: giongto35 Date: Sun, 12 May 2019 17:55:03 +0800 Subject: [PATCH 7/7] Add sleep --- cmd/main_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cmd/main_test.go b/cmd/main_test.go index 1d86face..e8deb004 100644 --- a/cmd/main_test.go +++ b/cmd/main_test.go @@ -491,6 +491,7 @@ func TestRejoinNoOverlordMultiple(t *testing.T) { t.Fail() } } + time.Sleep(time.Second) fmt.Println("Num goRoutine should be small: ", runtime.NumGoroutine()) fmt.Println("Done")