|
| 1 | +// This source code is dual-licensed under the Apache License, version |
| 2 | +// 2.0, and the Mozilla Public License, version 1.1. |
| 3 | +// |
| 4 | +// The APL v2.0: |
| 5 | +// |
| 6 | +//--------------------------------------------------------------------------- |
| 7 | +// Copyright (C) 2007-2015 Pivotal Software, Inc. |
| 8 | +// |
| 9 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | +// you may not use this file except in compliance with the License. |
| 11 | +// You may obtain a copy of the License at |
| 12 | +// |
| 13 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | +// |
| 15 | +// Unless required by applicable law or agreed to in writing, software |
| 16 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | +// See the License for the specific language governing permissions and |
| 19 | +// limitations under the License. |
| 20 | +//--------------------------------------------------------------------------- |
| 21 | +// |
| 22 | +// The MPL v1.1: |
| 23 | +// |
| 24 | +//--------------------------------------------------------------------------- |
| 25 | +// The contents of this file are subject to the Mozilla Public License |
| 26 | +// Version 1.1 (the "License"); you may not use this file except in |
| 27 | +// compliance with the License. You may obtain a copy of the License |
| 28 | +// at http://www.mozilla.org/MPL/ |
| 29 | +// |
| 30 | +// Software distributed under the License is distributed on an "AS IS" |
| 31 | +// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See |
| 32 | +// the License for the specific language governing rights and |
| 33 | +// limitations under the License. |
| 34 | +// |
| 35 | +// The Original Code is RabbitMQ. |
| 36 | +// |
| 37 | +// The Initial Developer of the Original Code is GoPivotal, Inc. |
| 38 | +// Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. |
| 39 | +//--------------------------------------------------------------------------- |
| 40 | + |
| 41 | +using NUnit.Framework; |
| 42 | +using System; |
| 43 | +using System.Threading; |
| 44 | +using RabbitMQ.Client; |
| 45 | + |
| 46 | +namespace RabbitMQ.Client.Unit |
| 47 | +{ |
| 48 | + [TestFixture] |
| 49 | + public class TestPublisherConfirms : IntegrationFixture |
| 50 | + { |
| 51 | + [Test] |
| 52 | + public void TestWaitForConfirmsWithoutTimeout() |
| 53 | + { |
| 54 | + TestWaitForConfirms(200, (ch) => |
| 55 | + { |
| 56 | + Assert.IsTrue(ch.WaitForConfirms()); |
| 57 | + });; |
| 58 | + } |
| 59 | + |
| 60 | + [Test] |
| 61 | + public void TestWaitForConfirmsWithTimeout() |
| 62 | + { |
| 63 | + TestWaitForConfirms(200, (ch) => |
| 64 | + { |
| 65 | + Assert.IsTrue(ch.WaitForConfirms(TimeSpan.FromSeconds(4))); |
| 66 | + }); ; |
| 67 | + } |
| 68 | + |
| 69 | + [Test] |
| 70 | + public void TestWaitForConfirmsWithEvents() |
| 71 | + { |
| 72 | + var ch = Conn.CreateModel(); |
| 73 | + ch.ConfirmSelect(); |
| 74 | + |
| 75 | + var q = ch.QueueDeclare().QueueName; |
| 76 | + var n = 200; |
| 77 | + // number of event handler invocations |
| 78 | + var c = 0; |
| 79 | + |
| 80 | + ch.BasicAcks += (_, args) => |
| 81 | + { |
| 82 | + Interlocked.Increment(ref c); |
| 83 | + }; |
| 84 | + try |
| 85 | + { |
| 86 | + for (int i = 0; i < n; i++) |
| 87 | + { |
| 88 | + ch.BasicPublish("", q, null, encoding.GetBytes("msg")); |
| 89 | + } |
| 90 | + Thread.Sleep(TimeSpan.FromSeconds(1)); |
| 91 | + ch.WaitForConfirms(TimeSpan.FromSeconds(5)); |
| 92 | + |
| 93 | + // Note: number of event invocations is not guaranteed |
| 94 | + // to be equal to N because acks can be batched, |
| 95 | + // so we primarily care about event handlers being invoked |
| 96 | + // in this test |
| 97 | + Assert.IsTrue(c > 20); |
| 98 | + } |
| 99 | + finally |
| 100 | + { |
| 101 | + ch.QueueDelete(q); |
| 102 | + ch.Close(); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + protected void TestWaitForConfirms(int numberOfMessagesToPublish, Action<IModel> fn) |
| 107 | + { |
| 108 | + var ch = Conn.CreateModel(); |
| 109 | + ch.ConfirmSelect(); |
| 110 | + |
| 111 | + var q = ch.QueueDeclare().QueueName; |
| 112 | + |
| 113 | + for (int i = 0; i < numberOfMessagesToPublish; i++) |
| 114 | + { |
| 115 | + ch.BasicPublish("", q, null, encoding.GetBytes("msg")); |
| 116 | + } |
| 117 | + try |
| 118 | + { |
| 119 | + fn(ch); |
| 120 | + } finally |
| 121 | + { |
| 122 | + ch.QueueDelete(q); |
| 123 | + ch.Close(); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments