Example #1
0
        private static Conversation HandleStockPriceUpdate(Envelope e)
        {
            Conversation conv = null;

            StockPriceUpdate m = e.Contents as StockPriceUpdate;
            var sigServ        = new SignatureService();
            var bits           = Convert.FromBase64String(m.SerializedStockList);
            var StocksList     = sigServ.Deserialize <MarketDay>(bits);

            if (!sigServ.VerifySignature(StocksList, m.StockListSignature))
            {
                Log.Error("Stock Price Update signature validation failed. Ignoring message.");
            }
            else
            {
                LeaderboardManager.Market = StocksList;

                conv = new StockUpdateConversation(m.ConversationID);
                conv.SetInitialState(new ProcessStockUpdateState(e, conv));

                //Send updated leaderboard to clients.
                Task.Run(() =>
                {
                    foreach (var clientIp in ClientManager.Clients.Keys)
                    {
                        var stockUpdateConv = new LeaderBoardUpdateRequestConversation(Config.GetInt(Config.BROKER_PROCESS_NUM));
                        stockUpdateConv.SetInitialState(new LeaderboardSendUpdateState(clientIp, stockUpdateConv, null));
                        ConversationManager.AddConversation(stockUpdateConv);
                    }
                });
            }
            return(conv);
        }
        public void Timeout()
        {
            var        conv           = new LeaderBoardUpdateRequestConversation(42);
            SortedList Records        = new SortedList();
            var        clientEndpoint = new IPEndPoint(IPAddress.Parse("111.11.2.3"), 124);

            ValuatedStock[] day1 = { user1VStock, user2VStock, user3VStock };
            LeaderboardManager.Market = new MarketDay("day-1", day1);

            //setup response message and mock
            var mock = new Mock <LeaderboardSendUpdateState>(clientEndpoint, conv, null)
            {
                CallBase = true
            };

            mock.Setup(st => st.Send())//Pretend message is sent and response comes back...
            .Callback(() =>
            {
                //pretend client never responds, do nothing
            }).CallBase().Verifiable();

            ////execute test
            conv.SetInitialState(mock.Object as LeaderboardSendUpdateState);
            var clients = ClientManager.Clients;

            Assert.IsTrue(ClientManager.TryToAdd(clientEndpoint));

            Assert.IsTrue(conv.CurrentState is LeaderboardSendUpdateState);
            mock.Verify(state => state.Prepare(), Times.Never);
            mock.Verify(state => state.Send(), Times.Never);

            ConversationManager.AddConversation(conv);

            Assert.IsTrue(conv.CurrentState is LeaderboardSendUpdateState);
            mock.Verify(state => state.Prepare(), Times.Once);
            mock.Verify(state => state.Send(), Times.Once);
            mock.Verify(state => state.HandleTimeout(), Times.Never);

            Assert.AreEqual(1, ClientManager.Clients.Count);

            Thread.Sleep((int)(Config.GetInt(Config.DEFAULT_TIMEOUT) * (Config.GetInt(Config.DEFAULT_RETRY_COUNT) + 1) * 1.1));

            Assert.AreEqual(0, ClientManager.Clients.Count);

            Assert.IsFalse(conv.CurrentState is LeaderboardSendUpdateState);
            Assert.IsTrue(conv.CurrentState is ConversationDoneState);
            mock.Verify(state => state.HandleMessage(It.IsAny <Envelope>()), Times.Never);
            mock.Verify(state => state.Prepare(), Times.Once);
            mock.Verify(state => state.Send(), Times.Exactly(3));
            mock.Verify(state => state.HandleTimeout(), Times.AtLeast(3));
        }
        public void Succeed()
        {
            var conv = new LeaderBoardUpdateRequestConversation(42);
            List <LeaderboardRecord> Records = new List <LeaderboardRecord>();
            IPEndPoint clientEndpoint        = new IPEndPoint(IPAddress.Parse("111.11.2.3"), 124);

            ValuatedStock[] day1 = { user1VStock, user2VStock, user3VStock };
            LeaderboardManager.Market = new MarketDay("day-1", day1);

            //setup response message and mock
            var mock = new Mock <LeaderboardSendUpdateState>(clientEndpoint, conv, null)
            {
                CallBase = true
            };

            mock.Setup(st => st.Send())//Pretend message is sent and response comes back...
            .Callback(() =>
            {
                var serliazedLeaders = Convert.FromBase64String(((mock.Object as LeaderboardSendUpdateState).OutboundMessage.Contents as UpdateLeaderBoardMessage).SerializedRecords);
                Records             = sigServe.Deserialize <List <LeaderboardRecord> >(serliazedLeaders);
                var responseMessage = new AckMessage()
                {
                    ConversationID = conv.Id, MessageID = "responseMessageID1234"
                };
                var responseEnv = new Envelope(responseMessage);
                ConversationManager.ProcessIncomingMessage(responseEnv);
            }).CallBase().Verifiable();

            ////execute test
            conv.SetInitialState(mock.Object as LeaderboardSendUpdateState);
            Assert.IsTrue(ClientManager.TryToAdd(clientEndpoint));

            Assert.IsTrue(conv.CurrentState is LeaderboardSendUpdateState);
            mock.Verify(state => state.Prepare(), Times.Never);
            mock.Verify(state => state.Send(), Times.Never);

            ConversationManager.AddConversation(conv);

            Assert.IsFalse(conv.CurrentState is LeaderboardSendUpdateState);
            Assert.IsTrue(conv.CurrentState is ConversationDoneState);
            mock.Verify(state => state.Prepare(), Times.Once);
            mock.Verify(state => state.Send(), Times.Once);

            Assert.AreEqual("port1", Records[0].Username);
            Assert.AreEqual("port2", Records[1].Username);
            Assert.AreEqual("port3", Records[2].Username);

            //new market day, change stock price, re-update leaderboard

            var conv2 = new LeaderBoardUpdateRequestConversation(42);
            List <LeaderboardRecord> Records2 = new List <LeaderboardRecord>();

            user2VStock.Close = 100;
            ValuatedStock[] day2 = { user1VStock, user2VStock, user3VStock };
            LeaderboardManager.Market = new MarketDay("day-2", day1);

            //setup response message and mock
            var mock2 = new Mock <LeaderboardSendUpdateState>(clientEndpoint, conv2, null)
            {
                CallBase = true
            };

            mock2.Setup(st => st.Send())//Pretend message is sent and response comes back...
            .Callback(() =>
            {
                var serliazedLeaders = Convert.FromBase64String(((mock2.Object as LeaderboardSendUpdateState).OutboundMessage.Contents as UpdateLeaderBoardMessage).SerializedRecords);
                Records2             = sigServe.Deserialize <List <LeaderboardRecord> >(serliazedLeaders);
                var responseMessage  = new AckMessage()
                {
                    ConversationID = conv2.Id, MessageID = "responseMessageID1234"
                };
                var responseEnv = new Envelope(responseMessage);
                ConversationManager.ProcessIncomingMessage(responseEnv);
            }).CallBase().Verifiable();

            ////execute test
            conv2.SetInitialState(mock2.Object as LeaderboardSendUpdateState);

            Assert.IsTrue(conv2.CurrentState is LeaderboardSendUpdateState);
            mock2.Verify(state => state.Prepare(), Times.Never);
            mock2.Verify(state => state.Send(), Times.Never);

            ConversationManager.AddConversation(conv2);

            Assert.IsFalse(conv2.CurrentState is LeaderboardSendUpdateState);
            Assert.IsTrue(conv2.CurrentState is ConversationDoneState);
            mock2.Verify(state => state.Prepare(), Times.Once);
            mock2.Verify(state => state.Send(), Times.Once);

            Assert.AreEqual("port1", Records2[0].Username);
            Assert.AreEqual("port3", Records2[1].Username);
            Assert.AreEqual("port2", Records2[2].Username);

            Assert.IsTrue(ClientManager.TryToRemove(clientEndpoint));
        }