public void SetUp() { _sut = CreateCaraBus(new CaraBusBaseOptions { MaxDegreeOfParallelism = 1 }); }
public static void Subscribe <T>(this ICaraBus caraBus, Action <T> handler, SubscribeOptions options = null) where T : class { caraBus.SubscribeAsync(typeof(T), (message, token) => { handler((T)message); return(Task.CompletedTask); }, options).ConfigureAwait(false).GetAwaiter().GetResult(); }
public static void Subscribe(this ICaraBus caraBus, Type messageType, Action <object> handler, SubscribeOptions options = null) { caraBus.SubscribeAsync(messageType, (message, token) => { handler(message); return(Task.CompletedTask); }, options).ConfigureAwait(false).GetAwaiter().GetResult(); }
public static Task StopAsync(this ICaraBus caraBus) { if (caraBus is ICaraBusStopAsync carabusStopAsync) { return(carabusStopAsync.StopAsync()); } return(Task.CompletedTask); }
public async Task PublishSubscribe_ShouldLimitSimultaneouslyRunningHandlersToSpecifiedValue_IfMaxConcurrentHandlersSpecified() { // given var delivery = new CountdownEvent(10); _sut = CreateCaraBus(new CaraBusBaseOptions { MaxDegreeOfParallelism = 5 }); _sut.Subscribe <TestMessage>(m => { Thread.Sleep(TimeSpan.FromMilliseconds(800)); delivery.Signal(); }, SubscribeOptions.NonExclusive()); await _sut.StartAsync(); // when var sw = new Stopwatch(); sw.Start(); for (var i = 0; i < 10; i++) { await _sut.PublishAsync(new TestMessage()); } delivery.Wait(TimeSpan.FromSeconds(15)); sw.Stop(); var executioTime = sw.Elapsed; // then Assert.That(executioTime.Seconds, Is.GreaterThanOrEqualTo(1)); Assert.That(executioTime.Seconds, Is.LessThanOrEqualTo(2)); // 10 messages, 5 max parallel degree, 10/5 = 2 messages per moment, 800ms * 2 = ~1600 }
public static Task SubscribeAsync <T>(this ICaraBus caraBus, Func <T, CancellationToken, Task> handler, SubscribeOptions options = null, CancellationToken cancellationToken = default(CancellationToken)) where T : class { return(caraBus.SubscribeAsync(typeof(T), (message, token) => handler((T)message, token), options, cancellationToken)); }
public static void Subscribe <T>(this ICaraBus caraBus, Func <T, Task> handler, SubscribeOptions options = null, CancellationToken cancellationToken = default(CancellationToken)) where T : class { caraBus.SubscribeAsync(typeof(T), (message, token) => handler((T)message), options, cancellationToken).ConfigureAwait(false).GetAwaiter().GetResult(); }
public static void Subscribe(this ICaraBus caraBus, Type messageType, Func <object, Task> handler, SubscribeOptions options = null, CancellationToken cancellationToken = default(CancellationToken)) { caraBus.SubscribeAsync(messageType, (message, token) => handler(message), options, cancellationToken).ConfigureAwait(false).GetAwaiter().GetResult(); }