Skip to content

Commit

Permalink
Expose way to clear history from MessageHistory
Browse files Browse the repository at this point in the history
* We don't necessarily want to always call ClearAll when using
  MessageHistory since it also removes the MessageTrackListener from
  each of the IMessagingHubs.
* Expose ClearHistory (previously a private method called clearData)
  • Loading branch information
smerrell committed Feb 10, 2014
1 parent 542d71a commit 1543854
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 5 deletions.
Expand Up @@ -207,6 +207,28 @@ public void coordinate_message_history_via_remote_service()
}
}

[Test]
public void coordinate_message_history_via_remote_service_and_clear_data_does_not_remove_listeners()
{

using (var runner = RemoteServiceRunner.For<SampleBootstrapper>())
{
runner.WaitForServiceToStart<SampleService.SampleService>();
runner.WaitForServiceToStart<SampleService.RemoteService>();

MessageHistory.StartListening(runner);
MessageHistory.ClearHistory();

var foo = new Foo();

EventAggregator.SentMessage(foo);


EventAggregator.Messaging.WaitForMessage<AllMessagesComplete>(() => runner.SendRemotely(foo))
.ShouldNotBeNull();
}
}

[Test]
public void spin_up_the_remote_service_for_the_sample_and_send_messages_back_and_forth()
{
Expand Down
Expand Up @@ -164,6 +164,28 @@ public void sends_the_all_clear_message_when_it_gets_everything()
MessageHistory.Record(MessageTrack.ForReceived(foo3));
assertAllCompleteMessage();
}

[Test]
public void clear_history_removes_all()
{
var foo1 = new Foo();
var foo2 = new Foo();
var foo3 = new Foo();

MessageHistory.Record(MessageTrack.ForReceived(foo1));
MessageHistory.Record(MessageTrack.ForReceived(foo2));
MessageHistory.Record(MessageTrack.ForReceived(foo3));

MessageHistory.Record(MessageTrack.ForSent(foo1));
MessageHistory.Record(MessageTrack.ForSent(foo2));
MessageHistory.Record(MessageTrack.ForSent(foo3));

MessageHistory.ClearHistory();

MessageHistory.Outstanding().Any().ShouldBeFalse();
MessageHistory.Received().Any().ShouldBeFalse();
MessageHistory.All().Any().ShouldBeFalse();
}
}

public class Foo
Expand Down
Expand Up @@ -207,6 +207,28 @@ public void coordinate_message_history_via_remote_service()
}
}

[Test]
public void coordinate_message_history_via_remote_service_and_clear_data_does_not_remove_listeners()
{

using (var runner = RemoteServiceRunner.For<SampleBootstrapper>())
{
runner.WaitForServiceToStart<SampleService.SampleService>();
runner.WaitForServiceToStart<SampleService.RemoteService>();

MessageHistory.StartListening(runner);
MessageHistory.ClearHistory();

var foo = new Foo();

EventAggregator.SentMessage(foo);


EventAggregator.Messaging.WaitForMessage<AllMessagesComplete>(() => runner.SendRemotely(foo))
.ShouldNotBeNull();
}
}

[Test]
public void spin_up_the_remote_service_for_the_sample_and_send_messages_back_and_forth()
{
Expand Down
8 changes: 3 additions & 5 deletions src/Bottles/Services/Messaging/Tracking/MessageHistory.cs
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using Bottles.Services.Remote;
Expand All @@ -23,7 +22,6 @@ public static void StartListening(params RemoteServiceRunner[] runners)
{
ClearAll();

_hubs.Clear();
_hubs.AddRange(runners.Select(x => x.Messaging));
_hubs.Add(EventAggregator.Messaging);
_listener = new MessageTrackListener();
Expand All @@ -33,7 +31,7 @@ public static void StartListening(params RemoteServiceRunner[] runners)

public static void ClearAll()
{
clearData();
ClearHistory();

if (_listener != null)
{
Expand All @@ -44,7 +42,7 @@ public static void ClearAll()
_hubs.Clear();
}

private static void clearData()
public static void ClearHistory()
{
_lock.Write(() => {
_sent.Clear();
Expand Down Expand Up @@ -108,7 +106,7 @@ public void Receive(MessageTrack message)

public static bool WaitForWorkToFinish(Action action, int timeoutMilliseconds = 5000)
{
clearData();
ClearHistory();
action();
return Wait.Until(() => !Outstanding().Any() && All().Any(), timeoutInMilliseconds: timeoutMilliseconds);
}
Expand Down

0 comments on commit 1543854

Please sign in to comment.