-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
- Loading branch information
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
namespace NATS.Client.JetStream.Internal; | ||
|
||
internal static class ReplyToDateTimeAndSeq | ||
{ | ||
internal static (DateTimeOffset? DateTime, long? Seq) Parse(string? reply) | ||
{ | ||
if (reply == null) | ||
{ | ||
return (null, null); | ||
} | ||
|
||
var ackSeperated = reply.Split("."); | ||
|
||
// It must be seperated by 9 dots as defined in | ||
// https://docs.nats.io/reference/reference-protocols/nats_api_reference#acknowledging-messages | ||
if (ackSeperated.Length != 9) | ||
{ | ||
return (null, null); | ||
} | ||
|
||
var timestamp = long.Parse(ackSeperated[^2]); | ||
var offset = DateTimeOffset.FromUnixTimeMilliseconds(timestamp / 1000000); | ||
var dateTime = new DateTimeOffset(offset.Ticks, TimeSpan.Zero); | ||
var seq = long.Parse(ackSeperated[5]); | ||
|
||
return (dateTime, seq); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,11 +12,13 @@ public readonly struct NatsJSMsg<T> | |
{ | ||
private readonly NatsJSContext _context; | ||
private readonly NatsMsg<T> _msg; | ||
private readonly Lazy<(DateTimeOffset? DateTime, long? Sequence)> _replyToDateTimeAndSeq; | ||
|
||
internal NatsJSMsg(NatsMsg<T> msg, NatsJSContext context) | ||
{ | ||
_msg = msg; | ||
_context = context; | ||
_replyToDateTimeAndSeq = new Lazy<(DateTimeOffset? DateTime, long? Sequnce)>(() => ReplyToDateTimeAndSeq.Parse(msg.ReplyTo)); | ||
} | ||
|
||
/// <summary> | ||
|
@@ -53,12 +55,12 @@ internal NatsJSMsg(NatsMsg<T> msg, NatsJSContext context) | |
/// <summary> | ||
/// The sequence number of the message. | ||
/// </summary> | ||
public long? Sequence => _msg.Seq; | ||
public long? Sequence => _replyToDateTimeAndSeq.Value.Sequence; | ||
|
||
/// <summary> | ||
/// The time of the message. | ||
/// </summary> | ||
public DateTimeOffset? DateTime => _msg.DateTime; | ||
public DateTimeOffset? DateTime => _replyToDateTimeAndSeq.Value.DateTime; | ||
|
||
/// <summary> | ||
/// Acknowledges the message was completely handled. | ||
|
@@ -112,10 +114,7 @@ private ValueTask SendAckAsync(ReadOnlySequence<byte> payload, AckOpts opts = de | |
|
||
return _msg.ReplyAsync( | ||
payload: payload, | ||
opts: new NatsPubOpts | ||
{ | ||
WaitUntilSent = opts.WaitUntilSent ?? _context.Opts.AckOpts.WaitUntilSent, | ||
}, | ||
opts: new NatsPubOpts {WaitUntilSent = opts.WaitUntilSent ?? _context.Opts.AckOpts.WaitUntilSent,}, | ||
Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs GitHub Actions / memory test (release/v2.9.23)
Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs GitHub Actions / memory test (release/v2.9.23)
Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs GitHub Actions / memory test (release/v2.9.23)
Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs GitHub Actions / memory test (release/v2.9.23)
Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs GitHub Actions / memory test (release/v2.9.23)
Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs GitHub Actions / memory test (release/v2.9.23)
Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs GitHub Actions / memory test (latest)
Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs GitHub Actions / memory test (latest)
Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs GitHub Actions / memory test (latest)
Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs GitHub Actions / memory test (latest)
Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs GitHub Actions / memory test (latest)
Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs GitHub Actions / memory test (latest)
Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs GitHub Actions / dotnet (release/v2.9.23)
Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs GitHub Actions / dotnet (release/v2.9.23)
Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs GitHub Actions / dotnet (release/v2.9.23)
Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs GitHub Actions / dotnet (release/v2.9.23)
Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs GitHub Actions / dotnet (release/v2.9.23)
Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs GitHub Actions / dotnet (release/v2.9.23)
Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs GitHub Actions / memory test (main)
Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs GitHub Actions / memory test (main)
Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs GitHub Actions / memory test (main)
Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs GitHub Actions / memory test (main)
Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs GitHub Actions / memory test (main)
Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs GitHub Actions / memory test (main)
Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs GitHub Actions / dotnet (latest)
Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs GitHub Actions / dotnet (latest)
Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs GitHub Actions / dotnet (latest)
Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs GitHub Actions / dotnet (latest)
Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs GitHub Actions / dotnet (latest)
Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs GitHub Actions / dotnet (latest)
Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs GitHub Actions / dotnet (main)
Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs GitHub Actions / dotnet (main)
Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs GitHub Actions / dotnet (main)
Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs GitHub Actions / dotnet (main)
Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs GitHub Actions / dotnet (main)
Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs GitHub Actions / dotnet (main)
|
||
cancellationToken: cancellationToken); | ||
} | ||
} | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using NATS.Client.JetStream.Internal; | ||
|
||
namespace NATS.Client.JetStream.Tests.Internal; | ||
|
||
public class ReplyToDateTimeAndSeqTest | ||
{ | ||
[Fact] | ||
public void ShouldParseReplyToDateTimeAndSeq() | ||
{ | ||
var (dateTime, seq) = ReplyToDateTimeAndSeq.Parse("$JS.ACK.UnitTest.GetEvents_0.1.100.1.1696023331771188000.0"); | ||
|
||
dateTime!.Value.ToString("O").Should().Be("2023-09-29T21:35:31.7710000+00:00"); | ||
seq.Should().Be(100); | ||
} | ||
|
||
[Fact] | ||
public void ShouldSetNullForReturnWhenReplyToIsNull() | ||
{ | ||
var (dateTime, seq) = ReplyToDateTimeAndSeq.Parse(null); | ||
|
||
dateTime.Should().BeNull(); | ||
seq.Should().BeNull(); | ||
} | ||
|
||
[Fact] | ||
public void ShouldSetNullWhenReplyToIsASimpleReqResponse() | ||
{ | ||
var (dateTime, seq) = ReplyToDateTimeAndSeq.Parse("_INBOX.1"); | ||
|
||
dateTime.Should().BeNull(); | ||
seq.Should().BeNull(); | ||
} | ||
} |