Skip to content

Instantly share code, notes, and snippets.

@dorny
Last active September 11, 2020 10:52
Show Gist options
  • Save dorny/6255e9a6679a2f582c98e3fa93b91df0 to your computer and use it in GitHub Desktop.
Save dorny/6255e9a6679a2f582c98e3fa93b91df0 to your computer and use it in GitHub Desktop.
Reproduces bug in STAN.NET Client - https://github.com/nats-io/stan.net/issues/184
using System;
using System.Threading.Tasks;
using NATS.Client;
using STAN.Client;
namespace StanNetBug
{
class Program
{
static async Task Main(string[] args)
{
// Connection setup
var cf = new ConnectionFactory();
using var nc = cf.CreateConnection("localhost");
var scf = new StanConnectionFactory();
var so = StanOptions.GetDefaultOptions();
so.NatsConn = nc; // STAN reuses the NATS connection
using var sc = scf.CreateConnection("cluster-id", "client-id", so);
var dummyMessage = new byte[] {1};
await sc.PublishAsync("subject", dummyMessage);
// Respond to request - this is normally implemented in different service
nc.SubscribeAsync("rpc.test", (s, msg) =>
{
msg.Message.Respond(dummyMessage);
});
// This will cause failure of following subscription
await nc.RequestAsync("rpc.test", dummyMessage);
try
{
var opts = StanSubscriptionOptions.GetDefaultOptions();
opts.StartWithLastReceived();
sc.Subscribe("subject", opts, (s, msg) =>
{
Console.WriteLine("Handler is invoked even if subscribe throws exception!");
});
}
catch (NATSTimeoutException ex)
{
Console.WriteLine($"Bug successfully reproduced!\n{ex.Message}\n{ex.StackTrace}");
}
await Task.Delay(1000);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment