Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,31 +66,22 @@ public void Apply(OrderShipped4185 e, OrderSummary4185 summary)
/// secondary <c>IDocumentStore</c> via constructor injection; that's what
/// this remaining test pins.
/// </summary>
public class Bug_4185_codegen_conflict_projection_with_secondary_store_dependency
public class Bug_4185_codegen_conflict_projection_with_secondary_store_dependency: HostedStoreContext
{
[Fact]
public async Task projection_with_secondary_store_dependency_should_work_at_runtime()
{
using var host = await Host.CreateDefaultBuilder()
.ConfigureServices(services =>
var host = await StartHostAsync(_ => { },
configureServices: services => services.AddMartenStore<IBug4185Store>(opts =>
{
services.AddMartenStore<IBug4185Store>(opts =>
{
opts.Connection(ConnectionSource.ConnectionString);
opts.DatabaseSchemaName = "bug4185_sec";
});

services.AddMarten(opts =>
{
opts.Connection(ConnectionSource.ConnectionString);
opts.DatabaseSchemaName = "bug4185_pri";
})
.AddProjectionWithServices<OrderProjection4185>(
ProjectionLifecycle.Inline,
ServiceLifetime.Singleton)
.ApplyAllDatabaseChangesOnStartup();
})
.StartAsync();
opts.Connection(ConnectionSource.ConnectionString);
opts.DatabaseSchemaName = $"{SchemaName}_sec";
}),
configureMarten: marten => marten
.AddProjectionWithServices<OrderProjection4185>(
ProjectionLifecycle.Inline,
ServiceLifetime.Singleton)
.ApplyAllDatabaseChangesOnStartup());

var store = host.Services.GetRequiredService<IDocumentStore>();
var streamId = Guid.NewGuid();
Expand Down
60 changes: 21 additions & 39 deletions src/CoreTests/Bugs/Bug_4187_ancillary_store_isolation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace CoreTests.Bugs;
/// Regression test for #4187: document tables from ancillary stores should never
/// be created in the main store's database.
/// </summary>
public class Bug_4187_ancillary_store_isolation
public class Bug_4187_ancillary_store_isolation: HostedStoreContext
{
// Types only used in the primary store
public class PrimaryDoc
Expand All @@ -36,27 +36,19 @@ public interface IAncillaryStore : IDocumentStore;
[Fact]
public async Task ancillary_store_types_should_not_appear_in_primary_store_schema()
{
using var host = await Host.CreateDefaultBuilder()
.ConfigureServices(services =>
var host = await StartHostAsync(opts =>
{
services.AddMarten(opts =>
{
opts.Connection(ConnectionSource.ConnectionString);
opts.DatabaseSchemaName = "primary_4187";
opts.RegisterDocumentType<PrimaryDoc>();
// Explicitly do NOT register AncillaryDoc
})
.ApplyAllDatabaseChangesOnStartup();

services.AddMartenStore<IAncillaryStore>(opts =>
{
opts.Connection(ConnectionSource.ConnectionString);
opts.DatabaseSchemaName = "ancillary_4187";
opts.RegisterDocumentType<AncillaryDoc>();
// Explicitly do NOT register PrimaryDoc
});
})
.StartAsync();
opts.RegisterDocumentType<PrimaryDoc>();
// Explicitly do NOT register AncillaryDoc
},
configureServices: services => services.AddMartenStore<IAncillaryStore>(opts =>
{
opts.Connection(ConnectionSource.ConnectionString);
opts.DatabaseSchemaName = $"{SchemaName}_ancillary";
opts.RegisterDocumentType<AncillaryDoc>();
// Explicitly do NOT register PrimaryDoc
}),
configureMarten: marten => marten.ApplyAllDatabaseChangesOnStartup());

var primaryStore = (DocumentStore)host.Services.GetRequiredService<IDocumentStore>();
var ancillaryStore = (DocumentStore)host.Services.GetRequiredService<IAncillaryStore>();
Expand Down Expand Up @@ -86,25 +78,15 @@ public async Task ancillary_store_types_should_not_appear_in_primary_store_schem
[Fact]
public async Task ancillary_store_ddl_should_not_contain_primary_store_types()
{
using var host = await Host.CreateDefaultBuilder()
.ConfigureServices(services =>
var host = await StartHostAsync(
opts => opts.RegisterDocumentType<PrimaryDoc>(),
configureServices: services => services.AddMartenStore<IAncillaryStore>(opts =>
{
services.AddMarten(opts =>
{
opts.Connection(ConnectionSource.ConnectionString);
opts.DatabaseSchemaName = "primary_4187b";
opts.RegisterDocumentType<PrimaryDoc>();
})
.ApplyAllDatabaseChangesOnStartup();

services.AddMartenStore<IAncillaryStore>(opts =>
{
opts.Connection(ConnectionSource.ConnectionString);
opts.DatabaseSchemaName = "ancillary_4187b";
opts.RegisterDocumentType<AncillaryDoc>();
});
})
.StartAsync();
opts.Connection(ConnectionSource.ConnectionString);
opts.DatabaseSchemaName = $"{SchemaName}_ancillary_b";
opts.RegisterDocumentType<AncillaryDoc>();
}),
configureMarten: marten => marten.ApplyAllDatabaseChangesOnStartup());

var primaryStore = (DocumentStore)host.Services.GetRequiredService<IDocumentStore>();
var ancillaryStore = (DocumentStore)host.Services.GetRequiredService<IAncillaryStore>();
Expand Down
17 changes: 5 additions & 12 deletions src/CoreTests/Bugs/Bug_5039_generic_secondary_store_marker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace CoreTests.Bugs;
/// contains a backtick + arity (e.g. <c>IMartenStoreMarker`1</c>), which is not a valid URI
/// hostname when composing the <c>marten://</c> subject in <c>SecondaryStoreConfig.Build</c>.
/// </summary>
public class Bug_5039_generic_secondary_store_marker
public class Bug_5039_generic_secondary_store_marker: HostedStoreContext
{
public sealed class MyContext;
public sealed class OtherContext;
Expand Down Expand Up @@ -44,23 +44,16 @@ public void sanitized_uri_strips_backtick_and_includes_generic_argument()
[Fact]
public async Task can_register_and_resolve_generic_marker_store()
{
using var host = await Host.CreateDefaultBuilder()
.ConfigureServices(services =>
var host = await StartHostAsync(_ => { },
configureServices: services =>
{
services.AddMarten(opts =>
{
opts.Connection(ConnectionSource.ConnectionString);
opts.DatabaseSchemaName = "bug5039_primary";
});

// This threw UriFormatException before the fix
services.AddMartenStore<IMartenStoreMarker<MyContext>>(opts =>
{
opts.Connection(ConnectionSource.ConnectionString);
opts.DatabaseSchemaName = "bug5039_ancillary";
opts.DatabaseSchemaName = $"{SchemaName}_ancillary";
});
})
.StartAsync();
});

var store = host.Services.GetRequiredService<IMartenStoreMarker<MyContext>>();
store.ShouldNotBeNull();
Expand Down
3 changes: 3 additions & 0 deletions src/CoreTests/CoreTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@
<Compile Include="..\Marten.Testing\Harness\DestructiveIntegrationContext.cs">
<Link>Harness\DestructiveIntegrationContext.cs</Link>
</Compile>
<Compile Include="..\Marten.Testing\Harness\HostedStoreContext.cs">
<Link>Harness\HostedStoreContext.cs</Link>
</Compile>
<Compile Include="..\Marten.Testing\Harness\IntegrationContext.cs">
<Link>Harness\IntegrationContext.cs</Link>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ public async Task query_session_with_EventTracingConnectionLifetime_uses_storeOp
{
var logger = new BatchSuccessRecordingLogger();

var store = DocumentStore.For(options =>
await using var store = DocumentStore.For(options =>
{
options.Connection(ConnectionSource.ConnectionString);
options.OpenTelemetry.TrackConnections = TrackLevel.Normal;
Expand Down
36 changes: 12 additions & 24 deletions src/CoreTests/SessionOptionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,11 @@ public async Task can_define_custom_timeout_via_pgcstring()

connectionStringBuilder.CommandTimeout = 1;

var documentStore = DocumentStore.For(c =>
var documentStore = SeparateStore(c =>
{
// Caller-supplied connection string; the base supplies this class's own
// schema so the write/read-back doesn't race schema-wiping tests.
c.Connection(connectionStringBuilder.ToString());

// Own schema: this test writes then reads back through a caller-supplied
// connection, and on the shared public schema it races any test that wipes
// the schema in between.
c.DatabaseSchemaName = "session_options_custom_connection";
});

using var query = documentStore.LightweightSession();
Expand All @@ -208,14 +205,11 @@ public async Task can_override_pgcstring_timeout_in_sessionoptions()

connectionStringBuilder.CommandTimeout = 1;

var documentStore = DocumentStore.For(c =>
var documentStore = SeparateStore(c =>
{
// Caller-supplied connection string; the base supplies this class's own
// schema so the write/read-back doesn't race schema-wiping tests.
c.Connection(connectionStringBuilder.ToString());

// Own schema: this test writes then reads back through a caller-supplied
// connection, and on the shared public schema it races any test that wipes
// the schema in between.
c.DatabaseSchemaName = "session_options_custom_connection";
});

var options = new SessionOptions { Timeout = 60 };
Expand All @@ -231,14 +225,11 @@ public async Task session_with_custom_connection_reusable_after_saveChanges()
{
var connectionStringBuilder = new NpgsqlConnectionStringBuilder(ConnectionSource.ConnectionString);

var documentStore = DocumentStore.For(c =>
var documentStore = SeparateStore(c =>
{
// Caller-supplied connection string; the base supplies this class's own
// schema so the write/read-back doesn't race schema-wiping tests.
c.Connection(connectionStringBuilder.ToString());

// Own schema: this test writes then reads back through a caller-supplied
// connection, and on the shared public schema it races any test that wipes
// the schema in between.
c.DatabaseSchemaName = "session_options_custom_connection";
});

var connection = new NpgsqlConnection(connectionStringBuilder.ToString());
Expand All @@ -259,14 +250,11 @@ public async Task session_with_custom_connection_reusable_after_saveChangesAsync
{
var connectionStringBuilder = new NpgsqlConnectionStringBuilder(ConnectionSource.ConnectionString);

var documentStore = DocumentStore.For(c =>
var documentStore = SeparateStore(c =>
{
// Caller-supplied connection string; the base supplies this class's own
// schema so the write/read-back doesn't race schema-wiping tests.
c.Connection(connectionStringBuilder.ToString());

// Own schema: this test writes then reads back through a caller-supplied
// connection, and on the shared public schema it races any test that wipes
// the schema in between.
c.DatabaseSchemaName = "session_options_custom_connection";
});

var connection = new NpgsqlConnection(connectionStringBuilder.ToString());
Expand Down
59 changes: 21 additions & 38 deletions src/CoreTests/configuring_marten_with_async_extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,34 @@

namespace CoreTests;

public class configuring_marten_with_async_extensions
public class configuring_marten_with_async_extensions: HostedStoreContext
{
[Fact]
public async Task feature_flag_positive()
{
var featureManager = Substitute.For<IFeatureManager>();
featureManager.IsEnabledAsync("Module1").Returns(true);

using var host = await Host.CreateDefaultBuilder()
.ConfigureServices(services =>
var host = await StartHostAsync(opts =>
{
// #4552: ApplyAllDatabaseChangesOnStartup takes the global advisory lock
// (default id 4004) to apply schema changes. That id is shared by every
// store in the suite, and advisory locks are connection-scoped, so a pooled
// connection from another test's startup-apply can still hold 4004 and make
// this acquisition time out under CI load ("Unable to attain a global lock in
// time"). Use a distinct lock id so this test can't contend with the default.
opts.ApplyChangesLockId = opts.ApplyChangesLockId + 4552;
},
configureServices: services =>
{
services.AddMarten(opts =>
{
opts.Connection(ConnectionSource.ConnectionString);
opts.DatabaseSchemaName = "async_config";

// #4552: ApplyAllDatabaseChangesOnStartup takes the global advisory lock
// (default id 4004) to apply schema changes. That id is shared by every
// store in the suite, and advisory locks are connection-scoped, so a pooled
// connection from another test's startup-apply can still hold 4004 and make
// this acquisition time out under CI load ("Unable to attain a global lock in
// time"). Use a distinct lock id so this test can't contend with the default.
opts.ApplyChangesLockId = opts.ApplyChangesLockId + 4552;
}).ApplyAllDatabaseChangesOnStartup();

#region sample_registering_async_config_marten

services.ConfigureMartenWithServices<FeatureManagementUsingExtension>();

#endregion
services.AddSingleton(featureManager);
}).StartAsync();
},
configureMarten: marten => marten.ApplyAllDatabaseChangesOnStartup());

var store = (DocumentStore)host.Services.GetRequiredService<IDocumentStore>();

Expand All @@ -61,18 +57,12 @@ public async Task feature_flag_negative()

featureManager.IsEnabledAsync("Module1").Returns(false);

using var host = await Host.CreateDefaultBuilder()
.ConfigureServices(services =>
var host = await StartHostAsync(_ => { },
configureServices: services =>
{
services.AddMarten(opts =>
{
opts.Connection(ConnectionSource.ConnectionString);
opts.DatabaseSchemaName = "async_config";
});

services.ConfigureMartenWithServices<FeatureManagementUsingExtension>();
services.AddSingleton(featureManager);
}).StartAsync();
});

var store = (DocumentStore)host.Services.GetRequiredService<IDocumentStore>();

Expand All @@ -88,17 +78,10 @@ public async Task bare_AddSingleton_IAsyncConfigureMarten_is_invoked()
// registered the impl but never wired AsyncConfigureMartenApplication, so
// Configure() silently never ran. AddMarten now registers the hosted service
// unconditionally, matching how bare AddSingleton<IConfigureMarten, T>() works.
using var host = await Host.CreateDefaultBuilder()
.ConfigureServices(services =>
{
services.AddMarten(opts =>
{
opts.Connection(ConnectionSource.ConnectionString);
opts.DatabaseSchemaName = "async_config_bare";
});

services.AddSingleton<IAsyncConfigureMarten, RecordingAsyncConfig>();
}).StartAsync();
var host = await StartHostAsync(
opts => opts.DatabaseSchemaName = $"{SchemaName}_bare",
configureServices: services =>
services.AddSingleton<IAsyncConfigureMarten, RecordingAsyncConfig>());

var recorded = host.Services.GetServices<IAsyncConfigureMarten>()
.OfType<RecordingAsyncConfig>()
Expand Down
Loading
Loading