Skip to content

Commit 3c286df

Browse files
authored
Merge pull request #12 from Shuttle/v20
V20
2 parents ac6f3af + 5e82c3d commit 3c286df

193 files changed

Lines changed: 12164 additions & 4250 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Shuttle.Recall.Tests/DefaultEventMethodInvokerFixture.cs

Lines changed: 0 additions & 42 deletions
This file was deleted.

Shuttle.Recall.Tests/EventHandler.cs

Lines changed: 0 additions & 61 deletions
This file was deleted.
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using Moq;
5+
using NUnit.Framework;
6+
using Shuttle.Core.Pipelines;
7+
8+
namespace Shuttle.Recall.Tests;
9+
10+
[TestFixture]
11+
public class EventHandlerInvokerFixture
12+
{
13+
public class EventA
14+
{
15+
}
16+
17+
public class EventHandlerA : IEventHandler<EventA>
18+
{
19+
public bool Invoked { get; private set; }
20+
21+
public async Task ProcessEventAsync(IEventHandlerContext<EventA> context)
22+
{
23+
Invoked = true;
24+
25+
await Task.CompletedTask;
26+
}
27+
}
28+
29+
[Test]
30+
public async Task Should_be_able_to_invoke_handler_instance_async()
31+
{
32+
var handler = new EventHandlerA();
33+
34+
var services = new ServiceCollection();
35+
36+
services.AddEventStore(builder =>
37+
{
38+
builder.AddProjection("projection-1")
39+
.AddEventHandler(handler);
40+
});
41+
42+
var serviceProvider = services.BuildServiceProvider();
43+
44+
var invoker = serviceProvider.GetRequiredService<IEventHandlerInvoker>();
45+
46+
var pipeline = new Pipeline(serviceProvider);
47+
48+
pipeline.State.SetProjectionEvent(new(new("projection-1", 0), new()
49+
{
50+
SequenceNumber = 1
51+
}));
52+
53+
pipeline.State.SetDomainEvent(new(new EventA(), 1));
54+
pipeline.State.SetEventEnvelope(new()
55+
{
56+
AssemblyQualifiedName = typeof(EventA).AssemblyQualifiedName!,
57+
EventType = typeof(EventA).FullName!
58+
});
59+
60+
61+
Assert.That(handler.Invoked, Is.False);
62+
63+
var result = await invoker.InvokeAsync(new PipelineContext<OnHandleEvent>(pipeline));
64+
65+
Assert.That(result, Is.True);
66+
Assert.That(handler.Invoked, Is.True);
67+
}
68+
69+
[Test]
70+
public async Task Should_be_able_to_invoke_delegate_handler_async()
71+
{
72+
var serviceProvider = new Mock<IServiceProvider>().Object;
73+
var configuration = new EventProcessorConfiguration();
74+
var invoker = new EventHandlerInvoker(serviceProvider, configuration);
75+
76+
var pipeline = new Pipeline(serviceProvider);
77+
78+
pipeline.State.SetProjectionEvent(new(new("projection-1", 0), new()
79+
{
80+
SequenceNumber = 1
81+
}));
82+
83+
pipeline.State.SetDomainEvent(new(new EventA(), 1));
84+
pipeline.State.SetEventEnvelope(new()
85+
{
86+
AssemblyQualifiedName = typeof(EventA).AssemblyQualifiedName!,
87+
EventType = typeof(EventA).FullName!
88+
});
89+
90+
var invoked = false;
91+
92+
configuration.GetProjection("projection-1").AddEventHandler(async (IEventHandlerContext<EventA> _) =>
93+
{
94+
invoked = true;
95+
96+
await Task.CompletedTask;
97+
});
98+
99+
var result = await invoker.InvokeAsync(new PipelineContext<OnHandleEvent>(pipeline));
100+
101+
Assert.That(result, Is.True);
102+
Assert.That(invoked, Is.True);
103+
}
104+
105+
[Test]
106+
public async Task Should_be_able_to_invoke_handler_async()
107+
{
108+
var handler = new EventHandlerA();
109+
var services = new ServiceCollection();
110+
111+
services.AddKeyedSingleton<IEventHandler<EventA>>($"[Shuttle.Recall.Projection/projection-1]:{typeof(EventA).FullName}", handler);
112+
113+
var serviceProvider = services.BuildServiceProvider();
114+
115+
var configuration = new EventProcessorConfiguration();
116+
var invoker = new EventHandlerInvoker(serviceProvider, configuration);
117+
118+
var pipeline = new Pipeline(serviceProvider);
119+
120+
pipeline.State.SetProjectionEvent(new(new("projection-1", 0), new()
121+
{
122+
SequenceNumber = 1
123+
}));
124+
125+
pipeline.State.SetDomainEvent(new(new EventA(), 1));
126+
pipeline.State.SetEventEnvelope(new()
127+
{
128+
AssemblyQualifiedName = typeof(EventA).AssemblyQualifiedName!,
129+
EventType = typeof(EventA).FullName!
130+
});
131+
132+
Assert.That(handler.Invoked, Is.False);
133+
134+
configuration.GetProjection("projection-1").AddHandlerEventType(typeof(EventA));
135+
136+
var result = await invoker.InvokeAsync(new PipelineContext<OnHandleEvent>(pipeline));
137+
138+
Assert.That(result, Is.True);
139+
Assert.That(handler.Invoked, Is.True);
140+
}
141+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System.Collections.Generic;
2+
using NUnit.Framework;
3+
using Shuttle.Recall.Tests.Implementation;
4+
5+
namespace Shuttle.Recall.Tests;
6+
7+
[TestFixture]
8+
public class EventMethodInvokerFixture
9+
{
10+
[Test]
11+
public void Should_be_able_to_invoke_public_methods()
12+
{
13+
var invoker = new EventMethodInvoker(new EventMethodInvokerConfiguration());
14+
15+
var events = new List<object>
16+
{
17+
new ThisHappened { ThisValue = "this" },
18+
new ThatHappened { ThatValue = "that" }
19+
};
20+
21+
var aggregateOne = new AggregateOne();
22+
23+
Assert.That(aggregateOne.ThisValue, Is.EqualTo(string.Empty));
24+
Assert.That(aggregateOne.ThatValue, Is.EqualTo(string.Empty));
25+
26+
invoker.Apply(aggregateOne, events);
27+
28+
Assert.That(aggregateOne.ThisValue, Is.EqualTo("this"));
29+
Assert.That(aggregateOne.ThatValue, Is.EqualTo("that"));
30+
31+
var aggregateTwo = new AggregateTwo();
32+
33+
Assert.That(aggregateTwo.ThisValue, Is.EqualTo(string.Empty));
34+
Assert.That(aggregateTwo.ThatValue, Is.EqualTo(string.Empty));
35+
36+
invoker.Apply(aggregateTwo, events);
37+
38+
Assert.That(aggregateTwo.ThisValue, Is.EqualTo("this"));
39+
Assert.That(aggregateTwo.ThatValue, Is.EqualTo("that"));
40+
}
41+
}

0 commit comments

Comments
 (0)