<commit_before>package com.relayrides.pushy.apns;

import static org.junit.Assert.*;

import java.util.Collection;
import java.util.Date;

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

import com.relayrides.pushy.apns.util.SimpleApnsPushNotification;

public class ApnsConnectionPoolTest {

	@Test
	public void testAddConnection() {
		final ApnsConnectionPool<SimpleApnsPushNotification> pool =
				new ApnsConnectionPool<SimpleApnsPushNotification>();

		assertEquals(0, pool.getAll().size());
		pool.addConnection(this.createTestConnection());
		assertEquals(1, pool.getAll().size());
	}

	@Test
	public void testRemoveConnection() {
		final ApnsConnectionPool<SimpleApnsPushNotification> pool =
				new ApnsConnectionPool<SimpleApnsPushNotification>();

		assertEquals(0, pool.getAll().size());

		final ApnsConnection<SimpleApnsPushNotification> testConnection = this.createTestConnection();
		pool.addConnection(testConnection);

		assertEquals(1, pool.getAll().size());

		pool.removeConnection(testConnection);
		assertEquals(0, pool.getAll().size());
	}

	@Test(timeout = 1000)
	public void testGetNextConnection() throws InterruptedException {

		{
			final ApnsConnectionPool<SimpleApnsPushNotification> pool =
					new ApnsConnectionPool<SimpleApnsPushNotification>();

			final ApnsConnection<SimpleApnsPushNotification> firstConnection = this.createTestConnection();

			pool.addConnection(firstConnection);

			assertEquals(firstConnection, pool.getNextConnection());

			final ApnsConnection<SimpleApnsPushNotification> secondConnection = this.createTestConnection();
			pool.addConnection(secondConnection);

			assertNotEquals(firstConnection, secondConnection);
			assertNotEquals("Subsequent calls to getNextConnection should return different connections.",
					pool.getNextConnection(), pool.getNextConnection());
		}

		{
			final ApnsConnectionPool<SimpleApnsPushNotification> pool =
					new ApnsConnectionPool<SimpleApnsPushNotification>();

			new Thread(new Runnable() {

				public void run() {
					try {
						Thread.sleep(50);
						pool.addConnection(createTestConnection());
					} catch (InterruptedException e) {
					}
				}
			}).start();

			assertNotNull(pool.getNextConnection());
		}
	}

	@Test
	public void testGetAll() {
		final ApnsConnectionPool<SimpleApnsPushNotification> pool =
				new ApnsConnectionPool<SimpleApnsPushNotification>();

		final ApnsConnection<SimpleApnsPushNotification> firstConnection = this.createTestConnection();
		final ApnsConnection<SimpleApnsPushNotification> secondConnection = this.createTestConnection();

		pool.addConnection(firstConnection);
		pool.addConnection(secondConnection);

		assertEquals(2, pool.getAll().size());
		assertTrue(pool.getAll().contains(firstConnection));
		assertTrue(pool.getAll().contains(secondConnection));
	}

	@Test
	public void testWaitForEmptyPool() throws InterruptedException {
		final ApnsConnectionPool<SimpleApnsPushNotification> pool =
				new ApnsConnectionPool<SimpleApnsPushNotification>();

		final ApnsConnection<SimpleApnsPushNotification> testConnection = this.createTestConnection();

		pool.addConnection(testConnection);

		new Thread(new Runnable() {

			public void run() {
				try {
					Thread.sleep(50);
					pool.removeConnection(testConnection);
				} catch (InterruptedException ignored) {
				}

			}
		}).start();

		assertFalse(pool.getAll().isEmpty());
		pool.waitForEmptyPool(new Date(System.currentTimeMillis() + 1000));
		assertTrue(pool.getAll().isEmpty());
	}

