Skip to content

Commit 0af249a

Browse files
authored
Fix #695 (#696)
* Fix #695
1 parent 8db6076 commit 0af249a

File tree

3 files changed

+48
-21
lines changed

3 files changed

+48
-21
lines changed

core/src/main/java/edu/wpi/grip/core/sources/CameraSource.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,12 +285,24 @@ public CameraSource stopAsync() {
285285

286286
@Override
287287
public void stopAndAwait() {
288-
stopAsync().cameraService.stopAndAwait();
288+
try {
289+
stopAsync().cameraService.stopAndAwait();
290+
} catch (IllegalStateException e) {
291+
if (!cameraService.state().equals(State.FAILED)) {
292+
throw e;
293+
}
294+
}
289295
}
290296

291297
@Override
292298
public void stopAndAwait(long timeout, TimeUnit unit) throws TimeoutException {
293-
stopAsync().cameraService.stopAndAwait(timeout, unit);
299+
try {
300+
stopAsync().cameraService.stopAndAwait(timeout, unit);
301+
} catch (IllegalStateException e) {
302+
if (!cameraService.state().equals(State.FAILED)) {
303+
throw e;
304+
}
305+
}
294306
}
295307

296308
@Override

core/src/test/java/edu/wpi/grip/core/sources/CameraSourceTest.java

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.google.common.base.Throwables;
1313
import com.google.common.eventbus.EventBus;
1414
import com.google.common.eventbus.Subscribe;
15+
import com.google.common.util.concurrent.MoreExecutors;
1516
import com.google.common.util.concurrent.Service;
1617
import com.google.inject.Guice;
1718
import com.google.inject.Injector;
@@ -41,7 +42,6 @@
4142
import java.util.logging.Level;
4243
import java.util.logging.Logger;
4344

44-
import static com.google.common.truth.Truth.assertThat;
4545
import static org.junit.Assert.assertEquals;
4646
import static org.junit.Assert.assertFalse;
4747
import static org.junit.Assert.assertNotEquals;
@@ -71,7 +71,7 @@ public void setUp() throws Exception {
7171
final EventBus eventBus = new EventBus();
7272
class UnhandledExceptionWitness {
7373
@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")
7575
@Subscribe
7676
public void onUnexpectedThrowableEvent(UnexpectedThrowableEvent event) {
7777
event.handleSafely((throwable, message, isFatal) -> {
@@ -120,14 +120,18 @@ public void testCallingStopAndStartDoesNotDeadlock() throws Exception {
120120
@Test
121121
public void testStartRethrowsIfFailure() throws Exception {
122122
mockFrameGrabberFactory.frameGrabber.setShouldThrowAtStart(true);
123+
Waiter failWaiter = new Waiter();
123124
// 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();
131135

132136
assertFalse("Camera service has stopped completely", cameraSourceWithMockGrabber.isRunning());
133137
}
@@ -193,18 +197,22 @@ public FrameGrabber create(String addressProperty) throws MalformedURLException
193197
}
194198
}, MockExceptionWitness.MOCK_FACTORY, 0);
195199

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());
196210
source.startAsync();
197211
waiter1.await();
198212
waiter2.await();
199213
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();
208216
}
209217

210218
@Test

ui/src/main/java/edu/wpi/grip/ui/analysis/AnalysisController.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import edu.wpi.grip.core.StepIndexer;
66
import edu.wpi.grip.core.events.BenchmarkEvent;
77
import edu.wpi.grip.core.events.RunStoppedEvent;
8+
import edu.wpi.grip.core.events.StepRemovedEvent;
89
import edu.wpi.grip.core.events.TimerEvent;
910
import edu.wpi.grip.core.metrics.BenchmarkRunner;
1011
import edu.wpi.grip.core.metrics.CsvExporter;
@@ -67,7 +68,7 @@ public class AnalysisController {
6768
private BenchmarkRunner benchmarker;
6869

6970
private final Callback<StepStatisticsEntry, Observable[]> extractor =
70-
entry -> new Observable[] {entry.stepProperty(), entry.analysisProperty()};
71+
entry -> new Observable[]{entry.stepProperty(), entry.analysisProperty()};
7172
private final ObservableList<StepStatisticsEntry> tableItems
7273
= FXCollections.observableArrayList(extractor);
7374

@@ -142,7 +143,13 @@ private void onRun(TimerEvent event) {
142143
}
143144

144145
@Subscribe
145-
@SuppressWarnings( {"PMD.UnusedPrivateMethod", "PMD.UnusedFormalParameter"})
146+
@SuppressWarnings("PMD.UnusedPrivateMethod")
147+
private void onStepRemoved(StepRemovedEvent e) {
148+
sampleMap.remove(e.getStep());
149+
}
150+
151+
@Subscribe
152+
@SuppressWarnings({"PMD.UnusedPrivateMethod", "PMD.UnusedFormalParameter"})
146153
private void onPipelineFinish(@Nullable RunStoppedEvent event) {
147154
double[] averageRunTimes = sortedStream(sampleMap)
148155
.parallel()

0 commit comments

Comments
 (0)