How to do unit test websocket with embedded jetty? -
i want write junit ut case websocket serverendpoint code using embedded jetty.
i tried things explained in below link: junit test javax.websocket on embedded jetty throws rejectedexecutionexception: nonblockingthread
i want test onmessage callback websocket. if dont use server.join() method connection closes opens. if use server.join() method nothing happens after joining.
below code.
server startup code::
public class embeddedjettyserver { private final int port; private server server; public embeddedjettyserver(int port) { this.port = port; } public void start() throws exception { server = new server(); serverconnector connector = new serverconnector(server); connector.setport(8080); server.addconnector(connector); // setup basic application "context" application @ "/" // known handler tree (in jetty speak) servletcontexthandler context = new servletcontexthandler(servletcontexthandler.sessions); context.setcontextpath("/"); server.sethandler(context); try { // initialize javax.websocket layer servercontainer wscontainer = websocketservercontainerinitializer.configurecontext(context); // add websocket endpoint javax.websocket layer wscontainer.addendpoint(websocketserver.class); system.out.println("begin start"); server.start(); server.dump(system.err); server.join(); } catch (throwable t) { t.printstacktrace(system.err); } } public void stop() throws exception { server.stop(); logger.info("jetty server stopped"); } public uri getwebsocketuri(class<websocketserver> class1) { return server.geturi(); }
}
client code:
@clientendpoint() public class websocketclientjetty { websocketcontainer container; public session connect(uri uri) throws exception { websocketcontainer container = containerprovider.getwebsocketcontainer(); try { // attempt connect session session = container.connecttoserver(websocketclientjetty.class,uri); // return container.connecttoserver(websocketclientjetty.class, uri); session.getbasicremote().sendtext("hello"); // close session // session.close(); return session; } { } } public void stop() throws exception{ if (container instanceof lifecycle) { ((lifecycle) container).stop(); } } @onopen public void onwebsocketconnect(session sess) { system.out.println("socket connected: " + sess); } @onmessage public void onwebsockettext(string message) { system.out.println("received text message: " + message); } @onclose public void onwebsocketclose(closereason reason) { system.out.println("socket closed: " + reason); } @onerror public void onwebsocketerror(throwable cause) { cause.printstacktrace(system.err); }
}
serverendpoint code:
@serverendpoint(value = "/echo", encoders={jsonencoder.class}) public class websocketserver { private static final logger logger = @onopen public void onopen(session session){ system.out.println("onopen"); code.... } @onmessage public void onmessage(string message, session session) throws ioexception{ system.out.println("onmessage"); .... } @onclose public void onclose(session session){ system.out.println("onclose"); ... }
}
ut case:
public class websocketjettytest { private static embeddedjettyserver server; @classrule public static final externalresource integrationserver = new externalresource() { @override protected void before() throws throwable { system.out.println("starting..."); server = new embeddedjettyserver(8080); server.start(); system.out.println("started"); } }; @before public void setup() throws exception { } @after public void shutdown() throws exception { server.stop(); } @test public void testsocket() throws exception { /*uri uri = server.getwebsocketuri(websocketserver.class);*/ uri uri = uri.create("ws://localhost:8080/echo"); websocketclientjetty client = new websocketclientjetty(); session session = client.connect(uri); session.getbasicremote().sendtext("hello"); thread.sleep(6000); client.stop(); } }
drop call to
server.join();
that makes current thread wait until server thread stops. making difficult you.
Comments
Post a Comment