public void A_start_message_is_received()
        {
            Message = new StartSimpleSaga
            {
                CorrelationId = SagaId,
                CustomerId    = CustomerId,
            };

            LocalBus.Publish(Message);
        }
		public void A_start_message_is_received()
		{
			Message = new StartSimpleSaga
				{
					CorrelationId = SagaId,
					CustomerId = CustomerId,
				};

			LocalBus.Publish(Message);
		}
示例#3
0
        public async Task run_single_message_scenario(int hostsCount)
        {
            var message = new StartSimpleSaga(Guid.NewGuid(), Guid.NewGuid());

            var receivedCount = 0;
            var tokenSource   = new CancellationTokenSource(TimeSpan.FromMinutes(1));

            Action <IMessageContext <StartSimpleSaga> > onMessage = ctx =>
            {
                receivedCount++;
                tokenSource.CancelAfter(TimeSpan.FromSeconds(5));

                ctx.Message.Id.Should().Be(message.Id);
                ctx.Message.CorrelationId.Should().Be(message.CorrelationId);
            };
            var createHostTasks = Enumerable.Range(1, hostsCount)
                                  .Select(async i =>
            {
                var host = await SetupHost(onMessage);
                await host.StartAsync(tokenSource.Token);
                return(host);
            }).ToArray();

            await Task.WhenAll(createHostTasks);

            if (tokenSource.IsCancellationRequested)
            {
                throw new Exception("a timeout occurred during hosts initialization.");
            }

            var producerHost = createHostTasks.First().Result;

            using var scope = producerHost.Services.CreateScope();
            var bus = scope.ServiceProvider.GetRequiredService <IMessageBus>();
            await bus.PublishAsync(message, tokenSource.Token);

            while (!tokenSource.IsCancellationRequested)
            {
                await Task.Delay(10);
            }

            foreach (var t in createHostTasks)
            {
                try
                {
                    t.Result.Dispose();
                }
                catch {}
            }

            receivedCount.Should().Be(1);
        }
        public async Task run_single_message_scenario()
        {
            var hostBuilder = CreateHostBuilder();

            var message = new StartSimpleSaga(Guid.NewGuid(), Guid.NewGuid());

            var received    = false;
            var tokenSource = new CancellationTokenSource(TimeSpan.FromMinutes(1));

            Action <StartSimpleSaga> onMessage = msg =>
            {
                received = true;

                tokenSource.Cancel();

                msg.Should().Be(message);
            };

            hostBuilder.ConfigureServices((ctx, services) =>
            {
                services.AddSingleton(onMessage);
            });

            var host = hostBuilder.Build();

            using var scope = host.Services.CreateScope();
            var bus = scope.ServiceProvider.GetRequiredService <IMessageBus>();

            await host.SetupInfrastructureAsync();

            await Task.WhenAll(new[]
            {
                host.RunAsync(token: tokenSource.Token),
                bus.PublishAsync(message, tokenSource.Token)
            });

            received.Should().BeTrue();
        }