Skip to content

Commit 734af2d

Browse files
author
Emile Joubert
committed
Consumer cancellation event test
1 parent f423adc commit 734af2d

File tree

2 files changed

+53
-25
lines changed

2 files changed

+53
-25
lines changed

projects/client/Unit/src/unit/Fixtures.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,6 @@ public class TimingFixture
7575
{
7676
public static readonly int TimingInterval = 200;
7777
public static readonly int SafetyMargin = 50;
78+
public static readonly int TestTimeout = 5000;
7879
}
7980
}

projects/client/Unit/src/unit/TestConsumerCancelNotify.cs

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -41,52 +41,79 @@
4141
using NUnit.Framework;
4242

4343
using System;
44-
using System.IO;
45-
using System.Text;
46-
using System.Collections;
4744
using System.Threading;
4845

49-
using RabbitMQ.Client;
50-
using RabbitMQ.Client.Impl;
51-
using RabbitMQ.Util;
46+
using RabbitMQ.Client.Events;
47+
5248

5349
namespace RabbitMQ.Client.Unit {
5450
[TestFixture]
55-
public class TestConsumerCancelNotify {
51+
public class TestConsumerCancelNotify : IntegrationFixture {
5652

57-
Object lockObject = new Object();
58-
bool notified = false;
53+
protected readonly Object lockObject = new Object();
54+
protected bool notifiedCallback;
55+
protected bool notifiedEvent;
5956

6057
[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+
}
6968

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);
7481
}
7582
Assert.IsTrue(notified);
7683
}
7784
}
7885

79-
public class CancelNotificationConsumer : QueueingBasicConsumer
86+
private class CancelNotificationConsumer : DefaultBasicConsumer
8087
{
8188
TestConsumerCancelNotify testClass;
89+
private bool EventMode;
8290

83-
public CancelNotificationConsumer(IModel model, TestConsumerCancelNotify tc) : base(model) {
91+
public CancelNotificationConsumer(IModel model, TestConsumerCancelNotify tc, bool EventMode) : base(model) {
8492
this.testClass = tc;
93+
this.EventMode = EventMode;
94+
if (EventMode)
95+
{
96+
ConsumerCancelled += Cancelled;
97+
}
8598
}
8699

87100
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;
90117
Monitor.PulseAll(testClass.lockObject);
91118
}
92119
}

0 commit comments

Comments
 (0)