Search in sources :

Example 1 with PortForwardResult

use of io.kubernetes.client.PortForward.PortForwardResult in project java by kubernetes-client.

the class PortForwardTest method testBrokenPortPassing.

@Test
public void testBrokenPortPassing() throws IOException, InterruptedException {
    WebSocketStreamHandler handler = new WebSocketStreamHandler();
    List<Integer> ports = new ArrayList<>();
    ports.add(80);
    final PortForwardResult result = new PortForwardResult(handler, ports);
    String msgData = "this is a test datum";
    handler.open("wss", null);
    handler.bytesMessage(makeStream(new byte[] { 66 }, msgData.getBytes(StandardCharsets.UTF_8)));
    final Object block = new Object();
    Thread t = new Thread(() -> {
        try {
            result.init();
        } catch (IOException ex) {
            thrownException = ex;
        } finally {
            synchronized (block) {
                block.notifyAll();
            }
        }
    });
    synchronized (block) {
        t.start();
        Thread.sleep(2000);
        handler.close();
        block.wait();
    }
    assertNotNull(thrownException);
    assertTrue(thrownException instanceof IOException);
}
Also used : PortForwardResult(io.kubernetes.client.PortForward.PortForwardResult) WebSocketStreamHandler(io.kubernetes.client.util.WebSocketStreamHandler) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Test(org.junit.Test)

Example 2 with PortForwardResult

use of io.kubernetes.client.PortForward.PortForwardResult in project java by kubernetes-client.

the class PortForwardTest method testPortForwardResult.

@Test
public void testPortForwardResult() throws IOException, InterruptedException {
    WebSocketStreamHandler handler = new WebSocketStreamHandler();
    List<Integer> ports = new ArrayList<>();
    ports.add(80);
    ports.add(800);
    final PortForwardResult result = new PortForwardResult(handler, ports);
    String msgData = "this is a test datum";
    String msgData2 = "this is a different test datum";
    handler.open("wss", null);
    handler.bytesMessage(makeStream(new byte[] { 0, 80, 0 }, msgData.getBytes(StandardCharsets.UTF_8)));
    handler.bytesMessage(makeStream(new byte[] { 2, 32, 3 }, msgData2.getBytes(StandardCharsets.UTF_8)));
    final Object block = new Object();
    Thread t = new Thread(new Runnable() {

        public void run() {
            try {
                result.init();
                synchronized (block) {
                    block.notifyAll();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    });
    synchronized (block) {
        t.start();
        block.wait();
    }
    InputStream is = result.getInputStream(80);
    assertNotNull(is);
    InputStream is2 = result.getInputStream(800);
    assertNotNull(is2);
    byte[] bytes = new byte[msgData.length()];
    for (int i = 0; i < msgData.length(); i++) {
        bytes[i] = (byte) is.read();
    }
    assertEquals(msgData, new String(bytes, StandardCharsets.UTF_8));
    bytes = new byte[msgData2.length()];
    for (int i = 0; i < msgData2.length(); i++) {
        bytes[i] = (byte) is2.read();
    }
    assertEquals(msgData2, new String(bytes, StandardCharsets.UTF_8));
    assertEquals(null, result.getInputStream(8080));
    assertEquals(null, result.getErrorStream(8080));
    assertEquals(null, result.getOutboundStream(8080));
}
Also used : InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) PortForwardResult(io.kubernetes.client.PortForward.PortForwardResult) WebSocketStreamHandler(io.kubernetes.client.util.WebSocketStreamHandler) Test(org.junit.Test)

Aggregations

PortForwardResult (io.kubernetes.client.PortForward.PortForwardResult)2 WebSocketStreamHandler (io.kubernetes.client.util.WebSocketStreamHandler)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 InputStream (java.io.InputStream)1