Ejemplo n.º 1
0
        public void Should_return_specified_retry_count()
        {
            var retryCount     = 10;
            var recoveryPolicy = RecoveryPolicyFactory.ConstantBackoff(TimeSpan.FromSeconds(1), retryCount);

            Assert.Equal(10, recoveryPolicy.RetryCount);
        }
Ejemplo n.º 2
0
        public void Should_throw_exception_when_retry_count_less_than_zero()
        {
            var delay      = TimeSpan.FromMilliseconds(-1);
            var retryCount = -1;

            Assert.Throws <ArgumentOutOfRangeException>(() => RecoveryPolicyFactory.ConstantBackoff(delay, retryCount: retryCount));
        }
        public async Task Should_trigger_ConnectionRecoveryError_when_connection_recovery_failed()
        {
            var host = CreateOpenedContainerHost();

            var connectionFactory = CreateConnectionFactory();

            connectionFactory.RecoveryPolicy = RecoveryPolicyFactory.ConstantBackoff(TimeSpan.FromMilliseconds(10), retryCount: 1);

            await using var connection = await connectionFactory.CreateAsync(host.Endpoint);

            var       connectionRecoveryFailed = new AutoResetEvent(false);
            Exception connectionRecoveryError  = null;

            connection.ConnectionRecoveryError += (sender, args) =>
            {
                connectionRecoveryError = args.Exception;
                connectionRecoveryFailed.Set();
            };

            Assert.True(connection.IsOpened);

            host.Dispose();

            Assert.True(connectionRecoveryFailed.WaitOne(Timeout));
            Assert.NotNull(connectionRecoveryError);
        }
Ejemplo n.º 4
0
        public void Should_return_constant_delay()
        {
            var delay          = TimeSpan.FromMilliseconds(10);
            var recoveryPolicy = RecoveryPolicyFactory.ConstantBackoff(delay);
            var result         = RecoveryPolicyUtils.GetDelays(recoveryPolicy);

            Assert.Equal(new double[] { 10, 10, 10, 10, 10 }, result);
        }
Ejemplo n.º 5
0
        public void Should_return_zero_delay_for_first_attempt_when_fast_first_flag_enabled()
        {
            var delay          = TimeSpan.FromMilliseconds(10);
            var recoveryPolicy = RecoveryPolicyFactory.ConstantBackoff(delay, fastFirst: true);
            var result         = RecoveryPolicyUtils.GetDelays(recoveryPolicy);

            Assert.Equal(new double[] { 0, 10, 10, 10, 10 }, result);
        }
Ejemplo n.º 6
0
        public async Task Throws_when_recovery_policy_gave_up_and_consumer_was_not_able_to_receive_message_if_ReceiveAsync_called_after_connection_lost()
        {
            var endpoint = GetUniqueEndpoint();

            var host = CreateOpenedContainerHost(endpoint);

            var connectionFactory = CreateConnectionFactory();

            connectionFactory.RecoveryPolicy = RecoveryPolicyFactory.ConstantBackoff(TimeSpan.FromMilliseconds(100), 1);

            await using var connection = await connectionFactory.CreateAsync(endpoint);

            var consumer = await connection.CreateConsumerAsync("a1", RoutingType.Anycast);

            await DisposeHostAndWaitUntilConnectionNotified(host, connection);

            var cts = new CancellationTokenSource(Timeout);
            await Assert.ThrowsAsync <ConsumerClosedException>(async() => await consumer.ReceiveAsync(cts.Token));
        }
Ejemplo n.º 7
0
        public async Task Throws_when_recovery_policy_gave_up_and_producer_was_not_able_to_SendAsync_message()
        {
            var endpoint = GetUniqueEndpoint();

            var host1 = CreateOpenedContainerHost(endpoint);

            var connectionFactory = CreateConnectionFactory();

            connectionFactory.RecoveryPolicy = RecoveryPolicyFactory.ConstantBackoff(TimeSpan.FromMilliseconds(100), 1);

            await using var connection = await connectionFactory.CreateAsync(endpoint);

            var producer = await connection.CreateProducerAsync("a1", RoutingType.Anycast);

            await DisposeHostAndWaitUntilConnectionNotified(host1, connection);

            var cts = new CancellationTokenSource(Timeout);
            await Assert.ThrowsAsync <ProducerClosedException>(() => producer.SendAsync(new Message("foo"), cts.Token));
        }
Ejemplo n.º 8
0
        public void Should_throw_exception_when_initial_delay_less_than_zero()
        {
            var delay = TimeSpan.FromMilliseconds(-1);

            Assert.Throws <ArgumentOutOfRangeException>(() => RecoveryPolicyFactory.ConstantBackoff(delay));
        }
Ejemplo n.º 9
0
        public void Should_return_no_limit_retry_count_when_no_retry_count_specified()
        {
            var recoveryPolicy = RecoveryPolicyFactory.ConstantBackoff(TimeSpan.FromSeconds(1));

            Assert.Equal(int.MaxValue, recoveryPolicy.RetryCount);
        }
        public void Should_be_possible_to_assign_custom_recovery_policy()
        {
            var connectionFactory = CreateConnectionFactory();

            connectionFactory.RecoveryPolicy = RecoveryPolicyFactory.ConstantBackoff(TimeSpan.FromSeconds(1));
        }
Ejemplo n.º 11
0
        public static async Task Main(string[] args)
        {
            var cts = new CancellationTokenSource();

            Console.CancelKeyPress += (_, _) => { cts.Cancel(); };

            using var loggerFactory = LoggerFactory.Create(builder =>
            {
                builder.SetMinimumLevel(LogLevel.Information);
                builder.AddConsole();
            });

            var connectionFactory = new ConnectionFactory
            {
                AutomaticRecoveryEnabled = true,
                RecoveryPolicy           = RecoveryPolicyFactory.ConstantBackoff(TimeSpan.FromSeconds(5), retryCount: 5),
                LoggerFactory            = loggerFactory
            };
            var endpoint = Endpoint.Create(host: "localhost", port: 5672, "guest", "guest");

            await using var connection = await connectionFactory.CreateAsync(endpoint, cts.Token);

            connection.ConnectionRecoveryError += (_, _) =>
            {
                Console.WriteLine("Disconnected");
                cts.Cancel();
            };

            var address = "my-address";
            var queue   = "my-queue2";

            var topologyManager = await connection.CreateTopologyManagerAsync(cts.Token);

            await topologyManager.DeclareQueueAsync(new QueueConfiguration
            {
                Address           = address,
                Name              = queue,
                AutoCreateAddress = true,
                RoutingType       = RoutingType.Multicast,
                Exclusive         = true,
                Durable           = true,
            }, cts.Token);

            await topologyManager.DisposeAsync();

            var consumer = await connection.CreateConsumerAsync(address, queue, cancellationToken : cts.Token);

            Console.WriteLine($"Attached to queue: {queue}");

            await Task.Run(async() =>
            {
                while (!cts.IsCancellationRequested)
                {
                    try
                    {
                        await using var transaction = new Transaction();
                        var msg = await consumer.ReceiveAsync(cts.Token);
                        await consumer.AcceptAsync(msg, transaction, cts.Token);
                        await transaction.CommitAsync(cts.Token);
                        Console.WriteLine($"Received message: {msg.GetBody<string>()}");
                    }
                    catch (OperationCanceledException)
                    {
                    }
                    catch (ActiveMQArtemisClientException e)
                    {
                        Console.Error.WriteLine(e);
                    }
                }
            }, cts.Token);
        }