Exemplo n.º 1
0
        public void TestUnknownClientExceptionFinishCommunication()
        {
            //testing only the acks
            SimulatedActorSystem system     = new SimulatedActorSystem();
            Dispatcher           dispatcher = new Dispatcher(system, 2);

            system.Spawn(dispatcher);
            TestClient client = new TestClient();

            system.Spawn(client);

            dispatcher.Tell(new InitCommunication(client, 10));

            while (client.ReceivedMessages.Count == 0)
            {
                system.RunFor(1);
            }
            Message initAckMessage = client.ReceivedMessages.Dequeue();

            Assert.AreEqual(typeof(InitAck), initAckMessage.GetType());
            InitAck initAck = (InitAck)initAckMessage;

            Assert.AreEqual(10, initAck.CommunicationId);

            SimulatedActor worker = initAck.Worker;

            worker.Tell(new FinishCommunication(11));
            while (client.ReceivedMessages.Count == 0)
            {
                system.RunFor(1);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Finishes communication with a client and sends a FinishAck message to it.
        /// After that other ClientMessage messages, using the communication ID given
        /// in the received message, cannot be sent to this worker anymore.
        /// </summary>
        ///
        /// <param name="m">non-null message of type FinishCommunication</param>
        protected void processFinishCommunication(Message m)
        {
            FinishCommunication finC = (FinishCommunication)m;

            if (!ongoingCommunications.ContainsKey(finC.CommunicationId))
            {
                throw new UnknownClientException("Unknown communication ID");
            }
            SimulatedActor client = ongoingCommunications[finC.CommunicationId];

            ongoingCommunications.Remove(finC.CommunicationId);
            client.Tell(new FinishAck(finC.CommunicationId));
        }
Exemplo n.º 3
0
        public void TestFinishCommunication()
        {
            //testing only the acks
            SimulatedActorSystem system     = new SimulatedActorSystem();
            Dispatcher           dispatcher = new Dispatcher(system, 2);

            system.Spawn(dispatcher);
            TestClient client = new TestClient();

            system.Spawn(client);

            dispatcher.Tell(new InitCommunication(client, 10));

            while (client.ReceivedMessages.Count == 0)
            {
                system.RunFor(1);
            }
            Message initAckMessage = client.ReceivedMessages.Dequeue();

            Assert.AreEqual(typeof(InitAck), initAckMessage.GetType());
            InitAck initAck = (InitAck)initAckMessage;

            Assert.AreEqual(10, initAck.CommunicationId);

            SimulatedActor worker = initAck.Worker;

            dispatcher.Tell(new Stop());

            system.RunUntil(system.currentTime + 10);

            worker.Tell(new FinishCommunication(10));
            while (client.ReceivedMessages.Count == 0)
            {
                system.RunFor(1);
            }

            Message operationFailedMessage = client.ReceivedMessages.Dequeue();

            Assert.AreEqual(typeof(OperationFailed), operationFailedMessage.GetType());

            // TODO run system until workers and dispatcher are stopped
            int endtime = system.currentTime + 20;

            system.RunUntil(endtime);
            Assert.AreEqual(system.currentTime, endtime + 1);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Performs checks on a user message, which should be published. If the
        /// checks are passed, a worker helper is spawned, which communicates with
        /// the message store to store the new user message.
        /// New messages must have zero likes, must not have a message ID assigned
        /// and must not be (strictly) longer than 10 characters.
        /// Only 10 characters are allowed to to alleviate exercise 4.
        /// </summary>
        /// <param name="m">non-null message of type Publish</param>
        protected void processPublish(Message m)
        {
            Publish publish = (Publish)m;

            if (!ongoingCommunications.ContainsKey(publish.CommunicationId))
            {
                throw new UnknownClientException("Unknown communication ID");
            }
            SimulatedActor client      = ongoingCommunications[publish.CommunicationId];
            UserMessage    userMessage = publish.Message;

            if (userMessage.Likes.Count > 0 || userMessage.Dislikes.Count > 0 || userMessage.MessageId != UserMessage.NEW ||
                userMessage.Message.Length > 10)
            {
                client.Tell(new OperationFailed(publish.CommunicationId));
            }
            else
            {
                MessageStoreMessage message = new UpdateMessageStore(userMessage, publish.CommunicationId);
                WorkerHelper        helper  = new WorkerHelper(messageStore, client, message, system);
                system.Spawn(helper);
            }
        }
Exemplo n.º 5
0
        public void test_cleanup()
        {
            //Worker 1
            worker_.Tell(new FinishCommunication(10));
            while (testclient_.ReceivedMessages.Count == 0)
            {
                system_.RunFor(1);
            }

            testclient_.ReceivedMessages.Dequeue();
        }