Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,13 @@
import edu.wpi.first.networktables.NetworkTable;
import edu.wpi.first.networktables.NetworkTableInstance;
import edu.wpi.first.wpilibj.Preferences;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.junit.rules.ExternalResource;

/**
* A JUnit rule that ensures that changes to preferences done by a test are not leaked out to other
* tests.
*/
public final class IsolatedPreferences extends ExternalResource {
private static final ScheduledExecutorService CLOSE_EXECUTOR =
Executors.newSingleThreadScheduledExecutor();
private NetworkTableInstance tempInstance;

/** Gets the {@link NetworkTable} that contains the preference values. */
Expand All @@ -26,12 +21,18 @@ public NetworkTable getPreferencesTable() {
protected void before() {
NetworkTableInstance.getDefault();
tempInstance = NetworkTableInstance.create();
tempInstance.startLocal();
Preferences.setNetworkTableInstance(tempInstance);
}

@Override
protected void after() {
if (!tempInstance.waitForListenerQueue(.1)) {
System.err.println(
"Timed out waiting for the NetworkTableInstance listener queue to empty (waited 100ms);"
+ " JVM may crash");
}
Preferences.setNetworkTableInstance(NetworkTableInstance.getDefault());
CLOSE_EXECUTOR.schedule(() -> tempInstance.close(), 10, TimeUnit.MILLISECONDS);
tempInstance.close();
}
}
Loading