Path: redb/redb.Tests.Integration/Tests/Unit/BulkInsertTimeoutTests.cs
Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb
Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Tests.Integration/Tests/Unit/BulkInsertTimeoutTests.cs
----
using System;
using AwesomeAssertions;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.DependencyInjection;
using redb.Core.Data;
using redb.Core.Extensions;
using redb.Core.Models.Configuration;
using redb.Core.Pro.Extensions;
using redb.MSSql.Data;
using redb.Postgres.Data;
using Xunit;
namespace redb.Tests.Integration.Tests.Unit;
///
/// The timeout of the bulk load of a save. SqlBulkCopy has its own timeout of 30 seconds, which the connection string's
/// Command Timeout does not reach, so a large AddNewObjectsAsync on a busy SQL Server failed with no setting to turn.
/// goes from the configuration through the context that the
/// provider registers to the bulk operations and onto SqlBulkCopy (and the PostgreSQL binary importer). Shown on the
/// configured objects; no server is opened.
///
public class BulkInsertTimeoutTests
{
private const string MsSqlConnectionString = "Server=unused;Database=unused;User Id=u;Password=p;TrustServerCertificate=true";
private const string PostgresConnectionString = "Host=unused;Database=unused;Username=u;Password=p";
[Fact]
public void Default_keeps_the_driver_value()
=> new RedbServiceConfiguration().BulkInsertTimeout.Should().BeNull();
[Fact]
public void Negative_timeout_is_refused_and_zero_is_no_limit()
{
var config = new RedbServiceConfiguration();
config.Invoking(c => c.BulkInsertTimeout = TimeSpan.FromSeconds(-1))
.Should().Throw().WithParameterName(nameof(RedbServiceConfiguration.BulkInsertTimeout));
config.BulkInsertTimeout = TimeSpan.Zero;
config.BulkInsertTimeout.Should().Be(TimeSpan.Zero);
}
[Theory]
[InlineData(null, 30)] // the driver's own default stays
[InlineData(0.0, 0)] // no limit
[InlineData(1.2, 2)] // rounded up, never shorter than asked
[InlineData(300.0, 300)]
public void SqlBulkCopy_takes_the_timeout(double? seconds, int expected)
{
using var connection = new SqlConnection(MsSqlConnectionString);
using var bulkCopy = new SqlBulkCopy(connection);
SqlBulkOperations.ApplyTimeout(bulkCopy, seconds is { } s ? TimeSpan.FromSeconds(s) : null);
bulkCopy.BulkCopyTimeout.Should().Be(expected);
}
[Fact]
public void MsSql_free_registration_hands_the_timeout_to_the_bulk_operations()
{
var services = new ServiceCollection();
services.AddRedb(o => redb.MSSql.Extensions.MsSqlOptionsExtensions.UseMsSql(o, MsSqlConnectionString)
.Configure(c => c.BulkInsertTimeout = TimeSpan.FromMinutes(5)));
BulkTimeoutOf(services, b => b.TimeoutSource()).Should().Be(TimeSpan.FromMinutes(5));
}
[Fact]
public void MsSql_pro_registration_hands_the_timeout_to_the_bulk_operations()
{
var services = new ServiceCollection();
services.AddRedbPro(o => redb.MSSql.Pro.Extensions.MsSqlProOptionsExtensions.UseMsSql(o, MsSqlConnectionString)
.Configure(c => c.BulkInsertTimeout = TimeSpan.FromMinutes(5)));
BulkTimeoutOf(services, b => b.TimeoutSource()).Should().Be(TimeSpan.FromMinutes(5));
}
[Fact]
public void Postgres_free_registration_hands_the_timeout_to_the_bulk_operations()
{
var services = new ServiceCollection();
services.AddRedb(o => redb.Postgres.Extensions.PostgresOptionsExtensions.UsePostgres(o, PostgresConnectionString)
.Configure(c => c.BulkInsertTimeout = TimeSpan.FromMinutes(5)));
BulkTimeoutOf(services, b => b.TimeoutSource()).Should().Be(TimeSpan.FromMinutes(5));
}
[Fact]
public void Postgres_pro_registration_hands_the_timeout_to_the_bulk_operations()
{
var services = new ServiceCollection();
services.AddRedbPro(o => redb.Postgres.Pro.Extensions.PostgresProOptionsExtensions.UsePostgres(o, PostgresConnectionString)
.Configure(c => c.BulkInsertTimeout = TimeSpan.FromMinutes(5)));
BulkTimeoutOf(services, b => b.TimeoutSource()).Should().Be(TimeSpan.FromMinutes(5));
}
[Fact]
public void Without_the_setting_the_bulk_operations_keep_the_driver_value()
{
var services = new ServiceCollection();
services.AddRedbPro(o => redb.MSSql.Pro.Extensions.MsSqlProOptionsExtensions.UseMsSql(o, MsSqlConnectionString));
BulkTimeoutOf(services, b => b.TimeoutSource()).Should().BeNull();
}
private static TimeSpan? BulkTimeoutOf(IServiceCollection services, Func read)
where TBulk : class, IBulkOperations
{
using var sp = services.BuildServiceProvider();
using var scope = sp.CreateScope();
var context = scope.ServiceProvider.GetRequiredService();
context.Bulk.Should().BeOfType();
return read((TBulk)context.Bulk);
}
}