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+ }
0 commit comments