<commit_before>/**
 * Copyright 2009 - 2010 <NAME> (<EMAIL>)
 *
 *    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 terrastore.util.io;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 * @author <NAME>
 */
public class IOUtilsTest {

    private static final String VALUE = new String(new byte[10000]);

    @Test
    public void testRead() throws Exception {
        ByteArrayInputStream stream = new ByteArrayInputStream(VALUE.getBytes());

        byte[] read = IOUtils.read(stream);

        assertArrayEquals(VALUE.getBytes(), read);
    }

    @Test
    public void testCompression() throws Exception {
        ByteArrayInputStream stream = new ByteArrayInputStream(VALUE.getBytes());

        byte[] read = IOUtils.readAndCompress(stream);

        assertArrayEquals(VALUE.getBytes(), IOUtils.readCompressed(new ByteArrayInputStream(read)));
    }

    @Test
    public void testMultithreadRead() throws Exception {
        final int total = 100;
        final List<byte[]> values = new ArrayList<byte[]>(total);

        ExecutorService executor = Executors.newCachedThreadPool();
        for (int i = 0; i < total; i++) {
            executor.execute(new Runnable() {

                @Override
                public void run() {
                    try {
                        ByteArrayInputStream stream = new ByteArrayInputStream(VALUE.getBytes());
                        byte[] read = IOUtils.read(stream);
                        values.add(read);
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                }
            });
        }
        executor.shutdown();
        executor.awaitTermination(3, TimeUnit.MINUTES);

        for (int i = 0; i < total; i++) {
            assertArrayEquals(VALUE.getBytes(), values.get(i));
        }
    }

    @Test
    public void testMultithreadCompression() throws Exception {
        final int total = 100;
        final List<byte[]> values = new ArrayList<byte[]>(total);

        ExecutorService executor = Executors.newCachedThreadPool();
        for (int i = 0; i < total; i++) {
            executor.execute(new Runnable() {

                @Override
                public void run() {
                    try {
                        ByteArrayInputStream stream = new ByteArrayInputStream(VALUE.getBytes());
                        byte[] read = IOUtils.readAndCompress(stream);
                        values.add(read);
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                }
            });
        }
        executor.shutdown();
        executor.awaitTermination(3, TimeUnit.MINUTES);

        for (int i = 0; i < total; i++) {
            assertArrayEquals(VALUE.getBytes(), IOUtils.readCompressed(new ByteArrayInputStream(values.get(i))));
        }
    }
}<commit_msg>Fixed seldom failing test due to concurrency issues.<commit_after>/**
 * Copyright 2009 - 2010 <NAME> (<EMAIL>)
 *
 *    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 terrastore.util.io;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 * @author <NAME>
 */
public class IOUtilsTest {

    private static final String VALUE = new String(new byte[10000]);

    @Test
    public void testRead() throws Exception {
        ByteArrayInputStream stream = new ByteArrayInputStream(VALUE.getBytes());

        byte[] read = IOUtils.read(stream);

        assertArrayEquals(VALUE.getBytes(), read);
    }

    @Test
    public void testCompression() throws Exception {
        ByteArrayInputStream stream = new ByteArrayInputStream(VALUE.getBytes());

        byte[] read = IOUtils.readAndCompress(stream);

        assertArrayEquals(VALUE.getBytes(), IOUtils.readCompressed(new ByteArrayInputStream(read)));
    }

    @Test
    public void testMultithreadRead() throws Exception {
        final int total = 100;
        final List<byte[]> values = Collections.synchronizedList(new ArrayList<byte[]>(total));

        ExecutorService executor = Executors.newCachedThreadPool();
        for (int i = 0; i < total; i++) {
            executor.execute(new Runnable() {

                @Override
                public void run() {
                    try {
                        ByteArrayInputStream stream = new ByteArrayInputStream(VALUE.getBytes());
                        byte[] read = IOUtils.read(stream);
                        values.add(read);
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                }
            });
        }
        executor.shutdown();
        executor.awaitTermination(3, TimeUnit.MINUTES);

        for (int i = 0; i < total; i++) {
            assertArrayEquals(VALUE.getBytes(), values.get(i));
        }
    }

    @Test
    public void testMultithreadCompression() throws Exception {
        final int total = 100;
        final List<byte[]> values = Collections.synchronizedList(new ArrayList<byte[]>(total));

        ExecutorService executor = Executors.newCachedThreadPool();
        for (int i = 0; i < total; i++) {
            executor.execute(new Runnable() {

                @Override
                public void run() {
                    try {
                        ByteArrayInputStream stream = new ByteArrayInputStream(VALUE.getBytes());
                        byte[] read = IOUtils.readAndCompress(stream);
                        values.add(read);
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                }
            });
        }
        executor.shutdown();
        executor.awaitTermination(3, TimeUnit.MINUTES);

        for (int i = 0; i < total; i++) {
            assertArrayEquals(VALUE.getBytes(), IOUtils.readCompressed(new ByteArrayInputStream(values.get(i))));
        }
    }
}
{'fraction_non_alphanumeric': 0.10054820163123412, 'fraction_numerical': 0.007755047466238802, 'mean_word_length': 2.115368596418159, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5994318', 'n_tokens_mistral': 1980, 'n_tokens_neox': 1889, 'n_words': 537}