private static void AmmendAccount(AccountAction action, Account account)
 {
     decimal currentAmount = account.Balance;
     account.Balance = action.TransactionType == TransactionType.Debit
         ? currentAmount - action.Amount
         : currentAmount + action.Amount;
 }
Beispiel #2
0
        public void AccountActorJsonSendReceiveTests()
        {
            var account = new Account(1, "Test Account", "11223", 0);
            var accountAction = new AccountAction(TransactionType.Credit, 10);
            
            using (var actor = NetMQActor.Create(new AccountShimHandler()))
            {
                actor.SendMoreFrame("AMEND ACCOUNT");
                actor.SendMoreFrame(JsonConvert.SerializeObject(accountAction));
                actor.SendFrame(JsonConvert.SerializeObject(account));

                var updatedAccount = JsonConvert.DeserializeObject<Account>(actor.ReceiveFrameString());

                Assert.AreEqual(10.0m, updatedAccount.Balance);
            }
        }
Beispiel #3
0
        public void AccountActorJsonSendReceiveTests()
        {
            var account       = new Account(1, "Test Account", "11223", 0);
            var accountAction = new AccountAction(TransactionType.Credit, 10);

            using (var actor = NetMQActor.Create(new AccountShimHandler()))
            {
                actor.SendMoreFrame("AMEND ACCOUNT");
                actor.SendMoreFrame(JsonConvert.SerializeObject(accountAction));
                actor.SendFrame(JsonConvert.SerializeObject(account));

                var updatedAccount = JsonConvert.DeserializeObject <Account>(actor.ReceiveFrameString());

                Assert.Equal(10.0m, updatedAccount.Balance);
            }
        }
Beispiel #4
0
        public void RunPipeline(PairSocket shim)
        {
            shim.SignalOK();

            while (true)
            {
                try
                {
                    //Message for this actor/shim handler is expected to be
                    //Frame[0] : Command
                    //Frame[2] : AccountAction as JSON
                    //Frame[1] : Account as JSON
                    //
                    //Result back to actor is a JSON message of the amended Account
                    NetMQMessage msg = shim.ReceiveMessage();

                    string command = msg[0].ConvertToString();

                    if (command == ActorKnownMessages.END_PIPE)
                    {
                        break;
                    }

                    if (msg[0].ConvertToString() == "AMEND ACCOUNT")
                    {
                        string        accountActionJson = msg[1].ConvertToString();
                        AccountAction accountAction     = JsonConvert.DeserializeObject <AccountAction>(accountActionJson);

                        string  accountJson = msg[2].ConvertToString();
                        Account account     = JsonConvert.DeserializeObject <Account>(accountJson);
                        AmmendAccount(accountAction, account);
                        shim.Send(JsonConvert.SerializeObject(account));
                    }

                    else
                    {
                        shim.Send("Error: invalid message to actor");
                    }
                }
                //You WILL need to decide what Exceptions should be caught here, this is for
                //demonstration purposes only, any unhandled falut will bubble up to callers code
                catch (Exception e)
                {
                    shim.Send(string.Format("Error: Exception occurred {0}", e.Message));
                }
            }
        }