@@ -13,16 +13,28 @@ namespace Akka.Cluster.Sharding.Tests;
13
13
14
14
public class AutomaticallyHandledExtractorMessagesSpec
15
15
{
16
+ public sealed record MyWrappedMessage ( string EntityId , string Message ) ;
17
+
16
18
// custom IMessageExtractor
17
19
public class MyMessageExtractor : IMessageExtractor
18
20
{
19
21
public string ? EntityId ( object message ) => message switch
20
22
{
21
23
string s => s ,
24
+ MyWrappedMessage wrapped => wrapped . EntityId ,
22
25
_ => null
23
26
} ;
24
27
25
- public object ? EntityMessage ( object message ) => message ;
28
+ public object ? EntityMessage ( object message )
29
+ {
30
+ switch ( message )
31
+ {
32
+ case MyWrappedMessage wrapped :
33
+ return wrapped . Message ;
34
+ default :
35
+ return message ;
36
+ }
37
+ }
26
38
27
39
public string ? ShardId ( object message ) => message switch
28
40
{
@@ -35,65 +47,47 @@ public string ShardId(string entityId, object? messageHint = null)
35
47
return entityId ;
36
48
}
37
49
}
38
-
39
- #pragma warning disable CS0618 // Type or member is obsolete
40
- private ExtractEntityId ExtractEntityId = message =>
41
- {
42
- if ( message is string s )
43
- return ( s , s ) ;
44
- return Option < ( string , object ) > . None ;
45
- } ;
46
50
47
- private ExtractShardId ExtractShardId = message =>
48
- {
49
- if ( message is string s )
50
- return s ;
51
- return null ! ;
52
- } ;
53
- #pragma warning restore CS0618 // Type or member is obsolete
51
+ public static readonly TheoryData < ( object shardingInput , object realMsg , string entityId , string shardId ) >
52
+ Messages = new ( )
53
+ {
54
+ // (new ShardRegion.StartEntity("foo"), new ShardRegion.StartEntity("foo"), "foo", "foo"),
55
+ ( new ShardingEnvelope ( "bar" , "baz" ) , "baz" , "bar" , "bar" ) , ( "bar" , "bar" , "bar" , "bar" ) ,
56
+ } ;
54
57
55
- public static readonly TheoryData < ( object shardingInput , object realMsg , string entityId , string shardId ) > Messages = new ( )
56
- {
57
- // (new ShardRegion.StartEntity("foo"), new ShardRegion.StartEntity("foo"), "foo", "foo"),
58
- ( new ShardingEnvelope ( "bar" , "baz" ) , "baz" , "bar" , "bar" ) ,
59
- ( "bar" , "bar" , "bar" , "bar" ) ,
60
- } ;
61
-
62
58
[ Theory ]
63
59
[ MemberData ( nameof ( Messages ) ) ]
64
- public void ShouldAutomaticallyHandleMessagesInCustomIMessageExtractor ( ( object shardingInput , object realMsg , string entityId , string shardId ) data )
60
+ public void ShouldAutomaticallyHandleMessagesInCustomIMessageExtractor (
61
+ ( object shardingInput , object realMsg , string entityId , string shardId ) data )
65
62
{
66
63
// arrange
67
64
var extractor = new ExtractorAdapter ( new MyMessageExtractor ( ) ) ;
68
-
65
+
69
66
// act
70
67
var entityId = extractor . EntityId ( data . shardingInput ) ;
71
68
var entityMessage = extractor . EntityMessage ( data . shardingInput ) ;
72
69
var shardId = extractor . ShardId ( entityId ! , data . shardingInput ) ;
73
-
70
+
74
71
// assert
75
72
entityId . Should ( ) . Be ( data . entityId ) ;
76
73
entityMessage . Should ( ) . Be ( data . realMsg ) ;
77
74
shardId . Should ( ) . Be ( data . shardId ) ;
78
75
}
79
-
80
- // NOTE: so the old delegates are hopeless and will simply not work - you HAVE to handle the messages yourself there
81
- // need to repeat of the previous test but using the deprecated delegate methods and the adapter
82
- // [Theory]
83
- // [MemberData(nameof(Messages))]
84
- // public void ShouldAutomaticallyHandleMessagesInCustomIMessageExtractorUsingDelegates((object shardingInput, object realMsg, string entityId, string shardId) data)
85
- // {
86
- // // arrange
87
- // var extractor = new ExtractorAdapter(new DeprecatedHandlerExtractorAdapter(ExtractEntityId, ExtractShardId));
88
- //
89
- // // act
90
- // var entityId = extractor.EntityId(data.shardingInput);
91
- // var entityMessage = extractor.EntityMessage(data.shardingInput);
92
- // var shardId = extractor.ShardId(entityId!, data.shardingInput);
93
- //
94
- // // assert
95
- // entityId.Should().Be(data.entityId);
96
- // entityMessage.Should().Be(data.realMsg);
97
- // shardId.Should().Be(data.shardId);
98
- // }
99
- }
76
+
77
+ [ Fact ]
78
+ public void ShouldUnwrapMessageInsideShardingEnvelope ( )
79
+ {
80
+ // arrange
81
+ var extractor = new ExtractorAdapter ( new MyMessageExtractor ( ) ) ;
82
+ var myMessage = new MyWrappedMessage ( "entity1" , "hello" ) ;
83
+ var envelope = new ShardingEnvelope ( "entity1" , myMessage ) ;
84
+
85
+ // act
86
+ var entityId = extractor . EntityId ( envelope ) ;
87
+ var entityMessage = extractor . EntityMessage ( envelope ) ;
88
+
89
+ // assert
90
+ entityId . Should ( ) . Be ( "entity1" ) ;
91
+ entityMessage . Should ( ) . Be ( "hello" ) ;
92
+ }
93
+ }
0 commit comments