OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ |
| 10 |
| 11 package org.appspot.apprtc; |
| 12 |
| 13 import org.appspot.apprtc.util.LooperExecutor; |
| 14 import org.appspot.apprtc.util.RobolectricLooperExecutor; |
| 15 import org.junit.After; |
| 16 import org.junit.Before; |
| 17 import org.junit.Test; |
| 18 import org.junit.runner.RunWith; |
| 19 import org.mockito.Mock; |
| 20 import org.mockito.MockitoAnnotations; |
| 21 import org.mockito.invocation.InvocationOnMock; |
| 22 import org.mockito.stubbing.Answer; |
| 23 import org.robolectric.RobolectricTestRunner; |
| 24 import org.robolectric.annotation.Config; |
| 25 import org.robolectric.shadows.ShadowLog; |
| 26 |
| 27 import java.util.concurrent.ArrayBlockingQueue; |
| 28 import java.util.concurrent.BlockingQueue; |
| 29 |
| 30 import static org.junit.Assert.fail; |
| 31 import static org.mockito.Matchers.any; |
| 32 import static org.mockito.Mockito.doAnswer; |
| 33 import static org.mockito.Mockito.timeout; |
| 34 import static org.mockito.Mockito.verify; |
| 35 import static org.mockito.Mockito.verifyNoMoreInteractions; |
| 36 import static org.mockito.Mockito.when; |
| 37 |
| 38 @RunWith(RobolectricTestRunner.class) |
| 39 @Config(manifest = Config.NONE) |
| 40 public class TCPChannelClientTest { |
| 41 private static final int PORT = 8888; |
| 42 /** |
| 43 * How long we wait before trying to connect to the server. Chosen quite arbit
rarily and |
| 44 * could be made smaller if need be. |
| 45 */ |
| 46 private static final int SERVER_WAIT = 10; |
| 47 private static final int CONNECT_TIMEOUT = 100; |
| 48 private static final int SEND_TIMEOUT = 100; |
| 49 private static final int DISCONNECT_TIMEOUT = 100; |
| 50 private static final String TEST_MESSAGE_SERVER = "Hello, Server!"; |
| 51 private static final String TEST_MESSAGE_CLIENT = "Hello, Client!"; |
| 52 |
| 53 @Mock TCPChannelClient.TCPChannelEvents serverEvents; |
| 54 @Mock TCPChannelClient.TCPChannelEvents clientEvents; |
| 55 |
| 56 private RobolectricLooperExecutor executor; |
| 57 private TCPChannelClient server; |
| 58 private TCPChannelClient client; |
| 59 |
| 60 |
| 61 @Before |
| 62 public void setUp() { |
| 63 ShadowLog.stream = System.out; |
| 64 |
| 65 MockitoAnnotations.initMocks(this); |
| 66 |
| 67 executor = new RobolectricLooperExecutor(); |
| 68 executor.requestStart(); |
| 69 } |
| 70 |
| 71 @After |
| 72 public void tearDown() { |
| 73 verifyNoMoreEvents(); |
| 74 |
| 75 executor.executeAndWait(new Runnable() { |
| 76 @Override |
| 77 public void run() { |
| 78 client.disconnect(); |
| 79 server.disconnect(); |
| 80 } |
| 81 }); |
| 82 |
| 83 // Stop the executor thread |
| 84 executor.requestStop(); |
| 85 try { |
| 86 executor.join(); |
| 87 } catch (InterruptedException e) { |
| 88 fail(e.getMessage()); |
| 89 } |
| 90 } |
| 91 |
| 92 @Test |
| 93 public void testConnectIPv4() { |
| 94 setUpIPv4Server(); |
| 95 try { |
| 96 Thread.sleep(SERVER_WAIT); |
| 97 } catch (InterruptedException e) { |
| 98 fail(e.getMessage()); |
| 99 } |
| 100 setUpIPv4Client(); |
| 101 |
| 102 verify(serverEvents, timeout(CONNECT_TIMEOUT)).onTCPConnected(true); |
| 103 verify(clientEvents, timeout(CONNECT_TIMEOUT)).onTCPConnected(false); |
| 104 } |
| 105 |
| 106 @Test |
| 107 public void testConnectIPv6() { |
| 108 setUpIPv6Server(); |
| 109 try { |
| 110 Thread.sleep(SERVER_WAIT); |
| 111 } catch (InterruptedException e) { |
| 112 fail(e.getMessage()); |
| 113 } |
| 114 setUpIPv6Client(); |
| 115 |
| 116 verify(serverEvents, timeout(CONNECT_TIMEOUT)).onTCPConnected(true); |
| 117 verify(clientEvents, timeout(CONNECT_TIMEOUT)).onTCPConnected(false); |
| 118 } |
| 119 |
| 120 @Test |
| 121 public void testSendData() { |
| 122 testConnectIPv4(); |
| 123 |
| 124 executor.executeAndWait(new Runnable() { |
| 125 @Override |
| 126 public void run() { |
| 127 client.send(TEST_MESSAGE_SERVER); |
| 128 server.send(TEST_MESSAGE_CLIENT); |
| 129 } |
| 130 }); |
| 131 |
| 132 verify(serverEvents, timeout(SEND_TIMEOUT)).onTCPMessage(TEST_MESSAGE_SERVER
); |
| 133 verify(clientEvents, timeout(SEND_TIMEOUT)).onTCPMessage(TEST_MESSAGE_CLIENT
); |
| 134 } |
| 135 |
| 136 @Test |
| 137 public void testDisconnectServer() { |
| 138 testConnectIPv4(); |
| 139 executor.executeAndWait(new Runnable() { |
| 140 @Override |
| 141 public void run() { |
| 142 server.disconnect(); |
| 143 } |
| 144 }); |
| 145 |
| 146 verify(serverEvents, timeout(DISCONNECT_TIMEOUT)).onTCPClose(); |
| 147 verify(clientEvents, timeout(DISCONNECT_TIMEOUT)).onTCPClose(); |
| 148 } |
| 149 |
| 150 @Test |
| 151 public void testDisconnectClient() { |
| 152 testConnectIPv4(); |
| 153 executor.executeAndWait(new Runnable() { |
| 154 @Override |
| 155 public void run() { |
| 156 client.disconnect(); |
| 157 } |
| 158 }); |
| 159 |
| 160 verify(serverEvents, timeout(DISCONNECT_TIMEOUT)).onTCPClose(); |
| 161 verify(clientEvents, timeout(DISCONNECT_TIMEOUT)).onTCPClose(); |
| 162 } |
| 163 |
| 164 private void setUpIPv4Server() { |
| 165 setUpServer("0.0.0.0", PORT); |
| 166 } |
| 167 |
| 168 private void setUpIPv4Client() { |
| 169 setUpClient("127.0.0.1", PORT); |
| 170 } |
| 171 |
| 172 private void setUpIPv6Server() { |
| 173 setUpServer("::", PORT); |
| 174 } |
| 175 |
| 176 private void setUpIPv6Client() { |
| 177 setUpClient("::1", PORT); |
| 178 } |
| 179 |
| 180 private void setUpServer(String ip, int port) { |
| 181 server = new TCPChannelClient(executor, serverEvents, ip, port); |
| 182 } |
| 183 |
| 184 private void setUpClient(String ip, int port) { |
| 185 client = new TCPChannelClient(executor, clientEvents, ip, port); |
| 186 } |
| 187 |
| 188 /** |
| 189 * Verifies no more server or client events have been issued |
| 190 */ |
| 191 private void verifyNoMoreEvents() { |
| 192 verifyNoMoreInteractions(serverEvents); |
| 193 verifyNoMoreInteractions(clientEvents); |
| 194 } |
| 195 } |
OLD | NEW |