Permalink
Browse files

Renamed MessageQueue to MessageStore and commented integration specs

  • Loading branch information...
1 parent 50f8651 commit 29435f016cf07e9b55bff5f8d3a3a8d5c0aaffa9 Tom Janssens committed Mar 19, 2012
@@ -6,15 +6,20 @@
namespace YakShayQRS.Specs
{
[TestClass]
+ // This class contains **ALL** the code required to get the system up & running
public class IntegrationSpecs
{
public class Account
{
+ // virtual props define the unique id, are used as a message filter
+ // and initialized upon loading the instance
protected virtual string AccountId { get; set; }
bool IsRegistered = false;
private Decimal Balance = 0;
+ // public non-virtual methods define the external interface (i.e. the commands);
+ // these methods are **NEVER** invoked when rebuilding current state from past messages
public void RegisterAccount(string OwnerName)
{
if (IsRegistered)
@@ -60,6 +65,10 @@ public void CancelTransferOnSource(string TargetAccountId, decimal Amount)
OnTransferCancelledOnSource(TargetAccountId, Amount);
}
+ // virtual methods emit messages before they get called &
+ // the virtual props are added to the emitted messages.
+ // When building the current object's state based on past messages,
+ // only the virtual methods get invoked.
protected virtual void OnAccountRegistered(string OwnerName) { IsRegistered = true; }
protected virtual void OnAmountDeposited(decimal Amount) { Balance += Amount; }
@@ -77,8 +86,12 @@ public void CancelTransferOnSource(string TargetAccountId, decimal Amount)
protected virtual void OnTransactionCancelled(string what, string reason) { }
}
+ // simply xlat events from one account to commands for another account
public class AccountTransferSaga
{
+ // no virtual props = no unique Id
+
+ // Processing these events emits commands (messages)
public void OnTransferProcessedOnSource(string AccountId, string TargetAccountId, decimal Amount)
{
ProcessTransferOnTarget(TargetAccountId, AccountId, Amount);
@@ -89,17 +102,21 @@ public void OnTransferFailedOnTarget(string AccountId, string SourceAccountId, d
CancelTransferOnSource(SourceAccountId, AccountId, Amount);
}
+ // these commands get emitted
protected virtual void ProcessTransferOnTarget(string AccountId, string SourceAccountId, decimal Amount) { }
protected virtual void CancelTransferOnSource(string AccountId, string TargetAccountId, decimal Amount) { }
}
+ // build a viewmodel to verify proper state
public class AccountBalances
{
public AccountBalances() { }
public Dictionary<string, Decimal> Balances = new Dictionary<string, decimal>();
+ // no non-virtual commands, as this class only processes messages, it does not emit any
+
public virtual void OnAccountRegistered(string AccountId) { Balances.Add(AccountId, 0); }
public virtual void OnAmountDeposited(string AccountId, decimal Amount) { Balances[AccountId] += Amount; }
@@ -117,21 +134,22 @@ public virtual void OnTransferProcessedOnTarget(string AccountId, string SourceA
public void Deposits_and_withdraws_should_not_interfere_with_each_other()
{
var SUT = new YakShayBus();
+ // register all types under test
SUT.RegisterType<Account>();
SUT.RegisterType<AccountTransferSaga>();
SUT.RegisterType<AccountBalances>();
- var mq = new MessageQueue();
+ var ms = new MessageStore();
- SUT.HandleUntilAllConsumed(Message.FromAction(x => x.RegisterAccount(AccountId: "account/1", OwnerName: "Tom")), mq.Add, mq.Filter);
- SUT.HandleUntilAllConsumed(Message.FromAction(x => x.RegisterAccount(AccountId: "account/2", OwnerName: "Ben")), mq.Add, mq.Filter);
- SUT.HandleUntilAllConsumed(Message.FromAction(x => x.DepositAmount(AccountId: "account/1", Amount: 126m)), mq.Add, mq.Filter);
- SUT.HandleUntilAllConsumed(Message.FromAction(x => x.DepositAmount(AccountId: "account/2", Amount: 10m)), mq.Add, mq.Filter);
- SUT.HandleUntilAllConsumed(Message.FromAction(x => x.Transfer(AccountId: "account/1", TargetAccountId: "account/2", Amount: 26m)), mq.Add, mq.Filter);
- SUT.HandleUntilAllConsumed(Message.FromAction(x => x.WithdrawAmount(AccountId: "account/2", Amount: 10m)), mq.Add, mq.Filter);
+ SUT.HandleUntilAllConsumed(Message.FromAction(x => x.RegisterAccount(AccountId: "account/1", OwnerName: "Tom")), ms.Add, ms.Filter);
+ SUT.HandleUntilAllConsumed(Message.FromAction(x => x.RegisterAccount(AccountId: "account/2", OwnerName: "Ben")), ms.Add, ms.Filter);
+ SUT.HandleUntilAllConsumed(Message.FromAction(x => x.DepositAmount(AccountId: "account/1", Amount: 126m)), ms.Add, ms.Filter);
+ SUT.HandleUntilAllConsumed(Message.FromAction(x => x.DepositAmount(AccountId: "account/2", Amount: 10m)), ms.Add, ms.Filter);
+ SUT.HandleUntilAllConsumed(Message.FromAction(x => x.Transfer(AccountId: "account/1", TargetAccountId: "account/2", Amount: 26m)), ms.Add, ms.Filter);
+ SUT.HandleUntilAllConsumed(Message.FromAction(x => x.WithdrawAmount(AccountId: "account/2", Amount: 10m)), ms.Add, ms.Filter);
var bal = new AccountBalances();
- SUT.ApplyHistory(bal, mq.Filter);
+ SUT.ApplyHistory(bal, ms.Filter);
bal.Balances.Count.ShouldBe(2);
bal.Balances["account/1"].ShouldBe(100m);
bal.Balances["account/2"].ShouldBe(26m);
@@ -40,7 +40,7 @@ public void Message_should_get_processed_and_generate_a_single_InterceptThis_mes
{
var SUT = new YakShayBus();
SUT.RegisterType<SomeTestClass>();
- var mq = new MessageQueue();
+ var ms = new MessageStore();
var msg = new Message("MethodToInvoke", new
{
SomePartOfTheUID = "A",
@@ -49,10 +49,10 @@ public void Message_should_get_processed_and_generate_a_single_InterceptThis_mes
SomeString = "ABC"
});
- SUT.Handle(msg, mq.Add);
+ SUT.Handle(msg, ms.Add);
- mq.msgs.Count.ShouldBe(1);
- mq.msgs.First().ToFriendlyString().ShouldBe(new Message("OnMethodWasInvoked", new
+ ms.msgs.Count.ShouldBe(1);
+ ms.msgs.First().ToFriendlyString().ShouldBe(new Message("OnMethodWasInvoked", new
{
SomePartOfTheUID = "A",
AnotherPartOfTheUID = "B",
@@ -62,8 +62,8 @@ public void Message_should_get_processed_and_generate_a_single_InterceptThis_mes
var ArInstance = new SomeTestClass { SomePartOfTheUID = "A", AnotherPartOfTheUID = "B" };
var AnotherArInstance = new SomeTestClass { SomePartOfTheUID = "X", AnotherPartOfTheUID = "Y" };
- SUT.ApplyHistory(ArInstance, mq.Filter);
- SUT.ApplyHistory(AnotherArInstance, mq.Filter);
+ SUT.ApplyHistory(ArInstance, ms.Filter);
+ SUT.ApplyHistory(AnotherArInstance, ms.Filter);
ArInstance.HasMethodBeenCalled.ShouldBe(true);
AnotherArInstance.HasMethodBeenCalled.ShouldBe(false);
@@ -3,7 +3,7 @@
namespace YakShayQRS
{
- public class MessageQueue
+ public class MessageStore
{
public List<Message> msgs = new List<Message>();
@@ -45,7 +45,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
- <Compile Include="MessageQueue.cs" />
+ <Compile Include="MessageStore.cs" />
<Compile Include="Message.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="YakShayBus.cs" />

0 comments on commit 29435f0

Please sign in to comment.