OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 package org.appspot.apprtc; | 11 package org.appspot.apprtc; |
12 | 12 |
13 import org.appspot.apprtc.util.RobolectricLooperExecutor; | 13 |
14 import org.junit.After; | 14 import org.junit.After; |
15 import org.junit.Before; | 15 import org.junit.Before; |
16 import org.junit.Test; | 16 import org.junit.Test; |
17 import org.junit.runner.RunWith; | 17 import org.junit.runner.RunWith; |
18 import org.mockito.Mock; | 18 import org.mockito.Mock; |
19 import org.mockito.MockitoAnnotations; | 19 import org.mockito.MockitoAnnotations; |
20 import org.robolectric.RobolectricTestRunner; | 20 import org.robolectric.RobolectricTestRunner; |
21 import org.robolectric.annotation.Config; | 21 import org.robolectric.annotation.Config; |
22 import org.robolectric.shadows.ShadowLog; | 22 import org.robolectric.shadows.ShadowLog; |
23 | 23 |
| 24 import java.util.concurrent.ExecutionException; |
| 25 import java.util.concurrent.ExecutorService; |
| 26 import java.util.concurrent.Executors; |
| 27 import java.util.concurrent.TimeUnit; |
| 28 |
24 import static org.junit.Assert.fail; | 29 import static org.junit.Assert.fail; |
25 import static org.mockito.Mockito.timeout; | 30 import static org.mockito.Mockito.timeout; |
26 import static org.mockito.Mockito.verify; | 31 import static org.mockito.Mockito.verify; |
27 import static org.mockito.Mockito.verifyNoMoreInteractions; | 32 import static org.mockito.Mockito.verifyNoMoreInteractions; |
28 | 33 |
29 @RunWith(RobolectricTestRunner.class) | 34 @RunWith(RobolectricTestRunner.class) |
30 @Config(manifest = Config.NONE) | 35 @Config(manifest = Config.NONE) |
31 public class TCPChannelClientTest { | 36 public class TCPChannelClientTest { |
32 private static final int PORT = 8888; | 37 private static final int PORT = 8888; |
33 /** | 38 /** |
34 * How long we wait before trying to connect to the server. Chosen quite arbit
rarily and | 39 * How long we wait before trying to connect to the server. Chosen quite arbit
rarily and |
35 * could be made smaller if need be. | 40 * could be made smaller if need be. |
36 */ | 41 */ |
37 private static final int SERVER_WAIT = 10; | 42 private static final int SERVER_WAIT = 10; |
38 private static final int CONNECT_TIMEOUT = 100; | 43 private static final int CONNECT_TIMEOUT = 100; |
39 private static final int SEND_TIMEOUT = 100; | 44 private static final int SEND_TIMEOUT = 100; |
40 private static final int DISCONNECT_TIMEOUT = 100; | 45 private static final int DISCONNECT_TIMEOUT = 100; |
| 46 private static final int TERMINATION_TIMEOUT = 1000; |
41 private static final String TEST_MESSAGE_SERVER = "Hello, Server!"; | 47 private static final String TEST_MESSAGE_SERVER = "Hello, Server!"; |
42 private static final String TEST_MESSAGE_CLIENT = "Hello, Client!"; | 48 private static final String TEST_MESSAGE_CLIENT = "Hello, Client!"; |
43 | 49 |
44 @Mock TCPChannelClient.TCPChannelEvents serverEvents; | 50 @Mock TCPChannelClient.TCPChannelEvents serverEvents; |
45 @Mock TCPChannelClient.TCPChannelEvents clientEvents; | 51 @Mock TCPChannelClient.TCPChannelEvents clientEvents; |
46 | 52 |
47 private RobolectricLooperExecutor executor; | 53 private ExecutorService executor; |
48 private TCPChannelClient server; | 54 private TCPChannelClient server; |
49 private TCPChannelClient client; | 55 private TCPChannelClient client; |
50 | 56 |
51 | 57 |
52 @Before | 58 @Before |
53 public void setUp() { | 59 public void setUp() { |
54 ShadowLog.stream = System.out; | 60 ShadowLog.stream = System.out; |
55 | 61 |
56 MockitoAnnotations.initMocks(this); | 62 MockitoAnnotations.initMocks(this); |
57 | 63 |
58 executor = new RobolectricLooperExecutor(); | 64 executor = Executors.newSingleThreadExecutor(); |
59 executor.requestStart(); | |
60 } | 65 } |
61 | 66 |
62 @After | 67 @After |
63 public void tearDown() { | 68 public void tearDown() { |
64 verifyNoMoreEvents(); | 69 verifyNoMoreEvents(); |
65 | 70 |
66 executor.executeAndWait(new Runnable() { | 71 executeAndWait(new Runnable() { |
67 @Override | 72 @Override |
68 public void run() { | 73 public void run() { |
69 client.disconnect(); | 74 client.disconnect(); |
70 server.disconnect(); | 75 server.disconnect(); |
71 } | 76 } |
72 }); | 77 }); |
73 | 78 |
74 // Stop the executor thread | 79 // Stop the executor thread |
75 executor.requestStop(); | 80 executor.shutdown(); |
76 try { | 81 try { |
77 executor.join(); | 82 executor.awaitTermination(TERMINATION_TIMEOUT, TimeUnit.MILLISECONDS); |
78 } catch (InterruptedException e) { | 83 } catch (InterruptedException e) { |
79 fail(e.getMessage()); | 84 fail(e.getMessage()); |
80 } | 85 } |
81 } | 86 } |
82 | 87 |
83 @Test | 88 @Test |
84 public void testConnectIPv4() { | 89 public void testConnectIPv4() { |
85 setUpIPv4Server(); | 90 setUpIPv4Server(); |
86 try { | 91 try { |
87 Thread.sleep(SERVER_WAIT); | 92 Thread.sleep(SERVER_WAIT); |
(...skipping 17 matching lines...) Expand all Loading... |
105 setUpIPv6Client(); | 110 setUpIPv6Client(); |
106 | 111 |
107 verify(serverEvents, timeout(CONNECT_TIMEOUT)).onTCPConnected(true); | 112 verify(serverEvents, timeout(CONNECT_TIMEOUT)).onTCPConnected(true); |
108 verify(clientEvents, timeout(CONNECT_TIMEOUT)).onTCPConnected(false); | 113 verify(clientEvents, timeout(CONNECT_TIMEOUT)).onTCPConnected(false); |
109 } | 114 } |
110 | 115 |
111 @Test | 116 @Test |
112 public void testSendData() { | 117 public void testSendData() { |
113 testConnectIPv4(); | 118 testConnectIPv4(); |
114 | 119 |
115 executor.executeAndWait(new Runnable() { | 120 executeAndWait(new Runnable() { |
116 @Override | 121 @Override |
117 public void run() { | 122 public void run() { |
118 client.send(TEST_MESSAGE_SERVER); | 123 client.send(TEST_MESSAGE_SERVER); |
119 server.send(TEST_MESSAGE_CLIENT); | 124 server.send(TEST_MESSAGE_CLIENT); |
120 } | 125 } |
121 }); | 126 }); |
122 | 127 |
123 verify(serverEvents, timeout(SEND_TIMEOUT)).onTCPMessage(TEST_MESSAGE_SERVER
); | 128 verify(serverEvents, timeout(SEND_TIMEOUT)).onTCPMessage(TEST_MESSAGE_SERVER
); |
124 verify(clientEvents, timeout(SEND_TIMEOUT)).onTCPMessage(TEST_MESSAGE_CLIENT
); | 129 verify(clientEvents, timeout(SEND_TIMEOUT)).onTCPMessage(TEST_MESSAGE_CLIENT
); |
125 } | 130 } |
126 | 131 |
127 @Test | 132 @Test |
128 public void testDisconnectServer() { | 133 public void testDisconnectServer() { |
129 testConnectIPv4(); | 134 testConnectIPv4(); |
130 executor.executeAndWait(new Runnable() { | 135 executeAndWait(new Runnable() { |
131 @Override | 136 @Override |
132 public void run() { | 137 public void run() { |
133 server.disconnect(); | 138 server.disconnect(); |
134 } | 139 } |
135 }); | 140 }); |
136 | 141 |
137 verify(serverEvents, timeout(DISCONNECT_TIMEOUT)).onTCPClose(); | 142 verify(serverEvents, timeout(DISCONNECT_TIMEOUT)).onTCPClose(); |
138 verify(clientEvents, timeout(DISCONNECT_TIMEOUT)).onTCPClose(); | 143 verify(clientEvents, timeout(DISCONNECT_TIMEOUT)).onTCPClose(); |
139 } | 144 } |
140 | 145 |
141 @Test | 146 @Test |
142 public void testDisconnectClient() { | 147 public void testDisconnectClient() { |
143 testConnectIPv4(); | 148 testConnectIPv4(); |
144 executor.executeAndWait(new Runnable() { | 149 executeAndWait(new Runnable() { |
145 @Override | 150 @Override |
146 public void run() { | 151 public void run() { |
147 client.disconnect(); | 152 client.disconnect(); |
148 } | 153 } |
149 }); | 154 }); |
150 | 155 |
151 verify(serverEvents, timeout(DISCONNECT_TIMEOUT)).onTCPClose(); | 156 verify(serverEvents, timeout(DISCONNECT_TIMEOUT)).onTCPClose(); |
152 verify(clientEvents, timeout(DISCONNECT_TIMEOUT)).onTCPClose(); | 157 verify(clientEvents, timeout(DISCONNECT_TIMEOUT)).onTCPClose(); |
153 } | 158 } |
154 | 159 |
(...skipping 21 matching lines...) Expand all Loading... |
176 client = new TCPChannelClient(executor, clientEvents, ip, port); | 181 client = new TCPChannelClient(executor, clientEvents, ip, port); |
177 } | 182 } |
178 | 183 |
179 /** | 184 /** |
180 * Verifies no more server or client events have been issued | 185 * Verifies no more server or client events have been issued |
181 */ | 186 */ |
182 private void verifyNoMoreEvents() { | 187 private void verifyNoMoreEvents() { |
183 verifyNoMoreInteractions(serverEvents); | 188 verifyNoMoreInteractions(serverEvents); |
184 verifyNoMoreInteractions(clientEvents); | 189 verifyNoMoreInteractions(clientEvents); |
185 } | 190 } |
| 191 |
| 192 /** |
| 193 * Queues runnable to be run and waits for it to be executed by the executor t
hread |
| 194 */ |
| 195 public void executeAndWait(Runnable runnable) { |
| 196 try { |
| 197 executor.submit(runnable).get(); |
| 198 } catch (Exception e) { |
| 199 fail(e.getMessage()); |
| 200 } |
| 201 } |
186 } | 202 } |
OLD | NEW |