@Test(expected = IllegalArgumentException.class) public void testBroadcastRangeNegative() { final CommDevice device = builders[0].build(); device.broadcast(mock(MessageContents.class), -1); }
@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(expected = IllegalArgumentException.class) public void testBroadcastRangeTooLarge() { builders[0].setMaxRange(10); final CommDevice device = builders[0].build(); device.broadcast(mock(MessageContents.class), 15); }
/** * 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 public void testBroadcastRangeWithoutMaxRange() { when(COMMUSERS[0].getPosition()).thenReturn(Optional.of(new Point(0, 0))); when(COMMUSERS[1].getPosition()).thenReturn(Optional.of(new Point(5, 0))); when(COMMUSERS[2].getPosition()).thenReturn(Optional.of(new Point(10, 0))); final CommDevice device1 = builders[0].build(); final CommDevice device2 = builders[1].build(); final CommDevice device3 = builders[2].build(); assertEquals(0, device2.getReceivedCount()); assertEquals(0, device3.getReceivedCount()); device1.broadcast(mock(MessageContents.class), 7); device1.sendMessages(); assertEquals(1, device2.getReceivedCount()); assertEquals(0, device3.getReceivedCount()); }
/** * 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()); }
/** * Tests that the same device is used again upon re-registering. */ @Test public void testRegisterUnregistered() { final ListenerEventHistory leh = new ListenerEventHistory(); model.getEventAPI().addListener(leh, EventTypes.ADD_COMM_USER, EventTypes.REMOVE_COMM_USER); final CommDevice originalDevice = agent1.device(); assertThat(model.contains(agent1)).isTrue(); model.unregister(agent1); assertThat(model.contains(agent1)).isFalse(); assertThat(leh.getEventTypeHistory()).containsExactly(REMOVE_COMM_USER) .inOrder(); model.register(agent1); assertThat(model.contains(agent1)).isTrue(); final CommDevice newDevice = agent1.device(); assertThat(originalDevice).isSameAs(newDevice); assertThat(leh.getEventTypeHistory()).containsExactly(REMOVE_COMM_USER, ADD_COMM_USER) .inOrder(); newDevice.broadcast(Contents.HELLO_WORLD); }
/** * 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()); }
agent1.device().broadcast(Contents.HELLO_WORLD);
agent2.device().broadcast(Contents.YO); } catch (final IllegalStateException e) { fail = true;
/** * 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()); }
rangedNoPos.device().broadcast(Contents.YO); model.afterTick(TimeLapseFactory.create(0, 100)); assertEquals(0, agent1.device().getReceivedCount());