OLD | NEW |
| (Empty) |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.chrome.browser; | |
6 | |
7 import static org.junit.Assert.assertFalse; | |
8 import static org.junit.Assert.assertTrue; | |
9 import static org.junit.Assert.fail; | |
10 | |
11 import org.junit.Test; | |
12 import org.junit.runner.RunWith; | |
13 import org.junit.runners.JUnit4; | |
14 | |
15 import java.util.concurrent.ExecutionException; | |
16 import java.util.concurrent.Executors; | |
17 import java.util.concurrent.Future; | |
18 import java.util.concurrent.atomic.AtomicBoolean; | |
19 | |
20 /** | |
21 * Unit tests for the ChromeBackgroundServiceWaiter. | |
22 */ | |
23 @RunWith(JUnit4.class) | |
24 public class ChromeBackgroundServiceWaiterTest { | |
25 private static final int TIMEOUT_SECONDS = 30; | |
26 | |
27 private AtomicBoolean mFinishedWaiting = new AtomicBoolean(); | |
28 private AtomicBoolean mWaitFailed = new AtomicBoolean(); | |
29 | |
30 private boolean getFinishedWaiting() { | |
31 return mFinishedWaiting.get(); | |
32 } | |
33 | |
34 private void setFinishedWaiting(boolean newValue) { | |
35 mFinishedWaiting.getAndSet(newValue); | |
36 } | |
37 | |
38 private boolean getWaitFailed() { | |
39 return mWaitFailed.get(); | |
40 } | |
41 | |
42 @Test | |
43 public void testWaiter() { | |
44 final ChromeBackgroundServiceWaiter waiter = | |
45 new ChromeBackgroundServiceWaiter(TIMEOUT_SECONDS); | |
46 | |
47 Future<?> future = Executors.newSingleThreadExecutor().submit(new Runnab
le() { | |
48 @Override | |
49 public void run() { | |
50 // The finished waiting flag should not be set, if it is, the wa
it operation failed. | |
51 if (getFinishedWaiting()) { | |
52 mWaitFailed.getAndSet(true); | |
53 } | |
54 waiter.onWaitDone(); | |
55 } | |
56 }); | |
57 | |
58 // Wait for the thread, and set the flag after we are done waiting. | |
59 waiter.startWaiting(); | |
60 setFinishedWaiting(true); | |
61 | |
62 // Wait for the "onWaitDone" thread to complete. | |
63 try { | |
64 future.get(); | |
65 } catch (InterruptedException | ExecutionException e) { | |
66 // Fail the test if we get an interrupted exception. | |
67 fail("InterruptedException or ExecutionException " + e); | |
68 } | |
69 | |
70 // Verify the thread unblocked, and the flag got set. | |
71 assertTrue(getFinishedWaiting()); | |
72 assertFalse(getWaitFailed()); | |
73 } | |
74 | |
75 } | |
OLD | NEW |