Skip to content

Commit 9545d6b

Browse files
sandst1yupliner
authored andcommitted
Fix MessageVersionStack to work with more than 2 versions (#899)
MessageVersionStack was working strangely when there were more than two versions of a message. If you have three versions of a message, i.e. MyMessage, MyMessageV2 and MyMessageV3, you've got a subscriber for each of them, and publish MyMessageV3, only MyMessageV3 and MyMessage are received, MyMessageV2 never arrives. The root of the problem was traced into how versioned exchanges are declared and bound to each other. The exchange for MyMessageV3 is bound to the relevant queues and into the exchange of MyMessage, but not into the exchange of MyMessageV2, which it should do as well. The reason for this was in MessageVersionStack.GetSupersededType, which takes a message type and all the ISupersede interfaces that the message implements, and returns the first in the list (if there are multiple levels of versions it should return the last). The fix was to change GetSupersededType to always return the last element in the list, after which it works with an arbitrary number of message versions.
1 parent 65cb9a4 commit 9545d6b

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

Source/EasyNetQ.Tests/MessageVersioningTests/MessageVersionStackTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ public void Versioned_messages_create_a_stack_containing_the_message_type_and_al
2525
Assert.Equal(typeof(MyMessageV2), stack.ElementAt( 1 ));
2626
}
2727

28+
[Fact]
29+
public void Versioned_message_stack_works_for_more_than_two_versions_and_types_are_ordered_oldest_first()
30+
{
31+
var stack = new MessageVersionStack( typeof( MyMessageV3 ));
32+
33+
Assert.Equal(typeof(MyMessage), stack.ElementAt( 0 ));
34+
Assert.Equal(typeof(MyMessageV2), stack.ElementAt( 1 ));
35+
Assert.Equal(typeof(MyMessageV3), stack.ElementAt( 2 ));
36+
}
37+
2838
[Fact]
2939
public void Pop_returns_the_top_of_the_stack()
3040
{
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using EasyNetQ.MessageVersioning;
2+
3+
namespace EasyNetQ.Tests.MessageVersioningTests
4+
{
5+
public class MyMessageV3 : MyMessageV2, ISupersede<MyMessageV2>
6+
{
7+
public int NumberInV3 { get; set; }
8+
}
9+
}

Source/EasyNetQ/MessageVersioning/MessageVersionStack.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ private static Type GetSupersededType( Type type )
6060
.GetInterfaces()
6161
.Where( t => t.GetTypeInfo().IsGenericType && t.GetGenericTypeDefinition() == typeof( ISupersede<> ) )
6262
.SelectMany( t => t.GetGenericArguments() )
63-
.FirstOrDefault();
63+
.LastOrDefault();
6464
}
6565

6666
private static void EnsureVersioningValid( Type messageType, Type supersededType )

0 commit comments

Comments
 (0)