final List<Message> messages = device.get().getUnreadMessages();
final List<Message> messages = device.get().getUnreadMessages();
/** * Tests that the returned list is unmodifiable. */ @Test public void testUnmodifiableUnreadMessages() { final List<Message> msgs = agent1.device().getUnreadMessages(); boolean fail = false; try { msgs.clear(); } catch (final UnsupportedOperationException e) { fail = true; } assertTrue(fail); }
@Override public void tick(TimeLapse timeLapse) { if (!destination.isPresent()) { destination = Optional.of(roadModel.get().getRandomPosition(rng)); } roadModel.get().moveTo(this, destination.get(), timeLapse); if (roadModel.get().getPosition(this).equals(destination.get())) { destination = Optional.absent(); } if (device.get().getUnreadCount() > 0) { lastReceiveTime = timeLapse.getStartTime(); device.get().getUnreadMessages(); device.get().broadcast(Messages.NICE_TO_MEET_YOU); } else if (device.get().getReceivedCount() == 0) { device.get().broadcast(Messages.HELLO_WORLD); } else if (lastReceiveTime > 10 * 1000) { device.get().broadcast(Messages.WHERE_IS_EVERYBODY); } }
/** * 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()); }
/** * 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()); }
agent1.device().broadcast(Contents.HELLO_WORLD); assertTrue(agent1.device().getUnreadMessages().isEmpty()); assertTrue(agent2.device().getUnreadMessages().isEmpty()); assertTrue(agent3.device().getUnreadMessages().isEmpty()); assertTrue(agent4.device().getUnreadMessages().isEmpty()); assertTrue(agent5.device().getUnreadMessages().isEmpty()); assertTrue(agent1.device().getUnreadMessages().isEmpty()); final List<Message> msgs2 = agent2.device().getUnreadMessages(); final List<Message> msgs3 = agent3.device().getUnreadMessages(); final List<Message> msgs4 = agent4.device().getUnreadMessages(); final List<Message> msgs5 = agent5.device().getUnreadMessages(); assertSame(msgs2.get(0), msgs5.get(0)); assertTrue(agent1.device().getUnreadMessages().isEmpty()); assertTrue(agent2.device().getUnreadMessages().isEmpty()); assertTrue(agent3.device().getUnreadMessages().isEmpty()); assertTrue(agent4.device().getUnreadMessages().isEmpty()); assertTrue(agent5.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 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()); }
/** * 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 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()); }