You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by bl...@apache.org on 2023/08/17 13:21:01 UTC

[pulsar-dotpulsar] branch master updated: A little clean up and fixing state issues

This is an automated email from the ASF dual-hosted git repository.

blankensteiner pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar-dotpulsar.git


The following commit(s) were added to refs/heads/master by this push:
     new 060f3b8  A little clean up and fixing state issues
060f3b8 is described below

commit 060f3b8b460ad82ec0d89e57f7b13de0c2fbfcdc
Author: Daniel Blankensteiner <db...@vmail.dk>
AuthorDate: Thu Aug 17 15:20:51 2023 +0200

    A little clean up and fixing state issues
---
 CHANGELOG.md                                       |   8 +-
 src/DotPulsar/Extensions/StateExtensions.cs        |  14 +-
 src/DotPulsar/Internal/Consumer.cs                 |  11 +-
 src/DotPulsar/Internal/Producer.cs                 |   6 +-
 src/DotPulsar/Internal/Reader.cs                   |  11 +-
 .../Extensions/StateExtensionsTests.cs             | 170 ++++++++++++++++++---
 6 files changed, 171 insertions(+), 49 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 34b653d..d1924d3 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,17 +8,23 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/)
 
 ### Added
 
-- Added Partitioned topic support for the Consumer and Reader
+- Added partitioned topic support for the Consumer and Reader
 - MessageId now includes an extra field for setting the topic
 - Support for `ProducerAccessMode` to prevent multiple producers on a single topic
 - A new `Fenced` state for producers which is a final state
 - The ability to explicitly set compression information on an outgoing message using `MessageMetadata`
 
+### Changed
+
+- The DelayedStateMonitor extension method now invokes onStateLeft when the initial state change is to a final state
+
 ### Fixed
 
 - Issue preventing readers from correctly going into the `Faulted` state
 - Calling `await Send(...)` on a producer did not correctly terminate with an exception when a send operation failed (e.g. because the producer faulted)
 - The 'Partition' in 'MessageId' will now be set to the correct partition when producing to partitioned topics
+- The OnStateChangeFrom extension method with delay functionality returned the inputted state but should return the current state
+- The DelayedStateMonitor extension method invoked onStateLeft with the inputted state but should have invoked it with the current state
 
 ### Deprecated
 
diff --git a/src/DotPulsar/Extensions/StateExtensions.cs b/src/DotPulsar/Extensions/StateExtensions.cs
index 59eb54a..43ad167 100644
--- a/src/DotPulsar/Extensions/StateExtensions.cs
+++ b/src/DotPulsar/Extensions/StateExtensions.cs
@@ -99,7 +99,7 @@ public static class StateExtensions
                 if (cancellationToken.IsCancellationRequested)
                     throw;
 
-                return state;
+                return currentState;
             }
         }
     }
@@ -116,15 +116,13 @@ public static class StateExtensions
         TimeSpan delay,
         Func<TEntity, TState, CancellationToken, ValueTask<TFaultContext>> onStateLeft,
         Func<TEntity, TState, TFaultContext, CancellationToken, ValueTask> onStateReached,
