Skip to content

Commit 40fc1a0

Browse files
committed
Reduce verbosity of the tests
1 parent 275fa36 commit 40fc1a0

File tree

1 file changed

+17
-30
lines changed

1 file changed

+17
-30
lines changed

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

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -81,20 +81,16 @@ public void TestChannelClosureIsObservableOnSubscription()
8181
[Test]
8282
public void TestSubscriptionAck()
8383
{
84-
Model.BasicQos(0, 1, false);
85-
string q = Model.QueueDeclare();
86-
Subscription sub = new Subscription(Model, q, false);
87-
88-
Model.BasicPublish("", q, null, enc.GetBytes("a message"));
89-
BasicDeliverEventArgs res = sub.Next();
90-
Assert.IsNotNull(res);
91-
sub.Ack();
92-
QueueDeclareOk ok = Model.QueueDeclarePassive(q);
93-
Assert.AreEqual(0, ok.MessageCount);
84+
TestSubscriptionAction((s) => s.Ack());
9485
}
9586

9687
[Test]
9788
public void TestSubscriptionNack()
89+
{
90+
TestSubscriptionAction((s) => s.Nack(false, false));
91+
}
92+
93+
private void TestSubscriptionAction(SubscriptionAction action)
9894
{
9995
Model.BasicQos(0, 1, false);
10096
string q = Model.QueueDeclare();
@@ -103,20 +99,22 @@ public void TestSubscriptionNack()
10399
Model.BasicPublish("", q, null, enc.GetBytes("a message"));
104100
BasicDeliverEventArgs res = sub.Next();
105101
Assert.IsNotNull(res);
106-
sub.Nack(false, false);
102+
action(sub);
107103
QueueDeclareOk ok = Model.QueueDeclarePassive(q);
108104
Assert.AreEqual(0, ok.MessageCount);
109105
}
110106

107+
protected delegate void SubscriptionAction(Subscription s);
108+
111109
protected class SubscriptionDrainer
112110
{
113111
protected Subscription m_subscription;
114-
protected bool m_ack;
112+
private SubscriptionAction PostProcess { get; set; }
115113

116-
public SubscriptionDrainer(Subscription sub, bool ack)
114+
public SubscriptionDrainer(Subscription sub, SubscriptionAction op)
117115
{
118116
m_subscription = sub;
119-
m_ack = ack;
117+
PostProcess = op;
120118
}
121119

122120
public void Drain()
@@ -130,7 +128,7 @@ public void Drain()
130128
if(ea != null)
131129
{
132130
Assert.That(ea, Is.TypeOf(typeof(BasicDeliverEventArgs)));
133-
PostProcess();
131+
this.PostProcess(m_subscription);
134132
} else
135133
{
136134
break;
@@ -146,32 +144,21 @@ public void Drain()
146144
#pragma warning restore
147145

148146
}
149-
150-
protected void PostProcess()
151-
{
152-
if(m_ack)
153-
{
154-
m_subscription.Ack();
155-
} else
156-
{
157-
m_subscription.Nack(false, false);
158-
}
159-
}
160147
}
161148

162149
[Test]
163150
public void TestConcurrentIterationAndAck()
164151
{
165-
TestConcurrentIterationWithDrainer(true);
152+
TestConcurrentIterationWithDrainer((s) => s.Ack());
166153
}
167154

168155
[Test]
169156
public void TestConcurrentIterationAndNack()
170157
{
171-
TestConcurrentIterationWithDrainer(false);
158+
TestConcurrentIterationWithDrainer((s) => s.Nack(false, false));
172159
}
173160

174-
protected void TestConcurrentIterationWithDrainer(bool ack)
161+
protected void TestConcurrentIterationWithDrainer(SubscriptionAction act)
175162
{
176163
IDictionary<string, object> args = new Dictionary<string, object>
177164
{
@@ -185,7 +172,7 @@ protected void TestConcurrentIterationWithDrainer(bool ack)
185172
List<Thread> ts = new List<Thread>();
186173
for (int i = 0; i < 50; i++)
187174
{
188-
SubscriptionDrainer drainer = new SubscriptionDrainer(sub, ack);
175+
SubscriptionDrainer drainer = new SubscriptionDrainer(sub, act);
189176
Thread t = new Thread(drainer.Drain);
190177
ts.Add(t);
191178
t.Start();

0 commit comments

Comments
 (0)