Skip to content

Commit

Permalink
Merge pull request #624 from eventflow/move-tests-to-netcore
Browse files Browse the repository at this point in the history
WIP: EventFlow.TestHelper to NET Standard
  • Loading branch information
rasmus authored May 28, 2019
2 parents 35a7903 + 1cc3982 commit 6d033c3
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 37 deletions.
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### New in 0.72 (not released yet)

* New: `EventFlow.TestHelpers` are now released as .NET Standard as well
* Fix: Upgrade `EventStore.Client` to v5.0.1 and use it for both .NET Framework and .NET Core
* Fix: Storing events in MS SQL Server using `MsSqlEventPersistence` now correctly
handles non-ANSI unicode characters in strings.
Expand Down
17 changes: 11 additions & 6 deletions Source/EventFlow.TestHelpers/EventFlow.TestHelpers.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="../Common.props" />
<PropertyGroup>
<TargetFramework>net452</TargetFramework>
<TargetFrameworks>net452;netstandard2.0</TargetFrameworks>
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
<GenerateAssemblyInfo>False</GenerateAssemblyInfo>
<GeneratePackageOnBuild>False</GeneratePackageOnBuild>
Expand Down Expand Up @@ -32,13 +32,18 @@
<PackageReference Include="Moq" Version="4.7.99" />
<PackageReference Include="NUnit" Version="3.11.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\EventFlow\EventFlow.csproj" />
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="System.Data.SqlClient" Version="4.3.0" />
</ItemGroup>

<ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net452'">
<Reference Include="System.Management" />
<Reference Include="System.Net.Http" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\EventFlow\EventFlow.csproj" />
</ItemGroup>

</Project>
13 changes: 9 additions & 4 deletions Source/EventFlow.TestHelpers/IntegrationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,11 @@ protected Task<ThingyAggregate> LoadAggregateAsync(ThingyId thingyId)
return AggregateStore.LoadAsync<ThingyAggregate, ThingyId>(thingyId);
}

protected async Task<PingId> PublishPingCommandAsync(ThingyId thingyId)
protected async Task<PingId> PublishPingCommandAsync(
ThingyId thingyId,
CancellationToken cancellationToken = default)
{
var pingIds = await PublishPingCommandsAsync(thingyId, 1).ConfigureAwait(false);
var pingIds = await PublishPingCommandsAsync(thingyId, 1, cancellationToken).ConfigureAwait(false);
return pingIds.Single();
}

Expand All @@ -116,7 +118,10 @@ protected Task<ThingySaga> LoadSagaAsync(ThingyId thingyId)
CancellationToken.None);
}

protected async Task<IReadOnlyCollection<PingId>> PublishPingCommandsAsync(ThingyId thingyId, int count)
protected async Task<IReadOnlyCollection<PingId>> PublishPingCommandsAsync(
ThingyId thingyId,
int count,
CancellationToken cancellationToken = default)
{
if (count <= 0) throw new ArgumentOutOfRangeException(nameof(count));

Expand All @@ -125,7 +130,7 @@ protected async Task<IReadOnlyCollection<PingId>> PublishPingCommandsAsync(Thing
for (var i = 0; i < count; i++)
{
var pingId = PingId.New;
await CommandBus.PublishAsync(new ThingyPingCommand(thingyId, pingId), CancellationToken.None).ConfigureAwait(false);
await CommandBus.PublishAsync(new ThingyPingCommand(thingyId, pingId), cancellationToken).ConfigureAwait(false);
pingIds.Add(pingId);
}

Expand Down
10 changes: 6 additions & 4 deletions Source/EventFlow.TestHelpers/ProcessHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Management;
using EventFlow.Core;
using EventFlow.Extensions;
using EventFlow.TestHelpers.Extensions;
Expand Down Expand Up @@ -91,8 +90,9 @@ void InitializeProcess(Process p)
{
process.OutputDataReceived -= OutHandler;
process.ErrorDataReceived -= ErrHandler;
#if NET452
KillProcessAndChildren(process.Id);
#endif
}
catch (Exception e)
{
Expand All @@ -105,14 +105,15 @@ void InitializeProcess(Process p)
});
}