-        CancellationToken cancellationToken) where TEntity : IState<TState> where TState : notnull where TFaultContext : class
+        CancellationToken cancellationToken = default) where TEntity : IState<TState> where TState : notnull where TFaultContext : class
     {
         while (true)
         {
             cancellationToken.ThrowIfCancellationRequested();
 
             var currentState = await stateImplementer.OnStateChangeFrom(state, delay, cancellationToken).ConfigureAwait(false);
-            if (stateImplementer.IsFinalState(currentState))
-                return;
 
             TFaultContext? faultContext = null;
 
@@ -137,7 +135,11 @@ public static class StateExtensions
                 // Ignore
             }
 
+            if (stateImplementer.IsFinalState(currentState))
+                return;
+
             currentState = await stateImplementer.OnStateChangeTo(state, cancellationToken).ConfigureAwait(false);
+
             if (stateImplementer.IsFinalState(currentState))
                 return;
 
@@ -165,7 +167,7 @@ public static class StateExtensions
         TimeSpan delay,
         Func<TEntity, TState, CancellationToken, ValueTask> onStateLeft,
         Func<TEntity, TState, CancellationToken, ValueTask> onStateReached,
-        CancellationToken cancellationToken) where TEntity : IState<TState> where TState : notnull
+        CancellationToken cancellationToken = default) where TEntity : IState<TState> where TState : notnull
     {
         async ValueTask<string> onStateLeftFunction(TEntity entity, TState state, CancellationToken cancellationToken)
         {
@@ -193,7 +195,7 @@ public static class StateExtensions
         TimeSpan delay,
         Action<TEntity, TState> onStateLeft,
         Action<TEntity, TState> onStateReached,
-        CancellationToken cancellationToken) where TEntity : IState<TState> where TState : notnull
+        CancellationToken cancellationToken = default) where TEntity : IState<TState> where TState : notnull
     {
         ValueTask<string> onStateLeftFunction(TEntity entity, TState state, CancellationToken cancellationToken)
         {
diff --git a/src/DotPulsar/Internal/Consumer.cs b/src/DotPulsar/Internal/Consumer.cs
index 5c0e597..b1aaf59 100644
--- a/src/DotPulsar/Internal/Consumer.cs
+++ b/src/DotPulsar/Internal/Consumer.cs
@@ -399,15 +399,10 @@ public sealed class Consumer<TMessage> : IConsumer<TMessage>
         return subConsumer;
     }
 
-    private string GetPartitionedTopicName(int partitionNumber)
-    {
-        return $"{Topic}-partition-{partitionNumber}";
-    }
+    private string GetPartitionedTopicName(int partitionNumber) => $"{Topic}-partition-{partitionNumber}";
 
-    private StateManager<ConsumerState> CreateStateManager()
-    {
-        return new StateManager<ConsumerState>(ConsumerState.Disconnected, ConsumerState.Closed, ConsumerState.ReachedEndOfTopic, ConsumerState.Faulted);
-    }
+    private static StateManager<ConsumerState> CreateStateManager()
+        => new(ConsumerState.Disconnected, ConsumerState.Closed, ConsumerState.ReachedEndOfTopic, ConsumerState.Faulted);
 
     private async Task Guard(CancellationToken cancellationToken)
     {
diff --git a/src/DotPulsar/Internal/Producer.cs b/src/DotPulsar/Internal/Producer.cs
index 5c06db9..7fcda17 100644
--- a/src/DotPulsar/Internal/Producer.cs
+++ b/src/DotPulsar/Internal/Producer.cs
@@ -338,7 +338,7 @@ public sealed class Producer<TMessage> : IProducer<TMessage>, IRegisterEvent
     internal async ValueTask WaitForSendQueueEmpty(CancellationToken cancellationToken)
         => await Task.WhenAll(_producers.Values.Select(producer => producer.WaitForSendQueueEmpty(cancellationToken).AsTask())).ConfigureAwait(false);
 
-    private void CompleteActivity(MessageId messageId, long payloadSize, Activity? activity)
+    private static void CompleteActivity(MessageId messageId, long payloadSize, Activity? activity)
     {
         if (activity is null)
             return;
@@ -353,7 +353,7 @@ public sealed class Producer<TMessage> : IProducer<TMessage>, IRegisterEvent
         activity.Dispose();
     }
 
-    private void FailActivity(Exception exception, Activity? activity)
+    private static void FailActivity(Exception exception, Activity? activity)
     {
         if (activity is null)
             return;
@@ -370,7 +370,7 @@ public sealed class Producer<TMessage> : IProducer<TMessage>, IRegisterEvent
             throw new ProducerDisposedException(GetType().FullName!);
     }
 
-    private StateManager<ProducerState> CreateStateManager()
+    private static StateManager<ProducerState> CreateStateManager()
         => new(ProducerState.Disconnected, ProducerState.Closed, ProducerState.Faulted, ProducerState.Fenced);
 
     public void Register(IEvent @event) { }
diff --git a/src/DotPulsar/Internal/Reader.cs b/src/DotPulsar/Internal/Reader.cs
index 34cf020..30b6244 100644
--- a/src/DotPulsar/Internal/Reader.cs
+++ b/src/DotPulsar/Internal/Reader.cs
@@ -256,10 +256,8 @@ public sealed class Reader<TMessage> : IReader<TMessage>
         }
     }
 
