14
14
[ TestFixture ]
15
15
public class RoutingToDispatchConnectorTests
16
16
{
17
+ [ Test ]
18
+ public async Task Should_preserve_message_state_for_one_routing_strategy_for_allocation_reasons ( )
19
+ {
20
+ var behavior = new RoutingToDispatchConnector ( ) ;
21
+ IEnumerable < TransportOperation > operations = null ;
22
+ var testableRoutingContext = new TestableRoutingContext
23
+ {
24
+ RoutingStrategies = new List < RoutingStrategy >
25
+ {
26
+ new DestinationRoutingStrategy ( "destination1" , "HeaderKeyAddedByTheRoutingStrategy1" , "HeaderValueAddedByTheRoutingStrategy1" )
27
+ }
28
+ } ;
29
+ var originalDispatchProperties = new DispatchProperties
30
+ {
31
+ { "SomeKey" , "SomeValue" }
32
+ } ;
33
+ testableRoutingContext . Extensions . Set ( originalDispatchProperties ) ;
34
+ var originalHeaders = new Dictionary < string , string > { { "SomeHeaderKey" , "SomeHeaderValue" } } ;
35
+ testableRoutingContext . Message = new OutgoingMessage ( "ID" , originalHeaders , Array . Empty < byte > ( ) ) ;
36
+ await behavior . Invoke ( testableRoutingContext , context =>
37
+ {
38
+ operations = context . Operations ;
39
+ return Task . CompletedTask ;
40
+ } ) ;
41
+
42
+ Assert . That ( operations , Has . Length . EqualTo ( 1 ) ) ;
43
+
44
+ TransportOperation destination1Operation = operations . ElementAt ( 0 ) ;
45
+ Assert . That ( destination1Operation . Message . MessageId , Is . EqualTo ( "ID" ) ) ;
46
+ Assert . That ( ( destination1Operation . AddressTag as UnicastAddressTag ) ? . Destination , Is . EqualTo ( "destination1" ) ) ;
47
+ Dictionary < string , string > destination1Headers = destination1Operation . Message . Headers ;
48
+ Assert . That ( destination1Headers , Contains . Item ( new KeyValuePair < string , string > ( "SomeHeaderKey" , "SomeHeaderValue" ) ) ) ;
49
+ Assert . That ( destination1Headers , Contains . Item ( new KeyValuePair < string , string > ( "HeaderKeyAddedByTheRoutingStrategy1" , "HeaderValueAddedByTheRoutingStrategy1" ) ) ) ;
50
+ Assert . That ( destination1Headers , Is . SameAs ( originalHeaders ) ) ;
51
+ DispatchProperties destination1DispatchProperties = destination1Operation . Properties ;
52
+ Assert . That ( destination1DispatchProperties , Contains . Item ( new KeyValuePair < string , string > ( "SomeKey" , "SomeValue" ) ) ) ;
53
+ Assert . That ( destination1DispatchProperties , Is . SameAs ( originalDispatchProperties ) ) ;
54
+ }
55
+
56
+ [ Test ]
57
+ public async Task Should_copy_message_state_for_multiple_routing_strategies ( )
58
+ {
59
+ var behavior = new RoutingToDispatchConnector ( ) ;
60
+ IEnumerable < TransportOperation > operations = null ;
61
+ var testableRoutingContext = new TestableRoutingContext
62
+ {
63
+ RoutingStrategies = new List < RoutingStrategy >
64
+ {
65
+ new DestinationRoutingStrategy ( "destination1" , "HeaderKeyAddedByTheRoutingStrategy1" , "HeaderValueAddedByTheRoutingStrategy1" ) ,
66
+ new DestinationRoutingStrategy ( "destination2" , "HeaderKeyAddedByTheRoutingStrategy2" , "HeaderValueAddedByTheRoutingStrategy2" )
67
+ }
68
+ } ;
69
+ var originalDispatchProperties = new DispatchProperties
70
+ {
71
+ { "SomeKey" , "SomeValue" }
72
+ } ;
73
+ testableRoutingContext . Extensions . Set ( originalDispatchProperties ) ;
74
+ var originalHeaders = new Dictionary < string , string > { { "SomeHeaderKey" , "SomeHeaderValue" } } ;
75
+ testableRoutingContext . Message = new OutgoingMessage ( "ID" , originalHeaders , Array . Empty < byte > ( ) ) ;
76
+ await behavior . Invoke ( testableRoutingContext , context =>
77
+ {
78
+ operations = context . Operations ;
79
+ return Task . CompletedTask ;
80
+ } ) ;
81
+
82
+ Assert . That ( operations , Has . Length . EqualTo ( 2 ) ) ;
83
+
84
+ TransportOperation destination1Operation = operations . ElementAt ( 0 ) ;
85
+ Assert . That ( destination1Operation . Message . MessageId , Is . EqualTo ( "ID" ) ) ;
86
+ Assert . That ( ( destination1Operation . AddressTag as UnicastAddressTag ) ? . Destination , Is . EqualTo ( "destination1" ) ) ;
87
+ Dictionary < string , string > destination1Headers = destination1Operation . Message . Headers ;
88
+ Assert . That ( destination1Headers , Contains . Item ( new KeyValuePair < string , string > ( "SomeHeaderKey" , "SomeHeaderValue" ) ) ) ;
89
+ Assert . That ( destination1Headers , Contains . Item ( new KeyValuePair < string , string > ( "HeaderKeyAddedByTheRoutingStrategy1" , "HeaderValueAddedByTheRoutingStrategy1" ) ) ) ;
90
+ Assert . That ( destination1Headers , Does . Not . Contain ( new KeyValuePair < string , string > ( "HeaderKeyAddedByTheRoutingStrategy2" , "HeaderValueAddedByTheRoutingStrategy2" ) ) ) ;
91
+ Assert . That ( destination1Headers , Is . Not . SameAs ( originalHeaders ) ) ;
92
+ DispatchProperties destination1DispatchProperties = destination1Operation . Properties ;
93
+ Assert . That ( destination1DispatchProperties , Contains . Item ( new KeyValuePair < string , string > ( "SomeKey" , "SomeValue" ) ) ) ;
94
+ Assert . That ( destination1DispatchProperties , Is . Not . SameAs ( originalDispatchProperties ) ) ;
95
+
96
+ TransportOperation destination2Operation = operations . ElementAt ( 1 ) ;
97
+ Assert . That ( destination2Operation . Message . MessageId , Is . EqualTo ( "ID" ) ) ;
98
+ Assert . That ( ( destination2Operation . AddressTag as UnicastAddressTag ) ? . Destination , Is . EqualTo ( "destination2" ) ) ;
99
+ Dictionary < string , string > destination2Headers = destination2Operation . Message . Headers ;
100
+ Assert . That ( destination2Headers , Contains . Item ( new KeyValuePair < string , string > ( "SomeHeaderKey" , "SomeHeaderValue" ) ) ) ;
101
+ Assert . That ( destination2Headers , Contains . Item ( new KeyValuePair < string , string > ( "HeaderKeyAddedByTheRoutingStrategy2" , "HeaderValueAddedByTheRoutingStrategy2" ) ) ) ;
102
+ Assert . That ( destination2Headers , Does . Not . Contain ( new KeyValuePair < string , string > ( "HeaderKeyAddedByTheRoutingStrategy1" , "HeaderValueAddedByTheRoutingStrategy1" ) ) ) ;
103
+ Assert . That ( destination2Headers , Is . Not . SameAs ( originalHeaders ) ) ;
104
+ DispatchProperties destination2DispatchProperties = destination2Operation . Properties ;
105
+ Assert . That ( destination2DispatchProperties , Is . Not . SameAs ( originalDispatchProperties ) ) ;
106
+ Assert . That ( destination2DispatchProperties , Contains . Item ( new KeyValuePair < string , string > ( "SomeKey" , "SomeValue" ) ) ) ;
107
+
108
+ Assert . That ( destination1Headers , Is . Not . SameAs ( destination2Headers ) ) ;
109
+ Assert . That ( destination1DispatchProperties , Is . Not . SameAs ( destination2DispatchProperties ) ) ;
110
+ }
111
+
17
112
[ Test ]
18
113
public async Task Should_preserve_headers_generated_by_custom_routing_strategy ( )
19
114
{
20
115
var behavior = new RoutingToDispatchConnector ( ) ;
21
116
Dictionary < string , string > headers = null ;
22
- await behavior . Invoke ( new TestableRoutingContext { RoutingStrategies = new List < RoutingStrategy > { new CustomRoutingStrategy ( ) } } , context =>
117
+ await behavior . Invoke ( new TestableRoutingContext { RoutingStrategies = new List < RoutingStrategy > { new HeaderModifyingRoutingStrategy ( ) } } , context =>
23
118
{
24
119
headers = context . Operations . First ( ) . Message . Headers ;
25
120
return Task . CompletedTask ;
@@ -113,7 +208,7 @@ static IOutgoingSendContext CreateContext(SendOptions options, bool fromHandler)
113
208
return context ;
114
209
}
115
210
116
- class CustomRoutingStrategy : RoutingStrategy
211
+ class HeaderModifyingRoutingStrategy : RoutingStrategy
117
212
{
118
213
public override AddressTag Apply ( Dictionary < string , string > headers )
119
214
{
@@ -122,6 +217,26 @@ public override AddressTag Apply(Dictionary<string, string> headers)
122
217
}
123
218
}
124
219
220
+ class DestinationRoutingStrategy : RoutingStrategy
221
+ {
222
+ public DestinationRoutingStrategy ( string destination , string headerKey , string headerValue )
223
+ {
224
+ this . destination = destination ;
225
+ this . headerKey = headerKey ;
226
+ this . headerValue = headerValue ;
227
+ }
228
+
229
+ public override AddressTag Apply ( Dictionary < string , string > headers )
230
+ {
231
+ headers [ headerKey ] = headerValue ;
232
+ return new UnicastAddressTag ( destination ) ;
233
+ }
234
+
235
+ readonly string destination ;
236
+ readonly string headerKey ;
237
+ readonly string headerValue ;
238
+ }
239
+
125
240
class MyMessage : IMessage
126
241
{
127
242
}
0 commit comments