@Test public void testGetServersForced() throws Exception { MasterServer.setRetries(1); M2A_SERVER_BATCH_Paket packet1 = mock(M2A_SERVER_BATCH_Paket.class); Vector<String> servers1 = new Vector<String>(); servers1.add("127.0.0.1:27015"); servers1.add("127.0.0.2:27015"); servers1.add("127.0.0.3:27015"); when(packet1.getServers()).thenReturn(servers1); when(this.server.socket.getReply()).thenReturn(packet1).thenThrow(new TimeoutException()); Vector<InetSocketAddress> servers = new Vector<InetSocketAddress>(); servers.add(new InetSocketAddress("127.0.0.1", 27015)); servers.add(new InetSocketAddress("127.0.0.2", 27015)); servers.add(new InetSocketAddress("127.0.0.3", 27015)); assertThat( this.server.getServers(MasterServer.REGION_EUROPE, "filter", true), is(equalTo(servers))); verify(this.server.socket) .send( argThat( new BaseMatcher<A2M_GET_SERVERS_BATCH2_Paket>() { public boolean matches(Object o) { if (!(o instanceof A2M_GET_SERVERS_BATCH2_Paket)) { return false; } A2M_GET_SERVERS_BATCH2_Paket packet = (A2M_GET_SERVERS_BATCH2_Paket) o; return Arrays.equals( packet.getBytes(), "\u0031\u00030.0.0.0:0\0filter\0".getBytes()); } public void describeTo(Description description) {} })); verify(this.server.socket) .send( argThat( new BaseMatcher<A2M_GET_SERVERS_BATCH2_Paket>() { public boolean matches(Object o) { if (!(o instanceof A2M_GET_SERVERS_BATCH2_Paket)) { return false; } A2M_GET_SERVERS_BATCH2_Paket packet = (A2M_GET_SERVERS_BATCH2_Paket) o; return Arrays.equals( packet.getBytes(), "\u0031\u0003127.0.0.3:27015\0filter\0".getBytes()); } public void describeTo(Description description) {} })); }
@Test public void testGetServersTimeout() throws Exception { int retries = new Random().nextInt(4) + 1; MasterServer.setRetries(retries); when(this.server.socket.getReply()).thenThrow(new TimeoutException()); try { this.server.getServers(); fail(); } catch (TimeoutException e) { } verify(this.server.socket, times(retries)) .send( argThat( new BaseMatcher<A2M_GET_SERVERS_BATCH2_Paket>() { public boolean matches(Object o) { if (!(o instanceof A2M_GET_SERVERS_BATCH2_Paket)) { return false; } A2M_GET_SERVERS_BATCH2_Paket packet = (A2M_GET_SERVERS_BATCH2_Paket) o; return Arrays.equals( packet.getBytes(), new byte[] { 0x31, (byte) 0xFF, 0x30, 0x2E, 0x30, 0x2E, 0x30, 0x2E, 0x30, 0x3a, 0x30, 0x00, 0x00 }); } public void describeTo(Description description) {} })); }