Navigation Menu

Skip to content

Commit

Permalink
implemented MessageHistory for testing scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydmiller committed Jul 16, 2013
1 parent c527763 commit 88d7de9
Show file tree
Hide file tree
Showing 6 changed files with 241 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/Bottles.Services.Tests/Bottles.Services.Tests.csproj
Expand Up @@ -77,6 +77,7 @@
<Compile Include="Messaging\MessageWaitConditionTester.cs" />
<Compile Include="Messaging\MessagingHubTester.cs" />
<Compile Include="Messaging\RemoteListenerTester.cs" />
<Compile Include="Messaging\Tracking\MessageHistoryTester.cs" />
<Compile Include="Messaging\Tracking\MessageSentTester.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Remote\BigRemoteServicesIntegrationTester.cs" />
Expand Down
141 changes: 141 additions & 0 deletions src/Bottles.Services.Tests/Messaging/Tracking/MessageHistoryTester.cs
@@ -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; }
}
}
4 changes: 3 additions & 1 deletion src/Bottles.Services/Bottles.Services.csproj
Expand Up @@ -69,7 +69,9 @@
<Compile Include="Messaging\MessagingHub.cs" />
<Compile Include="Messaging\RemoteListener.cs" />
<Compile Include="Messaging\ServiceMessage.cs" />
<Compile Include="Messaging\Tracking\TrackingClasses.cs" />
<Compile Include="Messaging\Tracking\AllMessagesComplete.cs" />
<Compile Include="Messaging\Tracking\MessageTrack.cs" />
<Compile Include="Messaging\Tracking\MessageHistory.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="BottleService.cs" />
<Compile Include="BottleServiceFinder.cs" />
Expand Down
@@ -0,0 +1,7 @@
namespace Bottles.Services.Messaging.Tracking
{
public class AllMessagesComplete
{

}
}
68 changes: 68 additions & 0 deletions src/Bottles.Services/Messaging/Tracking/MessageHistory.cs
@@ -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());
}

}
}
Expand Up @@ -4,11 +4,6 @@

namespace Bottles.Services.Messaging.Tracking
{
public static class MessageHistory
{

}

public class MessageTrack
{
public static readonly string Sent = "Sent";
Expand Down Expand Up @@ -45,7 +40,7 @@ private static MessageTrack derive(object message, string id)

private static void autodetermineId(object message, Type messageType, MessageTrack track)
{
var property = messageType.GetProperties().FirstOrDefault(x => x.Name.EqualsIgnoreCase("Id"));
var property = messageType.GetProperties().FirstOrDefault(x => FubuCore.StringExtensions.EqualsIgnoreCase(x.Name, "Id"));
if (property != null)
{
var rawValue = property.GetValue(message, null);
Expand All @@ -71,7 +66,25 @@ public static MessageTrack ForReceived(object message, string id = null)

public string Status { get; set; }


}
protected bool Equals(MessageTrack other)
{
return string.Equals(FullName, other.FullName) && string.Equals(Id, other.Id);
}

public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((MessageTrack) obj);
}

public override int GetHashCode()
{
unchecked
{
return ((FullName != null ? FullName.GetHashCode() : 0)*397) ^ (Id != null ? Id.GetHashCode() : 0);
}
}
}
}

0 comments on commit 88d7de9

Please sign in to comment.