|
41 | 41 | using NUnit.Framework;
|
42 | 42 |
|
43 | 43 | using System;
|
44 |
| -using System.IO; |
45 |
| -using System.Text; |
46 |
| -using System.Collections; |
47 | 44 | using System.Threading;
|
48 | 45 |
|
49 |
| -using RabbitMQ.Client; |
50 |
| -using RabbitMQ.Client.Impl; |
51 |
| -using RabbitMQ.Util; |
| 46 | +using RabbitMQ.Client.Events; |
| 47 | + |
52 | 48 |
|
53 | 49 | namespace RabbitMQ.Client.Unit {
|
54 | 50 | [TestFixture]
|
55 |
| - public class TestConsumerCancelNotify { |
| 51 | + public class TestConsumerCancelNotify : IntegrationFixture { |
56 | 52 |
|
57 |
| - Object lockObject = new Object(); |
58 |
| - bool notified = false; |
| 53 | + protected readonly Object lockObject = new Object(); |
| 54 | + protected bool notifiedCallback; |
| 55 | + protected bool notifiedEvent; |
59 | 56 |
|
60 | 57 | [Test]
|
61 |
| - public void TestConsumerCancelNotification() { |
62 |
| - string queue = "queue_consumer_notify"; |
63 |
| - ConnectionFactory connFactory = new ConnectionFactory(); |
64 |
| - IConnection conn = connFactory.CreateConnection(); |
65 |
| - IModel chan = conn.CreateModel(); |
66 |
| - chan.QueueDeclare(queue, false, true, false, null); |
67 |
| - IBasicConsumer consumer = new CancelNotificationConsumer(chan, this); |
68 |
| - chan.BasicConsume(queue, false, consumer); |
| 58 | + public void TestConsumerCancelNotification() |
| 59 | + { |
| 60 | + TestConsumerCancel("queue_consumer_cancel_notify", false, ref notifiedCallback); |
| 61 | + } |
| 62 | + |
| 63 | + [Test] |
| 64 | + public void TestConsumerCancelEvent() |
| 65 | + { |
| 66 | + TestConsumerCancel("queue_consumer_cancel_event", true, ref notifiedEvent); |
| 67 | + } |
69 | 68 |
|
70 |
| - chan.QueueDelete(queue); |
71 |
| - lock (lockObject) { |
72 |
| - if (!notified) { |
73 |
| - Monitor.Wait(lockObject); |
| 69 | + public void TestConsumerCancel(string queue, bool EventMode, ref bool notified) |
| 70 | + { |
| 71 | + Model.QueueDeclare(queue, false, true, false, null); |
| 72 | + IBasicConsumer consumer = new CancelNotificationConsumer(Model, this, EventMode); |
| 73 | + Model.BasicConsume(queue, false, consumer); |
| 74 | + |
| 75 | + Model.QueueDelete(queue); |
| 76 | + lock (lockObject) |
| 77 | + { |
| 78 | + if (!notified) |
| 79 | + { |
| 80 | + Monitor.Wait(lockObject, TimingFixture.TestTimeout); |
74 | 81 | }
|
75 | 82 | Assert.IsTrue(notified);
|
76 | 83 | }
|
77 | 84 | }
|
78 | 85 |
|
79 |
| - public class CancelNotificationConsumer : QueueingBasicConsumer |
| 86 | + private class CancelNotificationConsumer : DefaultBasicConsumer |
80 | 87 | {
|
81 | 88 | TestConsumerCancelNotify testClass;
|
| 89 | + private bool EventMode; |
82 | 90 |
|
83 |
| - public CancelNotificationConsumer(IModel model, TestConsumerCancelNotify tc) : base(model) { |
| 91 | + public CancelNotificationConsumer(IModel model, TestConsumerCancelNotify tc, bool EventMode) : base(model) { |
84 | 92 | this.testClass = tc;
|
| 93 | + this.EventMode = EventMode; |
| 94 | + if (EventMode) |
| 95 | + { |
| 96 | + ConsumerCancelled += Cancelled; |
| 97 | + } |
85 | 98 | }
|
86 | 99 |
|
87 | 100 | public override void HandleBasicCancel(string consumerTag) {
|
88 |
| - lock (testClass.lockObject) { |
89 |
| - testClass.notified = true; |
| 101 | + if (!EventMode) |
| 102 | + { |
| 103 | + lock (testClass.lockObject) |
| 104 | + { |
| 105 | + testClass.notifiedCallback = true; |
| 106 | + Monitor.PulseAll(testClass.lockObject); |
| 107 | + } |
| 108 | + } |
| 109 | + base.HandleBasicCancel(consumerTag); |
| 110 | + } |
| 111 | + |
| 112 | + private void Cancelled(object sender, ConsumerEventArgs arg) |
| 113 | + { |
| 114 | + lock (testClass.lockObject) |
| 115 | + { |
| 116 | + testClass.notifiedEvent = true; |
90 | 117 | Monitor.PulseAll(testClass.lockObject);
|
91 | 118 | }
|
92 | 119 | }
|
|
0 commit comments