public void Should_always_be_called() { bool called = false; var composer = new TaskComposer <int>(); composer.Execute(() => { throw new InvalidOperationException("This is expected"); }); composer.Finally(status => { called = true; }); Assert.Throws <AggregateException>(() => composer.Finish().Wait()); Assert.IsTrue(called); }
public void Should_always_be_called() { bool called = false; var composer = new TaskComposer<int>(); composer.Execute(() => { throw new InvalidOperationException("This is expected"); }); composer.Finally(status => { called = true; }); Assert.Throws<AggregateException>(() => composer.Finish().Wait()); Assert.IsTrue(called); }
Task StartStressGenerator(int instance, Task start) { var ready = new TaskCompletionSource<bool>(); var composer = new TaskComposer<bool>(_cancel.Token, false); var endpointAddress = _serviceBus.Endpoint.Address as IRabbitMqEndpointAddress; string queueName = string.Format("{0}_client_{1}", endpointAddress.Name, instance); Uri uri = RabbitMqEndpointAddress.Parse(_clientUri).ForQueue(queueName).Uri; var uriBuilder = new UriBuilder(uri); uriBuilder.Query = _clientUri.Query.Trim('?'); Uri address = uriBuilder.Uri; composer.Execute(() => { Interlocked.Increment(ref _instanceCount); }); IServiceBus bus = null; composer.Execute(() => { _log.InfoFormat("Creating {0}", address); bus = ServiceBusFactory.New(x => { x.UseRabbitMq(r => { r.ConfigureHost(address, h => { h.SetUsername(_username); h.SetPassword(_password); h.SetRequestedHeartbeat(_heartbeat); }); }); x.ReceiveFrom(address); }); }, false); Stopwatch clientTimer = null; composer.Execute(() => { ready.TrySetResult(true); return start; }); composer.Execute(() => clientTimer = Stopwatch.StartNew()); for (int requestClient = 0; requestClient < _requestsPerInstance; requestClient++) { int clientIndex = requestClient; composer.Execute(() => { Task task = composer.Compose(x => { for (int i = 0; i < _iterations; i++) { int iteration = i; x.Execute(() => { string messageContent = _mixed && iteration % 2 == 0 ? new string('*', 128) : _messageContent; var requestMessage = new StressfulRequestMessage(messageContent); ITaskRequest<StressfulRequest> taskRequest = bus.PublishRequestAsync<StressfulRequest>(requestMessage, r => { r.Handle<StressfulResponse>(response => { Interlocked.Increment(ref _responseCount); TimeSpan timeSpan = response.Timestamp - requestMessage.Timestamp; Interlocked.Add(ref _responseTime, (long)timeSpan.TotalMilliseconds); _timings[instance][clientIndex * _iterations + iteration] = (int)timeSpan.TotalMilliseconds; if (response.RequestId != requestMessage.RequestId) { Interlocked.Increment(ref _mismatchedResponseCount); } }); }); Interlocked.Increment(ref _requestCount); return taskRequest.Task; }); } }); return task; }); } composer.Execute(() => clientTimer.Stop()); composer.Execute(() => bus.Dispose(), false); composer.Compensate(compensation => { return compensation.Handled(); }); composer.Finally(status => { Interlocked.Add(ref _totalTime, clientTimer.ElapsedMilliseconds); int count = Interlocked.Decrement(ref _instanceCount); if (count == 0) Task.Factory.StartNew(() => _hostControl.Stop()); }, false); _clientTasks.Add(composer.Finish()); return ready.Task; }
Task StartStressGenerator(int instance, Task start) { var ready = new TaskCompletionSource<bool>(); var composer = new TaskComposer<bool>(_cancel.Token, false); string queueName = string.Format("{0}_client_{1}", _serviceBusAddress.Name, instance); Uri uri = RabbitMqEndpointAddress.Parse(_clientUri).ForQueue(queueName).Uri; var uriBuilder = new UriBuilder(uri); uriBuilder.Query = _clientUri.Query.Trim('?'); Uri address = uriBuilder.Uri; composer.Execute(() => { Interlocked.Increment(ref _instanceCount); }); composer.Execute(async () => { await Task.Yield(); }); IServiceBus bus = null; composer.Execute(() => { _log.InfoFormat("Creating {0}", address); bus = ServiceBusFactory.New(x => { x.UseRabbitMq(r => { r.ConfigureHost(address, h => { h.SetUsername(_username); h.SetPassword(_password); h.SetRequestedHeartbeat(_heartbeat); }); }); x.ReceiveFrom(address); }); }, false); Stopwatch clientTimer = null; composer.Execute(async () => { ready.TrySetResult(true); await start; }); composer.Execute(() => clientTimer = Stopwatch.StartNew()); composer.Execute(() => { Task task = composer.Compose(x => { for (int i = 0; i < _iterations; i++) { int iteration = i; x.Execute(() => { string messageContent = _mixed && iteration % 2 == 0 ? new string('*', 128) : _messageContent; var requestMessage = new StressfulRequestMessage(messageContent); Stopwatch sendTimer = Stopwatch.StartNew(); bus.Publish<StressfulRequest>(requestMessage); sendTimer.Stop(); Interlocked.Add(ref _sendTime, sendTimer.ElapsedTicks); _timings[instance][iteration] = (int)sendTimer.ElapsedTicks; Interlocked.Increment(ref _sendCount); }); } }); return task; }); composer.Execute(() => clientTimer.Stop()); composer.Execute(() => bus.Dispose(), false); composer.Compensate(compensation => { return compensation.Handled(); }); composer.Finally(status => { Interlocked.Add(ref _totalTime, clientTimer.ElapsedMilliseconds); int count = Interlocked.Decrement(ref _instanceCount); if (count == 0) Task.Factory.StartNew(() => _hostControl.Stop()); }, false); _clientTasks.Add(composer.Finish()); return ready.Task; }
Task StartStressGenerator(int instance, Task start) { var ready = new TaskCompletionSource <bool>(); var composer = new TaskComposer <bool>(_cancel.Token, false); var endpointAddress = _serviceBus.Endpoint.Address as IRabbitMqEndpointAddress; string queueName = string.Format("{0}_client_{1}", endpointAddress.Name, instance); Uri uri = RabbitMqEndpointAddress.Parse(_clientUri).ForQueue(queueName).Uri; var uriBuilder = new UriBuilder(uri); uriBuilder.Query = _clientUri.Query.Trim('?'); Uri address = uriBuilder.Uri; composer.Execute(() => { Interlocked.Increment(ref _instanceCount); }); IServiceBus bus = null; composer.Execute(() => { _log.InfoFormat("Creating {0}", address); bus = ServiceBusFactory.New(x => { x.UseRabbitMq(r => { r.ConfigureHost(address, h => { h.SetUsername(_username); h.SetPassword(_password); h.SetRequestedHeartbeat(_heartbeat); }); }); x.ReceiveFrom(address); }); }, false); Stopwatch clientTimer = null; composer.Execute(() => { ready.TrySetResult(true); return(start); }); composer.Execute(() => clientTimer = Stopwatch.StartNew()); for (int requestClient = 0; requestClient < _requestsPerInstance; requestClient++) { int clientIndex = requestClient; composer.Execute(() => { Task task = composer.Compose(x => { for (int i = 0; i < _iterations; i++) { int iteration = i; x.Execute(() => { string messageContent = _mixed && iteration % 2 == 0 ? new string('*', 128) : _messageContent; var requestMessage = new StressfulRequestMessage(messageContent); ITaskRequest <StressfulRequest> taskRequest = bus.PublishRequestAsync <StressfulRequest>(requestMessage, r => { r.Handle <StressfulResponse>(response => { Interlocked.Increment(ref _responseCount); TimeSpan timeSpan = response.Timestamp - requestMessage.Timestamp; Interlocked.Add(ref _responseTime, (long)timeSpan.TotalMilliseconds); _timings[instance][clientIndex * _iterations + iteration] = (int)timeSpan.TotalMilliseconds; if (response.RequestId != requestMessage.RequestId) { Interlocked.Increment(ref _mismatchedResponseCount); } }); }); Interlocked.Increment(ref _requestCount); return(taskRequest.Task); }); } }); return(task); }); } composer.Execute(() => clientTimer.Stop()); composer.Execute(() => bus.Dispose(), false); composer.Compensate(compensation => { return(compensation.Handled()); }); composer.Finally(status => { Interlocked.Add(ref _totalTime, clientTimer.ElapsedMilliseconds); int count = Interlocked.Decrement(ref _instanceCount); if (count == 0) { Task.Factory.StartNew(() => _hostControl.Stop()); } }, false); _clientTasks.Add(composer.Finish()); return(ready.Task); }
Task StartStressGenerator(int instance, Task start) { var ready = new TaskCompletionSource <bool>(); var composer = new TaskComposer <bool>(_cancel.Token, false); string queueName = string.Format("{0}_client_{1}", _serviceBusAddress.Name, instance); Uri uri = RabbitMqEndpointAddress.Parse(_clientUri).ForQueue(queueName).Uri; var uriBuilder = new UriBuilder(uri); uriBuilder.Query = _clientUri.Query.Trim('?'); Uri address = uriBuilder.Uri; composer.Execute(() => { Interlocked.Increment(ref _instanceCount); }); composer.Execute(async() => { await Task.Yield(); }); IServiceBus bus = null; composer.Execute(() => { _log.InfoFormat("Creating {0}", address); bus = ServiceBusFactory.New(x => { x.UseRabbitMq(r => { r.ConfigureHost(address, h => { h.SetUsername(_username); h.SetPassword(_password); h.SetRequestedHeartbeat(_heartbeat); }); }); x.ReceiveFrom(address); }); }, false); Stopwatch clientTimer = null; composer.Execute(async() => { ready.TrySetResult(true); await start; }); composer.Execute(() => clientTimer = Stopwatch.StartNew()); composer.Execute(() => { Task task = composer.Compose(x => { for (int i = 0; i < _iterations; i++) { int iteration = i; x.Execute(() => { string messageContent = _mixed && iteration % 2 == 0 ? new string('*', 128) : _messageContent; var requestMessage = new StressfulRequestMessage(messageContent); Stopwatch sendTimer = Stopwatch.StartNew(); bus.Publish <StressfulRequest>(requestMessage); sendTimer.Stop(); Interlocked.Add(ref _sendTime, sendTimer.ElapsedTicks); _timings[instance][iteration] = (int)sendTimer.ElapsedTicks; Interlocked.Increment(ref _sendCount); }); } }); return(task); }); composer.Execute(() => clientTimer.Stop()); composer.Execute(() => bus.Dispose(), false); composer.Compensate(compensation => { return(compensation.Handled()); }); composer.Finally(status => { Interlocked.Add(ref _totalTime, clientTimer.ElapsedMilliseconds); int count = Interlocked.Decrement(ref _instanceCount); if (count == 0) { Task.Factory.StartNew(() => _hostControl.Stop()); } }, false); _clientTasks.Add(composer.Finish()); return(ready.Task); }