Пример #1
0
        public async Task VeryBasicTransactionThing()
        {
            const string destinationQueueName = "another-queue";

            var network = new InMemNetwork();

            network.CreateQueue(destinationQueueName);

            var transport = new InMemTransport(network, "test-queue");

            transport.Initialize();

            using (var scope = new RebusTransactionScope())
            {
                var headers = new Dictionary <string, string> {
                    { Headers.MessageId, Guid.NewGuid().ToString() }
                };

                await transport.Send(
                    destinationAddress : destinationQueueName,
                    message : new TransportMessage(headers, new byte[] { 1, 2, 3 }),
                    context : scope.TransactionContext
                    );

                Assert.That(network.Count(destinationQueueName), Is.EqualTo(0),
                            $"Expected ZERO messages in queue '{destinationQueueName}' at this point, because the scope was not completed");

                await scope.CompleteAsync();
            }

            Assert.That(network.Count(destinationQueueName), Is.EqualTo(1),
                        $"Expected 1 message in queue '{destinationQueueName}' at this point, because the scope is completed now");
        }
        public async Task ItWorks(bool useExtensionMethod)
        {
            var network           = new InMemNetwork();
            var handlerWasEntered = new ManualResetEvent(false);
            var activator         = new BuiltinHandlerActivator();

            Using(activator);

            activator.Handle <string>(async(bus, context, message) =>
            {
                var cancellationToken = useExtensionMethod
                ? context.GetCancellationToken()
                : context.IncomingStepContext.Load <CancellationToken>();

                handlerWasEntered.Set();

                await Task.Delay(TimeSpan.FromSeconds(10), cancellationToken);
            });

            const string queueName = "cancellation-verification";

            Configure.With(activator)
            .Transport(t => t.UseInMemoryTransport(network, queueName))
            .Start();

            await activator.Bus.SendLocal("HEJ MED DIG");

            // wait until the handler is entered
            handlerWasEntered.WaitOrDie(TimeSpan.FromSeconds(1));

            // wait one more second
            await Task.Delay(TimeSpan.FromSeconds(1));

            // measure how long it takes to stop the bus
            var stopwatch = Stopwatch.StartNew();

            CleanUpDisposables();
            var elapsedDisposingTheBus = stopwatch.Elapsed;

            Assert.That(elapsedDisposingTheBus, Is.LessThan(TimeSpan.FromSeconds(2)),
                        "Expected the bus to have shut down very quickly");

            Assert.That(network.Count(queueName), Is.EqualTo(1),
                        "Expected the message to have been moved back into the input queue");
        }