Search in sources :

Example 1 with Extractor

use of org.assertj.core.api.iterable.Extractor in project java-driver by datastax.

the class ThreadingOptionsTest method should_use_provided_threading_options.

/**
 * Validates that when using a provided {@link ThreadingOptions} that its methods are used for creating
 * executors and that its {@link ThreadingOptions#createThreadFactory(String, String)} is used for initializing
 * netty resources.
 *
 * @test_category configuration
 */
@Test(groups = "short")
public void should_use_provided_threading_options() {
    ThreadingOptions spy = Mockito.spy(threadingOptions);
    Cluster cluster = createClusterBuilder().withPoolingOptions(new PoolingOptions().setConnectionsPerHost(HostDistance.LOCAL, 1, 1)).withReconnectionPolicy(new ConstantReconnectionPolicy(100)).withThreadingOptions(spy).build();
    try {
        String clusterName = cluster.getClusterName();
        cluster.init();
        // Ensure each method was invoked appropriately:
        // 1) 1 time for each create*Executor.
        // 2) createThreadFactory for netty executor group and timeouter.
        verify(spy).createExecutor(clusterName);
        verify(spy).createBlockingExecutor(clusterName);
        verify(spy).createReconnectionExecutor(clusterName);
        verify(spy).createScheduledTasksExecutor(clusterName);
        verify(spy).createReaperExecutor(clusterName);
        verify(spy).createThreadFactory(clusterName, "nio-worker");
        verify(spy).createThreadFactory(clusterName, "timeouter");
        cluster.connect();
        // Close all connections bringing the host down, this should cause some activity on
        // executor and reconnection executor.
        currentClient.disableListener();
        currentClient.closeConnections(CLOSE);
        TestUtils.waitForDown(TestUtils.IP_PREFIX + "1", cluster);
        currentClient.enableListener();
        TestUtils.waitForUp(TestUtils.IP_PREFIX + "1", cluster);
        Set<Thread> threads = Thread.getAllStackTraces().keySet();
        for (Thread thread : threads) {
            // all threads should use the custom factory and thus be marked daemon
            if (thread.getName().startsWith(clusterName + "-" + customPrefix)) {
                // all created threads should be daemon this should indicate that our custom thread factory was
                // used.
                assertThat(thread.isDaemon()).isTrue();
            }
        }
        final Pattern threadNamePattern = Pattern.compile(clusterName + "-" + customPrefix + "-(.*)-0");
        // Custom executor threads should be present.
        // NOTE: we don't validate blocking executor since it is hard to deterministically cause it to be used.
        assertThat(threads).extracting(new Extractor<Thread, String>() {

            @Override
            public String extract(Thread thread) {
                Matcher matcher = threadNamePattern.matcher(thread.getName());
                if (matcher.matches()) {
                    return matcher.group(1);
                } else {
                    return thread.getName();
                }
            }
        }).contains("nio-worker", "timeouter", "myExecutor", "myReconnection", "myScheduled-task-worker", "myConnection-reaper");
    } finally {
        cluster.close();
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Extractor(org.assertj.core.api.iterable.Extractor) ConstantReconnectionPolicy(com.datastax.driver.core.policies.ConstantReconnectionPolicy) Test(org.testng.annotations.Test)

Example 2 with Extractor

use of org.assertj.core.api.iterable.Extractor in project powermock by powermock.

the class ConstructorCallMockTransformerTest method should_provide_correct_constructor_param_and_arguments_when_cast_required.

@Test
public void should_provide_correct_constructor_param_and_arguments_when_cast_required() throws Exception {
    assumeClassLoaderMode();
    MockGatewaySpy.returnOnMethodCall(SUPPRESS);
    final Class<?> clazz = loadWithMockClassLoader(SuperClassCallSuperConstructorWithCast.class.getName());
    final Class<?> paramClass = loadWithMockClassLoader(ParameterInterface.class.getName());
    final Object param = loadWithMockClassLoader(ParameterImpl.class.getName()).newInstance();
    final Constructor<?> constructor = clazz.getConstructor(paramClass);
    constructor.newInstance(param);
    assertThatCorrectConstructorTypeProvided();
    final MethodCall methodCall = MockGatewaySpy.constructorCalls().get(0);
    assertThat(methodCall.args).as("Correct constructor arguments are provided").containsExactly(param);
    assertThat(methodCall.sig).as("Correct constructor signature is provided").hasSize(1).extracting(new Extractor<Class<?>, Object>() {

        @Override
        public Object extract(final Class<?> input) {
            return input.getName();
        }
    }).containsExactly(ParameterImpl.class.getName());
}
Also used : ParameterInterface(powermock.test.support.MainMockTransformerTestSupport.ParameterInterface) ParameterImpl(powermock.test.support.MainMockTransformerTestSupport.ParameterImpl) NestedTestClass(powermock.test.support.MainMockTransformerTestSupport.ParentTestClass.NestedTestClass) PublicSuperClass(powermock.test.support.MainMockTransformerTestSupport.SupportClasses.PublicSuperClass) SubClass(powermock.test.support.MainMockTransformerTestSupport.SupportClasses.SubClass) ParentTestClass(powermock.test.support.MainMockTransformerTestSupport.ParentTestClass) IndicateReloadClass(org.powermock.core.IndicateReloadClass) EnumClass(powermock.test.support.MainMockTransformerTestSupport.SupportClasses.EnumClass) Extractor(org.assertj.core.api.iterable.Extractor) MethodCall(org.powermock.core.transformers.mock.MockGatewaySpy.MethodCall) SuperClassCallSuperConstructorWithCast(powermock.test.support.MainMockTransformerTestSupport.SuperClassCallSuperConstructorWithCast) Test(org.junit.Test)

Aggregations

Extractor (org.assertj.core.api.iterable.Extractor)2 ConstantReconnectionPolicy (com.datastax.driver.core.policies.ConstantReconnectionPolicy)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Test (org.junit.Test)1 IndicateReloadClass (org.powermock.core.IndicateReloadClass)1 MethodCall (org.powermock.core.transformers.mock.MockGatewaySpy.MethodCall)1 Test (org.testng.annotations.Test)1 ParameterImpl (powermock.test.support.MainMockTransformerTestSupport.ParameterImpl)1 ParameterInterface (powermock.test.support.MainMockTransformerTestSupport.ParameterInterface)1 ParentTestClass (powermock.test.support.MainMockTransformerTestSupport.ParentTestClass)1 NestedTestClass (powermock.test.support.MainMockTransformerTestSupport.ParentTestClass.NestedTestClass)1 SuperClassCallSuperConstructorWithCast (powermock.test.support.MainMockTransformerTestSupport.SuperClassCallSuperConstructorWithCast)1 EnumClass (powermock.test.support.MainMockTransformerTestSupport.SupportClasses.EnumClass)1 PublicSuperClass (powermock.test.support.MainMockTransformerTestSupport.SupportClasses.PublicSuperClass)1 SubClass (powermock.test.support.MainMockTransformerTestSupport.SupportClasses.SubClass)1