本文整理汇总了C#中NATS.Client.ConnectionFactory.SubscribeAsync方法的典型用法代码示例。如果您正苦于以下问题:C# ConnectionFactory.SubscribeAsync方法的具体用法?C# ConnectionFactory.SubscribeAsync怎么用?C# ConnectionFactory.SubscribeAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NATS.Client.ConnectionFactory
的用法示例。
在下文中一共展示了ConnectionFactory.SubscribeAsync方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestAsyncSubHandlerAPI
public void TestAsyncSubHandlerAPI()
{
using (IConnection c = new ConnectionFactory().CreateConnection())
{
int received = 0;
EventHandler<MsgHandlerEventArgs> h = (sender, args) =>
{
Interlocked.Increment(ref received);
};
using (IAsyncSubscription s = c.SubscribeAsync("foo", h))
{
c.Publish("foo", null);
c.Flush();
Thread.Sleep(500);
}
using (IAsyncSubscription s = c.SubscribeAsync("foo", "bar", h))
{
c.Publish("foo", null);
c.Flush();
Thread.Sleep(500);
}
if (received != 2)
{
Assert.Fail("Received ({0}) != 2", received);
}
}
}
示例2: TestClosedConnections
public void TestClosedConnections()
{
IConnection c = new ConnectionFactory().CreateConnection();
ISyncSubscription s = c.SubscribeSync("foo");
c.Close();
// While we can annotate all the exceptions in the test framework,
// just do it manually.
UnitTestUtilities.testExpectedException(
() => { c.Publish("foo", null); },
typeof(NATSConnectionClosedException));
UnitTestUtilities.testExpectedException(
() => { c.Publish(new Msg("foo")); },
typeof(NATSConnectionClosedException));
UnitTestUtilities.testExpectedException(
() => { c.SubscribeAsync("foo"); },
typeof(NATSConnectionClosedException));
UnitTestUtilities.testExpectedException(
() => { c.SubscribeSync("foo"); },
typeof(NATSConnectionClosedException));
UnitTestUtilities.testExpectedException(
() => { c.SubscribeAsync("foo", "bar"); },
typeof(NATSConnectionClosedException));
UnitTestUtilities.testExpectedException(
() => { c.SubscribeSync("foo", "bar"); },
typeof(NATSConnectionClosedException));
UnitTestUtilities.testExpectedException(
() => { c.Request("foo", null); },
typeof(NATSConnectionClosedException));
UnitTestUtilities.testExpectedException(
() => { s.NextMessage(); },
typeof(NATSConnectionClosedException));
UnitTestUtilities.testExpectedException(
() => { s.NextMessage(100); },
typeof(NATSConnectionClosedException));
UnitTestUtilities.testExpectedException(
() => { s.Unsubscribe(); },
typeof(NATSConnectionClosedException));
UnitTestUtilities.testExpectedException(
() => { s.AutoUnsubscribe(1); },
typeof(NATSConnectionClosedException));
}
示例3: TestBasicReconnectFunctionality
public void TestBasicReconnectFunctionality()
{
Options opts = utils.DefaultTestOptions;
opts.Url = "nats://localhost:22222";
opts.MaxReconnect = 2;
opts.ReconnectWait = 1000;
Object testLock = new Object();
Object msgLock = new Object();
opts.DisconnectedEventHandler = (sender, args) =>
{
lock (testLock)
{
Monitor.Pulse(testLock);
}
};
opts.ReconnectedEventHandler = (sender, args) =>
{
// NOOP
};
NATSServer ns = utils.CreateServerOnPort(22222);
using (IConnection c = new ConnectionFactory().CreateConnection(opts))
{
IAsyncSubscription s = c.SubscribeAsync("foo");
s.MessageHandler += (sender, args) =>
{
lock (msgLock)
{
Monitor.Pulse(msgLock);
}
};
s.Start();
c.Flush();
lock (testLock)
{
ns.Shutdown();
Assert.True(Monitor.Wait(testLock, 100000));
}
c.Publish("foo", Encoding.UTF8.GetBytes("Hello"));
// restart the server.
using (ns = utils.CreateServerOnPort(22222))
{
lock (msgLock)
{
c.Flush(50000);
Assert.True(Monitor.Wait(msgLock, 10000));
}
Assert.True(c.Stats.Reconnects == 1);
}
}
}
示例4: TestCustomObjectSerialization
public void TestCustomObjectSerialization()
{
using (IEncodedConnection c = new ConnectionFactory().CreateEncodedConnection())
{
Object mu = new Object();
SerializationTestObj origObj = new SerializationTestObj();
EventHandler<EncodedMessageEventArgs> eh = (sender, args) =>
{
// Ensure we blow up in the cast
SerializationTestObj so = (SerializationTestObj)args.ReceivedObject;
Assert.IsTrue(so.Equals(origObj));
lock (mu)
{
Monitor.Pulse(mu);
}
};
using (IAsyncSubscription s = c.SubscribeAsync("foo", eh))
{
lock (mu)
{
c.Publish("foo", new SerializationTestObj());
c.Flush();
Monitor.Wait(mu, 1000);
}
}
}
}
示例5: TestServerAutoUnsub
public void TestServerAutoUnsub()
{
using (IConnection c = new ConnectionFactory().CreateConnection())
{
long received = 0;
int max = 10;
using (IAsyncSubscription s = c.SubscribeAsync("foo"))
{
s.MessageHandler += (sender, arg) =>
{
System.Console.WriteLine("Received msg.");
received++;
};
s.AutoUnsubscribe(max);
s.Start();
for (int i = 0; i < (max * 2); i++)
{
c.Publish("foo", Encoding.UTF8.GetBytes("hello"));
}
c.Flush();
Thread.Sleep(500);
if (received != max)
{
Assert.Fail("Recieved ({0}) != max ({1})",
received, max);
}
Assert.IsFalse(s.IsValid);
}
}
}
示例6: TestAsyncErrHandler
public void TestAsyncErrHandler()
{
object subLock = new object();
object testLock = new object();
IAsyncSubscription s;
Options opts = utils.DefaultTestOptions;
opts.SubChannelLength = 10;
bool handledError = false;
using (new NATSServer())
{
using (IConnection c = new ConnectionFactory().CreateConnection(opts))
{
using (s = c.SubscribeAsync("foo"))
{
c.Opts.AsyncErrorEventHandler = (sender, args) =>
{
lock (subLock)
{
if (handledError)
return;
handledError = true;
Assert.True(args.Subscription == s);
Assert.True(args.Error.Contains("Slow"));
// release the subscriber
Monitor.Pulse(subLock);
}
// release the test
lock (testLock) { Monitor.Pulse(testLock); }
};
bool blockedOnSubscriber = false;
s.MessageHandler += (sender, args) =>
{
lock (subLock)
{
if (blockedOnSubscriber)
return;
Assert.True(Monitor.Wait(subLock, 500));
blockedOnSubscriber = true;
}
};
s.Start();
lock (testLock)
{
for (int i = 0; i < (opts.SubChannelLength + 100); i++)
{
c.Publish("foo", null);
}
try
{
c.Flush(1000);
}
catch (Exception)
{
// ignore - we're testing the error handler, not flush.
}
Assert.True(Monitor.Wait(testLock, 1000));
}
}
}
}
}
示例7: TestReplyArg
public void TestReplyArg()
{
using (IConnection c = new ConnectionFactory().CreateConnection())
{
using (IAsyncSubscription s = c.SubscribeAsync("foo"))
{
s.MessageHandler += ExpectedReplyHandler;
s.Start();
lock(mu)
{
received = false;
c.Publish("foo", "bar", null);
Monitor.Wait(mu, 5000);
}
}
}
if (!received)
Assert.Fail("Message not received.");
}
示例8: TestLargeSubjectAndReply
public void TestLargeSubjectAndReply()
{
using (IConnection c = new ConnectionFactory().CreateConnection())
{
String subject = "";
for (int i = 0; i < 1024; i++)
{
subject += "A";
}
String reply = "";
for (int i = 0; i < 1024; i++)
{
reply += "A";
}
using (IAsyncSubscription s = c.SubscribeAsync(subject))
{
Object testLock = new Object();
s.MessageHandler += (sender, args) =>
{
if (!subject.Equals(args.Message.Subject))
Assert.Fail("Invalid subject received.");
if (!reply.Equals(args.Message.Reply))
Assert.Fail("Invalid subject received.");
lock (testLock)
{
Monitor.Pulse(testLock);
}
};
s.Start();
c.Publish(subject, reply, null);
c.Flush();
lock (testLock)
{
Assert.IsTrue(Monitor.Wait(testLock, 1000));
}
}
}
}
示例9: TestLargeMessage
public void TestLargeMessage()
{
using (IConnection c = new ConnectionFactory().CreateConnection())
{
int msgSize = 51200;
byte[] msg = new byte[msgSize];
for (int i = 0; i < msgSize; i++)
msg[i] = (byte)'A';
msg[msgSize-1] = (byte)'Z';
using (IAsyncSubscription s = c.SubscribeAsync("foo"))
{
Object testLock = new Object();
s.MessageHandler += (sender, args) =>
{
lock(testLock)
{
Monitor.Pulse(testLock);
}
Assert.IsTrue(compare(msg, args.Message.Data));
};
s.Start();
c.Publish("foo", msg);
c.Flush(1000);
lock(testLock)
{
Monitor.Wait(testLock, 2000);
}
}
}
}
示例10: TestSubDelTaskCountAutoUnsub
public void TestSubDelTaskCountAutoUnsub()
{
var opts = utils.DefaultTestOptions;
opts.SubscriberDeliveryTaskCount = 2;
using (new NATSServer())
{
using (IConnection c = new ConnectionFactory().CreateConnection(opts))
{
long received = 0;
int max = 10;
AutoResetEvent ev = new AutoResetEvent(false);
using (var s = c.SubscribeAsync("foo", (obj, args) =>
{
received++;
if (received > max)
ev.Set();
}))
{
s.AutoUnsubscribe(max);
for (int i = 0; i < max * 2; i++)
{
c.Publish("foo", null);
}
c.Flush();
// event should never fire.
Assert.False(ev.WaitOne(500));
// double check
Assert.True(received == max);
Assert.False(s.IsValid);
}
}
}
}
示例11: TestAsyncSubscribe
public void TestAsyncSubscribe()
{
using (IConnection c = new ConnectionFactory().CreateConnection())
{
using (IAsyncSubscription s = c.SubscribeAsync("foo"))
{
asyncSub = s;
s.MessageHandler += CheckReceivedAndValidHandler;
s.Start();
lock (mu)
{
received = false;
c.Publish("foo", omsg);
c.Flush();
Monitor.Wait(mu, 30000);
}
if (!received)
Assert.Fail("Did not receive message.");
}
}
}
示例12: TestStats
public void TestStats()
{
using (IConnection c = new ConnectionFactory().CreateConnection())
{
byte[] data = Encoding.UTF8.GetBytes("The quick brown fox jumped over the lazy dog");
int iter = 10;
for (int i = 0; i < iter; i++)
{
c.Publish("foo", data);
}
c.Flush(1000);
IStatistics stats = c.Stats;
Assert.AreEqual(iter, stats.OutMsgs);
Assert.AreEqual(iter * data.Length, stats.OutBytes);
c.ResetStats();
// Test both sync and async versions of subscribe.
IAsyncSubscription s1 = c.SubscribeAsync("foo");
s1.MessageHandler += (sender, arg) => { };
s1.Start();
ISyncSubscription s2 = c.SubscribeSync("foo");
for (int i = 0; i < iter; i++)
{
c.Publish("foo", data);
}
c.Flush(1000);
stats = c.Stats;
Assert.AreEqual(2 * iter, stats.InMsgs);
Assert.AreEqual(2 * iter * data.Length, stats.InBytes);
}
}
示例13: TestEncodedSerizationOverrides
public void TestEncodedSerizationOverrides()
{
using (IEncodedConnection c = new ConnectionFactory().CreateEncodedConnection())
{
c.OnDeserialize = deserializeFromXML;
c.OnSerialize = serializeToXML;
Object mu = new Object();
SerializationTestObj origObj = new SerializationTestObj();
origObj.a = 99;
EventHandler<EncodedMessageEventArgs> eh = (sender, args) =>
{
SerializationTestObj so = (SerializationTestObj)args.ReceivedObject;
Assert.IsTrue(so.Equals(origObj));
lock (mu)
{
Monitor.Pulse(mu);
}
};
using (IAsyncSubscription s = c.SubscribeAsync("foo", eh))
{
lock (mu)
{
c.Publish("foo", origObj);
c.Flush();
Monitor.Wait(mu, 1000);
}
}
}
}
示例14: TestEncodedObjectSerization
public void TestEncodedObjectSerization()
{
using (IEncodedConnection c = new ConnectionFactory().CreateEncodedConnection())
{
String myStr = "value";
Object mu = new Object();
EventHandler<EncodedMessageEventArgs> eh = (sender, args) =>
{
Assert.IsTrue(args.ReceivedObject.Equals(myStr));
lock (mu)
{
Monitor.Pulse(mu);
}
};
using (IAsyncSubscription s = c.SubscribeAsync("foo", eh))
{
lock (mu)
{
for (int i = 0; i < 10; i++)
c.Publish("foo", myStr);
c.Flush();
Monitor.Wait(mu, 1000);
}
}
using (IAsyncSubscription s = c.SubscribeAsync("foo", eh))
{
lock (mu)
{
c.Publish("foo", "bar", myStr);
c.Flush();
Monitor.Wait(mu, 1000);
}
}
}
}
示例15: TestEncodedObjectRequestReply
public void TestEncodedObjectRequestReply()
{
using (IEncodedConnection c = new ConnectionFactory().CreateEncodedConnection())
{
Object mu = new Object();
SerializationTestObj origObj = new SerializationTestObj();
EventHandler<EncodedMessageEventArgs> eh = (sender, args) =>
{
SerializationTestObj so = (SerializationTestObj)args.ReceivedObject;
Assert.IsTrue(so.Equals(origObj));
String str = "Received";
c.Publish(args.Reply, str);
c.Flush();
lock (mu)
{
Monitor.Pulse(mu);
}
};
using (IAsyncSubscription s = c.SubscribeAsync("foo", eh))
{
Assert.IsTrue("Received".Equals(c.Request("foo", origObj, 1000)));
Assert.IsTrue("Received".Equals(c.Request("foo", origObj)));
}
}
}