mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-26 01:51:41 +00:00
[ntcore] NetworkTables 4 (#3217)
This commit is contained in:
@@ -9,69 +9,83 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
import static org.junit.jupiter.api.parallel.ExecutionMode.SAME_THREAD;
|
||||
|
||||
import edu.wpi.first.networktables.NetworkTable;
|
||||
import edu.wpi.first.networktables.NetworkTableInstance;
|
||||
import edu.wpi.first.networktables.Topic;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Set;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.junit.jupiter.api.parallel.Execution;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
@Execution(SAME_THREAD)
|
||||
class PreferencesTest {
|
||||
private final NetworkTable m_table = NetworkTableInstance.getDefault().getTable("Preferences");
|
||||
private NetworkTableInstance m_inst;
|
||||
private NetworkTable m_table;
|
||||
|
||||
private static final String kFilename = "networktables.ini";
|
||||
|
||||
@BeforeAll
|
||||
static void setupAll() {
|
||||
NetworkTableInstance.getDefault().stopServer();
|
||||
}
|
||||
private static final String kFilename = "networktables.json";
|
||||
|
||||
@BeforeEach
|
||||
void setup(@TempDir Path tempDir) {
|
||||
m_table.getKeys().forEach(m_table::delete);
|
||||
m_inst = NetworkTableInstance.create();
|
||||
m_table = m_inst.getTable("Preferences");
|
||||
Preferences.setNetworkTableInstance(m_inst);
|
||||
|
||||
Path filepath = tempDir.resolve(kFilename);
|
||||
try (InputStream is = getClass().getResource("PreferencesTestDefault.ini").openStream()) {
|
||||
try (InputStream is = getClass().getResource("PreferencesTestDefault.json").openStream()) {
|
||||
Files.copy(is, filepath);
|
||||
} catch (IOException ex) {
|
||||
fail(ex);
|
||||
}
|
||||
|
||||
NetworkTableInstance.getDefault().startServer(filepath.toString());
|
||||
m_inst.startServer(filepath.toString(), "", 0, 0);
|
||||
try {
|
||||
int count = 0;
|
||||
while ((m_inst.getNetworkMode() & NetworkTableInstance.kNetModeStarting) != 0) {
|
||||
Thread.sleep(100);
|
||||
count++;
|
||||
if (count > 30) {
|
||||
throw new InterruptedException();
|
||||
}
|
||||
}
|
||||
} catch (InterruptedException ex) {
|
||||
fail("interrupted while waiting for server to start");
|
||||
}
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void cleanup() {
|
||||
NetworkTableInstance.getDefault().stopServer();
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void cleanupAll() {
|
||||
NetworkTableInstance.getDefault().startServer();
|
||||
m_inst.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
void removeAllTest() {
|
||||
Preferences.removeAll();
|
||||
|
||||
Set<String> keys = m_table.getKeys();
|
||||
keys.remove(".type");
|
||||
List<String> keys = new ArrayList<>();
|
||||
boolean anyPersistent = false;
|
||||
for (Topic topic : m_table.getTopics()) {
|
||||
if (topic.isPersistent()) {
|
||||
anyPersistent = true;
|
||||
keys.add(topic.getName());
|
||||
}
|
||||
}
|
||||
|
||||
assertTrue(
|
||||
keys.isEmpty(),
|
||||
assertFalse(
|
||||
anyPersistent,
|
||||
"Preferences was not empty! Preferences in table: " + Arrays.toString(keys.toArray()));
|
||||
}
|
||||
|
||||
@@ -98,19 +112,6 @@ class PreferencesTest {
|
||||
() -> assertFalse(Preferences.getBoolean("checkedValueBoolean", true)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void backupTest() {
|
||||
Preferences.removeAll();
|
||||
|
||||
assertAll(
|
||||
() -> assertEquals(0, Preferences.getLong("checkedValueLong", 0)),
|
||||
() -> assertEquals(0, Preferences.getDouble("checkedValueDouble", 0), 1e-6),
|
||||
() -> assertEquals("", Preferences.getString("checkedValueString", "")),
|
||||
() -> assertEquals(0, Preferences.getInt("checkedValueInt", 0)),
|
||||
() -> assertEquals(0, Preferences.getFloat("checkedValueFloat", 0), 1e-6),
|
||||
() -> assertTrue(Preferences.getBoolean("checkedValueBoolean", true)));
|
||||
}
|
||||
|
||||
@Nested
|
||||
class PutGetTests {
|
||||
@Test
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
package edu.wpi.first.wpilibj.event;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import edu.wpi.first.networktables.NetworkTableInstance;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class NetworkBooleanEventTest {
|
||||
NetworkTableInstance m_inst;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
m_inst = NetworkTableInstance.create();
|
||||
m_inst.startLocal();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void teardown() {
|
||||
m_inst.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testNetworkBooleanEvent() {
|
||||
var loop = new EventLoop();
|
||||
var counter = new AtomicInteger(0);
|
||||
|
||||
var pub = m_inst.getTable("TestTable").getBooleanTopic("Test").publish();
|
||||
|
||||
new NetworkBooleanEvent(loop, m_inst, "TestTable", "Test").ifHigh(counter::incrementAndGet);
|
||||
pub.set(false);
|
||||
loop.poll();
|
||||
assertEquals(0, counter.get());
|
||||
pub.set(true);
|
||||
loop.poll();
|
||||
assertEquals(1, counter.get());
|
||||
pub.set(false);
|
||||
loop.poll();
|
||||
assertEquals(1, counter.get());
|
||||
}
|
||||
}
|
||||
@@ -8,20 +8,22 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import edu.wpi.first.networktables.NetworkTableInstance;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class SendableCameraWrapperTest {
|
||||
NetworkTableInstance m_inst;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
NetworkTableInstance.getDefault().deleteAllEntries();
|
||||
m_inst = NetworkTableInstance.create();
|
||||
SendableCameraWrapper.clearWrappers();
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void tearDown() {
|
||||
NetworkTableInstance.getDefault().deleteAllEntries();
|
||||
@AfterEach
|
||||
void tearDown() {
|
||||
m_inst.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -9,6 +9,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import edu.wpi.first.networktables.GenericEntry;
|
||||
import edu.wpi.first.networktables.NetworkTableEntry;
|
||||
import edu.wpi.first.networktables.NetworkTableInstance;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
@@ -32,7 +33,7 @@ class ShuffleboardInstanceTest {
|
||||
|
||||
@Test
|
||||
void testPathFluent() {
|
||||
NetworkTableEntry entry =
|
||||
GenericEntry entry =
|
||||
m_shuffleboardInstance
|
||||
.getTab("Tab Title")
|
||||
.getLayout("Layout Title", "List Layout")
|
||||
@@ -45,13 +46,13 @@ class ShuffleboardInstanceTest {
|
||||
() ->
|
||||
assertEquals(
|
||||
"/Shuffleboard/Tab Title/Layout Title/Data",
|
||||
entry.getName(),
|
||||
entry.getTopic().getName(),
|
||||
"Entry path generated incorrectly"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testNestedLayoutsFluent() {
|
||||
NetworkTableEntry entry =
|
||||
GenericEntry entry =
|
||||
m_shuffleboardInstance
|
||||
.getTab("Tab")
|
||||
.getLayout("First", "List")
|
||||
@@ -66,7 +67,7 @@ class ShuffleboardInstanceTest {
|
||||
() ->
|
||||
assertEquals(
|
||||
"/Shuffleboard/Tab/First/Second/Third/Fourth/Value",
|
||||
entry.getName(),
|
||||
entry.getTopic().getName(),
|
||||
"Entry path generated incorrectly"));
|
||||
}
|
||||
|
||||
@@ -78,14 +79,14 @@ class ShuffleboardInstanceTest {
|
||||
ShuffleboardLayout third = second.getLayout("Third", "List");
|
||||
ShuffleboardLayout fourth = third.getLayout("Fourth", "List");
|
||||
SimpleWidget widget = fourth.add("Value", "string");
|
||||
NetworkTableEntry entry = widget.getEntry();
|
||||
GenericEntry entry = widget.getEntry();
|
||||
|
||||
assertAll(
|
||||
() -> assertEquals("string", entry.getString(null), "Wrong entry value"),
|
||||
() ->
|
||||
assertEquals(
|
||||
"/Shuffleboard/Tab/First/Second/Third/Fourth/Value",
|
||||
entry.getName(),
|
||||
entry.getTopic().getName(),
|
||||
"Entry path generated incorrectly"));
|
||||
}
|
||||
|
||||
|
||||
@@ -10,11 +10,13 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import edu.wpi.first.networktables.NetworkTable;
|
||||
import edu.wpi.first.networktables.NetworkTableInstance;
|
||||
import edu.wpi.first.wpilibj.UtilityClassTest;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class SmartDashboardTest extends UtilityClassTest<SmartDashboard> {
|
||||
private final NetworkTable m_table = NetworkTableInstance.getDefault().getTable("SmartDashboard");
|
||||
private NetworkTableInstance m_inst;
|
||||
private NetworkTable m_table;
|
||||
|
||||
SmartDashboardTest() {
|
||||
super(SmartDashboard.class);
|
||||
@@ -22,7 +24,14 @@ class SmartDashboardTest extends UtilityClassTest<SmartDashboard> {
|
||||
|
||||
@BeforeEach
|
||||
void beforeEach() {
|
||||
m_table.getKeys().forEach(m_table::delete);
|
||||
m_inst = NetworkTableInstance.create();
|
||||
m_table = m_inst.getTable("SmartDashboard");
|
||||
SmartDashboard.setNetworkTableInstance(m_inst);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void afterEach() {
|
||||
m_inst.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user