Permalink
Browse files

Using thread synchronisation methods instead of Thread.Sleep for inte…

…gration tests.
  • Loading branch information...
mikehadlow committed Jun 19, 2012
1 parent 6993e0c commit b384bb425e2fa896d8521b6be7154f55bbf6b1a3
@@ -43,11 +43,13 @@ public void Should_be_able_to_subscribe()
[Test]
public void Should_be_able_to_publish_and_subscribe()
{
var autoResetEvent = new AutoResetEvent(false);
MyMessage receivedMessage = null;
bus.Subscribe<MyMessage>("subscriberId", message =>
{
Console.WriteLine("Got message '{0}'", message.Text);
receivedMessage = message;
autoResetEvent.Set();
});
var publishedMessage = new MyMessage { Text = "Hello There Fido!" };
@@ -57,26 +59,30 @@ public void Should_be_able_to_publish_and_subscribe()
}
// give the task background thread time to process the message.
Thread.Sleep(100);
autoResetEvent.WaitOne();
receivedMessage.Text.ShouldEqual(publishedMessage.Text);
}
[Test]
public void Should_load_share_between_multiple_consumers()
{
var countdownEvent = new CountdownEvent(2);
MyMessage receivedMessage1 = null;
bus.Subscribe<MyMessage>("subscriberId", message =>
{
Console.WriteLine("Handler A got '{0}'", message.Text);
receivedMessage1 = message;
countdownEvent.Signal();
});
MyMessage receivedMessage2 = null;
bus.Subscribe<MyMessage>("subscriberId", message =>
{
Console.WriteLine("Handler B got '{0}'", message.Text);
receivedMessage2 = message;
countdownEvent.Signal();
});
var publishedMessage1 = new MyMessage { Text = "Hello There From The First!" };
@@ -88,7 +94,7 @@ public void Should_load_share_between_multiple_consumers()
}
// give the task background thread time to process the message.
Thread.Sleep(100);
countdownEvent.Wait();
receivedMessage1.Text.ShouldEqual(publishedMessage1.Text);
receivedMessage2.Text.ShouldEqual(publishedMessage2.Text);
@@ -97,18 +103,22 @@ public void Should_load_share_between_multiple_consumers()
[Test]
public void Should_work_with_topics()
{
var countdownEvent = new CountdownEvent(2);
MyMessage receivedMessage1 = null;
bus.Subscribe<MyMessage>("barSubscriber", "*.bar", message =>
{
Console.WriteLine("*.bar got '{0}'", message.Text);
receivedMessage1 = message;
countdownEvent.Signal();
});
MyMessage receivedMessage2 = null;
bus.Subscribe<MyMessage>("fooSubscriber", "foo.*", message =>
{
Console.WriteLine("foo.* got '{0}'", message.Text);
receivedMessage2 = message;
countdownEvent.Signal();
});
var fooNinja = new MyMessage { Text = "I should go to foo.ninja" };
@@ -120,7 +130,7 @@ public void Should_work_with_topics()
}
// give the task background thread time to process the message.
Thread.Sleep(100);
countdownEvent.Wait();
receivedMessage1.Text.ShouldEqual(niceBar.Text);
receivedMessage2.Text.ShouldEqual(fooNinja.Text);
@@ -28,6 +28,7 @@ public void TearDown()
public void Should_be_able_to_do_basic_request_response()
{
const string expectedResult = "Sending back 'Ninja!!'";
var autoResetEvent = new AutoResetEvent(false);
bus.Respond<TestRequestMessage, TestResponseMessage>(request =>
{
@@ -45,18 +46,24 @@ public void Should_be_able_to_do_basic_request_response()
var request = new TestRequestMessage {Text = "Ninja!!"};
channel.Request<TestRequestMessage, TestResponseMessage>(
request,
response => actualResult = response.Text);
response =>
{
actualResult = response.Text;
autoResetEvent.Set();
});
}
// give the bus a chance to deliver the message
Thread.Sleep(100);
autoResetEvent.WaitOne();
actualResult.ShouldEqual(expectedResult);
}
[Test]
public void Should_request_closures_work()
{
var countdownEvent = new CountdownEvent(3);
bus.Respond<TestRequestMessage, TestResponseMessage>(request =>
{
var response = new TestResponseMessage
@@ -82,12 +89,13 @@ public void Should_request_closures_work()
{
results[index] = response.Text;
Console.WriteLine("Got response '{0}' on index: {1}", response.Text, index);
countdownEvent.Signal();
});
}
}
// give the bus a chance to deliver the message
Thread.Sleep(100);
countdownEvent.Wait();
results[0].ShouldEqual("one--one");
results[1].ShouldEqual("two--two");
@@ -28,10 +28,15 @@ public void TearDown()
[Test, Explicit("Needs a Rabbit instance on localhost to work")]
public void Should_be_able_to_subscribe()
{
bus.Subscribe<MyMessage>("test", message => Console.WriteLine(message.Text));
var autoResetEvent = new AutoResetEvent(false);
bus.Subscribe<MyMessage>("test", message =>
{
Console.WriteLine(message.Text);
autoResetEvent.Set();
});
// allow time for messages to be consumed
Thread.Sleep(500);
autoResetEvent.WaitOne(1000);
Console.WriteLine("Stopped consuming");
}
@@ -53,11 +58,16 @@ public void Should_be_able_to_publish()
[Test, Explicit("Needs a Rabbit instance on localhost to work")]
public void Should_also_send_messages_to_second_subscriber()
{
var autoResetEvent = new AutoResetEvent(false);
var messageQueue2 = RabbitHutch.CreateBus("host=localhost");
messageQueue2.Subscribe<MyMessage>("test2", msg => Console.WriteLine(msg.Text));
messageQueue2.Subscribe<MyMessage>("test2", msg =>
{
Console.WriteLine(msg.Text);
autoResetEvent.Set();
});
// allow time for messages to be consumed
Thread.Sleep(500);
autoResetEvent.WaitOne(500);
Console.WriteLine("Stopped consuming");
}
@@ -67,10 +77,28 @@ public void Should_also_send_messages_to_second_subscriber()
[Test, Explicit("Needs a Rabbit instance on localhost to work")]
public void Should_two_subscriptions_from_the_same_app_should_also_both_get_all_messages()
{
bus.Subscribe<MyMessage>("test_a", msg => Console.WriteLine(msg.Text));
bus.Subscribe<MyOtherMessage>("test_b", msg => Console.WriteLine(msg.Text));
bus.Subscribe<MyMessage>("test_c", msg => Console.WriteLine(msg.Text));
bus.Subscribe<MyOtherMessage>("test_d", msg => Console.WriteLine(msg.Text));
var countdownEvent = new CountdownEvent(8);
bus.Subscribe<MyMessage>("test_a", msg =>
{
Console.WriteLine(msg.Text);
countdownEvent.Signal();
});
bus.Subscribe<MyOtherMessage>("test_b", msg =>
{
Console.WriteLine(msg.Text);
countdownEvent.Signal();
});
bus.Subscribe<MyMessage>("test_c", msg =>
{
Console.WriteLine(msg.Text);
countdownEvent.Signal();
});
bus.Subscribe<MyOtherMessage>("test_d", msg =>
{
Console.WriteLine(msg.Text);
countdownEvent.Signal();
});
using (var publishChannel = bus.OpenPublishChannel())
{
@@ -82,7 +110,7 @@ public void Should_two_subscriptions_from_the_same_app_should_also_both_get_all_
}
// allow time for messages to be consumed
Thread.Sleep(1000);
countdownEvent.Wait(1000);
Console.WriteLine("Stopped consuming");
}
@@ -93,32 +121,39 @@ public void Should_two_subscriptions_from_the_same_app_should_also_both_get_all_
[Test, Explicit("Needs a Rabbit instance on localhost to work")]
public void Long_running_subscriber_should_survive_a_rabbit_restart()
{
var autoResetEvent = new AutoResetEvent(false);
bus.Subscribe<MyMessage>("test", message =>
{
Console.Out.WriteLine("Restart RabbitMQ now.");
Thread.Sleep(5000);
Console.WriteLine(message.Text);
new Timer(x =>
{
Console.WriteLine(message.Text);
autoResetEvent.Set();
}, null, 5000, Timeout.Infinite);
});
// allow time for messages to be consumed
Thread.Sleep(7000);
autoResetEvent.WaitOne(7000);
Console.WriteLine("Stopped consuming");
}
[Test, Explicit("Needs a Rabbit instance on localhost to work")]
public void Should_subscribe_OK_before_connection_to_broker_is_complete()
{
var autoResetEvent = new AutoResetEvent(false);
var testLocalBus = RabbitHutch.CreateBus("host=localhost");
testLocalBus.Subscribe<MyMessage>("test", message =>
{
Console.Out.WriteLine("message.Text = {0}", message.Text);
autoResetEvent.Set();
});
Console.WriteLine("--- subscribed ---");
// allow time for bus to connect
Thread.Sleep(1000);
autoResetEvent.WaitOne(1000);
testLocalBus.Dispose();
}
}
@@ -15,7 +15,6 @@ public class PublishSubscribeWithTopicsTests
public void SetUp()
{
bus = RabbitHutch.CreateBus("host=localhost");
while (!bus.IsConnected) Thread.Sleep(10);
}
[TearDown]
@@ -43,19 +42,38 @@ public void Publish_some_messages_with_topics()
[Test, Explicit("Needs a Rabbit instance on localhost to work")]
public void Subscribe_to_messages_with_topics()
{
bus.Subscribe<MyMessage>("id1", "X.*", msg => Console.WriteLine("I Get X: {0}", msg.Text));
bus.Subscribe<MyMessage>("id2", "*.A", msg => Console.WriteLine("I Get A: {0}", msg.Text));
bus.Subscribe<MyMessage>("id3", msg => Console.WriteLine("I Get All: {0}", msg.Text));
var countdownEvent = new CountdownEvent(7);
Thread.Sleep(500);
bus.Subscribe<MyMessage>("id1", "X.*", msg =>
{
Console.WriteLine("I Get X: {0}", msg.Text);
countdownEvent.Signal();
});
bus.Subscribe<MyMessage>("id2", "*.A", msg =>
{
Console.WriteLine("I Get A: {0}", msg.Text);
countdownEvent.Signal();
});
bus.Subscribe<MyMessage>("id3", msg =>
{
Console.WriteLine("I Get All: {0}", msg.Text);
countdownEvent.Signal();
});
countdownEvent.Wait(1000);
}
[Test, Explicit("Needs a Rabbit instance on localhost to work")]
public void Should_subscribe_to_multiple_topic_strings()
{
bus.Subscribe<MyMessage>("id4", new[]{"Y.*", "*.B"}, msg => Console.WriteLine("I Get Y or B: {0}", msg.Text));
var countdownEvent = new CountdownEvent(7);
bus.Subscribe<MyMessage>("id4", new[]{"Y.*", "*.B"}, msg =>
{
Console.WriteLine("I Get Y or B: {0}", msg.Text);
countdownEvent.Signal();
});
Thread.Sleep(500);
countdownEvent.Wait(500);
}
}
}
Oops, something went wrong.

0 comments on commit b384bb4

Please sign in to comment.