mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-19 00:41:43 +00:00
[examples,cscore] Don't use assertTimeoutPreemptively in tests (#8951)
Particularly with short timeouts, this results in spurious failures under high CPU load.
This commit is contained in:
@@ -5,10 +5,8 @@
|
|||||||
package org.wpilib.vision.camera;
|
package org.wpilib.vision.camera;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
import java.time.Duration;
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
@@ -23,16 +21,15 @@ class UsbCameraTest {
|
|||||||
@EnabledOnOs(OS.LINUX)
|
@EnabledOnOs(OS.LINUX)
|
||||||
static class ConnectVerbose {
|
static class ConnectVerbose {
|
||||||
@Test
|
@Test
|
||||||
void setConnectVerboseEnabledTest() {
|
void setConnectVerboseEnabledTest() throws Exception {
|
||||||
try (UsbCamera camera = new UsbCamera("Nonexistent Camera", getNonexistentCameraDev())) {
|
try (UsbCamera camera = new UsbCamera("Nonexistent Camera", getNonexistentCameraDev())) {
|
||||||
camera.setConnectVerbose(1);
|
camera.setConnectVerbose(1);
|
||||||
|
|
||||||
CompletableFuture<String> result = new CompletableFuture<>();
|
CompletableFuture<String> result = new CompletableFuture<>();
|
||||||
CameraServerJNI.setLogger((level, file, line, message) -> result.complete(message), 20);
|
CameraServerJNI.setLogger((level, file, line, message) -> result.complete(message), 20);
|
||||||
|
|
||||||
assertTimeoutPreemptively(
|
assertTrue(
|
||||||
Duration.ofSeconds(5),
|
result.get(5, TimeUnit.SECONDS).contains("Attempting to connect to USB camera on "));
|
||||||
() -> assertTrue(result.get().contains("Attempting to connect to USB camera on ")));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,11 +5,9 @@
|
|||||||
package org.wpilib.snippets.i2ccommunication;
|
package org.wpilib.snippets.i2ccommunication;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.time.Duration;
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import org.junit.jupiter.api.AfterEach;
|
import org.junit.jupiter.api.AfterEach;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
@@ -65,6 +63,11 @@ class I2CCommunicationTest {
|
|||||||
i2c.resetData();
|
i2c.resetData();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String getWriteString() {
|
||||||
|
assertTrue(future.isDone(), "I2C write callback did not run during timing step");
|
||||||
|
return future.join();
|
||||||
|
}
|
||||||
|
|
||||||
@EnumSource(AllianceStationID.class)
|
@EnumSource(AllianceStationID.class)
|
||||||
@ParameterizedTest(name = "alliance[{index}]: {0}")
|
@ParameterizedTest(name = "alliance[{index}]: {0}")
|
||||||
void allianceTest(AllianceStationID alliance) {
|
void allianceTest(AllianceStationID alliance) {
|
||||||
@@ -75,7 +78,7 @@ class I2CCommunicationTest {
|
|||||||
|
|
||||||
SimHooks.stepTiming(0.02);
|
SimHooks.stepTiming(0.02);
|
||||||
|
|
||||||
String str = assertTimeoutPreemptively(Duration.ofMillis(20L), () -> future.get());
|
String str = getWriteString();
|
||||||
char expected = alliance.name().startsWith("RED") ? 'R' : 'B';
|
char expected = alliance.name().startsWith("RED") ? 'R' : 'B';
|
||||||
if (alliance.name().startsWith("UNKNOWN")) {
|
if (alliance.name().startsWith("UNKNOWN")) {
|
||||||
expected = 'U';
|
expected = 'U';
|
||||||
@@ -94,7 +97,7 @@ class I2CCommunicationTest {
|
|||||||
|
|
||||||
SimHooks.stepTiming(0.02);
|
SimHooks.stepTiming(0.02);
|
||||||
|
|
||||||
String str = assertTimeoutPreemptively(Duration.ofMillis(20L), () -> future.get());
|
String str = getWriteString();
|
||||||
char expected = enabled ? 'E' : 'D';
|
char expected = enabled ? 'E' : 'D';
|
||||||
|
|
||||||
assertEquals(expected, str.charAt(1));
|
assertEquals(expected, str.charAt(1));
|
||||||
@@ -110,7 +113,7 @@ class I2CCommunicationTest {
|
|||||||
|
|
||||||
SimHooks.stepTiming(0.02);
|
SimHooks.stepTiming(0.02);
|
||||||
|
|
||||||
String str = assertTimeoutPreemptively(Duration.ofMillis(20L), () -> future.get());
|
String str = getWriteString();
|
||||||
char expected = autonomous ? 'A' : 'T';
|
char expected = autonomous ? 'A' : 'T';
|
||||||
|
|
||||||
assertEquals(expected, str.charAt(2));
|
assertEquals(expected, str.charAt(2));
|
||||||
@@ -125,7 +128,7 @@ class I2CCommunicationTest {
|
|||||||
|
|
||||||
SimHooks.stepTiming(0.02);
|
SimHooks.stepTiming(0.02);
|
||||||
|
|
||||||
String str = assertTimeoutPreemptively(Duration.ofMillis(20L), () -> future.get());
|
String str = getWriteString();
|
||||||
String expected = String.format("%03d", (int) MatchState.getMatchTime());
|
String expected = String.format("%03d", (int) MatchState.getMatchTime());
|
||||||
|
|
||||||
assertEquals(expected, str.substring(3));
|
assertEquals(expected, str.substring(3));
|
||||||
|
|||||||
Reference in New Issue
Block a user