summaryrefslogtreecommitdiff
path: root/main/client/test/src
diff options
context:
space:
mode:
authorLi Haoyi <haoyi.sg@gmail.com>2018-05-22 21:46:45 -0700
committerLi Haoyi <haoyi.sg@gmail.com>2018-05-22 21:46:45 -0700
commitc04bfa1c0ee5a51ef5f63ade8e63d1f55f53fa3e (patch)
treeef3a2b179864b91fab91b45f59b787dd1dacfc88 /main/client/test/src
parent6e60ce3d921a9b5e4ced628e2014b707ce2bbbee (diff)
downloadmill-c04bfa1c0ee5a51ef5f63ade8e63d1f55f53fa3e.tar.gz
mill-c04bfa1c0ee5a51ef5f63ade8e63d1f55f53fa3e.tar.bz2
mill-c04bfa1c0ee5a51ef5f63ade8e63d1f55f53fa3e.zip
Migrate `ProxyOutputStream` to the `main.client` module, add unit/fuzz tests to make sure it works
Diffstat (limited to 'main/client/test/src')
-rw-r--r--main/client/test/src/mill/main/client/ClientTests.java90
1 files changed, 90 insertions, 0 deletions
diff --git a/main/client/test/src/mill/main/client/ClientTests.java b/main/client/test/src/mill/main/client/ClientTests.java
index 5ae44d3f..f3fcf154 100644
--- a/main/client/test/src/mill/main/client/ClientTests.java
+++ b/main/client/test/src/mill/main/client/ClientTests.java
@@ -1,10 +1,14 @@
package mill.main.client;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
import org.junit.Test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
+import java.io.OutputStream;
+import java.util.*;
public class ClientTests {
@Test
@@ -57,5 +61,91 @@ public class ClientTests {
assertEquals(i.available(), 0);
}
+ public byte[] readSamples(String ...samples) throws Exception{
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ for(String sample: samples) {
+ byte[] bytes = java.nio.file.Files.readAllBytes(
+ java.nio.file.Paths.get(getClass().getResource(sample).getFile())
+ );
+ out.write(bytes);
+ }
+ return out.toByteArray();
+ }
+ @Test
+ public void tinyProxyInputOutputStream() throws Exception{
+ proxyInputOutputStreams(
+ Arrays.copyOf(readSamples("/bandung.jpg"), 30),
+ readSamples(),
+ 10
+ );
+ }
+ @Test
+ public void leftProxyInputOutputStream() throws Exception{
+ proxyInputOutputStreams(
+ readSamples("/bandung.jpg", "/akanon.mid", "/gettysburg.txt", "/pip.tar.gz"),
+ readSamples(),
+ 2950
+ );
+ }
+ @Test
+ public void rightProxyInputOutputStream() throws Exception{
+ proxyInputOutputStreams(
+ readSamples(),
+ readSamples("/bandung.jpg", "/akanon.mid", "/gettysburg.txt", "/pip.tar.gz"),
+ 3000
+ );
+ }
+ @Test
+ public void mixedProxyInputOutputStream() throws Exception{
+ proxyInputOutputStreams(
+ readSamples("/bandung.jpg", "/gettysburg.txt"),
+ readSamples("/akanon.mid", "/pip.tar.gz"),
+ 3050
+ );
+ }
+
+ /**
+ * Make sure that when we shove data through both ProxyOutputStreams in
+ * variously sized chunks, we get the exact same bytes back out from the
+ * ProxyStreamPumper.
+ */
+ public void proxyInputOutputStreams(byte[] samples1,
+ byte[] samples2,
+ int chunkMax) throws Exception{
+
+ ByteArrayOutputStream pipe = new ByteArrayOutputStream();
+ OutputStream src1 = new ProxyOutputStream(pipe, 1);
+ OutputStream src2 = new ProxyOutputStream(pipe, -1);
+
+ Random random = new Random(31337);
+
+ int i1 = 0;
+ int i2 = 0;
+ while(i1 < samples1.length || i2 < samples2.length){
+ int chunk = random.nextInt(chunkMax);
+ if (random.nextBoolean() && i1 < samples1.length){
+ src1.write(samples1, i1, Math.min(samples1.length-i1, chunk));
+ src1.flush();
+ i1 += chunk;
+ }else if (i2 < samples2.length){
+ src2.write(samples2, i2, Math.min(samples2.length-i2, chunk));
+ src2.flush();
+ i2 += chunk;
+ }
+ }
+
+ byte[] bytes = pipe.toByteArray();
+
+
+ ByteArrayOutputStream dest1 = new ByteArrayOutputStream();
+ ByteArrayOutputStream dest2 = new ByteArrayOutputStream();
+ ProxyStreamPumper pumper = new ProxyStreamPumper(
+ new ByteArrayInputStream(bytes),
+ dest1, dest2
+ );
+ pumper.run();
+ assertTrue(Arrays.equals(samples1, dest1.toByteArray()));
+ assertTrue(Arrays.equals(samples2, dest2.toByteArray()));
+ }
} \ No newline at end of file