<commit_before>package org.sphinx.pool;

import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.sphinx.api.ISphinxClient;
import org.sphinx.api.SphinxClient;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;

import static org.testng.Assert.*;
import static org.mockito.Mockito.*;

/**
 * PooledSphinxDataSourceTest
 *
 * @author <NAME>
 * @since 28-05-2015
 */
public class PooledSphinxDataSourceTest {

    private GenericObjectPool<SphinxClient> pool = mock(GenericObjectPool.class);

    @AfterMethod
    public void resetMocks() {
        reset(pool);
    }


    /**
     * Test that data source configuration options are passed down to the factory.
     *
     * @throws Exception
     */
    @Test
    public void testHostAndPort() throws Exception {
        final String host = "localhost";
        final int port = 9312;

        PooledSphinxDataSource dataSource = new PooledSphinxDataSource();
        dataSource.setHost(host);
        dataSource.setPort(port);

        PooledSphinxClientFactory factory = dataSource.getFactory();
        assertEquals(host, factory.getHost());
        assertEquals(port, factory.getPort());
    }

    /**
     * Test getting a client from the pool is managed correctly.
     *
     * @throws Exception
     */
    @Test(groups = "integration")
    public void testGetClient() throws Exception {
        PooledSphinxDataSource dataSource = new PooledSphinxDataSource();

        // get client from pool
        ISphinxClient client = dataSource.getSphinxClient();

        assertNotNull(client);
        assertEquals(1, dataSource.getNumActive());
        assertEquals(0, dataSource.getNumIdle());

        // return to pool
        client.Close();
        assertEquals(0, dataSource.getNumActive());
        assertEquals(1, dataSource.getNumIdle());
    }

    /**
     * Test that a client borrowed from the pool is returned when closed.
     *
     * @throws Exception
     */
    @Test(groups = "mock")
    public void testEnsureBorrowAndClose() throws Exception {
        PooledSphinxDataSource dataSource = new PooledSphinxDataSource(pool);

        SphinxClient delegate = new SphinxClient();
        when(pool.borrowObject()).thenReturn(delegate);

        // verify borrow from pool on get
        ISphinxClient client = dataSource.getSphinxClient();
        verify(pool).borrowObject();

        // verify return to pool on client close
        client.Close();
        verify(pool).returnObject(delegate);
    }

    /**
     * Test that borrowed connections are tested for errors when setTestOnBorrow() is set to true.
     *
     * @throws Exception
     */
    @Test(groups = "mock")
    public void testEnableTestOnBorrow() throws Exception {
        PooledSphinxClientFactory factory = spy(new PooledSphinxClientFactory());
        PooledSphinxDataSource dataSource = new PooledSphinxDataSource(factory);
        dataSource.setTestOnBorrow(true);
        dataSource.setTestOnReturn(false);

        ISphinxClient client = dataSource.getSphinxClient();

        // verify that the validate method was called on the factory
        verify(factory, times(1)).validateObject(any(PooledObject.class));
    }

    /**
     * Test that returned connections are tested for errors when setTestOnReturn() is set to true.
     *
     * @throws Exception
     */
    @Test(groups = "mock")
    public void testEnableTestOnReturn() throws Exception {
        PooledSphinxClientFactory factory = spy(new PooledSphinxClientFactory());
        PooledSphinxDataSource dataSource = new PooledSphinxDataSource(factory);
        dataSource.setTestOnBorrow(false);
        dataSource.setTestOnReturn(true);

        ISphinxClient client = dataSource.getSphinxClient();
        client.Close();

        // verify that the validate method was called on the factory
        verify(factory, times(1)).validateObject(any(PooledObject.class));
    }
}<commit_msg>Test validation failure when borrowing from pool<commit_after>package org.sphinx.pool;

import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.sphinx.api.ISphinxClient;
import org.sphinx.api.SphinxClient;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;

import static org.testng.Assert.*;
import static org.mockito.Mockito.*;

/**
 * PooledSphinxDataSourceTest
 *
 * @author <NAME>
 * @since 28-05-2015
 */
public class PooledSphinxDataSourceTest {

    private GenericObjectPool<SphinxClient> pool = mock(GenericObjectPool.class);

