Beispiel #1
0
        public void transient_exception_is_retried_but_throws_eventually()
        {
            using var conn = new NpgsqlConnection(ConnectionSource.ConnectionString);
            conn.Open();

            var retryPolicy          = DefaultRetryPolicy.Twice(sleep: x => TimeSpan.FromMilliseconds(20));
            var retryPolicyDecorator = RetryPolicyDecorator.For(retryPolicy, runNumber =>
                                                                throw createNpgsqlException(true));


            Exception <NpgsqlException>
            .ShouldBeThrownBy(() => retryPolicyDecorator.Execute(() => conn.CreateCommand("").ExecuteNonQuery()));

            retryPolicyDecorator.ExecutionCount.ShouldBe(3);
        }
Beispiel #2
0
        public void non_transient_exception_is_not_retried()
        {
            using var conn = new NpgsqlConnection(ConnectionSource.ConnectionString);
            conn.Open();

            var retryPolicy          = DefaultRetryPolicy.Times(1, sleep: x => TimeSpan.FromMilliseconds(20));
            var retryPolicyDecorator = RetryPolicyDecorator.For(retryPolicy, runNumber =>
                                                                throw createNpgsqlException(false));


            Exception <NpgsqlException>
            .ShouldBeThrownBy(() => retryPolicyDecorator.Execute(() => conn.CreateCommand().ExecuteNonQuery()));

            retryPolicyDecorator.ExecutionCount.ShouldBe(1);
        }
        public void non_transient_exception_is_not_retried()
        {
            var retryPolicy          = DefaultRetryPolicy.Times(1, sleep: x => TimeSpan.FromMilliseconds(20));
            var retryPolicyDecorator = RetryPolicyDecorator.For(retryPolicy, runNumber =>
                                                                throw createNpgsqlException(false));

            using var connection = new ManagedConnection(new ConnectionSource(), retryPolicyDecorator);
            var cmd = new NpgsqlCommand();

            Exception <NpgsqlException>
            .ShouldBeThrownBy(() => connection.Execute(cmd));

            retryPolicyDecorator.ExecutionCount.ShouldBe(1);
            cmd.Dispose();
            connection.Dispose();
        }
Beispiel #4
0
        public async Task transient_exception_is_retried()
        {
            using var conn = new NpgsqlConnection(ConnectionSource.ConnectionString);
            await conn.OpenAsync();

            var retryPolicy          = DefaultRetryPolicy.Times(1, sleep: x => TimeSpan.FromMilliseconds(20));
            var retryPolicyDecorator = RetryPolicyDecorator.For(retryPolicy, runNumber =>
            {
                if (runNumber == 1)
                {
                    throw createNpgsqlException(true);
                }
            });

            await retryPolicyDecorator.ExecuteAsync(() => conn.CreateCommand("select 1").ExecuteNonQueryAsync(), default);

            retryPolicyDecorator.ExecutionCount.ShouldBe(2);
        }
        public async Task transient_exception_is_retried()
        {
            var retryPolicy          = DefaultRetryPolicy.Times(1, sleep: x => TimeSpan.FromMilliseconds(20));
            var retryPolicyDecorator = RetryPolicyDecorator.For(retryPolicy, runNumber =>
            {
                if (runNumber == 1)
                {
                    throw createNpgsqlException(true);
                }
            });

            using var connection = new ManagedConnection(new ConnectionSource(), retryPolicyDecorator);
            var cmd = new NpgsqlCommand("select 1");

            await connection.ExecuteAsync(cmd);

            retryPolicyDecorator.ExecutionCount.ShouldBe(2);
            cmd.Dispose();
            connection.Dispose();
        }