|
12 | 12 | import com.google.common.base.Throwables; |
13 | 13 | import com.google.common.eventbus.EventBus; |
14 | 14 | import com.google.common.eventbus.Subscribe; |
| 15 | +import com.google.common.util.concurrent.MoreExecutors; |
15 | 16 | import com.google.common.util.concurrent.Service; |
16 | 17 | import com.google.inject.Guice; |
17 | 18 | import com.google.inject.Injector; |
|
41 | 42 | import java.util.logging.Level; |
42 | 43 | import java.util.logging.Logger; |
43 | 44 |
|
44 | | -import static com.google.common.truth.Truth.assertThat; |
45 | 45 | import static org.junit.Assert.assertEquals; |
46 | 46 | import static org.junit.Assert.assertFalse; |
47 | 47 | import static org.junit.Assert.assertNotEquals; |
@@ -71,7 +71,7 @@ public void setUp() throws Exception { |
71 | 71 | final EventBus eventBus = new EventBus(); |
72 | 72 | class UnhandledExceptionWitness { |
73 | 73 | @SuppressFBWarnings(value = "UMAC_UNCALLABLE_METHOD_OF_ANONYMOUS_CLASS", |
74 | | - justification = "This method is called by Guava's EventBus") |
| 74 | + justification = "This method is called by Guava's EventBus") |
75 | 75 | @Subscribe |
76 | 76 | public void onUnexpectedThrowableEvent(UnexpectedThrowableEvent event) { |
77 | 77 | event.handleSafely((throwable, message, isFatal) -> { |
@@ -120,14 +120,18 @@ public void testCallingStopAndStartDoesNotDeadlock() throws Exception { |
120 | 120 | @Test |
121 | 121 | public void testStartRethrowsIfFailure() throws Exception { |
122 | 122 | mockFrameGrabberFactory.frameGrabber.setShouldThrowAtStart(true); |
| 123 | + Waiter failWaiter = new Waiter(); |
123 | 124 | // Problems starting should be restarted |
124 | | - try { |
125 | | - cameraSourceWithMockGrabber.startAsync().stopAndAwait(); |
126 | | - fail("Should have thrown an exception"); |
127 | | - } catch (IllegalStateException e) { |
128 | | - assertThat(e.getCause()).isNotNull(); |
129 | | - assertThat(e.getCause()).isInstanceOf(GrabberService.GrabberServiceException.class); |
130 | | - } |
| 125 | + cameraSourceWithMockGrabber.addListener(new Service.Listener() { |
| 126 | + @Override |
| 127 | + public void failed(Service.State from, Throwable failure) { |
| 128 | + failWaiter.assertNotNull(failure); |
| 129 | + failWaiter.assertTrue(failure instanceof GrabberService.GrabberServiceException); |
| 130 | + failWaiter.resume(); |
| 131 | + } |
| 132 | + }, MoreExecutors.directExecutor()); |
| 133 | + cameraSourceWithMockGrabber.startAsync().stopAndAwait(); |
| 134 | + failWaiter.await(); |
131 | 135 |
|
132 | 136 | assertFalse("Camera service has stopped completely", cameraSourceWithMockGrabber.isRunning()); |
133 | 137 | } |
@@ -193,18 +197,22 @@ public FrameGrabber create(String addressProperty) throws MalformedURLException |
193 | 197 | } |
194 | 198 | }, MockExceptionWitness.MOCK_FACTORY, 0); |
195 | 199 |
|
| 200 | + Waiter failedWaiter = new Waiter(); |
| 201 | + source.addListener(new Service.Listener() { |
| 202 | + @Override |
| 203 | + public void failed(Service.State from, Throwable failure) { |
| 204 | + failedWaiter.assertNotNull(failure); |
| 205 | + failedWaiter.assertNotNull(failure.getCause()); |
| 206 | + failedWaiter.assertEquals(GRABBER_START_MESSAGE, failure.getCause().getMessage()); |
| 207 | + failedWaiter.resume(); |
| 208 | + } |
| 209 | + }, MoreExecutors.directExecutor()); |
196 | 210 | source.startAsync(); |
197 | 211 | waiter1.await(); |
198 | 212 | waiter2.await(); |
199 | 213 | waiter3.await(); |
200 | | - try { |
201 | | - source.stopAndAwait(); |
202 | | - fail("This should have failed"); |
203 | | - } catch (IllegalStateException e) { |
204 | | - assertThat(e.getCause()).isNotNull(); |
205 | | - assertThat(e.getCause().getCause()).isNotNull(); |
206 | | - assertThat(e.getCause().getCause()).hasMessage(GRABBER_START_MESSAGE); |
207 | | - } |
| 214 | + source.stopAndAwait(); |
| 215 | + failedWaiter.await(); |
208 | 216 | } |
209 | 217 |
|
210 | 218 | @Test |
|
0 commit comments