	private ApnsConnection<SimpleApnsPushNotification> createTestConnection() {
		return new ApnsConnection<SimpleApnsPushNotification>(ApnsEnvironment.getSandboxEnvironment(), null, null, new ApnsConnectionListener<SimpleApnsPushNotification>() {
			public void handleConnectionSuccess(ApnsConnection<SimpleApnsPushNotification> connection) {}
			public void handleConnectionFailure(ApnsConnection<SimpleApnsPushNotification> connection, Throwable cause) {}
			public void handleConnectionClosure(ApnsConnection<SimpleApnsPushNotification> connection) {}<commit_msg>Poll for thread state (which is what we're really concerned about) instead of relying on Thread.sleep for "make sure we're blocking" tests.<commit_after>package com.relayrides.pushy.apns;

import static org.junit.Assert.*;

import java.util.Collection;

import org.junit.Test;

import com.relayrides.pushy.apns.util.SimpleApnsPushNotification;

public class ApnsConnectionPoolTest {

	@Test
	public void testAddConnection() {
		final ApnsConnectionPool<SimpleApnsPushNotification> pool =
				new ApnsConnectionPool<SimpleApnsPushNotification>();

		assertEquals(0, pool.getAll().size());
		pool.addConnection(this.createTestConnection());
		assertEquals(1, pool.getAll().size());
	}

	@Test
	public void testRemoveConnection() {
		final ApnsConnectionPool<SimpleApnsPushNotification> pool =
				new ApnsConnectionPool<SimpleApnsPushNotification>();

		assertEquals(0, pool.getAll().size());

		final ApnsConnection<SimpleApnsPushNotification> testConnection = this.createTestConnection();
		pool.addConnection(testConnection);

		assertEquals(1, pool.getAll().size());

		pool.removeConnection(testConnection);
		assertEquals(0, pool.getAll().size());
	}

	@Test(timeout = 1000)
	public void testGetNextConnection() throws InterruptedException {

		{
			final ApnsConnectionPool<SimpleApnsPushNotification> pool =
					new ApnsConnectionPool<SimpleApnsPushNotification>();

			final ApnsConnection<SimpleApnsPushNotification> firstConnection = this.createTestConnection();

			pool.addConnection(firstConnection);

			assertEquals(firstConnection, pool.getNextConnection());

			final ApnsConnection<SimpleApnsPushNotification> secondConnection = this.createTestConnection();
			pool.addConnection(secondConnection);

			assertNotEquals(firstConnection, secondConnection);
			assertNotEquals("Subsequent calls to getNextConnection should return different connections.",
					pool.getNextConnection(), pool.getNextConnection());
		}

		{
			final ApnsConnectionPool<SimpleApnsPushNotification> pool =
					new ApnsConnectionPool<SimpleApnsPushNotification>();

			final Object mutex = new Object();

			final class ConnectionConsumerThread extends Thread {

				private ApnsConnection<SimpleApnsPushNotification> connection;

				@Override
				public void run() {
					try {
						this.connection = pool.getNextConnection();

						synchronized (mutex) {
							mutex.notifyAll();
						}
					} catch (InterruptedException e) {
					}
				}
			}

			final ConnectionConsumerThread consumerThread = new ConnectionConsumerThread();

			consumerThread.start();

			while (!Thread.State.WAITING.equals(consumerThread.getState())) {
				Thread.yield();
			}

			// At this point, we know the consumer thread is waiting for a connection
			synchronized (mutex) {
				pool.addConnection(this.createTestConnection());
				mutex.wait();
			}

			assertNotNull(consumerThread.connection);
		}
	}

	@Test
	public void testGetAll() {
		final ApnsConnectionPool<SimpleApnsPushNotification> pool =
				new ApnsConnectionPool<SimpleApnsPushNotification>();

		final ApnsConnection<SimpleApnsPushNotification> firstConnection = this.createTestConnection();
		final ApnsConnection<SimpleApnsPushNotification> secondConnection = this.createTestConnection();

		pool.addConnection(firstConnection);
		pool.addConnection(secondConnection);

		assertEquals(2, pool.getAll().size());
		assertTrue(pool.getAll().contains(firstConnection));
		assertTrue(pool.getAll().contains(secondConnection));
	}

	@Test(timeout = 1000)
	public void testWaitForEmptyPool() throws InterruptedException {
		{
			final ApnsConnectionPool<SimpleApnsPushNotification> pool =
					new ApnsConnectionPool<SimpleApnsPushNotification>();

			assertTrue(pool.getAll().isEmpty());
			pool.waitForEmptyPool(null);
			assertTrue(pool.getAll().isEmpty());
		}

		{
			final ApnsConnectionPool<SimpleApnsPushNotification> pool =
					new ApnsConnectionPool<SimpleApnsPushNotification>();

			final ApnsConnection<SimpleApnsPushNotification> testConnection = this.createTestConnection();

			pool.addConnection(testConnection);

			final Object mutex = new Object();

			final class WaitForEmptyThread extends Thread {

				private boolean finished = false;

				@Override
				public void run() {
					try {
						pool.waitForEmptyPool(null);

						synchronized (mutex) {
							this.finished = true;
							mutex.notifyAll();
						}
					} catch (InterruptedException e) {
					}

				}
			}

			assertFalse(pool.getAll().isEmpty());

			final WaitForEmptyThread waitThread = new WaitForEmptyThread();
			waitThread.start();

			while (!Thread.State.WAITING.equals(waitThread.getState())) {
				Thread.yield();
			}

			// At this point, we know the consumer thread is waiting for the pool to become empty
			synchronized (mutex) {
				pool.removeConnection(testConnection);
				mutex.wait();
			}

			assertTrue(waitThread.finished);
			assertTrue(pool.getAll().isEmpty());
		}
	}

	private ApnsConnection<SimpleApnsPushNotification> createTestConnection() {
		return new ApnsConnection<SimpleApnsPushNotification>(ApnsEnvironment.getSandboxEnvironment(), null, null, new ApnsConnectionListener<SimpleApnsPushNotification>() {
			public void handleConnectionSuccess(ApnsConnection<SimpleApnsPushNotification> connection) {}
			public void handleConnectionFailure(ApnsConnection<SimpleApnsPushNotification> connection, Throwable cause) {}
			public void handleConnectionClosure(ApnsConnection<SimpleApnsPushNotification> connection) {}
{'fraction_non_alphanumeric': 0.10112711333750783, 'fraction_numerical': 0.0033395950740972655, 'mean_word_length': 5.577213452299245, 'pattern_counts': {'":': 0, '<': 54, '<?xml version=': 0, '>': 54, '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': '7488111', 'n_tokens_mistral': 3006, 'n_tokens_neox': 2527, 'n_words': 487}