122 lines
4.2 KiB
C#
122 lines
4.2 KiB
C#
using Microsoft.Extensions.Logging.Abstractions;
|
|
using ZB.MOM.WW.CBDDC.Core;
|
|
using ZB.MOM.WW.CBDDC.Core.Network;
|
|
using ZB.MOM.WW.CBDDC.Core.Storage;
|
|
|
|
namespace ZB.MOM.WW.CBDDC.Network.Tests;
|
|
|
|
public class MultiDatasetSyncOrchestratorTests
|
|
{
|
|
/// <summary>
|
|
/// Verifies multi-dataset sync is disabled, only the primary context is created.
|
|
/// </summary>
|
|
[Fact]
|
|
public void Constructor_WhenMultiDatasetDisabled_ShouldOnlyCreatePrimaryContext()
|
|
{
|
|
var sut = CreateSut(
|
|
[
|
|
new DatasetSyncOptions { DatasetId = DatasetId.Primary, Enabled = true },
|
|
new DatasetSyncOptions { DatasetId = DatasetId.Logs, Enabled = true }
|
|
],
|
|
new MultiDatasetRuntimeOptions
|
|
{
|
|
EnableMultiDatasetSync = false,
|
|
EnableDatasetPrimary = true,
|
|
EnableDatasetLogs = true
|
|
});
|
|
|
|
var datasetIds = sut.Contexts.Select(c => c.DatasetId).ToList();
|
|
datasetIds.Count.ShouldBe(1);
|
|
datasetIds[0].ShouldBe(DatasetId.Primary);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Verifies that failures in one orchestrator do not prevent remaining contexts from starting and stopping.
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task StartStop_WhenOneDatasetThrows_ShouldContinueOtherDatasets()
|
|
{
|
|
var orchestrators = new Dictionary<string, TrackingSyncOrchestrator>(StringComparer.Ordinal)
|
|
{
|
|
[DatasetId.Primary] = new TrackingSyncOrchestrator(),
|
|
[DatasetId.Logs] = new TrackingSyncOrchestrator(startException: new InvalidOperationException("boom")),
|
|
[DatasetId.Timeseries] = new TrackingSyncOrchestrator()
|
|
};
|
|
|
|
var sut = CreateSut(
|
|
[],
|
|
new MultiDatasetRuntimeOptions
|
|
{
|
|
EnableMultiDatasetSync = true,
|
|
EnableDatasetPrimary = true,
|
|
EnableDatasetLogs = true,
|
|
EnableDatasetTimeseries = true
|
|
},
|
|
options => orchestrators[DatasetId.Normalize(options.DatasetId)]);
|
|
|
|
await sut.Start();
|
|
await sut.Stop();
|
|
|
|
orchestrators[DatasetId.Primary].StartCalls.ShouldBe(1);
|
|
orchestrators[DatasetId.Primary].StopCalls.ShouldBe(1);
|
|
|
|
orchestrators[DatasetId.Logs].StartCalls.ShouldBe(1);
|
|
orchestrators[DatasetId.Logs].StopCalls.ShouldBe(1);
|
|
|
|
orchestrators[DatasetId.Timeseries].StartCalls.ShouldBe(1);
|
|
orchestrators[DatasetId.Timeseries].StopCalls.ShouldBe(1);
|
|
}
|
|
|
|
private static MultiDatasetSyncOrchestrator CreateSut(
|
|
IEnumerable<DatasetSyncOptions> datasetOptions,
|
|
MultiDatasetRuntimeOptions runtimeOptions,
|
|
Func<DatasetSyncOptions, ISyncOrchestrator>? orchestratorFactory = null)
|
|
{
|
|
return new MultiDatasetSyncOrchestrator(
|
|
Substitute.For<IDiscoveryService>(),
|
|
Substitute.For<IOplogStore>(),
|
|
Substitute.For<IDocumentStore>(),
|
|
Substitute.For<ISnapshotMetadataStore>(),
|
|
Substitute.For<ISnapshotService>(),
|
|
Substitute.For<IPeerNodeConfigurationProvider>(),
|
|
NullLoggerFactory.Instance,
|
|
datasetOptions,
|
|
runtimeOptions,
|
|
orchestratorFactory: orchestratorFactory);
|
|
}
|
|
|
|
private sealed class TrackingSyncOrchestrator(Exception? startException = null, Exception? stopException = null)
|
|
: ISyncOrchestrator
|
|
{
|
|
/// <summary>
|
|
/// Number of times <see cref="Start" /> has been called.
|
|
/// </summary>
|
|
public int StartCalls { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Number of times <see cref="Stop" /> has been called.
|
|
/// </summary>
|
|
public int StopCalls { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Starts the orchestrator.
|
|
/// </summary>
|
|
public Task Start()
|
|
{
|
|
StartCalls++;
|
|
if (startException != null) throw startException;
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Stops the orchestrator.
|
|
/// </summary>
|
|
public Task Stop()
|
|
{
|
|
StopCalls++;
|
|
if (stopException != null) throw stopException;
|
|
return Task.CompletedTask;
|
|
}
|
|
}
|
|
}
|