<commit_before>/*
 * Copyright (c) 2009-2010, tempus-fugit committers
 *
 * 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.google.code.tempusfugit.concurrency;

import junit.framework.AssertionFailedError;
import org.junit.AfterClass;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.HashSet;
import java.util.Set;

import static java.util.Collections.synchronizedSet;
import static junit.framework.Assert.fail;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;

@RunWith(ConcurrentTestRunner.class)
public class ConcurrentTestRunnerTest {

    private static final Set<String> threads = synchronizedSet(new HashSet<String>());

    @Test
    public void shouldRunInParallel1() {
        logCurrentThread();
    }

    @Test
    public void shouldRunInParallel2() {
        logCurrentThread();
    }

    @Test
    public void shouldRunInParallel3() {
        logCurrentThread();
    }

    @Test
    public void shouldRunInParallel4() {
        logCurrentThread();
    }

    @Test
    public void shouldRunInParallel5() {
        logCurrentThread();
    }

    private void logCurrentThread() {
        threads.add(Thread.currentThread().getName());
    }

    @AfterClass
    public static void assertTestThreadsSpawned() {
        assertThat(threads.size(), is(5));
    }

    @Test (expected = AssertionFailedError.class)
    public void concurrentFailuresFailInTheMainTestThread() throws InterruptedException {
        fail();
    }

}<commit_msg>fixing test, the ConcurrentTestRunner uses a cached thread pool so its possible that if quick enough, the test will reuse a particular thread and so fail the assertion that each test method must run on its own thread. They do run in seperate threads from the main thread but may not all be unique within the context of the test.<commit_after>/*
 * Copyright (c) 2009-2010, tempus-fugit committers
 *
 * 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.google.code.tempusfugit.concurrency;

import com.google.code.tempusfugit.temporal.Condition;
import junit.framework.AssertionFailedError;
import org.junit.AfterClass;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeoutException;

import static com.google.code.tempusfugit.temporal.Duration.seconds;
import static com.google.code.tempusfugit.temporal.Timeout.timeout;
import static com.google.code.tempusfugit.temporal.WaitFor.waitOrTimeout;
import static java.util.Collections.synchronizedSet;
import static junit.framework.Assert.fail;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;

@RunWith(ConcurrentTestRunner.class)
public class ConcurrentTestRunnerTest {

    private static final Set<String> threads = synchronizedSet(new HashSet<String>());

    @Test
    public void shouldRunInParallel1() throws TimeoutException, InterruptedException {
        logCurrentThread();
    }

    @Test
    public void shouldRunInParallel2() throws TimeoutException, InterruptedException {
        logCurrentThread();
    }

    @Test
    public void shouldRunInParallel3() throws TimeoutException, InterruptedException {
        logCurrentThread();
    }

    @Test
    public void shouldRunInParallel4() throws TimeoutException, InterruptedException {
        logCurrentThread();
    }

    @Test
    public void shouldRunInParallel5() throws TimeoutException, InterruptedException {
        logCurrentThread();
    }

    private void logCurrentThread() throws TimeoutException, InterruptedException {
        threads.add(Thread.currentThread().getName());
        waitToForceCachedThreadPoolToCreateNewThread();
    }

    private void waitToForceCachedThreadPoolToCreateNewThread() throws InterruptedException, TimeoutException {
        waitOrTimeout(new Condition() {
            public boolean isSatisfied() {
                return threads.size() == 5;
            }
        }, timeout(seconds(1)));
    }

    @AfterClass
    public static void assertTestThreadsSpawned() {
        assertThat(threads.size(), is(5));
    }

    @Test (expected = AssertionFailedError.class)
    public void concurrentFailuresFailInTheMainTestThread() throws InterruptedException {
        fail();
    }

}
{'fraction_non_alphanumeric': 0.0833019932305378, 'fraction_numerical': 0.007145543437382475, 'mean_word_length': 3.809222423146474, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '6207880', 'n_tokens_mistral': 1457, 'n_tokens_neox': 1418, 'n_words': 438}