Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implemented MessageHistory for testing scenarios
- Loading branch information
1 parent
c527763
commit 88d7de9
Showing
6 changed files
with
241 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
src/Bottles.Services.Tests/Messaging/Tracking/MessageHistoryTester.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
using System; | ||
using Bottles.Services.Messaging; | ||
using Bottles.Services.Messaging.Tracking; | ||
using NUnit.Framework; | ||
using System.Linq; | ||
using FubuTestingSupport; | ||
using Rhino.Mocks; | ||
|
||
namespace Bottles.Services.Tests.Messaging.Tracking | ||
{ | ||
[TestFixture] | ||
public class MessageHistoryTester | ||
{ | ||
private IListener<AllMessagesComplete> listener; | ||
|
||
[SetUp] | ||
public void SetUp() | ||
{ | ||
MessageHistory.ClearAll(); | ||
|
||
listener = MockRepository.GenerateMock<IListener<AllMessagesComplete>>(); | ||
EventAggregator.Messaging.AddListener(listener); | ||
|
||
} | ||
|
||
[TearDown] | ||
public void TearDown() | ||
{ | ||
EventAggregator.Messaging.RemoveListener(listener); | ||
} | ||
|
||
private void assertHasNotReceivedAllCompleteMessage() | ||
{ | ||
listener.AssertWasNotCalled(x => x.Receive(null), x => x.IgnoreArguments()); | ||
} | ||
|
||
private void assertAllCompleteMessage() | ||
{ | ||
listener.AssertWasCalled(x => x.Receive(null), x => x.IgnoreArguments()); | ||
} | ||
|
||
[Test] | ||
public void track_outstanding() | ||
{ | ||
var foo1 = new Foo(); | ||
var foo2 = new Foo(); | ||
var foo3 = new Foo(); | ||
|
||
MessageHistory.Record(MessageTrack.ForSent(foo1)); | ||
MessageHistory.Record(MessageTrack.ForSent(foo2)); | ||
MessageHistory.Record(MessageTrack.ForSent(foo3)); | ||
|
||
MessageHistory.Outstanding().Select(x => x.Id) | ||
.ShouldHaveTheSameElementsAs(foo1.Id.ToString(), foo2.Id.ToString(), foo3.Id.ToString()); | ||
|
||
MessageHistory.Record(MessageTrack.ForReceived(foo2)); | ||
|
||
MessageHistory.Outstanding().Select(x => x.Id) | ||
.ShouldHaveTheSameElementsAs(foo1.Id.ToString(), foo3.Id.ToString()); | ||
|
||
MessageHistory.Record(MessageTrack.ForReceived(foo3)); | ||
|
||
MessageHistory.Outstanding().Select(x => x.Id) | ||
.ShouldHaveTheSameElementsAs(foo1.Id.ToString()); | ||
|
||
|
||
} | ||
|
||
[Test] | ||
public void track_received() | ||
{ | ||
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.Received().Select(x => x.Id) | ||
.ShouldHaveTheSameElementsAs(foo1.Id.ToString(), foo2.Id.ToString(), foo3.Id.ToString()); | ||
|
||
} | ||
|
||
[Test] | ||
public void clear_all_absolutely_has_to_work() | ||
{ | ||
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.ClearAll(); | ||
|
||
MessageHistory.Outstanding().Any().ShouldBeFalse(); | ||
MessageHistory.Received().Any().ShouldBeFalse(); | ||
MessageHistory.All().Any().ShouldBeFalse(); | ||
|
||
} | ||
|
||
[Test] | ||
public void sends_the_all_clear_message_when_it_gets_everything() | ||
{ | ||
var foo1 = new Foo(); | ||
var foo2 = new Foo(); | ||
var foo3 = new Foo(); | ||
|
||
MessageHistory.Record(MessageTrack.ForSent(foo1)); | ||
MessageHistory.Record(MessageTrack.ForSent(foo2)); | ||
MessageHistory.Record(MessageTrack.ForSent(foo3)); | ||
|
||
assertHasNotReceivedAllCompleteMessage(); | ||
|
||
MessageHistory.Record(MessageTrack.ForReceived(foo1)); | ||
assertHasNotReceivedAllCompleteMessage(); | ||
|
||
MessageHistory.Record(MessageTrack.ForReceived(foo2)); | ||
assertHasNotReceivedAllCompleteMessage(); | ||
|
||
MessageHistory.Record(MessageTrack.ForReceived(foo3)); | ||
assertAllCompleteMessage(); | ||
} | ||
} | ||
|
||
public class Foo | ||
{ | ||
public Foo() | ||
{ | ||
Id = Guid.NewGuid(); | ||
} | ||
|
||
public Guid Id { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/Bottles.Services/Messaging/Tracking/AllMessagesComplete.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Bottles.Services.Messaging.Tracking | ||
{ | ||
public class AllMessagesComplete | ||
{ | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using FubuCore; | ||
|
||
namespace Bottles.Services.Messaging.Tracking | ||
{ | ||
public static class MessageHistory | ||
{ | ||
private readonly static IList<MessageTrack> _sent = new List<MessageTrack>(); | ||
private readonly static IList<MessageTrack> _received = new List<MessageTrack>(); | ||
private readonly static IList<MessageTrack> _outstanding = new List<MessageTrack>(); | ||
|
||
private readonly static ReaderWriterLockSlim _lock = new ReaderWriterLockSlim(); | ||
|
||
public static void ClearAll() | ||
{ | ||
_lock.Write(() => { | ||
_sent.Clear(); | ||
_received.Clear(); | ||
_outstanding.Clear(); | ||
}); | ||
} | ||
|
||
public static void Record(MessageTrack track) | ||
{ | ||
_lock.Write(() => { | ||
if (track.Status == MessageTrack.Sent) | ||
{ | ||
_sent.Add(track); | ||
_outstanding.Add(track); | ||
} | ||
else | ||
{ | ||
_received.Add(track); | ||
_outstanding.Remove(track); | ||
} | ||
}); | ||
|
||
_lock.Read(() => { | ||
if (!_outstanding.Any()) | ||
{ | ||
EventAggregator.SendMessage(new AllMessagesComplete()); | ||
} | ||
return true; | ||
}); | ||
|
||
|
||
} | ||
|
||
public static IEnumerable<MessageTrack> Received() | ||
{ | ||
return _lock.Read(() => _received.ToArray()); | ||
} | ||
|
||
public static IEnumerable<MessageTrack> Outstanding() | ||
{ | ||
return _lock.Read(() => _outstanding.ToArray()); | ||
} | ||
|
||
public static IEnumerable<MessageTrack> All() | ||
{ | ||
return _lock.Read(() => _sent.Union(_received).ToList()); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters