Browse Source

Merge pull request #98 from lmanners/snapshot_optional

Allow SubscriptionSnapshotGenerator to skip sending a snapshot.
Mendel Monteiro-Beckerman 4 years ago
parent
commit
bdf15d40b4

+ 28 - 10
src/Abc.Zebus.Tests/Subscriptions/SnapshotGeneratorTests.cs

@@ -8,40 +8,58 @@ namespace Abc.Zebus.Tests.Subscriptions
     [TestFixture]
     public class SnapshotGeneratorTests
     {
-        private class EventTest : IEvent
+        private class TestEvent : IEvent
         {
         }
 
-        private class SnapshotTest : IEvent
+        private class TestSnapshot : IEvent
         {
         }
 
-        private class SnapshotGeneratorTest : SubscriptionSnapshotGenerator<SnapshotTest, EventTest>
+        private class TestSnapshotGenerator : SubscriptionSnapshotGenerator<TestSnapshot, TestEvent>
         {
-            public SnapshotGeneratorTest(IBus bus)
+            private readonly TestSnapshot _testSnapshot;
+
+            public TestSnapshotGenerator(IBus bus, TestSnapshot testSnapshot)
                 : base(bus)
             {
+                _testSnapshot = testSnapshot;
             }
 
-            protected override SnapshotTest GenerateSnapshot(SubscriptionsForType messageSubscription)
+            protected override TestSnapshot GenerateSnapshot(SubscriptionsForType messageSubscription)
             {
-                return new SnapshotTest();
+                return _testSnapshot;
             }
         }
 
         [Test]
-        public void should_Generate_snapshot_and_publish_it_to_the_specified_peer()
+        public void should_generate_snapshot_and_publish_it_to_the_specified_peer()
+        {
+            // Arrange
+            var testBus = new TestBus();
+            var snapshotGenerator = new TestSnapshotGenerator(testBus, new TestSnapshot());
+
+            // Act
+            var peerId = new PeerId("testPeerId");
+            snapshotGenerator.Handle(new SubscriptionsUpdated(new SubscriptionsForType(new MessageTypeId(typeof(TestEvent))), peerId));
+
+            // Assert
+            testBus.ExpectExactly(peerId, new TestSnapshot());
+        }
+
+        [Test]
+        public void should_not_generate_snapshot_if_snapshot_generator_returns_null()
         {
             // Arrange
             var testBus = new TestBus();
-            var snapshotGenerator = new SnapshotGeneratorTest(testBus);
+            var snapshotGenerator = new TestSnapshotGenerator(testBus, null);
 
             // Act
             var peerId = new PeerId("testPeerId");
-            snapshotGenerator.Handle(new SubscriptionsUpdated(new SubscriptionsForType(new MessageTypeId(typeof(EventTest))), peerId));
+            snapshotGenerator.Handle(new SubscriptionsUpdated(new SubscriptionsForType(new MessageTypeId(typeof(TestEvent))), peerId));
 
             // Assert
-            testBus.ExpectExactly(peerId, new SnapshotTest());
+            testBus.ExpectNothing();
         }
     }
 }

+ 6 - 2
src/Abc.Zebus/Subscriptions/SubscriptionSnapshotGenerator.cs

@@ -12,9 +12,10 @@ namespace Abc.Zebus.Subscriptions
     /// - The <see cref="GenerateSnapshot"/> method will be invoked after the directory state is updated.
     /// - Messages can be sent to the subscribing peer before the snapshot.
     /// - The subscribing peer should properly handle (discard) messages received before the snapshot.
+    /// - If the implementer of GenerateSnapshot returns null, no snapshot message will be sent.
     /// </remarks>
     public abstract class SubscriptionSnapshotGenerator<TSnapshotMessage, TMessage> : SubscriptionHandler<TMessage>
-        where TSnapshotMessage : IEvent
+        where TSnapshotMessage : class, IEvent
         where TMessage : IEvent
     {
         private readonly IBus _bus;
@@ -27,6 +28,9 @@ namespace Abc.Zebus.Subscriptions
         protected sealed override void OnSubscriptionsUpdated(SubscriptionsForType subscriptions, PeerId peerId)
         {
             var snapshot = GenerateSnapshot(subscriptions);
+            if (snapshot == null)
+                return;
+
             var internalBus = (IInternalBus)_bus;
             internalBus.Publish(snapshot, peerId);
         }
@@ -36,6 +40,6 @@ namespace Abc.Zebus.Subscriptions
         /// </summary>
         /// <param name="messageSubscription">The subscription of <see cref="TMessage"/></param>
         /// <returns>An instance of the snapshot message</returns>
-        protected abstract TSnapshotMessage GenerateSnapshot(SubscriptionsForType messageSubscription);
+        protected abstract TSnapshotMessage? GenerateSnapshot(SubscriptionsForType messageSubscription);
     }
 }