Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | package org.mozilla.gecko.background.common; |
michael@0 | 5 | |
michael@0 | 6 | import junit.framework.AssertionFailedError; |
michael@0 | 7 | |
michael@0 | 8 | import org.mozilla.gecko.background.helpers.AndroidSyncTestCase; |
michael@0 | 9 | import org.mozilla.gecko.background.testhelpers.WaitHelper; |
michael@0 | 10 | import org.mozilla.gecko.background.testhelpers.WaitHelper.InnerError; |
michael@0 | 11 | import org.mozilla.gecko.background.testhelpers.WaitHelper.TimeoutError; |
michael@0 | 12 | import org.mozilla.gecko.sync.ThreadPool; |
michael@0 | 13 | |
michael@0 | 14 | public class TestWaitHelper extends AndroidSyncTestCase { |
michael@0 | 15 | private static final String ERROR_UNIQUE_IDENTIFIER = "error unique identifier"; |
michael@0 | 16 | |
michael@0 | 17 | public static int NO_WAIT = 1; // Milliseconds. |
michael@0 | 18 | public static int SHORT_WAIT = 100; // Milliseconds. |
michael@0 | 19 | public static int LONG_WAIT = 3 * SHORT_WAIT; |
michael@0 | 20 | |
michael@0 | 21 | private Object notifyMonitor = new Object(); |
michael@0 | 22 | // Guarded by notifyMonitor. |
michael@0 | 23 | private boolean performNotifyCalled = false; |
michael@0 | 24 | private boolean performNotifyErrorCalled = false; |
michael@0 | 25 | private void setPerformNotifyCalled() { |
michael@0 | 26 | synchronized (notifyMonitor) { |
michael@0 | 27 | performNotifyCalled = true; |
michael@0 | 28 | } |
michael@0 | 29 | } |
michael@0 | 30 | private void setPerformNotifyErrorCalled() { |
michael@0 | 31 | synchronized (notifyMonitor) { |
michael@0 | 32 | performNotifyErrorCalled = true; |
michael@0 | 33 | } |
michael@0 | 34 | } |
michael@0 | 35 | private void resetNotifyCalled() { |
michael@0 | 36 | synchronized (notifyMonitor) { |
michael@0 | 37 | performNotifyCalled = false; |
michael@0 | 38 | performNotifyErrorCalled = false; |
michael@0 | 39 | } |
michael@0 | 40 | } |
michael@0 | 41 | private void assertBothCalled() { |
michael@0 | 42 | synchronized (notifyMonitor) { |
michael@0 | 43 | assertTrue(performNotifyCalled); |
michael@0 | 44 | assertTrue(performNotifyErrorCalled); |
michael@0 | 45 | } |
michael@0 | 46 | } |
michael@0 | 47 | private void assertErrorCalled() { |
michael@0 | 48 | synchronized (notifyMonitor) { |
michael@0 | 49 | assertFalse(performNotifyCalled); |
michael@0 | 50 | assertTrue(performNotifyErrorCalled); |
michael@0 | 51 | } |
michael@0 | 52 | } |
michael@0 | 53 | private void assertCalled() { |
michael@0 | 54 | synchronized (notifyMonitor) { |
michael@0 | 55 | assertTrue(performNotifyCalled); |
michael@0 | 56 | assertFalse(performNotifyErrorCalled); |
michael@0 | 57 | } |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | public WaitHelper waitHelper; |
michael@0 | 61 | |
michael@0 | 62 | public TestWaitHelper() { |
michael@0 | 63 | super(); |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | public void setUp() { |
michael@0 | 67 | WaitHelper.resetTestWaiter(); |
michael@0 | 68 | waitHelper = WaitHelper.getTestWaiter(); |
michael@0 | 69 | resetNotifyCalled(); |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | public void tearDown() { |
michael@0 | 73 | assertTrue(waitHelper.isIdle()); |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | public Runnable performNothingRunnable() { |
michael@0 | 77 | return new Runnable() { |
michael@0 | 78 | public void run() { |
michael@0 | 79 | } |
michael@0 | 80 | }; |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | public Runnable performNotifyRunnable() { |
michael@0 | 84 | return new Runnable() { |
michael@0 | 85 | public void run() { |
michael@0 | 86 | setPerformNotifyCalled(); |
michael@0 | 87 | waitHelper.performNotify(); |
michael@0 | 88 | } |
michael@0 | 89 | }; |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | public Runnable performNotifyAfterDelayRunnable(final int delayInMillis) { |
michael@0 | 93 | return new Runnable() { |
michael@0 | 94 | public void run() { |
michael@0 | 95 | try { |
michael@0 | 96 | Thread.sleep(delayInMillis); |
michael@0 | 97 | } catch (InterruptedException e) { |
michael@0 | 98 | fail("Interrupted."); |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | setPerformNotifyCalled(); |
michael@0 | 102 | waitHelper.performNotify(); |
michael@0 | 103 | } |
michael@0 | 104 | }; |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | public Runnable performNotifyErrorRunnable() { |
michael@0 | 108 | return new Runnable() { |
michael@0 | 109 | public void run() { |
michael@0 | 110 | setPerformNotifyCalled(); |
michael@0 | 111 | waitHelper.performNotify(new AssertionFailedError(ERROR_UNIQUE_IDENTIFIER)); |
michael@0 | 112 | } |
michael@0 | 113 | }; |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | public Runnable inThreadPool(final Runnable runnable) { |
michael@0 | 117 | return new Runnable() { |
michael@0 | 118 | @Override |
michael@0 | 119 | public void run() { |
michael@0 | 120 | ThreadPool.run(runnable); |
michael@0 | 121 | } |
michael@0 | 122 | }; |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | public Runnable inThread(final Runnable runnable) { |
michael@0 | 126 | return new Runnable() { |
michael@0 | 127 | @Override |
michael@0 | 128 | public void run() { |
michael@0 | 129 | new Thread(runnable).start(); |
michael@0 | 130 | } |
michael@0 | 131 | }; |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | protected void expectAssertionFailedError(Runnable runnable) { |
michael@0 | 135 | try { |
michael@0 | 136 | waitHelper.performWait(runnable); |
michael@0 | 137 | } catch (InnerError e) { |
michael@0 | 138 | AssertionFailedError inner = (AssertionFailedError)e.innerError; |
michael@0 | 139 | setPerformNotifyErrorCalled(); |
michael@0 | 140 | String message = inner.getMessage(); |
michael@0 | 141 | assertTrue("Expected '" + message + "' to contain '" + ERROR_UNIQUE_IDENTIFIER + "'", |
michael@0 | 142 | message.contains(ERROR_UNIQUE_IDENTIFIER)); |
michael@0 | 143 | } |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | protected void expectAssertionFailedErrorAfterDelay(int wait, Runnable runnable) { |
michael@0 | 147 | try { |
michael@0 | 148 | waitHelper.performWait(wait, runnable); |
michael@0 | 149 | } catch (InnerError e) { |
michael@0 | 150 | AssertionFailedError inner = (AssertionFailedError)e.innerError; |
michael@0 | 151 | setPerformNotifyErrorCalled(); |
michael@0 | 152 | String message = inner.getMessage(); |
michael@0 | 153 | assertTrue("Expected '" + message + "' to contain '" + ERROR_UNIQUE_IDENTIFIER + "'", |
michael@0 | 154 | message.contains(ERROR_UNIQUE_IDENTIFIER)); |
michael@0 | 155 | } |
michael@0 | 156 | } |
michael@0 | 157 | |
michael@0 | 158 | public void testPerformWait() { |
michael@0 | 159 | waitHelper.performWait(performNotifyRunnable()); |
michael@0 | 160 | assertCalled(); |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | public void testPerformWaitInThread() { |
michael@0 | 164 | waitHelper.performWait(inThread(performNotifyRunnable())); |
michael@0 | 165 | assertCalled(); |
michael@0 | 166 | } |
michael@0 | 167 | |
michael@0 | 168 | public void testPerformWaitInThreadPool() { |
michael@0 | 169 | waitHelper.performWait(inThreadPool(performNotifyRunnable())); |
michael@0 | 170 | assertCalled(); |
michael@0 | 171 | } |
michael@0 | 172 | |
michael@0 | 173 | public void testPerformTimeoutWait() { |
michael@0 | 174 | waitHelper.performWait(SHORT_WAIT, performNotifyRunnable()); |
michael@0 | 175 | assertCalled(); |
michael@0 | 176 | } |
michael@0 | 177 | |
michael@0 | 178 | public void testPerformTimeoutWaitInThread() { |
michael@0 | 179 | waitHelper.performWait(SHORT_WAIT, inThread(performNotifyRunnable())); |
michael@0 | 180 | assertCalled(); |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | public void testPerformTimeoutWaitInThreadPool() { |
michael@0 | 184 | waitHelper.performWait(SHORT_WAIT, inThreadPool(performNotifyRunnable())); |
michael@0 | 185 | assertCalled(); |
michael@0 | 186 | } |
michael@0 | 187 | |
michael@0 | 188 | public void testPerformErrorWaitInThread() { |
michael@0 | 189 | expectAssertionFailedError(inThread(performNotifyErrorRunnable())); |
michael@0 | 190 | assertBothCalled(); |
michael@0 | 191 | } |
michael@0 | 192 | |
michael@0 | 193 | public void testPerformErrorWaitInThreadPool() { |
michael@0 | 194 | expectAssertionFailedError(inThreadPool(performNotifyErrorRunnable())); |
michael@0 | 195 | assertBothCalled(); |
michael@0 | 196 | } |
michael@0 | 197 | |
michael@0 | 198 | public void testPerformErrorTimeoutWaitInThread() { |
michael@0 | 199 | expectAssertionFailedErrorAfterDelay(SHORT_WAIT, inThread(performNotifyErrorRunnable())); |
michael@0 | 200 | assertBothCalled(); |
michael@0 | 201 | } |
michael@0 | 202 | |
michael@0 | 203 | public void testPerformErrorTimeoutWaitInThreadPool() { |
michael@0 | 204 | expectAssertionFailedErrorAfterDelay(SHORT_WAIT, inThreadPool(performNotifyErrorRunnable())); |
michael@0 | 205 | assertBothCalled(); |
michael@0 | 206 | } |
michael@0 | 207 | |
michael@0 | 208 | public void testTimeout() { |
michael@0 | 209 | try { |
michael@0 | 210 | waitHelper.performWait(SHORT_WAIT, performNothingRunnable()); |
michael@0 | 211 | } catch (TimeoutError e) { |
michael@0 | 212 | setPerformNotifyErrorCalled(); |
michael@0 | 213 | assertEquals(SHORT_WAIT, e.waitTimeInMillis); |
michael@0 | 214 | } |
michael@0 | 215 | assertErrorCalled(); |
michael@0 | 216 | } |
michael@0 | 217 | |
michael@0 | 218 | /** |
michael@0 | 219 | * This will pass. The sequence in the main thread is: |
michael@0 | 220 | * - A short delay. |
michael@0 | 221 | * - performNotify is called. |
michael@0 | 222 | * - performWait is called and immediately finds that performNotify was called before. |
michael@0 | 223 | */ |
michael@0 | 224 | public void testDelay() { |
michael@0 | 225 | try { |
michael@0 | 226 | waitHelper.performWait(1, performNotifyAfterDelayRunnable(SHORT_WAIT)); |
michael@0 | 227 | } catch (AssertionFailedError e) { |
michael@0 | 228 | setPerformNotifyErrorCalled(); |
michael@0 | 229 | assertTrue(e.getMessage(), e.getMessage().contains("TIMEOUT")); |
michael@0 | 230 | } |
michael@0 | 231 | assertCalled(); |
michael@0 | 232 | } |
michael@0 | 233 | |
michael@0 | 234 | public Runnable performNotifyMultipleTimesRunnable() { |
michael@0 | 235 | return new Runnable() { |
michael@0 | 236 | public void run() { |
michael@0 | 237 | waitHelper.performNotify(); |
michael@0 | 238 | setPerformNotifyCalled(); |
michael@0 | 239 | waitHelper.performNotify(); |
michael@0 | 240 | } |
michael@0 | 241 | }; |
michael@0 | 242 | } |
michael@0 | 243 | |
michael@0 | 244 | public void testPerformNotifyMultipleTimesFails() { |
michael@0 | 245 | try { |
michael@0 | 246 | waitHelper.performWait(NO_WAIT, performNotifyMultipleTimesRunnable()); // Not run on thread, so runnable executes before performWait looks for notifications. |
michael@0 | 247 | } catch (WaitHelper.MultipleNotificationsError e) { |
michael@0 | 248 | setPerformNotifyErrorCalled(); |
michael@0 | 249 | } |
michael@0 | 250 | assertBothCalled(); |
michael@0 | 251 | assertFalse(waitHelper.isIdle()); // First perform notify should be hanging around. |
michael@0 | 252 | waitHelper.performWait(NO_WAIT, performNothingRunnable()); |
michael@0 | 253 | } |
michael@0 | 254 | |
michael@0 | 255 | public void testNestedWaitsAndNotifies() { |
michael@0 | 256 | waitHelper.performWait(new Runnable() { |
michael@0 | 257 | @Override |
michael@0 | 258 | public void run() { |
michael@0 | 259 | waitHelper.performWait(new Runnable() { |
michael@0 | 260 | public void run() { |
michael@0 | 261 | setPerformNotifyCalled(); |
michael@0 | 262 | waitHelper.performNotify(); |
michael@0 | 263 | } |
michael@0 | 264 | }); |
michael@0 | 265 | setPerformNotifyErrorCalled(); |
michael@0 | 266 | waitHelper.performNotify(); |
michael@0 | 267 | } |
michael@0 | 268 | }); |
michael@0 | 269 | assertBothCalled(); |
michael@0 | 270 | } |
michael@0 | 271 | |
michael@0 | 272 | public void testAssertIsReported() { |
michael@0 | 273 | try { |
michael@0 | 274 | waitHelper.performWait(1, new Runnable() { |
michael@0 | 275 | @Override |
michael@0 | 276 | public void run() { |
michael@0 | 277 | assertTrue("unique identifier", false); |
michael@0 | 278 | } |
michael@0 | 279 | }); |
michael@0 | 280 | } catch (AssertionFailedError e) { |
michael@0 | 281 | setPerformNotifyErrorCalled(); |
michael@0 | 282 | assertTrue(e.getMessage(), e.getMessage().contains("unique identifier")); |
michael@0 | 283 | } |
michael@0 | 284 | assertErrorCalled(); |
michael@0 | 285 | } |
michael@0 | 286 | |
michael@0 | 287 | /** |
michael@0 | 288 | * The inner wait will timeout, but the outer wait will succeed. The sequence in the helper thread is: |
michael@0 | 289 | * - A short delay. |
michael@0 | 290 | * - performNotify is called. |
michael@0 | 291 | * |
michael@0 | 292 | * The sequence in the main thread is: |
michael@0 | 293 | * - performWait is called and times out because the helper thread does not call |
michael@0 | 294 | * performNotify quickly enough. |
michael@0 | 295 | */ |
michael@0 | 296 | public void testDelayInThread() throws InterruptedException { |
michael@0 | 297 | waitHelper.performWait(new Runnable() { |
michael@0 | 298 | @Override |
michael@0 | 299 | public void run() { |
michael@0 | 300 | try { |
michael@0 | 301 | waitHelper.performWait(NO_WAIT, inThread(new Runnable() { |
michael@0 | 302 | public void run() { |
michael@0 | 303 | try { |
michael@0 | 304 | Thread.sleep(SHORT_WAIT); |
michael@0 | 305 | } catch (InterruptedException e) { |
michael@0 | 306 | fail("Interrupted."); |
michael@0 | 307 | } |
michael@0 | 308 | |
michael@0 | 309 | setPerformNotifyCalled(); |
michael@0 | 310 | waitHelper.performNotify(); |
michael@0 | 311 | } |
michael@0 | 312 | })); |
michael@0 | 313 | } catch (WaitHelper.TimeoutError e) { |
michael@0 | 314 | setPerformNotifyErrorCalled(); |
michael@0 | 315 | assertEquals(NO_WAIT, e.waitTimeInMillis); |
michael@0 | 316 | } |
michael@0 | 317 | } |
michael@0 | 318 | }); |
michael@0 | 319 | assertBothCalled(); |
michael@0 | 320 | } |
michael@0 | 321 | |
michael@0 | 322 | /** |
michael@0 | 323 | * The inner wait will timeout, but the outer wait will succeed. The sequence in the helper thread is: |
michael@0 | 324 | * - A short delay. |
michael@0 | 325 | * - performNotify is called. |
michael@0 | 326 | * |
michael@0 | 327 | * The sequence in the main thread is: |
michael@0 | 328 | * - performWait is called and times out because the helper thread does not call |
michael@0 | 329 | * performNotify quickly enough. |
michael@0 | 330 | */ |
michael@0 | 331 | public void testDelayInThreadPool() throws InterruptedException { |
michael@0 | 332 | waitHelper.performWait(new Runnable() { |
michael@0 | 333 | @Override |
michael@0 | 334 | public void run() { |
michael@0 | 335 | try { |
michael@0 | 336 | waitHelper.performWait(NO_WAIT, inThreadPool(new Runnable() { |
michael@0 | 337 | public void run() { |
michael@0 | 338 | try { |
michael@0 | 339 | Thread.sleep(SHORT_WAIT); |
michael@0 | 340 | } catch (InterruptedException e) { |
michael@0 | 341 | fail("Interrupted."); |
michael@0 | 342 | } |
michael@0 | 343 | |
michael@0 | 344 | setPerformNotifyCalled(); |
michael@0 | 345 | waitHelper.performNotify(); |
michael@0 | 346 | } |
michael@0 | 347 | })); |
michael@0 | 348 | } catch (WaitHelper.TimeoutError e) { |
michael@0 | 349 | setPerformNotifyErrorCalled(); |
michael@0 | 350 | assertEquals(NO_WAIT, e.waitTimeInMillis); |
michael@0 | 351 | } |
michael@0 | 352 | } |
michael@0 | 353 | }); |
michael@0 | 354 | assertBothCalled(); |
michael@0 | 355 | } |
michael@0 | 356 | } |