Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Draft] Add issue347Test #1051

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/test/java/org/java_websocket/issues/AllIssueTests.java
Expand Up @@ -43,7 +43,8 @@
org.java_websocket.issues.Issue765Test.class,
org.java_websocket.issues.Issue825Test.class,
org.java_websocket.issues.Issue834Test.class,
org.java_websocket.issues.Issue962Test.class
org.java_websocket.issues.Issue962Test.class
org.java_websocket.issues.Issue347Test.class
})
/**
* Start all tests for issues
Expand Down
120 changes: 120 additions & 0 deletions src/test/java/org/java_websocket/issues/Issue347Test.java
@@ -0,0 +1,120 @@
package org.java_websocket.issues;

import org.java_websocket.WebSocket;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.handshake.ServerHandshake;
import org.java_websocket.server.WebSocketServer;
import org.java_websocket.util.SocketUtil;
import org.java_websocket.util.ThreadCheck;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

// this tests whether the client gets properly closed while it is connecting. the method
// WebSocketClient.closeConnection(...) is called shortly after WebSocketClient.connect();
// the client is using a socket that takes at least 100ms to connect. then we check for zombies.

@RunWith(Parameterized.class)
public class Issue347Test {

private static final int NUMBER_OF_TESTS = 20;
private static final int DELAY_MULTIPLIER = 10;

private static WebSocketServer server;
private static int port;
static CountDownLatch countServerDownLatch = new CountDownLatch( 1 );

@Parameterized.Parameter
public int delay;

@Rule
public ThreadCheck zombies = new ThreadCheck();

@BeforeClass
public static void startServer() throws Exception {
port = SocketUtil.getAvailablePort();
server = new WebSocketServer(new InetSocketAddress(port) , 16) {
@Override
public void onOpen(WebSocket conn, ClientHandshake handshake) {}

@Override
public void onClose(WebSocket conn, int code, String reason, boolean remote) {}

@Override
public void onMessage(WebSocket conn, String message) {}

@Override
public void onError(WebSocket conn, Exception ex) {}

@Override
public void onStart() {
countServerDownLatch.countDown();
}
};
server.setConnectionLostTimeout(0);
server.start();
countServerDownLatch.await();
}

@AfterClass
public static void stopServer() throws InterruptedException, IOException {
server.stop();
}

@Test(timeout = 5000)
public void runTestScenario() throws Exception {
final WebSocketClient client = new WebSocketClient( new URI("ws://localhost:" + port)) {
@Override
public void onOpen(ServerHandshake handshakedata) {}

@Override
public void onMessage( String message ) {}

@Override
public void onClose( int code, String reason, boolean remote ) {}

@Override
public void onError( Exception ex ) {}
};
client.setSocket(new SlowSocket());
client.connect();
Thread.sleep(delay);
client.closeConnection(1000, "foo");
}

@Parameterized.Parameters
public static Collection<Integer[]> data() {
List<Integer[]> ret = new ArrayList<Integer[]>(NUMBER_OF_TESTS);
for (int i = 0; i < NUMBER_OF_TESTS; i++) ret.add(new Integer[]{i * DELAY_MULTIPLIER});
return ret;
}

private static class SlowSocket extends Socket {
@Override public void connect(SocketAddress socketAddress, int i) throws IOException {
sleep(100);
super.connect(socketAddress, i);
}
}

@SuppressWarnings("SameParameterValue")
private static void sleep(long interval) {
try {
Thread.sleep(interval);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}