public void should_be_ok_for_unknow_user() { ThrottleHelper h = new ThrottleHelper(10000, sammy, pepe); h.Add("riton", "r1"); Assert.IsTrue(h.Check("riton")); }
public static void Main(String[] args) { ManualResetEvent evtShutdown = new ManualResetEvent(false); long ThrottleTime = 0; int lap = 0; int maxLaps = 500; long startTicks, elapsed; if (args.Length > 0) { int.TryParse(args[0], out maxLaps); } ThrottleHelper throt = new ThrottleHelper(maxLaps); startTicks = DateTime.Now.Ticks; while (evtShutdown.WaitOne(1) == false) { ++lap; if ((ThrottleTime = throt.VerifyLaps()) > 0) { elapsed = (long)(new TimeSpan(DateTime.Now.Ticks - startTicks)).TotalMilliseconds; Console.WriteLine(" ThrotteTime={0}/Laps={1} TotalTime={2}", ThrottleTime, lap, elapsed); lap = 0; startTicks = DateTime.Now.Ticks; } } }
public void should_accept_one_request() { ThrottleHelper h = new ThrottleHelper(10000, sammy, pepe); h.Add("sammy", "r1"); Assert.IsTrue(h.Check("sammy")); }
public void should_accept_user_for_unknow_route() { ThrottleHelper h = new ThrottleHelper(10000, sammy, pepe); for (int i = 0; i < 100; i++) { h.Add("sammy", "r5"); } Assert.IsTrue(h.Check("sammy")); }
public void should_reject_user_for_too_much_request() { ThrottleHelper h = new ThrottleHelper(10000, sammy, pepe); for (int i = 0; i < 6; i++) { h.Add("sammy", "r1"); } Assert.IsFalse(h.Check("sammy")); }
public void should_accept_under_limitation_request() { ThrottleHelper h = new ThrottleHelper(10000, sammy, pepe); for (int i = 0; i < 400; i++) { h.Add("pepe", "r4"); } Assert.IsTrue(h.Check("pepe")); }
public void should_accept_slow_requests_cadency() { ThrottleHelper h = new ThrottleHelper(500, sammy, pepe); for (int i = 0; i < 10; i++) { h.Add("sammy", "r1"); Thread.Sleep(100); } Assert.IsTrue(h.Check("sammy")); }
public void should_accept_user_with_another_rejected() { ThrottleHelper h = new ThrottleHelper(10000, sammy, pepe); for (int i = 0; i < 6; i++) { h.Add("sammy", "r1"); } for (int i = 0; i < 10; i++) { h.Add("pepe", "r1"); } Assert.IsFalse(h.Check("sammy")); Assert.IsTrue(h.Check("pepe")); }
public void should_not_be_too_slow() { ThrottleHelper h = new ThrottleHelper(1, sammy, pepe); for (int i = 0; i < 10000; i++) { h.Add("sammy", "r1"); } for (int i = 0; i < 5000; i++) { h.Add("sammy", "r2"); } for (int i = 0; i < 20000; i++) { h.Add("pepe", "r1"); } DateTime before = DateTime.Now; h.Add("sammy", "r1"); h.Check("sammy"); DateTime after = DateTime.Now; Assert.IsTrue((after - before).TotalMilliseconds < 10); }
private void OnLoaded(object sender, RoutedEventArgs routedEventArgs) { throttleHelper?.Dispose(); throttleHelper = new ThrottleHelper(150, ScrollIntoSelectedInternal); }
private void cmdAnswerMessage_Click(object sender, System.EventArgs e) { string szSrvQueue = txtSrvQueueName.Text.Trim(); string szResponseQueue = null; string szSrvAdminQueue = txtSrvAdminQueueName.Text.Trim(); MessageQueue queue = null, queueAnswer = null; System.Messaging.Message ReceivedMsg = null; System.Messaging.Message AnswerMsg = null; int iTimeToReach; int iTimeToBeReceived; int iQtdMsgsxs; int.TryParse(txtTimeToReachClient.Text, out iTimeToReach); int.TryParse(txtTimeToBeReceivedClient.Text, out iTimeToBeReceived); int.TryParse(txtMsgsxs.Text, out iQtdMsgsxs); ThrottleHelper throttler = null; try { // Verifica se especificada fila de leitura if (szSrvQueue.Length == 0) { MessageBox.Show("Preciso do nome da fila local"); return; } CreateQueue(".\\" + szSrvQueue); queue = new MessageQueue(".\\" + szSrvQueue, true); bool bAllMessages = true; string szResponseQueueCache = string.Empty; if (iQtdMsgsxs > 0) { throttler = new ThrottleHelper(iQtdMsgsxs); } do { try { ReceivedMsg = queue.Receive(new System.TimeSpan(0, 0, 0, 0, 1), MessageQueueTransactionType.None); szResponseQueue = ReceivedMsg.ResponseQueue.FormatName; AnswerMsg = new System.Messaging.Message("bodybodybodybodybodybodybodybodybodybodybody"); if (queueAnswer == null || szResponseQueueCache != szResponseQueue) { if (queueAnswer != null) { queueAnswer.Close(); queueAnswer = null; } queueAnswer = new MessageQueue("FORMATNAME:" + szResponseQueue, false); szResponseQueueCache = szResponseQueue; } // Verifica se especificado algum Ack. // Caso exista, deve existir uma fila administrativa if ((szSrvAdminQueue.Length == 0) && (false == _ackMask.Equals(AcknowledgeTypes.None))) { MessageBox.Show("Quando especificado Acknowledges, preciso da fila admin."); return; } // Se informada fila administrativa, // Cria referencia para a mesma if (szSrvAdminQueue.Length > 0) { AnswerMsg.AdministrationQueue = new MessageQueue("FORMATNAME:DIRECT=OS:" + Dns.GetHostName() + "\\" + szSrvAdminQueue, false); } // Nao Adiciona o GUID do emissor da mensagem // para que o msmq nao tente usar o AD para identificar o emissor no destino AnswerMsg.AttachSenderId = false; if (iTimeToReach > 0) { AnswerMsg.TimeToReachQueue = new System.TimeSpan(0, 0, 0, 0, iTimeToReach); } if (iTimeToBeReceived > 0) { AnswerMsg.TimeToBeReceived = new System.TimeSpan(0, 0, 0, 0, iTimeToBeReceived); } AnswerMsg.AcknowledgeType = _ackMask; queueAnswer.Send(AnswerMsg, "Answer"); if (throttler != null) { throttler.VerifyLaps(); } //MessageBox.Show( String.Format( "Enviada resposta para:{0}", szResponseQueue ), "Queues" ); } catch (MessageQueueException ex) { //if (ex.MessageQueueErrorCode == MessageQueueErrorCode.IOTimeout) bAllMessages = false; } } while (bAllMessages); } catch (Exception ex) { MessageBox.Show(String.Format("Exception no envio da resposta: {0}", ex)); } finally { if (queue != null) { queue.Close(); queue = null; } if (queueAnswer != null) { queueAnswer.Close(); queueAnswer = null; } } }
private void cmdSendMessage_Click(object sender, System.EventArgs e) { string szSrvName = txtSrvName.Text.Trim(); string szSrvDstQueue = txtSrvQueueName.Text.Trim(); string szCliAdminQueue = txtCliAdminQueueName.Text.Trim(); string szClieQueue = txtCliQueueName.Text.Trim(); int iTimeToReach; int iTimeToBeReceived; int iQtdMsgs; int iQtdMsgsxs; MessageQueue queue = null; SetResultAck(); int.TryParse(txtTimeToReach.Text, out iTimeToReach); int.TryParse(txtTimeToBeReceived.Text, out iTimeToBeReceived); int.TryParse(txtQtdMsgs.Text, out iQtdMsgs); int.TryParse(txtMsgsxs.Text, out iQtdMsgsxs); ThrottleHelper throttler = null; try { // Verifica se especificada o nome do servidor de destino if (szSrvName.Length == 0) { MessageBox.Show("Preciso do nome do servidor de destino"); return; } // Verifica se especificada fila de destino if (szSrvDstQueue.Length == 0) { MessageBox.Show("Preciso do nome da fila de destino"); return; } // Verifica se especificado algum Ack. // Caso exista, deve existir uma fila administrativa if ((szCliAdminQueue.Length == 0) && (false == _ackMask.Equals(AcknowledgeTypes.None))) { MessageBox.Show("Quando especificado Acknowledges, preciso da fila admin."); return; } // Cria um ponteiro para a fila de destino queue = new MessageQueue("FORMATNAME:DIRECT=OS:" + szSrvName + "\\" + szSrvDstQueue, false); System.Messaging.Message msg = new System.Messaging.Message( "<?xml version=\"1.0\"?><string>Message test</string>" ); // Se informada fila administrativa, // Cria referencia para a mesma if (szCliAdminQueue.Length > 0) { CreateQueue(".\\" + szCliAdminQueue); msg.AdministrationQueue = new MessageQueue("FORMATNAME:DIRECT=OS:" + Dns.GetHostName() + "\\" + szCliAdminQueue, false); } CreateQueue(".\\" + szClieQueue); msg.ResponseQueue = new MessageQueue("FORMATNAME:DIRECT=OS:" + Dns.GetHostName() + "\\" + szClieQueue, false); // Nao Adiciona o GUID do emissor da mensagem // para que o msmq nao tente usar o AD para identificar o emissor no destino msg.AttachSenderId = false; msg.AcknowledgeType = _ackMask; if (iTimeToReach > 0) { msg.TimeToReachQueue = new System.TimeSpan(0, 0, 0, 0, iTimeToReach); } if (iTimeToBeReceived > 0) { msg.TimeToBeReceived = new System.TimeSpan(0, 0, 0, 0, iTimeToBeReceived); } if (iQtdMsgs < 1) { iQtdMsgs = 1; } try { if (iQtdMsgsxs > 0) { throttler = new ThrottleHelper(iQtdMsgsxs); } for (int i = 0; i < iQtdMsgs; i++) { queue.Send(msg, "Mensagem teste 0x" + i.ToString("X").PadLeft(4, '0')); if (throttler != null) { throttler.VerifyLaps(); } } } catch (MessageQueueException ex) { // A fila nao existe no destinatario if (ex.ErrorCode == -2147467259) { MessageBox.Show("A fila [" + szSrvName + "\\" + szSrvDstQueue + "] não existe."); } } queue.Close(); } catch (Exception ex) { MessageBox.Show(String.Format("Exception no envio da mensagem: {0}", ex)); } }