-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a sample program to test concurrent subscribers (#13)
- Loading branch information
1 parent
84bf4e2
commit d0aaae9
Showing
3 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using LiquidProjections; | ||
using LiquidProjections.Abstractions; | ||
using LiquidProjections.PollingEventStore; | ||
|
||
namespace SampleSubscriber | ||
{ | ||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
// var adapter = new PollingEventStoreAdapter(new PassiveEventStore(), 0, 5.Seconds(), 1000, () => DateTime.UtcNow, messageFunc => Console.WriteLine(messageFunc())); | ||
var adapter = new PollingEventStoreAdapter(new PassiveEventStore(), 0, 5.Seconds(), 1000, () => DateTime.UtcNow); | ||
|
||
int maxSubscrbiers = 5; | ||
|
||
for (int id = 0; id < maxSubscrbiers; id++) | ||
{ | ||
int localId = id; | ||
adapter.Subscribe(0, new Subscriber | ||
{ | ||
HandleTransactions = (transactions, info) => | ||
{ | ||
Console.WriteLine( | ||
$"Subscriber {info.Id} received transactions {transactions.First().Checkpoint} to {transactions.Last().Checkpoint} on thead {Thread.CurrentThread.ManagedThreadId}"); | ||
Thread.Sleep(500); | ||
return Task.FromResult(0); | ||
}, | ||
NoSuchCheckpoint = info => Task.FromResult(0) | ||
}, id.ToString()); | ||
|
||
Console.WriteLine($"Started subscriber {localId}"); | ||
} | ||
|
||
Console.WriteLine("Press a key to shutdown"); | ||
Console.ReadLine(); | ||
|
||
adapter.Dispose(); | ||
} | ||
} | ||
|
||
internal class PassiveEventStore : IPassiveEventStore | ||
{ | ||
private const long MaxCheckpoint = 100000; | ||
|
||
public IEnumerable<Transaction> GetFrom(long? previousCheckpoint) | ||
{ | ||
previousCheckpoint = previousCheckpoint ?? 0; | ||
if (previousCheckpoint > MaxCheckpoint) | ||
{ | ||
return new List<Transaction>(); | ||
} | ||
|
||
var transactions = new List<Transaction>(); | ||
|
||
long firstCheckpoint = previousCheckpoint.Value + 1; | ||
|
||
for (long checkpoint = firstCheckpoint; checkpoint < firstCheckpoint + 1000 && checkpoint < MaxCheckpoint; checkpoint++) | ||
{ | ||
transactions.Add(new Transaction | ||
{ | ||
Checkpoint = checkpoint, | ||
Events = new List<EventEnvelope>(), | ||
Headers = new Dictionary<string, object>(), | ||
Id = Guid.NewGuid().ToString(), | ||
StreamId = Guid.NewGuid().ToString(), | ||
TimeStampUtc = DateTime.UtcNow | ||
}); | ||
} | ||
|
||
Thread.Sleep(1000); | ||
|
||
return transactions; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp2.0</TargetFramework> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\..\Src\LiquidProjections.PollingEventStore\LiquidProjections.PollingEventStore.csproj" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Reference Include="FluentAssertions.Core, Version=4.19.3.0, Culture=neutral, PublicKeyToken=33f2691a05b67b6a"> | ||
<HintPath>..\..\packages\FluentAssertions.4.19.3\lib\net45\FluentAssertions.Core.dll</HintPath> | ||
</Reference> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageReference Include="FluentAssertions" Version="5.3.2" /> | ||
</ItemGroup> | ||
</Project> |