    @AfterMethod
    public void resetMocks() {
        reset(pool);
    }


    /**
     * Test that data source configuration options are passed down to the factory.
     *
     * @throws Exception
     */
    @Test
    public void testHostAndPort() throws Exception {
        final String host = "localhost";
        final int port = 9312;

        PooledSphinxDataSource dataSource = new PooledSphinxDataSource();
        dataSource.setHost(host);
        dataSource.setPort(port);

        PooledSphinxClientFactory factory = dataSource.getFactory();
        assertEquals(host, factory.getHost());
        assertEquals(port, factory.getPort());
    }

    /**
     * Test getting a client from the pool is managed correctly.
     *
     * @throws Exception
     */
    @Test
    public void testGetClient() throws Exception {
        PooledSphinxDataSource dataSource = new PooledSphinxDataSource();

        // get client from pool
        ISphinxClient client = dataSource.getSphinxClient();

        assertNotNull(client);
        assertEquals(1, dataSource.getNumActive());
        assertEquals(0, dataSource.getNumIdle());

        // return to pool
        client.Close();
        assertEquals(0, dataSource.getNumActive());
        assertEquals(1, dataSource.getNumIdle());
    }

    /**
     * Test that a client borrowed from the pool is returned when closed.
     *
     * @throws Exception
     */
    @Test(groups = "mock")
    public void testEnsureBorrowAndClose() throws Exception {
        PooledSphinxDataSource dataSource = new PooledSphinxDataSource(pool);

        SphinxClient delegate = new SphinxClient();
        when(pool.borrowObject()).thenReturn(delegate);

        // verify borrow from pool on get
        ISphinxClient client = dataSource.getSphinxClient();
        verify(pool).borrowObject();

        // verify return to pool on client close
        client.Close();
        verify(pool).returnObject(delegate);
    }

    /**
     * Test that borrowed connections are tested for errors when setTestOnBorrow() is set to true.
     *
     * @throws Exception
     */
    @Test(groups = "mock")
    public void testEnableTestOnBorrow() throws Exception {
        PooledSphinxClientFactory factory = spy(new PooledSphinxClientFactory());
        PooledSphinxDataSource dataSource = new PooledSphinxDataSource(factory);
        dataSource.setTestOnBorrow(true);
        dataSource.setTestOnReturn(false);

        ISphinxClient client = dataSource.getSphinxClient();

        // verify that the validate method was called on the factory
        verify(factory, times(1)).validateObject(any(PooledObject.class));
    }

    /**
     * Test that returned connections are tested for errors when setTestOnReturn() is set to true.
     *
     * @throws Exception
     */
    @Test(groups = "mock")
    public void testEnableTestOnReturn() throws Exception {
        PooledSphinxClientFactory factory = spy(new PooledSphinxClientFactory());
        PooledSphinxDataSource dataSource = new PooledSphinxDataSource(factory);
        dataSource.setTestOnBorrow(false);
        dataSource.setTestOnReturn(true);

        ISphinxClient client = dataSource.getSphinxClient();
        client.Close();

        // verify that the validate method was called on the factory
        verify(factory, times(1)).validateObject(any(PooledObject.class));
    }

    /**
     * Test what happens when the pool continuously fails to produce a valid instance of the sphinx client.
     *
     * @throws Exception
     */
    @Test
    public void testClientFailValidation() throws Exception {
        PooledSphinxClientFactory factory = spy(new PooledSphinxClientFactory());
        PooledSphinxDataSource dataSource = new PooledSphinxDataSource(factory);
        dataSource.setTestOnBorrow(true);

        doReturn(false).when(factory).validateObject(any(PooledObject.class));

        // try and get from pool, object should be invalid.
        // this really only happens if after several tries, all attempts are invalid
        try {
            dataSource.getSphinxClient();
        } catch (Exception e) {
            assertEquals("Unable to validate object", e.getCause().getMessage());
        }
    }
}
{'fraction_non_alphanumeric': 0.09323666024696953, 'fraction_numerical': 0.004531550923303501, 'mean_word_length': 2.9694244604316546, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '7658849', 'n_tokens_mistral': 2424, 'n_tokens_neox': 2357, 'n_words': 605}