/** * Tests the correct sending and receiving of a direct message. */ @Test public void testSendDirectMsg() { agent1.device().send(Contents.YO, agent2); assertTrue(agent2.device().getUnreadMessages().isEmpty()); // send messages model.afterTick(TimeLapseFactory.create(0, 100)); assertTrue(agent1.device().getUnreadMessages().isEmpty()); final List<Message> msgs = agent2.device().getUnreadMessages(); assertEquals(1, msgs.size()); assertFalse(msgs.get(0).toString().isEmpty()); assertSame(Contents.YO, msgs.get(0).getContents()); assertSame(agent1, msgs.get(0).getSender()); assertTrue(agent2.device().getUnreadMessages().isEmpty()); }
/** * Test for unreliable direct message. */ @Test public void testSendDirectMsgUnreliable() { final Agent unreliable = new Agent(new Point(5, 5), 0); model.register(unreliable); unreliable.device().send(Contents.YO, agent1); model.afterTick(TimeLapseFactory.create(0, 100)); assertTrue(agent1.device().getUnreadMessages().isEmpty()); agent1.device().send(Contents.YO, unreliable); model.afterTick(TimeLapseFactory.create(0, 100)); assertTrue(unreliable.device().getUnreadMessages().isEmpty()); }
/** * Tests broadcasting from and to unreliable agents. */ @Test public void testBroadcastUnreliable() { final Agent unreliable = new Agent(new Point(5, 5), 0); model.register(unreliable); unreliable.device().broadcast(Contents.YO); model.afterTick(TimeLapseFactory.create(0, 100)); assertTrue(agent1.device().getUnreadMessages().isEmpty()); assertTrue(agent2.device().getUnreadMessages().isEmpty()); agent1.device().broadcast(Contents.YO); model.afterTick(TimeLapseFactory.create(0, 100)); assertTrue(unreliable.device().getUnreadMessages().isEmpty()); assertEquals(1, agent2.device().getUnreadMessages().size()); }
/** * Test that a broadcast only ends up at agents within the range. */ @Test public void testBroadcastWithRange() { final Agent ranged = new RangedAgent(new Point(0, 5), 5); model.register(ranged); ranged.device().broadcast(Contents.YO); model.afterTick(TimeLapseFactory.create(0, 100)); assertFalse(agent1.device().getUnreadMessages().isEmpty()); assertFalse(agent2.device().getUnreadMessages().isEmpty()); assertTrue(agent3.device().getUnreadMessages().isEmpty()); assertFalse(agent4.device().getUnreadMessages().isEmpty()); assertTrue(agent5.device().getUnreadMessages().isEmpty()); }
model.afterTick(TimeLapseFactory.create(0, 100));
assertTrue(agent5.device().getUnreadMessages().isEmpty()); model.afterTick(TimeLapseFactory.create(0, 100)); assertTrue(agent1.device().getUnreadMessages().isEmpty()); final List<Message> msgs2 = agent2.device().getUnreadMessages();
/** * Tests that the messages appear in the device in the actual send ordering, * which is the order in which agents have been registered in the comm model * and not the order of broadcast invocation. */ @Test public void testBroadcastReceiveOrdering() { agent4.device().broadcast(Contents.YO); agent1.device().broadcast(Contents.HELLO_WORLD); agent2.device().broadcast(Contents.YO); agent1.device().broadcast(Contents.HELLO_WORLD); agent3.device().broadcast(Contents.YO); agent4.device().broadcast(Contents.YO); model.afterTick(TimeLapseFactory.create(0, 100)); final List<Message> msgs = agent5.device().getUnreadMessages(); assertSame(agent1, msgs.get(0).getSender()); assertSame(agent1, msgs.get(1).getSender()); assertSame(agent2, msgs.get(2).getSender()); assertSame(agent3, msgs.get(3).getSender()); assertSame(agent4, msgs.get(4).getSender()); assertSame(agent4, msgs.get(5).getSender()); }
/** * Test for send message with limited range. */ @Test public void testSendDirectMsgWithRange() { final Agent ranged = new RangedAgent(new Point(0, 5), 5); model.register(ranged); // is possible, is within range ranged.device().send(Contents.YO, agent1); // not possible, too far ranged.device().send(Contents.YO, agent3); // receiving messages from outside range is possible agent3.device().send(Contents.YO, ranged); model.afterTick(TimeLapseFactory.create(0, 100)); final Message m = agent1.device().getUnreadMessages().get(0); assertSame(ranged, m.getSender()); assertTrue(agent3.device().getUnreadMessages().isEmpty()); assertSame(agent3, ranged.device().getUnreadMessages().get(0) .getSender()); }
/** * Tests what happens if an agent no longer has a position. */ @Test public void testSenderIsRemovedDuringSend() { final Agent a = new Agent(new Point(0, 0)); final Agent b = new RangedAgent(new Point(0, 0), 5); model.register(a); model.register(b); // a is removed during the same tick as that it sends a message, this // doesn't matter since it has no range, therefore it sends anyway. a.device().broadcast(Contents.YO); a.setPosition(null); model.afterTick(TimeLapseFactory.create(0, 100)); assertEquals(1, agent1.device().getReceivedCount()); // b is removed during the same tick as that is sends a message, since it is // a ranged device the message is not sent and will stay in the outbox. b.device().broadcast(Contents.YO); b.setPosition(null); model.afterTick(TimeLapseFactory.create(0, 100)); assertEquals(1, agent1.device().getReceivedCount()); assertEquals(1, b.device().getOutbox().size()); }
model.afterTick(TimeLapseFactory.create(0, 100)); assertEquals(0, agent1.device().getReceivedCount()); assertEquals(0, agent1.device().getUnreadCount()); model.afterTick(TimeLapseFactory.create(0, 100)); assertEquals(0, agent1.device().getReceivedCount()); assertEquals(0, agent2.device().getReceivedCount()); model.afterTick(TimeLapseFactory.create(0, 100)); assertEquals(1, rangedNoPos.device().getReceivedCount()); model.afterTick(TimeLapseFactory.create(0, 100)); assertEquals(1, rangedNoPos.device().getReceivedCount()); model.afterTick(TimeLapseFactory.create(0, 100)); assertEquals(2, agent1.device().getReceivedCount()); assertEquals(0, agent2.device().getReceivedCount());