use of io.kubernetes.client.util.WebSocketStreamHandler 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));
}
use of io.kubernetes.client.util.WebSocketStreamHandler in project java by kubernetes-client.
the class WebsocketStreamHandlerTest method testHandlerReceivingData.
@Test
public void testHandlerReceivingData() throws IOException {
int testStreamId = 0;
byte testData = 1;
byte[] testDatas = // first byte stands for stream id,
new byte[] { (byte) testStreamId, testData, testData };
ByteArrayInputStream testBytesInputStream = new ByteArrayInputStream(testDatas);
WebSocketStreamHandler handler = new WebSocketStreamHandler();
MockWebSocket mockWebSocket = new MockWebSocket();
handler.open(testProtocol, mockWebSocket);
InputStream inputStream = handler.getInputStream(testStreamId);
// handler receiving
handler.bytesMessage(testBytesInputStream);
byte[] receivingData = new byte[16];
inputStream.read(receivingData);
handler.close();
assertEquals(testData, receivingData[0]);
assertEquals(testData, receivingData[1]);
assertTrue(mockWebSocket.closed);
}
use of io.kubernetes.client.util.WebSocketStreamHandler in project java by kubernetes-client.
the class PortForward method forward.
/**
* PortForward to a container.
*
* @param namespace The namespace of the Pod
* @param name The name of the Pod
* @param ports The ports to forward
* @return The result of the Port Forward request.
*/
public PortForwardResult forward(String namespace, String name, List<Integer> ports) throws ApiException, IOException {
String path = makePath(namespace, name);
WebSocketStreamHandler handler = new WebSocketStreamHandler();
PortForwardResult result = new PortForwardResult(handler, ports);
List<Pair> queryParams = new ArrayList<>(ports.size());
for (Integer port : ports) {
queryParams.add(new Pair("ports", port.toString()));
}
WebSockets.stream(path, "GET", queryParams, apiClient, handler);
try {
handler.waitForInitialized();
} catch (InterruptedException ex) {
throw new ApiException(ex);
}
Throwable err = handler.getError();
if (err != null) {
throw new ApiException(err);
}
// Wait for streams to start.
result.init();
return result;
}
Aggregations