Code Quality · java-codehunger/Java-WebSocket@08be869 · GitHub
Skip to content

Commit 08be869

Browse files
committed
Code Quality
Reduce complexity Fix broken test
1 parent 67ff5f8 commit 08be869

6 files changed

Lines changed: 77 additions & 46 deletions

File tree

src/main/java/org/java_websocket/client/WebSocketClient.java

Lines changed: 36 additions & 24 deletions

src/main/java/org/java_websocket/server/WebSocketServer.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ public void stop(int timeout) throws InterruptedException {
287287
}
288288
}
289289

290-
public void stop() throws IOException, InterruptedException {
290+
public void stop() throws InterruptedException {
291291
stop(0);
292292
}
293293

@@ -538,10 +538,8 @@ private boolean doRead(SelectionKey key, Iterator<SelectionKey> i)
538538
private void doWrite(SelectionKey key) throws WrappedIOException {
539539
WebSocketImpl conn = (WebSocketImpl) key.attachment();
540540
try {
541-
if (SocketChannelIOHelper.batch(conn, conn.getChannel())) {
542-
if (key.isValid()) {
543-
key.interestOps(SelectionKey.OP_READ);
544-
}
541+
if (SocketChannelIOHelper.batch(conn, conn.getChannel()) && key.isValid()) {
542+
key.interestOps(SelectionKey.OP_READ);
545543
}
546544
} catch (IOException e) {
547545
throw new WrappedIOException(conn, e);
@@ -693,9 +691,6 @@ private void handleFatal(WebSocket conn, Exception e) {
693691
}
694692
try {
695693
stop();
696-
} catch (IOException e1) {
697-
log.error("Error during shutdown", e1);
698-
onError(null, e1);
699694
} catch (InterruptedException e1) {
700695
Thread.currentThread().interrupt();
701696
log.error("Interrupt during stop", e);

src/test/java/org/java_websocket/protocols/AllProtocolTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
@RunWith(Suite.class)
3333
@Suite.SuiteClasses({
3434
org.java_websocket.protocols.ProtocolTest.class,
35-
org.java_websocket.protocols.ProtoclHandshakeRejectionTest.class
35+
ProtocolHandshakeRejectionTest.class
3636
})
3737
/**
3838
* Start all tests for protocols

src/test/java/org/java_websocket/protocols/ProtoclHandshakeRejectionTest.java renamed to src/test/java/org/java_websocket/protocols/ProtocolHandshakeRejectionTest.java

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
import org.junit.BeforeClass;
5252
import org.junit.Test;
5353

54-
public class ProtoclHandshakeRejectionTest {
54+
public class ProtocolHandshakeRejectionTest {
5555

5656
private static final String additionalHandshake = "HTTP/1.1 101 Websocket Connection Upgrade\r\nUpgrade: websocket\r\nConnection: Upgrade\r\n";
5757
private static Thread thread;
@@ -355,31 +355,31 @@ public void testHandshakeRejectionTestCase13() throws Exception {
355355

356356
@Test(timeout = 5000)
357357
public void testHandshakeRejectionTestCase14() throws Exception {
358-
ArrayList<IProtocol> protocols = new ArrayList<IProtocol>();
358+
ArrayList<IProtocol> protocols = new ArrayList<>();
359359
protocols.add(new Protocol("chat"));
360360
protocols.add(new Protocol("chat2"));
361361
testProtocolRejection(14, new Draft_6455(Collections.<IExtension>emptyList(), protocols));
362362
}
363363

364364
@Test(timeout = 5000)
365365
public void testHandshakeRejectionTestCase15() throws Exception {
366-
ArrayList<IProtocol> protocols = new ArrayList<IProtocol>();
366+
ArrayList<IProtocol> protocols = new ArrayList<>();
367367
protocols.add(new Protocol("chat"));
368368
protocols.add(new Protocol("chat2"));
369369
testProtocolRejection(15, new Draft_6455(Collections.<IExtension>emptyList(), protocols));
370370
}
371371

372372
@Test(timeout = 5000)
373373
public void testHandshakeRejectionTestCase16() throws Exception {
374-
ArrayList<IProtocol> protocols = new ArrayList<IProtocol>();
374+
ArrayList<IProtocol> protocols = new ArrayList<>();
375375
protocols.add(new Protocol("chat"));
376376
protocols.add(new Protocol("chat2"));
377377
testProtocolRejection(16, new Draft_6455(Collections.<IExtension>emptyList(), protocols));
378378
}
379379

380380
@Test(timeout = 5000)
381381
public void testHandshakeRejectionTestCase17() throws Exception {
382-
ArrayList<IProtocol> protocols = new ArrayList<IProtocol>();
382+
ArrayList<IProtocol> protocols = new ArrayList<>();
383383
protocols.add(new Protocol("chat"));
384384
protocols.add(new Protocol(""));
385385
testProtocolRejection(17, new Draft_6455(Collections.<IExtension>emptyList(), protocols));
@@ -402,7 +402,7 @@ public void testHandshakeRejectionTestCase20() throws Exception {
402402

403403
@Test(timeout = 5000)
404404
public void testHandshakeRejectionTestCase21() throws Exception {
405-
ArrayList<IProtocol> protocols = new ArrayList<IProtocol>();
405+
ArrayList<IProtocol> protocols = new ArrayList<>();
406406
protocols.add(new Protocol("chat1"));
407407
protocols.add(new Protocol("chat2"));
408408
protocols.add(new Protocol("chat3"));
@@ -411,7 +411,7 @@ public void testHandshakeRejectionTestCase21() throws Exception {
411411

412412
@Test(timeout = 5000)
413413
public void testHandshakeRejectionTestCase22() throws Exception {
414-
ArrayList<IProtocol> protocols = new ArrayList<IProtocol>();
414+
ArrayList<IProtocol> protocols = new ArrayList<>();
415415
protocols.add(new Protocol("chat2"));
416416
protocols.add(new Protocol("chat3"));
417417
protocols.add(new Protocol("chat1"));
@@ -420,7 +420,7 @@ public void testHandshakeRejectionTestCase22() throws Exception {
420420

421421
@Test(timeout = 5000)
422422
public void testHandshakeRejectionTestCase23() throws Exception {
423-
ArrayList<IProtocol> protocols = new ArrayList<IProtocol>();
423+
ArrayList<IProtocol> protocols = new ArrayList<>();
424424
protocols.add(new Protocol("chat3"));
425425
protocols.add(new Protocol("chat2"));
426426
protocols.add(new Protocol("chat1"));
@@ -463,7 +463,7 @@ public void testHandshakeRejectionTestCase29() throws Exception {
463463
private void testProtocolRejection(int i, Draft_6455 draft) throws Exception {
464464
final int finalI = i;
465465
final boolean[] threadReturned = {false};
466-
WebSocketClient webSocketClient = new WebSocketClient(
466+
final WebSocketClient webSocketClient = new WebSocketClient(
467467
new URI("ws://localhost:" + port + "/" + finalI), draft) {
468468
@Override
469469
public void onOpen(ServerHandshake handshakedata) {
@@ -509,6 +509,7 @@ public void onClose(int code, String reason, boolean remote) {
509509
case 2:
510510
case 3:
511511
case 4:
512+
case 17:
512513
case 20:
513514
case 24:
514515
case 25:
@@ -533,10 +534,9 @@ public void onClose(int code, String reason, boolean remote) {
533534
case 6:
534535
case 7:
535536
case 8:
536-
case 17:
537+
case 14:
537538
assertEquals("chat", getProtocol().getProvidedProtocol());
538539
break;
539-
case 14:
540540
case 22:
541541
assertEquals("chat2", getProtocol().getProvidedProtocol());
542542
break;
@@ -588,9 +588,24 @@ public void onError(Exception ex) {
588588
fail("There should not be an exception");
589589
}
590590
};
591-
Thread finalThread = new Thread(webSocketClient);
591+
final AssertionError[] exc = new AssertionError[1];
592+
exc[0] = null;
593+
Thread finalThread = new Thread(new Runnable() {
594+
@Override
595+
public void run() {
596+
try {
597+
webSocketClient.run();
598+
}catch(AssertionError e){
599+
exc[0] = e;
600+
}
601+
}
602+
603+
});
592604
finalThread.start();
593605
finalThread.join();
606+
if (exc[0] != null) {
607+
throw exc[0];
608+
}
594609

595610
if (!threadReturned[0]) {
596611
fail("Error");

src/test/java/org/java_websocket/server/AllServerTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@
2525

2626
package org.java_websocket.server;
2727

28+
import org.java_websocket.protocols.ProtocolHandshakeRejectionTest;
2829
import org.junit.runner.RunWith;
2930
import org.junit.runners.Suite;
3031

3132

3233
@RunWith(Suite.class)
3334
@Suite.SuiteClasses({
3435
org.java_websocket.server.DefaultWebSocketServerFactoryTest.class,
35-
org.java_websocket.protocols.ProtoclHandshakeRejectionTest.class
36+
ProtocolHandshakeRejectionTest.class
3637
})
3738
/**
3839
* Start all tests for the server

src/test/java/org/java_websocket/server/WebSocketServerTest.java

Lines changed: 8 additions & 0 deletions

0 commit comments

Comments
 (0)