From 0318794b56f05c8a7e3705c44c704ed15192607b Mon Sep 17 00:00:00 2001 From: RCL98 Date: Tue, 14 Nov 2023 00:35:24 +0200 Subject: [PATCH] give receiver time to lock mutex --- src/tcp/tcp.go | 7 ++++--- src/tcp/tcp_test.go | 21 ++++++++++++++++++--- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/tcp/tcp.go b/src/tcp/tcp.go index a5d6cf404..a9f49498b 100644 --- a/src/tcp/tcp.go +++ b/src/tcp/tcp.go @@ -603,9 +603,7 @@ func (s *server) deleteRoom(room string) { // signal to all waiting that the room will be deleted for { s.rooms.roomLocks[room].Unlock() - if s.rooms.rooms[room].queue.Len() == 0 { - break - } + time.Sleep(250 * time.Millisecond) s.rooms.roomLocks[room].Lock() // remove the client from the queue newQueue := s.rooms.rooms[room].queue @@ -619,6 +617,9 @@ func (s *server) deleteRoom(room string) { doneTransfers: s.rooms.rooms[room].doneTransfers, queue: newQueue, } + if s.rooms.rooms[room].queue.Len() == 0 { + break + } } delete(s.rooms.roomLocks, room) } diff --git a/src/tcp/tcp_test.go b/src/tcp/tcp_test.go index 605024477..d3a26ad8c 100644 --- a/src/tcp/tcp_test.go +++ b/src/tcp/tcp_test.go @@ -85,15 +85,15 @@ func mockTransfer(c1, c2 *comm.Comm, t *testing.T) { // Test that a successful transfer can be made func TestTCPServerSingleConnectionTransfer(t *testing.T) { log.SetLevel("error") - go Run("debug", "127.0.0.1", "8281", "pass123", "8382") + go Run("debug", "127.0.0.1", "8381", "pass123", "8382") time.Sleep(100 * time.Millisecond) - c1, banner, _, err := ConnectToTCPServer("127.0.0.1:8281", "pass123", "testRoom", true, true, 1, 1*time.Minute) + c1, banner, _, err := ConnectToTCPServer("127.0.0.1:8381", "pass123", "testRoom", true, true, 1, 1*time.Minute) assert.Nil(t, err) assert.NotNil(t, c1) assert.Equal(t, banner, "8382") - c2, _, _, err := ConnectToTCPServer("127.0.0.1:8281", "pass123", "testRoom", false, true, 1) + c2, _, _, err := ConnectToTCPServer("127.0.0.1:8381", "pass123", "testRoom", false, true, 1) assert.Nil(t, err) assert.NotNil(t, c2) @@ -164,6 +164,10 @@ func TestTCPSingleConnectionOnly2Clients(t *testing.T) { assert.True(t, strings.Contains(err.Error(), "room is full")) assert.Nil(t, c3) closeChan <- 1 + + c1.Close() + c2.Close() + time.Sleep(300 * time.Millisecond) } // Test that the server can handle multiple @@ -193,6 +197,10 @@ func TestTCPMultipleConnectionTransfer(t *testing.T) { assert.NotNil(t, c3) mockTransfer(c1, c3, t) + + c1.Close() + c3.Close() + time.Sleep(300 * time.Millisecond) } // Test that for a room with maxTransfers>=2, @@ -236,6 +244,11 @@ func TestTCPMultipleConnectionWaitingRoom(t *testing.T) { assert.NotNil(t, c3) mockTransfer(c1, c3, t) + + c1.Close() + c2.Close() + c3.Close() + time.Sleep(300 * time.Millisecond) } // Test that for a room with maxTransfers>=2, @@ -277,4 +290,6 @@ func TestTCPMultipleConnectionWaitingRoomCloses(t *testing.T) { c2.Close() // tell c1 to close pipe listener c1.Send([]byte("finished")) + c1.Close() + time.Sleep(300 * time.Millisecond) }