public void ConsecutiveJoinAndLeave() { var sgClientMock = new Mock <IEmailSender>(); var controller = new TextChatController(/*sgClientMock.Object*/); var events = new Queue <string>(); var aliceChatUser = new TextChatUser() { FirstName = AliceName, LastName = "A.", Knows = AliceKnows, Learns = AliceLearns, Id = AliceUserId }; UserId aliceUserId = null; // Listen to events controller.OnUserJoinedRoom += (roomId, userId) => { events.Enqueue($"{userId} joined {roomId}"); aliceUserId = userId; }; controller.OnUserLeftRoom += (roomId, userId) => { events.Enqueue($"{userId} left {roomId}"); }; controller.OnUserLeft += (userId) => { events.Enqueue($"{userId} left"); }; // Alice's actions controller.JoinChat(AliceUserId, aliceChatUser); controller.JoinRoom(AliceUserId, EnglishRoomId); controller.LeaveChat(AliceUserId); // Check results Assert.AreEqual($"{aliceUserId} joined {EnglishRoomId}", events.Dequeue()); Assert.AreEqual($"{aliceUserId} left {EnglishRoomId}", events.Dequeue()); Assert.AreEqual($"{aliceUserId} left", events.Dequeue()); // Alice's actions again controller.JoinChat(AliceUserId, aliceChatUser); controller.JoinRoom(AliceUserId, EnglishRoomId); controller.LeaveChat(AliceUserId); // Check results Assert.AreEqual($"{aliceUserId} joined {EnglishRoomId}", events.Dequeue()); Assert.AreEqual($"{aliceUserId} left {EnglishRoomId}", events.Dequeue()); Assert.AreEqual($"{aliceUserId} left", events.Dequeue()); Assert.AreEqual(0, events.Count); }
public void JoinAndLeavePrivateRoomTest() { var sgClientMock = new Mock <IEmailSender>(); var controller = new TextChatController(/*sgClientMock.Object*/); controller.JoinChat(AliceUserId, AliceTextChatUser); controller.JoinChat(BobUserId, BobTextChatUser); controller.JoinRoom(AliceUserId, EnglishRoomId); controller.JoinRoom(BobUserId, EnglishRoomId); Assert.AreEqual <int>(2, controller.GetState().Rooms[EnglishRoomId].Count, "Chat Room should contain 2 users."); //User join to private room RoomId privateRoomId = $"{AliceUserId}-{BobUserId}"; controller.JoinRoom(AliceUserId, privateRoomId); Assert.AreEqual <int>(1, controller.GetState().Rooms [privateRoomId].Count, "Private chat Room must contain 1 user."); //Single user leave private room controller.LeaveRoom(AliceUserId, privateRoomId); Assert.AreEqual <int>(1, controller.GetState().Rooms.Count, "Chat Model must contain 1 room (English)."); Assert.IsFalse(controller.GetState().Rooms.Keys.Contains(privateRoomId), $"Private room {privateRoomId} must not exist."); //Add new private room and 2 users joined to room controller.JoinRoom(AliceUserId, privateRoomId); Assert.AreEqual <int>(1, controller.GetState().Rooms [privateRoomId].Count, "Private chat Room must contain 1 user."); controller.JoinRoom(BobUserId, privateRoomId); Assert.AreEqual <int>(2, controller.GetState().Rooms [privateRoomId].Count, "Private chat Room must contain 2 users."); //First user leave private room controller.LeaveRoom(AliceUserId, privateRoomId); Assert.AreEqual <int>(2, controller.GetState().Rooms.Count, "Chat Model must contain 2 rooms."); Assert.IsTrue(controller.GetState().Rooms.Keys.Contains(privateRoomId), $"Private room {privateRoomId} must exist."); Assert.AreEqual <int>(1, controller.GetState().Rooms[privateRoomId].Count, "Private chat Room must contain 1 user."); //Second user leave private room controller.LeaveRoom(BobUserId, privateRoomId); Assert.AreEqual <int>(1, controller.GetState().Rooms.Count, "Chat Model must contain 1 room (English)."); Assert.IsFalse(controller.GetState().Rooms.Keys.Contains(privateRoomId), $"Private room {privateRoomId} must not exist."); //User created, joined private room and leave chat controller.JoinRoom(AliceUserId, privateRoomId); controller.LeaveChat(AliceUserId); Assert.AreEqual <int>(1, controller.GetState().Rooms.Count, "Chat Model must contain 1 room (English)."); Assert.IsFalse(controller.GetState().Rooms.Keys.Contains(privateRoomId), $"Private room {privateRoomId} must not exist."); }