Skip to content

Commit

Permalink
Add IsSuccess extension method for PubAckResponse (#493)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtmk authored May 8, 2024
1 parent 197b976 commit 15eb447
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/NATS.Client.JetStream/NatsJSExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,16 @@ public static void EnsureSuccess(this PubAckResponse ack)
if (ack.Duplicate)
throw new NatsJSDuplicateMessageException(ack.Seq);
}

/// <summary>
/// Checks if there are no errors and message is not a duplicate.
/// </summary>
/// <param name="ack">ACK response.</param>
/// <returns>True if there are no errors and message is not a duplicate.</returns>
/// <exception cref="ArgumentNullException"><see cref="PubAckResponse"/> is <c>NULL</c>.</exception>
public static bool IsSuccess(this PubAckResponse ack)
{
ArgumentNullException.ThrowIfNull(ack);
return ack.Error == null && !ack.Duplicate;
}
}
4 changes: 4 additions & 0 deletions tests/NATS.Client.JetStream.Tests/PublishTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public async Task Publish_test()
Assert.Equal(1, (int)ack.Seq);
Assert.Equal("s1", ack.Stream);
Assert.False(ack.Duplicate);
Assert.True(ack.IsSuccess());
}

// Duplicate
Expand All @@ -49,6 +50,7 @@ public async Task Publish_test()
Assert.Null(ack1.Error);
Assert.Equal(2, (int)ack1.Seq);
Assert.False(ack1.Duplicate);
Assert.True(ack1.IsSuccess());

var ack2 = await js.PublishAsync(
subject: "s1.foo",
Expand All @@ -58,6 +60,7 @@ public async Task Publish_test()
cancellationToken: cts.Token);
Assert.Null(ack2.Error);
Assert.True(ack2.Duplicate);
Assert.False(ack2.IsSuccess());
}

// ExpectedStream
Expand All @@ -77,6 +80,7 @@ public async Task Publish_test()
Assert.Equal(400, ack2.Error?.Code);
Assert.Equal(10060, ack2.Error?.ErrCode);
Assert.Equal("expected stream does not match", ack2.Error?.Description);
Assert.False(ack2.IsSuccess());
}

// ExpectedLastSequence
Expand Down

0 comments on commit 15eb447

Please sign in to comment.