Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom inbox prefix #322

Merged
merged 4 commits into from
Nov 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/NATS.Client/Conn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3224,9 +3224,11 @@ public Task<Msg> RequestAsync(string subject, byte[] data, int offset, int count
/// <returns>A unique inbox string.</returns>
public string NewInbox()
{
var prefix = opts.CustomInboxPrefix ?? IC.inboxPrefix;

if (!opts.UseOldRequestStyle)
{
return IC.inboxPrefix + Guid.NewGuid().ToString("N");
return prefix + Guid.NewGuid().ToString("N");
}
else
{
Expand All @@ -3237,7 +3239,7 @@ public string NewInbox()

r.NextBytes(buf);

return IC.inboxPrefix + BitConverter.ToString(buf).Replace("-","");
return prefix + BitConverter.ToString(buf).Replace("-","");
}
}

Expand Down
18 changes: 18 additions & 0 deletions src/NATS.Client/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ public void SetJWTEventHandlers(EventHandler<UserJWTEventArgs> JWTEventHandler,
internal string token;
internal string nkey;

internal string customInboxPrefix;

// Options can only be publicly created through
// ConnectionFactory.GetDefaultOptions();
internal Options() { }
Expand Down Expand Up @@ -222,6 +224,7 @@ internal Options(Options o)
verbose = o.verbose;
subscriberDeliveryTaskCount = o.subscriberDeliveryTaskCount;
subscriptionBatchSize = o.subscriptionBatchSize;
customInboxPrefix = o.customInboxPrefix;

if (o.url != null)
{
Expand Down Expand Up @@ -463,6 +466,21 @@ public string Token
set { token = value; }
}

/// <summary>
/// Gets or sets a custom inbox prefix.
/// </summary>
public string CustomInboxPrefix
{
get => customInboxPrefix;
set
{
if (value != null && !Subscription.IsValidPrefix(value))
throw new ArgumentException("Prefix would result in an invalid subject.");

customInboxPrefix = value;
}
}

/// <summary>
/// Adds an X.509 certifcate from a file for use with a secure connection.
/// </summary>
Expand Down
13 changes: 13 additions & 0 deletions src/NATS.Client/Subscription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,19 @@ public static bool IsValidSubject(string subject)
return true;
}

/// <summary>
/// Checks if a prefix is valid.
/// </summary>
/// <param name="prefix"></param>
/// <returns></returns>
public static bool IsValidPrefix(string prefix)
{
if (ContainsInvalidChars(prefix))
return false;

return !prefix.StartsWith(".") && prefix.EndsWith(".");
}

/// <summary>
/// Checks if the queue group name is valid.
/// </summary>
Expand Down
37 changes: 37 additions & 0 deletions src/Tests/IntegrationTests/TestSubscriptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,43 @@ public void TestRespond()
}
}

[Fact]
public void TestRespondWithCustomInbox()
{
var opts = Context.GetTestOptionsWithDefaultTimeout(Context.Server1.Port);
opts.CustomInboxPrefix = "_TEST.";

using (NATSServer.CreateFastAndVerify(Context.Server1.Port))
{
using (var cn = Context.ConnectionFactory.CreateConnection(opts))
using (var requestSub = cn.SubscribeSync("foo"))
{
var replyTo = cn.NewInbox();
Assert.StartsWith("_TEST.", replyTo);

using (var responderSub = cn.SubscribeSync(replyTo))
{
cn.Publish("foo", replyTo, SamplePayload.Random());

var request = requestSub.NextMessage(1000);
Assert.NotNull(request);
Assert.Equal(replyTo, request.Reply);

var reply = SamplePayload.Random();
request.Respond(reply);

var response = responderSub.NextMessage(1000);
Assert.NotNull(response);
Assert.Equal(replyTo, response.Subject);
Assert.Equal(reply, response.Data);

requestSub.Unsubscribe();
responderSub.Unsubscribe();
}
}
}
}

[Fact]
public void TestRespondWithAutoUnsubscribe()
{
Expand Down
27 changes: 27 additions & 0 deletions src/Tests/UnitTests/TestOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// limitations under the License.

using System;
using System.Reflection.Emit;
using NATS.Client;
using Xunit;

Expand All @@ -38,5 +39,31 @@ public void TestBadOptionSubscriptionBatchSize()

Assert.ThrowsAny<ArgumentException>(() => opts.SubscriptionBatchSize = 0);
}

[Theory]
[InlineData("")]
[InlineData("\r")]
[InlineData("\n")]
[InlineData("\t")]
[InlineData("Test")]
[InlineData(".Test.")]
public void TestBadCustomPrefix(string customPrefix)
{
var opts = GetDefaultOptions();

Assert.ThrowsAny<ArgumentException>(() => opts.CustomInboxPrefix = customPrefix);
}

[Theory]
[InlineData("Test.")]
[InlineData("Test.SubTest.")]
[InlineData("_Test.")]
[InlineData("_Test.SubTest.")]
public void TestOkCustomPrefix(string customPrefix)
{
var opts = GetDefaultOptions();

opts.CustomInboxPrefix = customPrefix;
}
}
}