<commit_before>/* The MIT License (MIT)
 * Copyright (c) 2014 <NAME>
 * http://opensource.org/licenses/MIT
 */

package com.github.dozedoff.commonj.io;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.hasItems;
import static org.junit.Assert.assertThat;

import java.util.LinkedList;

import org.junit.Before;
import org.junit.Test;

public class DataProducerTest {
	final int SLEEP_TIME = 100;
	DataProducerDummy dummy;

	@Before
	public void setUp() throws Exception {
		dummy = new DataProducerDummy();
	}

	// TODO split clear test into testClearInputQueue and testClearOutputQueue
	@Test(timeout = 1000)
	public void testClear() throws InterruptedException {
		String test[] = { "1", "2", "3" };
		LinkedList<Integer> data = new LinkedList<>();

		dummy.addToLoad(test);
		Thread.sleep(SLEEP_TIME);
		dummy.drainTo(data, 10);
		assertThat(data.size(), is(3));

		data = new LinkedList<>();

		dummy.addToLoad(test);
		dummy.clear();
		dummy.addToLoad("5");
		Thread.sleep(SLEEP_TIME);
		dummy.drainTo(data, 10);

		assertThat(data.size(), is(1));
	}

	@Test
	public void testAddToLoadIArray() throws InterruptedException {
		String test[] = { "1", "2", "3" };

		dummy.addToLoad(test);
		Thread.sleep(SLEEP_TIME);
		assertThat(dummy.getProcessed(), is(3));
	}

	@Test
	public void testAddToLoadListOfI() throws InterruptedException {
		LinkedList<String> test = new LinkedList<>();
		test.add("1");
		test.add("2");
		test.add("3");

		dummy.addToLoad(test);
		Thread.sleep(SLEEP_TIME);
		assertThat(dummy.getProcessed(), is(3));
	}

	@Test(timeout = 200)
	public void testTakeData() throws InterruptedException {
		String test[] = { "1", "2", "3" };

		dummy.addToLoad(test);
		assertThat(dummy.takeData(), is(1));
		assertThat(dummy.takeData(), is(2));
		assertThat(dummy.takeData(), is(3));
	}

	@Test
	public void testDrainTo() throws InterruptedException {
		String test[] = { "1", "2", "3" };

		dummy.addToLoad(test);
		Thread.sleep(SLEEP_TIME);
		LinkedList<Integer> result = new LinkedList<>();
		dummy.drainTo(result, 10);

		assertThat(result, hasItems(1, 2, 3));
		assertThat(dummy.getProcessed(), is(3));
	}
}<commit_msg>Replaced fixed wait with spin locks<commit_after>/* The MIT License (MIT)
 * Copyright (c) 2014 <NAME>
 * http://opensource.org/licenses/MIT
 */

package com.github.dozedoff.commonj.io;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.hasItems;
import static org.junit.Assert.assertThat;

import java.util.LinkedList;

import org.junit.Before;
import org.junit.Test;

public class DataProducerTest {
	DataProducerDummy dummy;

	@Before
	public void setUp() throws Exception {
		dummy = new DataProducerDummy();
	}

	private void spinWaitUntil(int availableWork) {
		while (dummy.availableWork() != availableWork) {
		}
	}

	// TODO split clear test into testClearInputQueue and testClearOutputQueue
	@Test(timeout = 1000)
	public void testClear() throws InterruptedException {
		String test[] = { "1", "2", "3" };
		LinkedList<Integer> data = new LinkedList<>();

		dummy.addToLoad(test);
		spinWaitUntil(test.length);
		dummy.drainTo(data, 10);
		assertThat(data.size(), is(3));

		data = new LinkedList<>();

		dummy.addToLoad(test);
		dummy.clear();
		dummy.addToLoad("5");
		spinWaitUntil(1);
		dummy.drainTo(data, 10);

		assertThat(data.size(), is(1));
	}

	@Test
	public void testAddToLoadIArray() throws InterruptedException {
		String test[] = { "1", "2", "3" };

		dummy.addToLoad(test);
		spinWaitUntil(test.length);
		assertThat(dummy.getProcessed(), is(3));
	}

	@Test
	public void testAddToLoadListOfI() throws InterruptedException {
		LinkedList<String> test = new LinkedList<>();
		test.add("1");
		test.add("2");
		test.add("3");

		dummy.addToLoad(test);
		spinWaitUntil(test.size());
		assertThat(dummy.getProcessed(), is(3));
	}

	@Test(timeout = 200)
	public void testTakeData() throws InterruptedException {
		String test[] = { "1", "2", "3" };

		dummy.addToLoad(test);
		assertThat(dummy.takeData(), is(1));
		assertThat(dummy.takeData(), is(2));
		assertThat(dummy.takeData(), is(3));
	}

	@Test
	public void testDrainTo() throws InterruptedException {
		String test[] = { "1", "2", "3" };

		dummy.addToLoad(test);
		spinWaitUntil(test.length);
		LinkedList<Integer> result = new LinkedList<>();
		dummy.drainTo(result, 10);

		assertThat(result, hasItems(1, 2, 3));
		assertThat(dummy.getProcessed(), is(3));
	}
}
{'fraction_non_alphanumeric': 0.15890164303398605, 'fraction_numerical': 0.020706729687148324, 'mean_word_length': 4.824377457404981, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3523009', 'n_tokens_mistral': 1683, 'n_tokens_neox': 1531, 'n_words': 332}