|
12 | 12 | */ |
13 | 13 | package io.kubernetes.client; |
14 | 14 |
|
| 15 | +import static org.junit.Assert.assertArrayEquals; |
15 | 16 | import static org.junit.Assert.assertEquals; |
16 | 17 | import static org.junit.Assert.assertTrue; |
17 | 18 |
|
18 | 19 | import io.kubernetes.client.util.WebSocketStreamHandler; |
19 | 20 | import java.io.ByteArrayInputStream; |
20 | | -import java.io.ByteArrayOutputStream; |
21 | 21 | import java.io.IOException; |
22 | 22 | import java.io.InputStream; |
| 23 | +import java.io.OutputStream; |
23 | 24 | import java.nio.charset.Charset; |
24 | 25 | import okhttp3.Request; |
25 | 26 | import okhttp3.WebSocket; |
26 | | -import okio.BufferedSink; |
27 | 27 | import okio.ByteString; |
28 | | -import okio.Okio; |
29 | 28 | import org.junit.Test; |
30 | 29 |
|
31 | 30 | public class WebsocketStreamHandlerTest { |
@@ -59,34 +58,99 @@ public void testHandlerReceivingData() throws IOException { |
59 | 58 | assertTrue(mockWebSocket.closed); |
60 | 59 | } |
61 | 60 |
|
| 61 | + @Test |
| 62 | + public void testHandlerSendingData() throws IOException { |
| 63 | + int testStreamId = 0; |
| 64 | + byte testData = 1; |
| 65 | + byte[] testDatas = |
| 66 | + new byte[] {(byte) testStreamId, testData, testData}; // first byte stands for stream id, |
| 67 | + ByteArrayInputStream testBytesInputStream = new ByteArrayInputStream(testDatas); |
| 68 | + |
| 69 | + WebSocketStreamHandler handler = new WebSocketStreamHandler(); |
| 70 | + MockWebSocket mockWebSocket = new MockWebSocket(); |
| 71 | + |
| 72 | + handler.open(testProtocol, mockWebSocket); |
| 73 | + |
| 74 | + OutputStream outputStream = handler.getOutputStream(testStreamId); |
| 75 | + |
| 76 | + byte[] bytes = "This is a test string".getBytes("UTF-8"); |
| 77 | + byte[] output = new byte[bytes.length + 1]; |
| 78 | + output[0] = 0; |
| 79 | + for (int i = 0; i < bytes.length; i++) { |
| 80 | + output[i + 1] = bytes[i]; |
| 81 | + } |
| 82 | + outputStream.write(bytes); |
| 83 | + outputStream.flush(); |
| 84 | + |
| 85 | + assertArrayEquals(output, mockWebSocket.data); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void testHandlerSendingLargeData() throws IOException { |
| 90 | + int testStreamId = 0; |
| 91 | + byte testData = 1; |
| 92 | + byte[] testDatas = |
| 93 | + new byte[] {(byte) testStreamId, testData, testData}; // first byte stands for stream id, |
| 94 | + ByteArrayInputStream testBytesInputStream = new ByteArrayInputStream(testDatas); |
| 95 | + |
| 96 | + WebSocketStreamHandler handler = new WebSocketStreamHandler(); |
| 97 | + MockWebSocket mockWebSocket = new MockWebSocket(); |
| 98 | + |
| 99 | + handler.open(testProtocol, mockWebSocket); |
| 100 | + |
| 101 | + OutputStream outputStream = handler.getOutputStream(testStreamId); |
| 102 | + |
| 103 | + byte[] bytes = new byte[20 * 1024 * 1024]; |
| 104 | + byte[] output = new byte[bytes.length + 2]; |
| 105 | + |
| 106 | + for (int i = 0; i < bytes.length; i++) { |
| 107 | + bytes[i] = (byte) i; |
| 108 | + } |
| 109 | + // First stream header |
| 110 | + output[0] = 0; |
| 111 | + for (int i = 0; i < 15 * 1024 * 1024; i++) { |
| 112 | + output[i + 1] = bytes[i]; |
| 113 | + } |
| 114 | + // Second stream header |
| 115 | + output[15 * 1024 * 1024 + 1] = 0; |
| 116 | + for (int i = 15 * 1024 * 1024; i < bytes.length; i++) { |
| 117 | + output[i + 2] = bytes[i]; |
| 118 | + } |
| 119 | + outputStream.write(bytes); |
| 120 | + outputStream.flush(); |
| 121 | + |
| 122 | + assertArrayEquals(output, mockWebSocket.data); |
| 123 | + } |
| 124 | + |
62 | 125 | private class MockWebSocket implements WebSocket { |
63 | | - private byte[] data; |
| 126 | + byte[] data; |
64 | 127 | private boolean closed = false; |
65 | 128 |
|
| 129 | + private byte[] append(byte[] one, byte[] two) { |
| 130 | + if (one == null || one.length == 0) { |
| 131 | + return two; |
| 132 | + } |
| 133 | + |
| 134 | + byte[] result = new byte[one.length + two.length]; |
| 135 | + for (int i = 0; i < one.length; i++) { |
| 136 | + result[i] = one[i]; |
| 137 | + } |
| 138 | + for (int i = 0; i < two.length; i++) { |
| 139 | + result[i + one.length] = two[i]; |
| 140 | + } |
| 141 | + return result; |
| 142 | + } |
| 143 | + |
66 | 144 | @Override |
67 | 145 | public boolean send(String s) { |
68 | | - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
69 | | - BufferedSink sink = Okio.buffer(Okio.sink(outputStream)); |
70 | | - try { |
71 | | - sink.writeString(s, Charset.defaultCharset()); |
72 | | - } catch (IOException e) { |
73 | | - throw new RuntimeException(e); |
74 | | - } |
75 | | - this.data = outputStream.toByteArray(); |
| 146 | + this.data = append(data, s.getBytes(Charset.defaultCharset())); |
76 | 147 | return true; |
77 | 148 | } |
78 | 149 |
|
79 | 150 | @Override |
80 | 151 | public boolean send(ByteString byteString) { |
81 | | - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
82 | | - BufferedSink sink = Okio.buffer(Okio.sink(outputStream)); |
83 | | - try { |
84 | | - sink.write(byteString); |
85 | | - } catch (IOException e) { |
86 | | - throw new RuntimeException(e); |
87 | | - } |
88 | | - this.data = outputStream.toByteArray(); |
89 | | - return false; |
| 152 | + this.data = append(data, byteString.toByteArray()); |
| 153 | + return true; |
90 | 154 | } |
91 | 155 |
|
92 | 156 | @Override |
|
0 commit comments