<commit_before>package com.timgroup.eventsubscription.healthcheck;

import org.junit.After;
import org.junit.Test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

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

public class ServerSubscriptionListenerTest {

    private final TestPositionCodec testPositionCodec = new TestPositionCodec();
    private final CountDownLatch latch = new CountDownLatch(1);
    private final ExecutorService executorService = Executors.newFixedThreadPool(1);

    @After public void
    cleanup() {
        executorService.shutdown();
    }

    @Test public void
    awaits_for_caught_up() throws InterruptedException {
        ServerSubscriptionListener listener = new ServerSubscriptionListener();
        listener.caughtUpAt(new TestPosition(1));

        executorService.submit(() -> {
            listener.await(new TestPosition(2), testPositionCodec);
            latch.countDown();
        });

        listener.caughtUpAt(new TestPosition(2));
        latch.await(200, TimeUnit.MILLISECONDS);
        assertThat(latch.getCount(), is(0L));
    }

    @Test public void
    rethrows_termination_exception() {
        ServerSubscriptionListener listener = new ServerSubscriptionListener();
        Exception expected = new Exception("Termination exception");

        listener.terminated(new TestPosition(1), expected);

        try {
            listener.await(new TestPosition(Long.MAX_VALUE), testPositionCodec);
        } catch (IllegalStateException e) {
            assertThat(e.getCause(), equalTo(expected));
            return;<commit_msg>SteveH: check return value from await rather than latch count separately<commit_after>package com.timgroup.eventsubscription.healthcheck;

import org.junit.After;
import org.junit.Test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.fail;

public class ServerSubscriptionListenerTest {

    private final TestPositionCodec testPositionCodec = new TestPositionCodec();
    private final CountDownLatch latch = new CountDownLatch(1);
    private final ExecutorService executorService = Executors.newFixedThreadPool(1);

    @After public void
    cleanup() {
        executorService.shutdown();
    }

    @Test public void
    awaits_for_caught_up() throws InterruptedException {
        ServerSubscriptionListener listener = new ServerSubscriptionListener();
        listener.caughtUpAt(new TestPosition(1));

        executorService.submit(() -> {
            listener.await(new TestPosition(2), testPositionCodec);
            latch.countDown();
        });

        listener.caughtUpAt(new TestPosition(2));
        if (!latch.await(200, TimeUnit.MILLISECONDS))
            throw new AssertionError("listener await didn't complete");
    }

    @Test public void
    rethrows_termination_exception() {
        ServerSubscriptionListener listener = new ServerSubscriptionListener();
        Exception expected = new Exception("Termination exception");

        listener.terminated(new TestPosition(1), expected);

        try {
            listener.await(new TestPosition(Long.MAX_VALUE), testPositionCodec);
        } catch (IllegalStateException e) {
            assertThat(e.getCause(), equalTo(expected));
            return;
{'fraction_non_alphanumeric': 0.08968850698174007, 'fraction_numerical': 0.00510204081632653, 'mean_word_length': 3.7817715019255456, 'pattern_counts': {'":': 0, '<': 3, '<?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': '7156309', 'n_tokens_mistral': 982, 'n_tokens_neox': 982, 'n_words': 211}