Skip to content
Merged
Show file tree
Hide file tree
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
10 changes: 10 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: build
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with: { distribution: temurin, java-version: '17', cache: maven }
- run: mvn -B -f JSC/pom.xml test
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

# Maven build output
target/
20 changes: 13 additions & 7 deletions JSC/JConnection/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,35 @@
<groupId>org.example</groupId>
<artifactId>JConnection</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<junit.version>5.10.2</junit.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;


@org.junit.jupiter.api.Disabled("Manual: needs EchoServer running on :5656. Automated coverage: JSERVER EchoRoundTripTest")
class JConnectionTest {

JConnection jConnection;
Expand Down
26 changes: 26 additions & 0 deletions JSC/JSERVER/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,44 @@
<groupId>org.example</groupId>
<artifactId>JSERVER</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<junit.version>5.10.2</junit.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>JConnection</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>


</project>
67 changes: 67 additions & 0 deletions JSC/JSERVER/src/test/java/jsc/jServer/EchoRoundTripTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package jsc.jServer;

import jsc.jConnection.JConnection;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

/** End-to-end: real JServer on an ephemeral port, real JConnection client, messages echoed back in order. */
class EchoRoundTripTest {

public static class Echo extends JRequestManager {
@Override
public void accept(String req) { write(req); }
}

private JServer server;
private JConnection client;
private final BlockingQueue<String> received = new LinkedBlockingQueue<>();

@BeforeEach
void setUp() throws Exception {
server = new JServer(0, Echo.class); // port 0 = OS picks a free port
Thread t = new Thread(server::start, "jserver");
t.setDaemon(true);
t.start();
client = new JConnection("127.0.0.1", server.getServerSocket().getLocalPort(), received::add);
client.run();
}

@AfterEach
void tearDown() {
client.close();
server.setRunning(false);
server.close();
}

@Test
void echoesMessagesInOrder() throws Exception {
String[] msgs = {"hello", "1/2~3>4;5,6", "unicode ✓"};
for (String m : msgs) client.write(m);
for (String m : msgs) assertEquals(m, received.poll(5, TimeUnit.SECONDS));
}

@Test
void pausedReadHoldsMessagesUntilResume() throws Exception {
client.write("before");
assertEquals("before", received.poll(5, TimeUnit.SECONDS));
client.setReadInterrupt(true);
client.write("m1");
client.write("m2");
client.write("m3");
Thread.sleep(500);
// The pause is checked before each blocking readUTF(), so a read already in flight
// can still deliver one message (documented limitation); the rest must be held.
int leaked = received.size();
assertTrue(leaked <= 1, "at most one in-flight message may arrive while paused, got " + leaked);
client.setReadInterrupt(false);
for (String m : new String[]{"m1", "m2", "m3"}) assertEquals(m, received.poll(5, TimeUnit.SECONDS));
}
}
15 changes: 15 additions & 0 deletions JSC/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Aggregator so `mvn test` builds JConnection first, then JSERVER (which depends on it). -->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>JSC</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>JConnection</module>
<module>JSERVER</module>
</modules>
</project>
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Prashant Joshi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Loading
Loading