/*
 * Copyright (C) 2015 Benjamin Bader
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bendb.thrifty.transport;

import org.junit.Ignore;
import org.junit.Test;

import java.net.ProtocolException;

import okio.Buffer;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;

/**
 * @author bendb
 */
public class LengthPrefixingSinkTest {
    @Test
    @Ignore("This is flapping in CI, thanks to oomkiller")
    public void doesNotEncodeNegativeLength() throws Exception {
        byte[] data = new byte[1024 * 1024];
        int iterations = Integer.MAX_VALUE / data.length + 1;

        Buffer buffer = new Buffer();
        LengthPrefixingSink sink = new LengthPrefixingSink(buffer);

        try {
            Buffer b = new Buffer();
            for (int i = 0; i < iterations; ++i) {
                b.write(data);
                sink.write(b, data.length);
            }
        } catch (OutOfMemoryError ignored) {
            // Failed to run - need moar RAM!
            return;
        }

        try {
            sink.flush();
            fail();
        } catch (ProtocolException expected) {
            assertThat(
                    expected.getMessage(),
                    containsString("Cannot write more than Integer.MAX_VALUE"));
        }
    }

    @Test
    public void flushedDataBeginsWithFrameLength() throws Exception {
        Buffer target = new Buffer();
        Buffer source = new Buffer();
        LengthPrefixingSink sink = new LengthPrefixingSink(target);

        source.writeUtf8("this text contains thirty-seven bytes");

        sink.write(source, source.size());
        sink.flush();

        assertThat(target.size(), is(41L));
        assertThat(target.readInt(), is(37));
        assertThat(target.readUtf8(), is("this text contains thirty-seven bytes"));
    }
}

@@ -633,33 +633,8 @@
 t;%0A%0A
-import org.junit.Ignore;%0A
 impo
@@ -806,18 +806,112 @@
 atchers.
-is
+equalTo;%0Aimport static org.hamcrest.CoreMatchers.is;%0Aimport static org.hamcrest.CoreMatchers.not
 ;%0Aimport
@@ -988,33 +988,51 @@
 il;%0A
-%0A/**%0A * @author bendb%0A */
+import static org.junit.Assume.assumeThat;%0A
 %0Apub
@@ -1081,67 +1081,8 @@
 est%0A
-    @Ignore(%22This is flapping in CI, thanks to oomkiller%22)%0A
     
@@ -1134,32 +1134,189 @@
 ows Exception %7B%0A
+        assumeThat(%0A                %22Don't run this in Travis-CI; it angers the oomkiller%22,%0A                System.getenv(%22TRAVIS%22), not(equalTo(%22true%22)));%0A%0A
         byte%5B%5D d

Replace Ignore with Assume to suppress the test only on Travis