public void TestMethod_ReleaseMessage() { string testName = "ReleaseMessage"; const int nMsgs = 20; Connection connection = new Connection(testTarget.Address); Session session = new Session(connection); SenderLink sender = new SenderLink(session, "sender-" + testName, testTarget.Path); for (int i = 0; i < nMsgs; ++i) { Message message = new Message(); message.Properties = new Properties() { MessageId = "msg" + i }; message.ApplicationProperties = new ApplicationProperties(); message.ApplicationProperties["sn"] = i; sender.Send(message, null, null); } ReceiverLink receiver = new ReceiverLink(session, "receiver-" + testName, testTarget.Path); for (int i = 0; i < nMsgs; ++i) { Message message = receiver.Receive(); Trace.WriteLine(TraceLevel.Verbose, "receive: {0}", message.Properties.MessageId); if (i % 2 == 0) { receiver.Accept(message); } else { receiver.Release(message); } } receiver.Close(); ReceiverLink receiver2 = new ReceiverLink(session, "receiver2-" + testName, testTarget.Path); for (int i = 0; i < nMsgs / 2; ++i) { Message message = receiver2.Receive(); Trace.WriteLine(TraceLevel.Verbose, "receive: {0}", message.Properties.MessageId); receiver2.Accept(message); } receiver2.Close(); sender.Close(); session.Close(); connection.Close(); }
public void ContainerHostSourceLinkEndpointTest() { string name = MethodInfo.GetCurrentMethod().Name; int count = 100; Queue<Message> messages = new Queue<Message>(); for (int i = 0; i < count; i++) { messages.Enqueue(new Message("test") { Properties = new Properties() { MessageId = name + i } }); } var source = new TestMessageSource(messages); this.host.RegisterLinkProcessor(new TestLinkProcessor(link => new SourceLinkEndpoint(source, link))); var connection = new Connection(Address); var session = new Session(connection); var receiver = new ReceiverLink(session, "receiver0", name); int released = 0; int rejected = 0; for (int i = 1; i <= count; i++) { Message message = receiver.Receive(); if (i % 5 == 0) { receiver.Reject(message); rejected++; } else if (i % 17 == 0) { receiver.Release(message); released++; } else { receiver.Accept(message); } } receiver.Close(); session.Close(); connection.Close(); Assert.AreEqual(released, messages.Count); Assert.AreEqual(rejected, source.DeadletterMessage.Count); }
public void TransactedRetiringAndPosting() { string testName = "TransactedRetiringAndPosting"; int nMsgs = 10; Connection connection = new Connection(this.address); Session session = new Session(connection); SenderLink sender = new SenderLink(session, "sender-" + testName, "q1"); for (int i = 0; i < nMsgs; i++) { Message message = new Message("test"); message.Properties = new Properties() { MessageId = "msg" + i, GroupId = testName }; sender.Send(message); } ReceiverLink receiver = new ReceiverLink(session, "receiver-" + testName, "q1"); receiver.SetCredit(2, false); Message message1 = receiver.Receive(); Message message2 = receiver.Receive(); // ack message1 and send a new message in a txn using (var ts = new TransactionScope()) { receiver.Accept(message1); Message message = new Message("test"); message.Properties = new Properties() { MessageId = "msg" + nMsgs, GroupId = testName }; sender.Send(message); ts.Complete(); } // ack message2 and send a new message in a txn but abort the txn using (var ts = new TransactionScope()) { receiver.Accept(message2); Message message = new Message("test"); message.Properties = new Properties() { MessageId = "msg" + (nMsgs + 1), GroupId = testName }; sender.Send(message1); } receiver.Release(message2); // receive all messages. should see the effect of the first txn receiver.SetCredit(nMsgs, false); for (int i = 1; i <= nMsgs; i++) { Message message = receiver.Receive(); Trace.WriteLine(TraceLevel.Information, "receive: {0}", message.Properties.MessageId); receiver.Accept(message); Assert.AreEqual("msg" + i, message.Properties.MessageId); } connection.Close(); }
public static void Main(string[] args) { Trace.TraceLevel = TraceLevel.Frame; Trace.TraceListener = (f, a) => { var t = DateTime.Now.ToString("[hh:ss.fff]") + " " + string.Format(f, a); Console.WriteLine(t); }; connection = new Connection(amqpAddress, null, new Open() { ContainerId = Guid.NewGuid().ToString(), ChannelMax = 64, MaxFrameSize = 200, }, null); connection.Closed = OnClosed; session = new Session(connection); session.Closed = OnClosed; var linkName = Guid.NewGuid().ToString(); senderLink = new SenderLink(session, linkName, new Attach() { Target = new Target() { Address = "TestQueue1", }, //RcvSettleMode = ReceiverSettleMode.Second, //SndSettleMode = SenderSettleMode.Settled, }, null); senderLink.Closed = OnClosed; for (int i = 0; i < 10; i++) { senderLink.Send(CreateMessage(), 5000); } senderLink.Close(); linkName = Guid.NewGuid().ToString(); receiverLink = new ReceiverLink(session, linkName, "TestQueue1"); receiverLink.Closed = OnClosed; receiverLink.SetCredit(1); var message = receiverLink.Receive(20000); int receiveCount = 0; while(message != null) { receiveCount++; //Console.WriteLine(message.Body.GetType()); Console.WriteLine(message.BodySection.GetType()); Console.WriteLine("Receive #{0}. Message = \"{1}\"", receiveCount.ToString(), Encoding.UTF8.GetString(message.GetBody<byte[]>())); if (receiveCount % 7 == 0) receiverLink.Release(message); else if (receiveCount % 4 == 0) receiverLink.Reject(message); else receiverLink.Accept(message); Thread.Sleep(10000); message = receiverLink.Receive(20000); } receiverLink.Close(); session.Close(); connection.Close(); }
public void TransactedRetiring() { string testName = "TransactedRetiring"; int nMsgs = 10; Connection connection = new Connection(this.address); Session session = new Session(connection); SenderLink sender = new SenderLink(session, "sender-" + testName, "q1"); // send one extra for validation for (int i = 0; i < nMsgs + 1; i++) { Message message = new Message("test"); message.Properties = new Properties() { MessageId = "msg" + i, GroupId = testName }; sender.Send(message); } ReceiverLink receiver = new ReceiverLink(session, "receiver-" + testName, "q1"); Message[] messages = new Message[nMsgs]; for (int i = 0; i < nMsgs; i++) { messages[i] = receiver.Receive(); Trace.WriteLine(TraceLevel.Information, "receive: {0}", messages[i].Properties.MessageId); } // commit harf using (var ts = new TransactionScope()) { for (int i = 0; i < nMsgs / 2; i++) { receiver.Accept(messages[i]); } ts.Complete(); } // rollback using (var ts = new TransactionScope()) { for (int i = nMsgs / 2; i < nMsgs; i++) { receiver.Accept(messages[i]); } } // after rollback, messages should be still acquired { Message message = receiver.Receive(); Assert.AreEqual("msg" + nMsgs, message.Properties.MessageId); receiver.Release(message); } // commit using (var ts = new TransactionScope()) { for (int i = nMsgs / 2; i < nMsgs; i++) { receiver.Accept(messages[i]); } ts.Complete(); } // only the last message is left { Message message = receiver.Receive(); Assert.AreEqual("msg" + nMsgs, message.Properties.MessageId); receiver.Accept(message); } // at this point, the queue should have zero messages. // If there are messages, it is a bug in the broker. connection.Close(); }