#if NET452
private static void KillProcessAndChildren(int pid)
{
var searcher = new ManagementObjectSearcher("Select * From Win32_Process Where ParentProcessID=" + pid);
var searcher = new System.Management.ManagementObjectSearcher("Select * From Win32_Process Where ParentProcessID=" + pid);
var moc = searcher.Get();

foreach (var o in moc)
{
var mo = (ManagementObject)o;
var mo = (System.Management.ManagementObject)o;
KillProcessAndChildren(Convert.ToInt32(mo["ProcessID"]));
}

Expand All @@ -128,5 +129,6 @@ private static void KillProcessAndChildren(int pid)
// Process already exited.
}
}
#endif
}
}
37 changes: 20 additions & 17 deletions Source/EventFlow.TestHelpers/Suites/TestSuiteForReadModelStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ await PublishPingCommandAsync(id).ConfigureAwait(false)
}
}

[Test, Timeout(10000)]
[Test]
public virtual async Task OptimisticConcurrencyCheck()
{
// Simulates a state in which two read models have been loaded to memory
Expand All @@ -250,23 +250,26 @@ public virtual async Task OptimisticConcurrencyCheck()
// a controlled delay and a set of AutoResetEvent is used to ensure
// that the read store is in the desired state before continuing

// Arrange
var id = ThingyId.New;
var waitState = new WaitState();
await PublishPingCommandsAsync(id, 1).ConfigureAwait(false);

// Arrange
_waitStates[id.Value] = waitState;
var delayedPublishTask = Task.Run(() => PublishPingCommandsAsync(id, 1));
waitState.ReadStoreReady.WaitOne();
_waitStates.Remove(id.Value);
await PublishPingCommandsAsync(id, 1).ConfigureAwait(false);
waitState.ReadStoreContinue.Set();
await delayedPublishTask.ConfigureAwait(false);
using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10)))
{
// Arrange
var id = ThingyId.New;
var waitState = new WaitState();
await PublishPingCommandsAsync(id, 1, cts.Token).ConfigureAwait(false);

// Arrange
_waitStates[id.Value] = waitState;
var delayedPublishTask = Task.Run(() => PublishPingCommandsAsync(id, 1, cts.Token), cts.Token);
waitState.ReadStoreReady.WaitOne(TimeSpan.FromSeconds(10));
_waitStates.Remove(id.Value);
await PublishPingCommandsAsync(id, 1, cts.Token).ConfigureAwait(false);
waitState.ReadStoreContinue.Set();
await delayedPublishTask.ConfigureAwait(false);

// Assert
var readModel = await QueryProcessor.ProcessAsync(new ThingyGetQuery(id)).ConfigureAwait(false);
readModel.PingsReceived.Should().Be(3);
// Assert
var readModel = await QueryProcessor.ProcessAsync(new ThingyGetQuery(id), cts.Token).ConfigureAwait(false);
readModel.PingsReceived.Should().Be(3);
}
}

[Test]
Expand Down
14 changes: 8 additions & 6 deletions Source/EventFlow.TestHelpers/Suites/TestSuiteForScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,17 @@ protected override IEventFlowOptions Options(IEventFlowOptions eventFlowOptions)
}

[Test]
[Timeout(10000)]
public async Task AsynchronousSubscribesGetInvoked()
{
// Act
var pingId = await PublishPingCommandAsync(A<ThingyId>()).ConfigureAwait(false);
using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10)))
{
// Act
var pingId = await PublishPingCommandAsync(A<ThingyId>(), cts.Token).ConfigureAwait(false);

// Assert
var receivedPingId = await Task.Run(() => _testAsynchronousSubscriber.PingIds.Take()).ConfigureAwait(false);
receivedPingId.Should().IsSameOrEqualTo(pingId);
// Assert
var receivedPingId = await Task.Run(() => _testAsynchronousSubscriber.PingIds.Take(), cts.Token).ConfigureAwait(false);
receivedPingId.Should().IsSameOrEqualTo(pingId);
}
}

[Test]
Expand Down

0 comments on commit 6d033c3

Please sign in to comment.