Skip to content

Commit b200208

Browse files
committed
Make test non-async but still threaded
1 parent 6989f9f commit b200208

File tree

1 file changed

+27
-33
lines changed

1 file changed

+27
-33
lines changed

projects/Unit/TestFloodPublishing.cs

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ public class TestFloodPublishing
5353
private readonly byte[] _body = new byte[2048];
5454
private readonly TimeSpan _tenSeconds = TimeSpan.FromSeconds(10);
5555

56-
/*
5756
[Test]
5857
public void TestUnthrottledFloodPublishing()
5958
{
@@ -84,8 +83,6 @@ public void TestUnthrottledFloodPublishing()
8483
{
8584
now = DateTime.Now;
8685
shouldStop = DateTime.Now > stopTime;
87-
TestContext.Out.WriteLine("@@@@@@@@ NUNIT Checking now {0} stopTime {1} shouldStop {2}", now, stopTime, shouldStop);
88-
Console.Error.WriteLine("@@@@@@@@ STDERR Checking now {0} stopTime {1} shouldStop {2}", now, stopTime, shouldStop);
8986
if (shouldStop)
9087
{
9188
break;
@@ -97,16 +94,15 @@ public void TestUnthrottledFloodPublishing()
9794
}
9895
}
9996
}
100-
*/
10197

10298
[Test]
103-
public async Task TestMultithreadFloodPublishing()
99+
public void TestMultithreadFloodPublishing()
104100
{
105-
string message = "test message";
106-
int threadCount = 1;
107-
int publishCount = 100;
108-
int receivedCount = 0;
101+
string testName = TestContext.CurrentContext.Test.FullName;
102+
string message = string.Format("Hello from test {0}", testName);
109103
byte[] sendBody = Encoding.UTF8.GetBytes(message);
104+
int publishCount = 4096;
105+
int receivedCount = 0;
110106

111107
var cf = new ConnectionFactory()
112108
{
@@ -116,39 +112,37 @@ public async Task TestMultithreadFloodPublishing()
116112

117113
using (IConnection c = cf.CreateConnection())
118114
{
115+
string queueName = null;
119116
using (IModel m = c.CreateModel())
120117
{
121118
QueueDeclareOk q = m.QueueDeclare();
122-
IBasicProperties bp = m.CreateBasicProperties();
123-
124-
var consumer = new EventingBasicConsumer(m);
125-
var tcs = new TaskCompletionSource<bool>();
126-
consumer.Received += (o, a) =>
127-
{
128-
var receivedMessage = Encoding.UTF8.GetString(a.Body.ToArray());
129-
Assert.AreEqual(message, receivedMessage);
130-
131-
var result = Interlocked.Increment(ref receivedCount);
132-
if (result == threadCount * publishCount)
133-
{
134-
tcs.SetResult(true);
135-
}
136-
};
137-
138-
string tag = m.BasicConsume(q.QueueName, true, consumer);
139-
var cts = new CancellationTokenSource(_tenSeconds);
119+
queueName = q.QueueName;
120+
}
140121

141-
using (var timeoutRegistration = cts.Token.Register(() => tcs.SetCanceled()))
122+
Task pub = Task.Run(() =>
123+
{
124+
using (IModel m = c.CreateModel())
142125
{
126+
IBasicProperties bp = m.CreateBasicProperties();
143127
for (int i = 0; i < publishCount; i++)
144128
{
145-
m.BasicPublish(string.Empty, q.QueueName, bp, sendBody);
129+
m.BasicPublish(string.Empty, queueName, bp, sendBody);
146130
}
147-
bool allMessagesReceived = await tcs.Task;
148-
Assert.IsTrue(allMessagesReceived);
149131
}
150-
m.BasicCancel(tag);
151-
Assert.AreEqual(threadCount * publishCount, receivedCount);
132+
});
133+
134+
using (IModel consumerModel = c.CreateModel())
135+
{
136+
var consumer = new EventingBasicConsumer(consumerModel);
137+
consumer.Received += (o, a) =>
138+
{
139+
string receivedMessage = Encoding.UTF8.GetString(a.Body.ToArray());
140+
Assert.AreEqual(message, receivedMessage);
141+
Interlocked.Increment(ref receivedCount);
142+
};
143+
consumerModel.BasicConsume(queueName, true, consumer);
144+
Assert.IsTrue(pub.Wait(_tenSeconds));
145+
Assert.AreEqual(publishCount, receivedCount);
152146
}
153147
}
154148
}

0 commit comments

Comments
 (0)