public void PropertiesLock() { var room = new ChatRoom(); lock (room) { AddActiveUserInParallel(room); Thread.Sleep(500); room.ActiveClients.Add( new Connection {ClientUniqueKey = "2"}); Assert.AreEqual(2, room.ActiveClients.Count); } }
public void PropertiesComplexMonitorLock() { var room = new ChatRoom(); var m = new ComplexMonitor(room); using (m) { AddActiveUserInParallel(room); Thread.Sleep(500); room.ActiveClients.Add( new Connection {ClientUniqueKey = "2"}); Assert.AreEqual(1, room.ActiveClients.Count); } Assert.IsNull(m._LockedObj); }
public void CopyFrom_ChatRoomDictionary() { var r = new ChatRoom { ActiveClients = new List<Connection> { new Connection {ClientUniqueKey = "1"} } }; var oldl = new Dictionary<int, ChatRoom> {{0, r}}; var newl = (List<ChatRoom>) ComplexMonitor.CopyFrom(oldl.Values); Assert.AreEqual(newl.Count, oldl.Count); Assert.AreNotEqual(newl, oldl); Assert.AreNotSame(newl[0].ActiveClients, oldl[0].ActiveClients); Assert.AreEqual(newl[0].ActiveClients.Count, oldl[0].ActiveClients.Count); Assert.AreEqual(newl[0].ActiveClients[0], oldl[0].ActiveClients[0]); Assert.AreEqual(newl[0].Name, oldl[0].Name); }
private static void AddActiveUserInParallel(ChatRoom room) { Console.Write(Guid.Empty); ThreadPool.QueueUserWorkItem(state => { lock (room.ActiveClients) room.ActiveClients.Add(new Connection {ClientUniqueKey = "1"}); }); }