public void TestOrderedConsumerAsync() { Console.SetOut(new ConsoleWriter(output)); Context.RunInJsServer(c => { // Setup IJetStream js = c.CreateJetStreamContext(); string subject = Subject(222); CreateMemoryStream(c, Stream(222), subject); // Get this in place before any subscriptions are made JetStream.PushMessageManagerFactoryImpl = (conn, so, cc, queueMode, syncMode) => new OrderedTestDropSimulator(conn, so, cc, queueMode, syncMode); // The options will be used in various ways PushSubscribeOptions pso = PushSubscribeOptions.Builder().WithOrdered(true).Build(); // Test queue exception void DummyTestHandler(object sender, MsgHandlerEventArgs args) { } NATSJetStreamClientException e = Assert.Throws <NATSJetStreamClientException>(() => js.PushSubscribeAsync(subject, QUEUE, DummyTestHandler, false, pso)); Assert.Contains(JsSubOrderedNotAllowOnQueues.Id, e.Message); // Setup async subscription CountdownEvent latch = new CountdownEvent(6); InterlockedInt received = new InterlockedInt(); InterlockedLong[] ssFlags = new InterlockedLong[6]; InterlockedLong[] csFlags = new InterlockedLong[6]; void TestHandler(object sender, MsgHandlerEventArgs args) { int i = received.Increment() - 1; ssFlags[i] = new InterlockedLong((long)args.msg.MetaData.StreamSequence); csFlags[i] = new InterlockedLong((long)args.msg.MetaData.ConsumerSequence); latch.Signal(); } js.PushSubscribeAsync(subject, TestHandler, false, pso); Thread.Sleep(1000); // Published messages will be intercepted by the OrderedTestDropSimulator JsPublish(js, subject, 101, 6); // wait for the messages latch.Wait(); // Loop through the messages to make sure I get stream sequence 1 to 6 ulong expectedStreamSeq = 1; while (expectedStreamSeq <= 6) { int idx = (int)expectedStreamSeq - 1; Assert.Equal(expectedStreamSeq, (ulong)ssFlags[idx].Read()); Assert.Equal(ExpectedConSeqNums[idx], (ulong)csFlags[idx].Read()); ++expectedStreamSeq; } }); }
public void TestPushSyncFlowControl() { InterlockedInt fcps = new InterlockedInt(); Action <Options> optionsModifier = opts => { opts.FlowControlProcessedEventHandler = (sender, args) => { fcps.Increment(); }; }; Context.RunInJsServer(new TestServerInfo(TestSeedPorts.AutoPort.Increment()), optionsModifier, c => { // create the stream. CreateDefaultTestStream(c); // Create our JetStream context. IJetStream js = c.CreateJetStreamContext(); byte[] data = new byte[8192]; int MSG_COUNT = 1000; for (int x = 100_000; x < MSG_COUNT + 100_000; x++) { byte[] fill = Encoding.ASCII.GetBytes("" + x); Array.Copy(fill, 0, data, 0, 6); js.Publish(new Msg(SUBJECT, data)); } InterlockedInt count = new InterlockedInt(); HashSet <string> set = new HashSet <string>(); ConsumerConfiguration cc = ConsumerConfiguration.Builder().WithFlowControl(1000).Build(); PushSubscribeOptions pso = PushSubscribeOptions.Builder().WithConfiguration(cc).Build(); IJetStreamPushSyncSubscription ssub = js.PushSubscribeSync(SUBJECT, pso); for (int x = 0; x < MSG_COUNT; x++) { Msg msg = ssub.NextMessage(1000); byte[] fill = new byte[6]; Array.Copy(msg.Data, 0, fill, 0, 6); string id = Encoding.ASCII.GetString(fill); if (set.Add(id)) { count.Increment(); } msg.Ack(); } Assert.Equal(MSG_COUNT, count.Read()); Assert.True(fcps.Read() > 0); }); }
public void TestPushAsyncFlowControl() { InterlockedInt fcps = new InterlockedInt(); Action <Options> optionsModifier = opts => { opts.FlowControlProcessedEventHandler = (sender, args) => { fcps.Increment(); }; }; Context.RunInJsServer(new TestServerInfo(TestSeedPorts.AutoPort.Increment()), optionsModifier, c => { // create the stream. CreateDefaultTestStream(c); // Create our JetStream context. IJetStream js = c.CreateJetStreamContext(); byte[] data = new byte[8192]; int msgCount = 1000; for (int x = 100_000; x < msgCount + 100_000; x++) { byte[] fill = Encoding.ASCII.GetBytes("" + x); Array.Copy(fill, 0, data, 0, 6); js.Publish(new Msg(SUBJECT, data)); } InterlockedInt count = new InterlockedInt(); HashSet <string> set = new HashSet <string>(); CountdownEvent latch = new CountdownEvent(msgCount); // create our message handler, does not ack void Handler(object sender, MsgHandlerEventArgs args) { byte[] fill = new byte[6]; Array.Copy(args.Message.Data, 0, fill, 0, 6); string id = Encoding.ASCII.GetString(fill); if (set.Add(id)) { count.Increment(); } args.Message.Ack(); latch.Signal(); } // subscribe using the handler ConsumerConfiguration cc = ConsumerConfiguration.Builder().WithFlowControl(1000).Build(); PushSubscribeOptions pso = PushSubscribeOptions.Builder().WithConfiguration(cc).Build(); js.PushSubscribeAsync(SUBJECT, Handler, false, pso); // wait for messages to arrive using the countdown latch. latch.Wait(); Assert.Equal(msgCount, count.Read()); Assert.True(fcps.Read() > 0); }); }