Skip to content

Commit

Permalink
make tests more stable
Browse files Browse the repository at this point in the history
  • Loading branch information
dernasherbrezon committed Aug 16, 2024
1 parent 34c03f1 commit 27a17e2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
3 changes: 1 addition & 2 deletions src/test/java/ru/r2cloud/it/util/BaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public abstract class BaseTest {
public static final int ROTCTRLD_PORT = 8004;
private static final int ROTCTRLD_PORT_LORA = 8006;
private static final int LORA_AT_WIFI_PORT = 8005;
private static final int SPYSERVER_MOCK = 8008;

protected R2Cloud server;
private CelestrakServer celestrak;
Expand Down Expand Up @@ -105,7 +104,7 @@ public void start() throws Exception {
"Using auto-detected IIO context at URI \"usb:0.1.5\"\nIIO context created with usb backend.\nBackend description string: Linux (none) 4.19.0-119999-g6edc6cd #319 SMP PREEMPT Mon Jul 6 15:45:01 CEST 2020 armv7l\nIIO context has 15 attributes:\n hw_model: Analog Devices PlutoSDR Rev.B (Z7010-AD9363A)\n hw_model_variant: 0\n hw_serial: 10447354119600050d003000d4311fd131\n");
plutoTestServer.start();

spyServerMock = new SpyServerMock("127.0.0.1", SPYSERVER_MOCK);
spyServerMock = new SpyServerMock("127.0.0.1");
spyServerMock.setDeviceInfo(SpyServerReaderTest.createAirSpy());
spyServerMock.setSync(SpyServerReaderTest.createValidSync());
spyServerMock.start();
Expand Down
19 changes: 15 additions & 4 deletions src/test/java/ru/r2cloud/satellite/reader/SpyServerMock.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class SpyServerMock {

private static final Logger LOG = LoggerFactory.getLogger(SpyServerMock.class);
private final String host;
private final int port;
private int port = 8008;
private ServerSocket socket;
private SpyServerDeviceInfo deviceInfo;
private SpyClientSync sync;
Expand All @@ -35,9 +35,12 @@ public class SpyServerMock {
private CountDownLatch latch = new CountDownLatch(1);
private Map<SpyServerParameter, Long> params = new HashMap<>();

public SpyServerMock(String host, int port) {
public SpyServerMock(String host) {
this.host = host;
this.port = port;
}

public int getPort() {
return port;
}

public void setDeviceInfo(SpyServerDeviceInfo deviceInfo) {
Expand All @@ -64,7 +67,15 @@ public void waitForDataSent() throws InterruptedException {
public void start() throws IOException {
socket = new ServerSocket();
socket.setReuseAddress(true);
socket.bind(new InetSocketAddress(host, port));
for (int i = 0; i < 10; i++) {
try {
socket.bind(new InetSocketAddress(host, port));
break;
} catch (IOException e) {
LOG.info("can't bind on: {} trying next. Error: {}", port, e.getMessage());
port++;
}
}
new Thread(new Runnable() {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,25 @@ public class SpyServerReaderTest {

@Test
public void testDeviceNotConnected() throws Exception {
deviceConfiguration.setPort(8009);
mock = new SpyServerMock(deviceConfiguration.getHost(), deviceConfiguration.getPort());
mock = new SpyServerMock(deviceConfiguration.getHost());
mock.start();

deviceConfiguration.setPort(mock.getPort());
SpyServerReader reader = new SpyServerReader(config, createValidRequest(), deviceConfiguration, createValidTransmitter());
reader.complete();
assertNull(reader.start());
}

@Test
public void testSuccess() throws Exception {
deviceConfiguration.setPort(8008);
mock = new SpyServerMock(deviceConfiguration.getHost(), deviceConfiguration.getPort());
mock = new SpyServerMock(deviceConfiguration.getHost());
mock.start();

mock.setDeviceInfo(createAirSpy());
mock.setSync(createValidSync());
mock.setData(createSample(), SpyClient.SPYSERVER_MSG_TYPE_INT16_IQ);

deviceConfiguration.setPort(mock.getPort());
ObservationRequest req = createValidRequest();
SpyServerReader reader = new SpyServerReader(config, req, deviceConfiguration, createValidTransmitter());
new Thread(new Runnable() {
Expand Down

0 comments on commit 27a17e2

Please sign in to comment.