<commit_before>package org.jtrim.event;

import java.util.concurrent.Phaser;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import static org.junit.Assert.*;

/**
 *
 * @author <NAME>
 */
public class InitLaterListenerRefTest {
    public InitLaterListenerRefTest() {
    }

    @BeforeClass
    public static void setUpClass() {
    }

    @AfterClass
    public static void tearDownClass() {
    }

    @Before
    public void setUp() {
    }

    @After
    public void tearDown() {
    }

    @Test
    public void testPreUnregisterInit() {
        ListenerRef wrappedRef = new DummyListenerRef();
        InitLaterListenerRef listenerRef = new InitLaterListenerRef();

        assertTrue(listenerRef.isRegistered());

        listenerRef.init(wrappedRef);

        assertTrue(listenerRef.isRegistered());
        assertTrue(wrappedRef.isRegistered());

        listenerRef.unregister();

        assertFalse(listenerRef.isRegistered());
        assertFalse(wrappedRef.isRegistered());
    }

    @Test
    public void testPostUnregisterInit() {
        ListenerRef wrappedRef = new DummyListenerRef();
        InitLaterListenerRef listenerRef = new InitLaterListenerRef();

        assertTrue(listenerRef.isRegistered());

        listenerRef.unregister();

        assertTrue(listenerRef.isRegistered());

        listenerRef.init(wrappedRef);

        assertFalse(listenerRef.isRegistered());
        assertFalse(wrappedRef.isRegistered());
    }

    @Test(timeout = 10000)
    public void testConcurrentUnregisterInit() throws InterruptedException {
        final int testCount = 100;

        final ListenerRef[] wrappedRefs = new ListenerRef[testCount];
        final InitLaterListenerRef[] listenerRefs = new InitLaterListenerRef[testCount];

        for (int i = 0; i < testCount; i++) {
            wrappedRefs[i] = new DummyListenerRef();
            listenerRefs[i] = new InitLaterListenerRef();
        }

        final Phaser phaser = new Phaser(2);

        Thread unregisterThread = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < testCount; i++) {
                    phaser.arriveAndAwaitAdvance();
                    listenerRefs[i].unregister();
                }
            }
        });
        unregisterThread.start();

        Thread initThread = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < testCount; i++) {
                    phaser.arriveAndAwaitAdvance();
                    listenerRefs[i].init(wrappedRefs[i]);
                }
            }
        });
        initThread.start();

        unregisterThread.join();
        initThread.join();

        for (int i = 0; i < testCount; i++) {
            assertFalse(
                    "Every wrapped listener should have been unregistered.",
                    wrappedRefs[i].isRegistered());
        }
    }

    private static class DummyListenerRef implements ListenerRef {
        private volatile boolean registered;

        public DummyListenerRef() {
            this.registered = true;
        }

        @Override<commit_msg>Added more tests for InitLaterListenerRef<commit_after>package org.jtrim.event;

import java.util.concurrent.Phaser;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import static org.junit.Assert.*;

/**
 *
 * @author <NAME>
 */
public class InitLaterListenerRefTest {
    public InitLaterListenerRefTest() {
    }

    @BeforeClass
    public static void setUpClass() {
    }

    @AfterClass
    public static void tearDownClass() {
    }

    @Before
    public void setUp() {
    }

    @After
    public void tearDown() {
    }

    @Test
    public void testPreUnregisterInit() {
        ListenerRef wrappedRef = new DummyListenerRef();
        InitLaterListenerRef listenerRef = new InitLaterListenerRef();

        assertTrue(listenerRef.isRegistered());

        listenerRef.init(wrappedRef);

        assertTrue(listenerRef.isRegistered());
        assertTrue(wrappedRef.isRegistered());

        listenerRef.unregister();

        assertFalse(listenerRef.isRegistered());
        assertFalse(wrappedRef.isRegistered());
    }

    @Test
    public void testPostUnregisterInit() {
        ListenerRef wrappedRef = new DummyListenerRef();
        InitLaterListenerRef listenerRef = new InitLaterListenerRef();

        assertTrue(listenerRef.isRegistered());

        listenerRef.unregister();

        assertTrue(listenerRef.isRegistered());

        listenerRef.init(wrappedRef);

        assertFalse(listenerRef.isRegistered());
        assertFalse(wrappedRef.isRegistered());
    }

    @Test(timeout = 10000)
    public void testConcurrentUnregisterInit() throws InterruptedException {
        final int testCount = 100;

        final ListenerRef[] wrappedRefs = new ListenerRef[testCount];
        final InitLaterListenerRef[] listenerRefs = new InitLaterListenerRef[testCount];

        for (int i = 0; i < testCount; i++) {
            wrappedRefs[i] = new DummyListenerRef();
            listenerRefs[i] = new InitLaterListenerRef();
        }

        final Phaser phaser = new Phaser(2);

        Thread unregisterThread = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < testCount; i++) {
                    phaser.arriveAndAwaitAdvance();
                    listenerRefs[i].unregister();
                }
            }
        });
        unregisterThread.start();

        Thread initThread = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < testCount; i++) {
                    phaser.arriveAndAwaitAdvance();
                    listenerRefs[i].init(wrappedRefs[i]);
                }
            }
        });
        initThread.start();

        unregisterThread.join();
        initThread.join();

        for (int i = 0; i < testCount; i++) {
            assertFalse(
                    "Every wrapped listener should have been unregistered.",
                    wrappedRefs[i].isRegistered());
        }
    }

    @Test(expected = IllegalStateException.class)
    public void testInitTwice() {
        InitLaterListenerRef listenerRef = new InitLaterListenerRef();
        listenerRef.init(new DummyListenerRef());
        listenerRef.init(new DummyListenerRef());
    }

    @Test(expected = NullPointerException.class)
    public void testInitNull() {
        InitLaterListenerRef listenerRef = new InitLaterListenerRef();
        listenerRef.init(null);
    }

    private static class DummyListenerRef implements ListenerRef {
        private volatile boolean registered;

        public DummyListenerRef() {
            this.registered = true;
        }

        @Override
{'fraction_non_alphanumeric': 0.09897025171624714, 'fraction_numerical': 0.0037185354691075517, 'mean_word_length': 2.2480260102183, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 5, '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': '3021549', 'n_tokens_mistral': 1933, 'n_tokens_neox': 1817, 'n_words': 404}