-    private StateManager<ReaderState> CreateStateManager()
-    {
-        return new StateManager<ReaderState>(ReaderState.Disconnected, ReaderState.Closed, ReaderState.ReachedEndOfTopic, ReaderState.Faulted);
-    }
+    private static StateManager<ReaderState> CreateStateManager()
+        => new(ReaderState.Disconnected, ReaderState.Closed, ReaderState.ReachedEndOfTopic, ReaderState.Faulted);
 
     private SubReader<TMessage> CreateSubReader(string topic)
     {
@@ -291,10 +289,7 @@ public sealed class Reader<TMessage> : IReader<TMessage>
         return subReader;
     }
 
-    private string GetPartitionedTopicName(int partitionNumber)
-    {
-        return $"{Topic}-partition-{partitionNumber}";
-    }
+    private string GetPartitionedTopicName(int partitionNumber) => $"{Topic}-partition-{partitionNumber}";
 
     private async Task Guard(CancellationToken cancellationToken)
     {
diff --git a/tests/DotPulsar.Tests/Extensions/StateExtensionsTests.cs b/tests/DotPulsar.Tests/Extensions/StateExtensionsTests.cs
index b2b74b1..5bdf717 100644
--- a/tests/DotPulsar.Tests/Extensions/StateExtensionsTests.cs
+++ b/tests/DotPulsar.Tests/Extensions/StateExtensionsTests.cs
@@ -29,82 +29,206 @@ using Xunit;
 public class StateExtensionsTests
 {
     [Theory, Tests.AutoData]
-    public async Task OnStateChangeTo_WhenChangingToFinalStateInitially_ShouldReturnFinalState([Frozen] IState<ProducerState> sut)
+    public async Task OnStateChangeTo_WhenChangingToFinalStateInitially_ShouldReturnFinalState([Frozen] IState<ProducerState> uut)
     {
         // Arrange
         const ProducerState expected = ProducerState.Faulted;
-        sut.OnStateChangeTo(ProducerState.Disconnected, Arg.Any<CancellationToken>()).Returns(expected);
-        sut.IsFinalState(expected).Returns(true);
+        uut.OnStateChangeTo(ProducerState.Disconnected, Arg.Any<CancellationToken>()).Returns(expected);
+        uut.IsFinalState(expected).Returns(true);
 
         // Act
-        var actual = await sut.OnStateChangeTo(ProducerState.Disconnected, TimeSpan.FromSeconds(1));
+        var actual = await uut.OnStateChangeTo(ProducerState.Disconnected, TimeSpan.FromSeconds(1));
 
         // Assert
         actual.Should().Be(expected);
     }
 
     [Theory, Tests.AutoData]
-    public async Task OnStateChangeTo_WhenChangingToFinalStateWhileWaitingForDelay_ShouldReturnFinalState([Frozen] IState<ProducerState> sut)
+    public async Task OnStateChangeTo_WhenChangingToFinalStateWhileWaitingForDelay_ShouldReturnFinalState([Frozen] IState<ProducerState> uut)
     {
         // Arrange
         const ProducerState expected = ProducerState.Faulted;
-        sut.OnStateChangeTo(ProducerState.Disconnected, Arg.Any<CancellationToken>()).Returns(ProducerState.Disconnected);
-        sut.IsFinalState(ProducerState.Disconnected).Returns(false);
-        sut.OnStateChangeFrom(ProducerState.Disconnected, Arg.Any<CancellationToken>()).Returns(expected);
-        sut.IsFinalState(expected).Returns(true);
+        uut.OnStateChangeTo(ProducerState.Disconnected, Arg.Any<CancellationToken>()).Returns(ProducerState.Disconnected);
+        uut.IsFinalState(ProducerState.Disconnected).Returns(false);
+        uut.OnStateChangeFrom(ProducerState.Disconnected, Arg.Any<CancellationToken>()).Returns(expected);
+        uut.IsFinalState(expected).Returns(true);
 
         // Act
-        var actual = await sut.OnStateChangeTo(ProducerState.Disconnected, TimeSpan.FromSeconds(1));
+        var actual = await uut.OnStateChangeTo(ProducerState.Disconnected, TimeSpan.FromSeconds(1));
 
         // Assert
         actual.Should().Be(expected);
     }
 
     [Theory, Tests.AutoData]
-    public async Task OnStateChangeTo_WhenTheDelayIsExceeded_ShouldReturnState([Frozen] IState<ProducerState> sut)
+    public async Task OnStateChangeTo_WhenTheDelayIsExceeded_ShouldReturnState([Frozen] IState<ProducerState> uut)
     {
         // Arrange
         const ProducerState expected = ProducerState.Disconnected;
-        sut.OnStateChangeTo(expected, Arg.Any<CancellationToken>()).Returns(expected);
-        sut.IsFinalState(expected).Returns(false);
-        sut.OnStateChangeFrom(expected, Arg.Any<CancellationToken>()).Throws<OperationCanceledException>();
+        uut.OnStateChangeTo(expected, Arg.Any<CancellationToken>()).Returns(expected);
+        uut.IsFinalState(expected).Returns(false);
+        uut.OnStateChangeFrom(expected, Arg.Any<CancellationToken>()).Throws<OperationCanceledException>();
 
         // Act
-        var actual = await sut.OnStateChangeTo(expected, TimeSpan.FromSeconds(1));
+        var actual = await uut.OnStateChangeTo(expected, TimeSpan.FromSeconds(1));
 
         // Assert
         actual.Should().Be(expected);
     }
 
     [Theory, Tests.AutoData]
-    public async Task OnStateChangeTo_WhenCancellationTokenIsCancelledInitially_ShouldThrowException([Frozen] IState<ProducerState> sut)
+    public async Task OnStateChangeTo_WhenCancellationTokenIsCancelledInitially_ShouldThrowException([Frozen] IState<ProducerState> uut)
     {
         // Arrange
         var cts = new CancellationTokenSource();
         cts.Cancel();
-        sut.OnStateChangeTo(ProducerState.Disconnected, cts.Token).Throws<OperationCanceledException>();
+        uut.OnStateChangeTo(ProducerState.Disconnected, cts.Token).Throws<OperationCanceledException>();
 
         // Act
-        var exception = await Record.ExceptionAsync(() => sut.OnStateChangeTo(ProducerState.Disconnected, TimeSpan.FromSeconds(1), cts.Token).AsTask());
+        var exception = await Record.ExceptionAsync(() => uut.OnStateChangeTo(ProducerState.Disconnected, TimeSpan.FromSeconds(1), cts.Token).AsTask());
 
         // Assert
         exception.Should().BeOfType<OperationCanceledException>();
     }
 
     [Theory, Tests.AutoData]
-    public async Task OnStateChangeTo_WhenCancellationTokenIsCancelledWhileWaitingForDelay_ShouldThrowException([Frozen] IState<ProducerState> sut)
+    public async Task OnStateChangeTo_WhenCancellationTokenIsCancelledWhileWaitingForDelay_ShouldThrowException([Frozen] IState<ProducerState> uut)
     {
         // Arrange
         var cts = new CancellationTokenSource();
         cts.Cancel();
-        sut.OnStateChangeTo(ProducerState.Disconnected, cts.Token).Returns(ProducerState.Disconnected);
-        sut.IsFinalState(ProducerState.Disconnected).Returns(false);
-        sut.OnStateChangeFrom(ProducerState.Disconnected, Arg.Any<CancellationToken>()).Throws<OperationCanceledException>();
+        uut.OnStateChangeTo(ProducerState.Disconnected, cts.Token).Returns(ProducerState.Disconnected);
+        uut.IsFinalState(ProducerState.Disconnected).Returns(false);
+        uut.OnStateChangeFrom(ProducerState.Disconnected, Arg.Any<CancellationToken>()).Throws<OperationCanceledException>();
 
         // Act
-        var exception = await Record.ExceptionAsync(() => sut.OnStateChangeTo(ProducerState.Disconnected, TimeSpan.FromSeconds(1), cts.Token).AsTask());
+        var exception = await Record.ExceptionAsync(() => uut.OnStateChangeTo(ProducerState.Disconnected, TimeSpan.FromSeconds(1), cts.Token).AsTask());
 
         // Assert
         exception.Should().BeOfType<OperationCanceledException>();
     }
+
+    [Theory, Tests.AutoData]
+    public async Task OnStateChangeFrom_WhenChangingToFinalStateInitially_ShouldReturnFinalState([Frozen] IState<ProducerState> uut)
+    {
+        // Arrange
+        const ProducerState expected = ProducerState.Faulted;
+        uut.OnStateChangeFrom(ProducerState.Disconnected, Arg.Any<CancellationToken>()).Returns(expected);
+        uut.IsFinalState(expected).Returns(true);
+
+        // Act
+        var actual = await uut.OnStateChangeFrom(ProducerState.Disconnected, TimeSpan.FromSeconds(1));
+
+        // Assert
+        actual.Should().Be(expected);
+    }
+
+    [Theory, Tests.AutoData]
+    public async Task OnStateChangeFrom_WhenChangingToFinalStateWhileWaitingForDelay_ShouldReturnFinalState([Frozen] IState<ProducerState> uut)
+    {
+        // Arrange
+        const ProducerState expected = ProducerState.Faulted;
+        uut.OnStateChangeFrom(ProducerState.Disconnected, Arg.Any<CancellationToken>()).Returns(ProducerState.Disconnected);
+        uut.IsFinalState(ProducerState.Disconnected).Returns(false);
+        uut.OnStateChangeTo(ProducerState.Disconnected, Arg.Any<CancellationToken>()).Returns(expected);
+        uut.IsFinalState(expected).Returns(true);
+
+        // Act
+        var actual = await uut.OnStateChangeFrom(ProducerState.Disconnected, TimeSpan.FromSeconds(1));
+
+        // Assert
+        actual.Should().Be(expected);
+    }
+
+    [Theory, Tests.AutoData]
+    public async Task OnStateChangeFrom_WhenTheDelayIsExceeded_ShouldReturnState([Frozen] IState<ProducerState> uut)
+    {
+        // Arrange
+        const ProducerState expected = ProducerState.Disconnected;
+        uut.OnStateChangeFrom(ProducerState.Connected, Arg.Any<CancellationToken>()).Returns(expected);
+        uut.IsFinalState(expected).Returns(false);
+        uut.OnStateChangeTo(ProducerState.Connected, Arg.Any<CancellationToken>()).Throws<OperationCanceledException>();
+
+        // Act
+        var actual = await uut.OnStateChangeFrom(ProducerState.Connected, TimeSpan.FromSeconds(1));
+
+        // Assert
+        actual.Should().Be(expected);
+    }
+
+    [Theory, Tests.AutoData]
+    public async Task OnStateChangeFrom_WhenCancellationTokenIsCancelledInitially_ShouldThrowException([Frozen] IState<ProducerState> uut)
+    {
+        // Arrange
+        var cts = new CancellationTokenSource();
+        cts.Cancel();
+        uut.OnStateChangeFrom(ProducerState.Disconnected, cts.Token).Throws<OperationCanceledException>();
+
+        // Act
+        var exception = await Record.ExceptionAsync(() => uut.OnStateChangeFrom(ProducerState.Disconnected, TimeSpan.FromSeconds(1), cts.Token).AsTask());
+
+        // Assert
+        exception.Should().BeOfType<OperationCanceledException>();
+    }
+
+    [Theory, Tests.AutoData]
+    public async Task OnStateChangeFrom_WhenCancellationTokenIsCancelledWhileWaitingForDelay_ShouldThrowException([Frozen] IState<ProducerState> uut)
+    {
+        // Arrange
+        var cts = new CancellationTokenSource();
+        cts.Cancel();
+        uut.OnStateChangeFrom(ProducerState.Disconnected, cts.Token).Returns(ProducerState.Disconnected);
+        uut.IsFinalState(ProducerState.Disconnected).Returns(false);
+        uut.OnStateChangeTo(ProducerState.Disconnected, Arg.Any<CancellationToken>()).Throws<OperationCanceledException>();
+
+        // Act
+        var exception = await Record.ExceptionAsync(() => uut.OnStateChangeFrom(ProducerState.Disconnected, TimeSpan.FromSeconds(1), cts.Token).AsTask());
+
+        // Assert
+        exception.Should().BeOfType<OperationCanceledException>();
+    }
+
+    [Theory, Tests.AutoData]
+    public async Task DelayedStateMonitor_WhenChangingToFinalStateInitially_ShouldInvokeOnStateLeft([Frozen] IState<ProducerState> uut)
+    {
+        // Arrange
+        const ProducerState expected = ProducerState.Faulted;
+        uut.OnStateChangeFrom(ProducerState.Connected, Arg.Any<CancellationToken>()).Returns(expected);
+        uut.IsFinalState(expected).Returns(true);
+        ProducerState? stateLeft = null;
+        ProducerState? stateReached = null;
+        void onStateLeft(IState<ProducerState> _, ProducerState state) => stateLeft = state;
+        void onStateReached(IState<ProducerState> _, ProducerState state) => stateReached = state;
+
+        // Act
+        await uut.DelayedStateMonitor(ProducerState.Connected, TimeSpan.FromSeconds(1), onStateLeft, onStateReached);
+
+        // Assert
+        stateLeft.HasValue.Should().BeTrue();
+        stateLeft!.Value.Should().Be(expected);
+        stateReached.HasValue.Should().BeFalse();
+    }
+
+    [Theory, Tests.AutoData]
+    public async Task DelayedStateMonitor_WhenChangingToFinalStateWhileWaiting_ShouldInvokeOnStateLeft([Frozen] IState<ProducerState> uut)
+    {
+        // Arrange
+        const ProducerState expected = ProducerState.Disconnected;
+        uut.OnStateChangeFrom(ProducerState.Connected, Arg.Any<CancellationToken>()).Returns(expected);
+        uut.IsFinalState(ProducerState.Disconnected).Returns(false);
+        uut.OnStateChangeTo(ProducerState.Connected, Arg.Any<CancellationToken>()).Returns(x => throw new OperationCanceledException(), x => ProducerState.Faulted);
+        uut.IsFinalState(ProducerState.Faulted).Returns(true);
+        ProducerState? stateLeft = null;
+        ProducerState? stateReached = null;
+        void onStateLeft(IState<ProducerState> _, ProducerState state) => stateLeft = state;
+        void onStateReached(IState<ProducerState> _, ProducerState state) => stateReached = state;
+
+        // Act
+        await uut.DelayedStateMonitor(ProducerState.Connected, TimeSpan.FromSeconds(1), onStateLeft, onStateReached);
+
+        // Assert
+        stateLeft.HasValue.Should().BeTrue();
+        stateLeft!.Value.Should().Be(expected);
+        stateReached.HasValue.Should().BeFalse();
+    }
 }