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); }
internal void SendMessage(string contactUri, string imText) { try { if (_Client.State == ClientState.SignedIn) { ConversationManager myConversationManager = _Client.ConversationManager; Conversation myConversation = myConversationManager.AddConversation(); Participant myParticipant = myConversation.AddParticipant(_Client.ContactManager.GetContactByUri(contactUri)); if (myParticipant.IsSelf == false) { if (myConversation.State == ConversationState.Active && myConversation.Properties.ContainsKey(ConversationProperty.Subject)) { myConversation.Properties[ConversationProperty.Subject] = SUBJECT; } if (myConversation.Modalities.ContainsKey(ModalityTypes.InstantMessage)) { InstantMessageModality imModality = (InstantMessageModality)myConversation.Modalities[ModalityTypes.InstantMessage]; if (imModality.CanInvoke(ModalityAction.SendInstantMessage)) { Dictionary <InstantMessageContentType, string> textMessage = new Dictionary <InstantMessageContentType, string>(); textMessage.Add(InstantMessageContentType.PlainText, SUBJECT + " : " + imText); imModality.BeginSendMessage(textMessage, SendMessageCallback, imModality); } } } } } catch { } }
private static void RequestStockStream() { var conv = new StockHistoryRequestConversation(Config.GetInt(Config.BROKER_PROCESS_NUM)); conv.SetInitialState(new StockHistoryRequestState(conv)); ConversationManager.AddConversation(conv); }
public static void RunSimulation() { var tickDelay = Config.GetInt(Config.STOCK_TICK_DELAY); var runSimLocal = true; lock (RunSimLock) { runSimLocal = IsSimRunning; } while (runSimLocal) { Thread.Sleep(tickDelay); var nextMarketDay = StockData.AdvanceDay(); foreach (var clientIp in ClientManager.Clients.Keys) { var updateConv = new StockUpdateSendConversation(Config.GetInt(Config.STOCK_SERVER_PROCESS_NUM)); updateConv.SetInitialState(new StockUpdateSendState(nextMarketDay, clientIp, updateConv, null)); ConversationManager.AddConversation(updateConv); } lock (RunSimLock) { runSimLocal = IsSimRunning; } } }
public TraderModel() { if (Current == null) { Current = this; } else { throw new Exception("Only one Trader model should be used."); } Current = this; var getStockHistConv = new StockHistoryRequestConversation(); getStockHistConv.SetInitialState(new StockHistoryRequestState(getStockHistConv)); ConversationManager.AddConversation(getStockHistConv); var stockStreamConv = new StockStreamRequestConversation(Config.GetClientProcessNumber()); stockStreamConv.SetInitialState(new StockStreamRequestState(Config.GetClientProcessNumber(), stockStreamConv)); ConversationManager.AddConversation(stockStreamConv); StockUpdateEventHandler += HandleStockUpdate; }
public override ConversationState HandleMessage(Envelope incomingMessage) { Log.Debug($"{nameof(HandleMessage)} (enter)"); ConversationState nextState = null; switch (incomingMessage.Contents) { case StockHistoryResponseMessage m: var stockHistory = m.RecentHistory; Log.Info($"Received stock stream response with {stockHistory.Count} days of recent trading."); nextState = new ConversationDoneState(Conversation, this); var streamConv = new StockStreamRequestConversation(Config.GetInt(Config.BROKER_PROCESS_NUM)); streamConv.SetInitialState(new StockStreamRequestState(Config.GetInt(Config.BROKER_PROCESS_NUM), streamConv)); ConversationManager.AddConversation(streamConv); break; case ErrorMessage m: Log.Error($"Received error message as reply...\n{m.ErrorText}"); nextState = new ConversationDoneState(Conversation, this); break; default: Log.Error($"No logic to process incoming message of type {incomingMessage.Contents?.GetType()}."); Log.Error($"Ending conversation {Conversation.Id}."); nextState = new ConversationDoneState(Conversation, this); break; } Log.Debug($"{nameof(HandleMessage)} (exit)"); return(nextState); }
void StartConversation(string myRemoteParticipantUri, string MSG) { foreach (var con in _conversationManager.Conversations) { if (con.Participants.Where(p => p.Contact.Uri == "sip:" + myRemoteParticipantUri).Count() > 0) { if (con.Participants.Count == 2) { con.End(); break; } } } Conversation _Conversation = _conversationManager.AddConversation(); _Conversation.AddParticipant(_lyncClient.ContactManager.GetContactByUri(myRemoteParticipantUri)); Dictionary <InstantMessageContentType, String> messages = new Dictionary <InstantMessageContentType, String>(); messages.Add(InstantMessageContentType.PlainText, MSG); InstantMessageModality m = (InstantMessageModality)_Conversation.Modalities[ModalityTypes.InstantMessage]; m.BeginSendMessage(messages, null, messages); LogMessage(myRemoteParticipantUri, MSG); }
public void RequestSingleTimeoutThenSucceed() { int processId = 1; var testStock = new Stock("TST", "Test Stock"); var vStock = new ValuatedStock(("1984-02-22,11.0289,11.0822,10.7222,10.7222,197402").Split(','), testStock); var conv = new StockHistoryRequestConversation(processId); int requests = 0; //setup mock with response message var mock = new Mock <StockHistoryRequestState>(conv) { CallBase = true }; mock.Setup(st => st.Send()) .Callback(() => { //Pretend message is sent and response comes back, but only after second request... if (++requests > 1) { var responseMessage = new StockHistoryResponseMessage() { ConversationID = conv.Id, MessageID = "345-234-56" }; var responseEnv = new Envelope(responseMessage); ConversationManager.ProcessIncomingMessage(responseEnv); } }); conv.SetInitialState(mock.Object as StockHistoryRequestState); Assert.IsTrue(conv.CurrentState is StockHistoryRequestState); mock.Verify(state => state.Prepare(), Times.Never); mock.Verify(state => state.Send(), Times.Never); Assert.IsFalse(ConversationManager.ConversationExists(conv.Id)); ConversationManager.AddConversation(conv); Assert.IsTrue(conv.CurrentState is StockHistoryRequestState); mock.Verify(state => state.Prepare(), Times.Once); mock.Verify(state => state.Send(), Times.Once); mock.Verify(state => state.HandleTimeout(), Times.Never); Assert.IsTrue(ConversationManager.ConversationExists(conv.Id)); Thread.Sleep((int)(Config.GetInt(Config.DEFAULT_TIMEOUT) * 1.5)); Assert.IsFalse(conv.CurrentState is StockHistoryRequestState); Assert.IsTrue(conv.CurrentState is ConversationDoneState); mock.Verify(state => state.Prepare(), Times.Once); mock.Verify(state => state.Send(), Times.Exactly(2)); mock.Verify(state => state.HandleTimeout(), Times.Exactly(1)); Assert.IsTrue(ConversationManager.ConversationExists(conv.Id)); }
private void btnConversationStart_Click(object sender, RoutedEventArgs e) { _remoteUri = cboContacts.SelectedValue.ToString(); ConversationManager conversationManager = _client.ConversationManager; conversationManager.ConversationAdded += new EventHandler <ConversationManagerEventArgs> (conversationManager_ConversationAdded); Conversation conversation = conversationManager.AddConversation(); }
public void RequestSingleTimeoutThenSucceed() { int portfolioId = 42; var testStock = new Stock("TST", "Test Stock"); var vStock = new ValuatedStock(("1984-02-22,11.0289,11.0822,10.7222,10.7222,197402").Split(','), testStock); var conv = new InitiateTransactionConversation(portfolioId); int requests = 0; //setup response message and mock var mock = new Mock <InitTransactionStartingState>(conv, vStock, 1) { CallBase = true }; mock.Setup(prep => prep.Prepare()).Verifiable(); //ensure DoPrepare is called. mock.Setup(st => st.OnHandleMessage(It.IsAny <Envelope>(), 0)).CallBase(); //Skip mock's HandleMessage override. mock.Setup(st => st.Send()) //Pretend message is sent and response comes back... .Callback(() => { if (++requests > 1) { var responseMessage = new PortfolioUpdateMessage() { ConversationID = conv.Id }; var responseEnv = new Envelope(responseMessage); ConversationManager.ProcessIncomingMessage(responseEnv); } }).CallBase().Verifiable(); //execute test conv.SetInitialState(mock.Object as InitTransactionStartingState); Assert.IsTrue(conv.CurrentState is InitTransactionStartingState); mock.Verify(state => state.Prepare(), Times.Never); mock.Verify(state => state.Send(), Times.Never); ConversationManager.AddConversation(conv); Assert.IsTrue(conv.CurrentState is InitTransactionStartingState); mock.Verify(state => state.Prepare(), Times.Once); mock.Verify(state => state.Send(), Times.Once); mock.Verify(state => state.HandleTimeout(), Times.Never); Thread.Sleep((int)(Config.GetInt(Config.DEFAULT_TIMEOUT) * 1.5)); Assert.IsFalse(conv.CurrentState is InitTransactionStartingState); Assert.IsTrue(conv.CurrentState is ConversationDoneState); mock.Verify(state => state.Prepare(), Times.Once); mock.Verify(state => state.Send(), Times.AtLeast(2)); mock.Verify(state => state.HandleTimeout(), Times.AtLeast(1)); }
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 AddConversation(LyncConversation lyncConversation) { if (_currentLyncConversation != null) { _currentLyncConversation.Close(); } _currentLyncConversation = lyncConversation; ConversationManager.ConversationAdded += OnConversationManagerConversationAdded; ConversationManager.ConversationRemoved += OnConversationManagerConversationRemoved; var conversation = ConversationManager.AddConversation(); conversation.StateChanged += OnConversationStateChanged; }
/// <summary> /// Start the conversation with the specified participant using sip address /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnStartConv_Click(object sender, RoutedEventArgs e) { TextRange tr = new TextRange(rtbParticipants.Document.ContentStart, rtbParticipants.Document.ContentEnd); if (String.IsNullOrEmpty(tr.Text.Trim())) { txtErrors.Text = "No participants specified!"; return; } String[] participants = tr.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); RemoteUserUri = participants[0]; ConversationManager conversationManager = client.ConversationManager; conversationManager.ConversationAdded += new EventHandler <ConversationManagerEventArgs>(conversationManager_ConversationAdded); Conversation conversation = conversationManager.AddConversation(); }
public void RequestTimeout() { int processId = 1; var testStock = new Stock("TST", "Test Stock"); var vStock = new ValuatedStock(("1984-02-22,11.0289,11.0822,10.7222,10.7222,197402").Split(','), testStock); var conv = new StockHistoryRequestConversation(processId); //setup mock with response message var mock = new Mock <StockHistoryRequestState>(conv) { CallBase = true }; mock.Setup(st => st.Send()) .Callback(() => { //Pretend server isn't there, do nothing... }); conv.SetInitialState(mock.Object as StockHistoryRequestState); Assert.IsTrue(conv.CurrentState is StockHistoryRequestState); mock.Verify(state => state.Prepare(), Times.Never); mock.Verify(state => state.Send(), Times.Never); Assert.IsFalse(ConversationManager.ConversationExists(conv.Id)); ConversationManager.AddConversation(conv); Assert.IsTrue(conv.CurrentState is StockHistoryRequestState); mock.Verify(state => state.Prepare(), Times.Once); mock.Verify(state => state.Send(), Times.Once); mock.Verify(state => state.HandleTimeout(), Times.Never); Assert.IsTrue(ConversationManager.ConversationExists(conv.Id)); Thread.Sleep((int)(Config.GetInt(Config.DEFAULT_TIMEOUT) * (Config.GetInt(Config.DEFAULT_RETRY_COUNT) + 1) * 1.2)); Assert.IsFalse(conv.CurrentState is StockHistoryRequestState); 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)); Assert.IsTrue(ConversationManager.ConversationExists(conv.Id)); }
private void Submit_Click(object sender, RoutedEventArgs e) { //registering if (register.IsChecked.Value) { var PRC = new CreatePortfolioRequestConversation(Config.GetClientProcessNumber()); PRC.SetInitialState(new CreatePortfolioRequestState(user.Text, pass.Password, confirm.Password, this, PRC, null)); ConversationManager.AddConversation(PRC); } //logging in else { var loginConv = new GetPortfolioRequestConversation(Config.GetClientProcessNumber()); loginConv.SetInitialState(new GetPortfolioRequestState(user.Text, pass.Password, this, loginConv)); ConversationManager.AddConversation(loginConv); } }
public void RequestTimeout() { int portfolioId = 42; var testStock = new Stock("TST", "Test Stock"); var vStock = new ValuatedStock(("1984-02-22,11.0289,11.0822,10.7222,10.7222,197402").Split(','), testStock); var conv = new InitiateTransactionConversation(portfolioId); //setup response message and mock var mock = new Mock <InitTransactionStartingState>(conv, vStock, 1) { CallBase = true }; mock.Setup(prep => prep.Prepare()).Verifiable(); //ensure DoPrepare is called. mock.Setup(st => st.OnHandleMessage(It.IsAny <Envelope>(), 0)).CallBase(); //Skip mock's HandleMessage override. mock.Setup(st => st.Send()).CallBase().Verifiable(); //execute test conv.SetInitialState(mock.Object as InitTransactionStartingState); Assert.IsTrue(conv.CurrentState is InitTransactionStartingState); mock.Verify(state => state.Prepare(), Times.Never); mock.Verify(state => state.Send(), Times.Never); ConversationManager.AddConversation(conv); Assert.IsTrue(conv.CurrentState is InitTransactionStartingState); mock.Verify(state => state.Prepare(), Times.Once); mock.Verify(state => state.Send(), Times.Once); mock.Verify(state => state.HandleTimeout(), Times.Never); Thread.Sleep((int)(Config.GetInt(Config.DEFAULT_TIMEOUT) * (Config.GetInt(Config.DEFAULT_RETRY_COUNT) + 1) * 1.1)); Assert.IsFalse(conv.CurrentState is InitTransactionStartingState); 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.AtLeast(3)); mock.Verify(state => state.HandleTimeout(), Times.AtLeast(3)); }
public void RequestSucceed() { int processIdId = 42; var username = "******"; var password = "******"; var conv = new CreatePortfolioRequestConversation(processIdId); //setup response message and mock var mock = new Mock <CreatePortfolioRequestState>(username, password, password, null, conv, null) { CallBase = true }; mock.Setup(st => st.Send())//Pretend message is sent and response comes back... .Callback(() => { var responseMessage = new PortfolioUpdateMessage() { ConversationID = conv.Id, MessageID = "responseMessageID1234" }; var responseEnv = new Envelope(responseMessage); ConversationManager.ProcessIncomingMessage(responseEnv); }).CallBase().Verifiable(); //execute test conv.SetInitialState(mock.Object as CreatePortfolioRequestState); Assert.IsTrue(conv.CurrentState is CreatePortfolioRequestState); mock.Verify(state => state.Prepare(), Times.Never); mock.Verify(state => state.Send(), Times.Never); ConversationManager.AddConversation(conv); Assert.IsFalse(conv.CurrentState is CreatePortfolioRequestState); Assert.IsTrue(conv.CurrentState is ConversationDoneState); mock.Verify(state => state.Prepare(), Times.Once); mock.Verify(state => state.Send(), Times.Once); }
public void RequestSucceed() { int portfolioId = 42; var testStock = new Stock("TST", "Test Stock"); var vStock = new ValuatedStock(("1984-02-22,11.0289,11.0822,10.7222,10.7222,197402").Split(','), testStock); var conv = new InitiateTransactionConversation(portfolioId); //setup response message and mock var mock = new Mock <InitTransactionStartingState>(conv, vStock, 1) { CallBase = true }; mock.Setup(st => st.Send())//Pretend message is sent and response comes back... .Callback(() => { var responseMessage = new PortfolioUpdateMessage() { ConversationID = conv.Id, MessageID = "responseMessageID1234" }; var responseEnv = new Envelope(responseMessage); ConversationManager.ProcessIncomingMessage(responseEnv); }).CallBase().Verifiable(); //execute test conv.SetInitialState(mock.Object as InitTransactionStartingState); Assert.IsTrue(conv.CurrentState is InitTransactionStartingState); mock.Verify(state => state.Prepare(), Times.Never); mock.Verify(state => state.Send(), Times.Never); ConversationManager.AddConversation(conv); Assert.IsFalse(conv.CurrentState is InitTransactionStartingState); Assert.IsTrue(conv.CurrentState is ConversationDoneState); mock.Verify(state => state.Prepare(), Times.Once); mock.Verify(state => state.Send(), Times.Once); }
public void SucessfulStockStreamRequestTest() { //Simulate application-level ids int processId = 1; //Create a new StockStreamRequestConv_Initor conversation var stockStreamConv = new StockHistoryRequestConversation(processId); stockStreamConv.SetInitialState(new StockHistoryRequestState(stockStreamConv)); ConversationManager.AddConversation(stockStreamConv); string conversationId = stockStreamConv.Id; //Verify conversation exists in Conversation Manager Assert.IsTrue(ConversationManager.ConversationExists(conversationId)); //Create fake response message and process it var stockStreamResponse = new TcpEnvelope(new StockHistoryResponseMessage() { MessageID = "123-abc" }); stockStreamResponse.Contents.ConversationID = stockStreamConv.Id; ConversationManager.ProcessIncomingMessage(stockStreamResponse); //Conversation over but we hold onto the done state for a little //in case we need to handle a retry. Ensure it has not yet been //removed from Conversation Manager Assert.IsTrue(ConversationManager.ConversationExists(conversationId)); var retrycount = Config.GetInt(Config.DEFAULT_RETRY_COUNT); var timeout = Config.GetInt(Config.DEFAULT_TIMEOUT); Thread.Sleep(retrycount * timeout * 2); //Conversation should have cleaned itself up now... Assert.IsFalse(ConversationManager.ConversationExists(conversationId)); }
public void Succeed() { var conv = new StockUpdateSendConversation(42); var Stocks = new MarketDay("Today", new ValuatedStock[] { user1VStock, user2VStock, user3VStock }); IPEndPoint clientEndpoint = new IPEndPoint(IPAddress.Parse("111.11.2.3"), 124); //setup response message and mock var mock = new Mock <StockUpdateSendState>(Stocks, clientEndpoint, conv, null) { CallBase = true }; mock.Setup(st => st.Send())//Pretend message is sent and response comes back... .Callback(() => { var serliaizedDay = ((mock.Object as StockUpdateSendState).OutboundMessage.Contents as StockPriceUpdate).SerializedStockList; var sigServ = new SignatureService(); Stocks = sigServ.Deserialize <MarketDay>(Convert.FromBase64String(serliaizedDay)); 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 StockUpdateSendState); Assert.IsTrue(ClientManager.TryToAdd(clientEndpoint)); Assert.IsTrue(conv.CurrentState is StockUpdateSendState); mock.Verify(state => state.Prepare(), Times.Never); mock.Verify(state => state.Send(), Times.Never); ConversationManager.AddConversation(conv); Assert.IsFalse(conv.CurrentState is StockUpdateSendState); Assert.IsTrue(conv.CurrentState is ConversationDoneState); mock.Verify(state => state.Prepare(), Times.Once); mock.Verify(state => state.Send(), Times.Once); // These fail becaue the traded compaines are not the passed in values from above. Assert.AreEqual("U1STK", Stocks.TradedCompanies[0].Symbol); Assert.AreEqual("U2STK", Stocks.TradedCompanies[1].Symbol); Assert.AreEqual("U3STK", Stocks.TradedCompanies[2].Symbol); var conv2 = new StockUpdateSendConversation(42); var Stocks2 = new MarketDay("Today", new ValuatedStock[] { user1VStock, user2VStock, user3VStock }); //setup response message and mock var mock2 = new Mock <StockUpdateSendState>(Stocks2, clientEndpoint, conv2, null) { CallBase = true }; mock2.Setup(st => st.Send())//Pretend message is sent and response comes back... .Callback(() => { var serliaizedDay = ((mock2.Object as StockUpdateSendState).OutboundMessage.Contents as StockPriceUpdate).SerializedStockList; var sigServ = new SignatureService(); Stocks2 = sigServ.Deserialize <MarketDay>(Convert.FromBase64String(serliaizedDay)); 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 StockUpdateSendState); Assert.IsTrue(conv2.CurrentState is StockUpdateSendState); mock2.Verify(state => state.Prepare(), Times.Never); mock2.Verify(state => state.Send(), Times.Never); ConversationManager.AddConversation(conv2); Assert.IsFalse(conv2.CurrentState is StockUpdateSendState); Assert.IsTrue(conv2.CurrentState is ConversationDoneState); mock2.Verify(state => state.Prepare(), Times.Once); mock2.Verify(state => state.Send(), Times.Once); // These fail becaue the traded compaines are not the passed in values from above. Assert.AreEqual("U1STK", Stocks2.TradedCompanies[0].Symbol); Assert.AreEqual("U2STK", Stocks2.TradedCompanies[1].Symbol); Assert.AreEqual("U3STK", Stocks2.TradedCompanies[2].Symbol); Assert.IsTrue(ClientManager.TryToRemove(clientEndpoint)); }
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)); }