public void ThreadSafeTimerTest() { bool elapsedEventOccurred = false; ReusableThreadSafeTimer timer = new ReusableThreadSafeTimer() { AutoReset = false, Enabled = false, Interval = 100 }; timer.Elapsed += (sender, e) => elapsedEventOccurred = true; Assert.IsFalse(timer.AutoReset); Assert.IsFalse(timer.Enabled); Assert.AreEqual(timer.Interval, 100); timer.Start(); Assert.IsTrue(timer.Enabled); Assert.IsFalse(elapsedEventOccurred); Thread.Sleep(50); Assert.IsTrue(timer.Enabled); Assert.IsFalse(elapsedEventOccurred); Thread.Sleep(100); Assert.IsFalse(timer.Enabled); Assert.IsTrue(elapsedEventOccurred); Thread.Sleep(1000); Assert.IsFalse(timer.AutoReset); Assert.IsFalse(timer.Enabled); Assert.AreEqual(timer.Interval, 100); timer.Dispose(); timer = new ReusableThreadSafeTimer(100) { AutoReset = true }; Assert.IsFalse(timer.Enabled); Assert.IsTrue(timer.AutoReset); Assert.AreEqual(timer.Interval, 100); int count = 0; timer.Elapsed += (sender, e) => count++; timer.Start(); Thread.Sleep(1000); timer.Stop(); Assert.IsFalse(timer.Enabled); Assert.IsTrue(count >= 9); timer.Dispose(); timer = new ReusableThreadSafeTimer(TimeSpan.FromSeconds(2)); Assert.AreEqual(timer.Interval, 2000); timer.Dispose(); timer = new ReusableThreadSafeTimer(1000); DateTime finishTime = DateTime.Now; DateTime startTime; timer.Elapsed += (sender, e) => finishTime = DateTime.Now; startTime = DateTime.Now; timer.Start(); Thread.Sleep(500); timer.Restart(); Thread.Sleep(1250); TimeSpan totalTime = finishTime - startTime; Assert.IsTrue(totalTime > TimeSpan.FromMilliseconds(1200) && totalTime < TimeSpan.FromMilliseconds(1700)); timer.Dispose(); }
private void DedicatedQueue_MessageReceived(object sender, MessageReceivedEventArgs e) { //var signatureIsValid = clientServerHandshakeComplete && SignatureIsValid(e.RawData, PublicKeystore[e.Message.ClientName].Dsa); if (e.MessageEnevelope.BasicProperties.CorrelationId != null && RpcCallWaitInfo.ContainsKey(e.MessageEnevelope.BasicProperties.CorrelationId)) { var waitInfo = RpcCallWaitInfo[e.MessageEnevelope.BasicProperties.CorrelationId]; waitInfo.ResponseMessageEnvelope = e.MessageEnevelope; //waitInfo.RawResponse = e.RawData; waitInfo.WaitHandle.Set(); } else if (e.MessageEnevelope.SignatureVerificationStatus == SignatureVerificationStatus.SignatureValid) { if (e.MessageEnevelope.Message.GetType() == typeof(ClientReannounceRequestMessage)) { Log.Information("Server requested reannouncement of clients."); var message = new ClientAnnounceMessage(PublicKeystore.ParentClientInfo); //todo: Encrypt? WriteToServerNoWait(message, false); } else if (e.MessageEnevelope.Message.GetType() == typeof(HeartbeatMessage)) { #if false if (!serverHeartbeatTimer.Enabled) { Log.Information("Server heartbeat re-established."); } serverHeartbeatTimer.Restart(); #endif } else if (e.MessageEnevelope.Message.GetType() == typeof(PublicKeyUpdate)) { var tempMessage = (PublicKeyUpdate)e.MessageEnevelope.Message; PublicKeystore.Add(tempMessage.ClientInfo); //TODO: Re-enable Log.Verbose($"Received public key '{tempMessage.ClientInfo.ECKey.GetPublicKeyHash()}' for client '{tempMessage.ClientInfo.Name}'."); } else if (e.MessageEnevelope.Message.GetType() == typeof(TrustZoneSharedKeyUpdate)) { var tempMessage = (TrustZoneSharedKeyUpdate)e.MessageEnevelope.Message; if (e.MessageEnevelope.SenderIdentityHash != this.PublicKeystore.TrustCoordinatorIdentity.IdentityHash) { throw new Exception("Trust Zone Key Update from client that is not the coordinator."); } PublicKeystore.TrustZoneSharedKeys.Add(tempMessage.SharedKey); Log.Verbose($"Received trust zone shared key"); } else { FireMessageReceivedEvent(e); } } #if false else if (clientServerHandshakeComplete) { throw new Exception(); } #endif }