Search in sources :

Example 76 with Executor

use of java.util.concurrent.Executor in project neo4j by neo4j.

the class ClusterContextImplTest method shouldKeepTrackOfInstancesWeHaveContacted.

@Test
public void shouldKeepTrackOfInstancesWeHaveContacted() throws Exception {
    // Given
    InstanceId me = new InstanceId(1);
    InstanceId joiningOne = new InstanceId(2);
    InstanceId joiningTwo = new InstanceId(3);
    CommonContextState commonContextState = mock(CommonContextState.class, RETURNS_MOCKS);
    Timeouts timeouts = mock(Timeouts.class);
    Executor executor = mock(Executor.class);
    HeartbeatContext heartbeatContext = mock(HeartbeatContext.class);
    ClusterContext context = new ClusterContextImpl(me, commonContextState, NullLogProvider.getInstance(), timeouts, executor, mock(ObjectOutputStreamFactory.class), mock(ObjectInputStreamFactory.class), mock(LearnerContext.class), heartbeatContext, mock(Config.class));
    ClusterMessage.ConfigurationRequestState requestOne = mock(ClusterMessage.ConfigurationRequestState.class);
    when(requestOne.getJoiningId()).thenReturn(joiningOne);
    ClusterMessage.ConfigurationRequestState requestTwo = mock(ClusterMessage.ConfigurationRequestState.class);
    when(requestTwo.getJoiningId()).thenReturn(joiningTwo);
    // When
    // Instance two contacts us but we are not in the header
    context.addContactingInstance(requestOne, "4, 5");
    // Then we haven't contacted instance 2
    assertFalse(context.haveWeContactedInstance(requestOne));
    // When
    // Instance 2 reports that we have contacted it after all
    context.addContactingInstance(requestOne, "4, 5, 1");
    // Then
    assertTrue(context.haveWeContactedInstance(requestOne));
    // When
    // Instance 3 says we have contacted it
    context.addContactingInstance(requestTwo, "2, 5, 1");
    // Then
    assertTrue(context.haveWeContactedInstance(requestTwo));
    // When
    // For some reason we are not in the header of 3 in subsequent responses (a delayed one, for example)
    context.addContactingInstance(requestTwo, "2, 5");
    // Then
    // The state should still keep the fact we've contacted it already
    assertTrue(context.haveWeContactedInstance(requestTwo));
}
Also used : InstanceId(org.neo4j.cluster.InstanceId) Timeouts(org.neo4j.cluster.timeout.Timeouts) Config(org.neo4j.kernel.configuration.Config) ClusterContext(org.neo4j.cluster.protocol.cluster.ClusterContext) LearnerContext(org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.LearnerContext) Executor(java.util.concurrent.Executor) HeartbeatContext(org.neo4j.cluster.protocol.heartbeat.HeartbeatContext) ObjectInputStreamFactory(org.neo4j.cluster.protocol.atomicbroadcast.ObjectInputStreamFactory) ObjectOutputStreamFactory(org.neo4j.cluster.protocol.atomicbroadcast.ObjectOutputStreamFactory) ClusterMessage(org.neo4j.cluster.protocol.cluster.ClusterMessage) Test(org.junit.Test)

Example 77 with Executor

use of java.util.concurrent.Executor in project neo4j by neo4j.

the class MultiPaxosContextTest method shouldDeepClone.

@Test
public void shouldDeepClone() throws Exception {
    // Given
    ObjectStreamFactory objStream = new ObjectStreamFactory();
    AcceptorInstanceStore acceptorInstances = mock(AcceptorInstanceStore.class);
    Executor executor = mock(Executor.class);
    Timeouts timeouts = mock(Timeouts.class);
    ClusterConfiguration clusterConfig = new ClusterConfiguration("myCluster", NullLogProvider.getInstance());
    ElectionCredentialsProvider electionCredentials = mock(ElectionCredentialsProvider.class);
    Config config = mock(Config.class);
    when(config.get(ClusterSettings.max_acceptors)).thenReturn(10);
    MultiPaxosContext ctx = new MultiPaxosContext(new InstanceId(1), Collections.<ElectionRole>emptyList(), clusterConfig, executor, NullLogProvider.getInstance(), objStream, objStream, acceptorInstances, timeouts, electionCredentials, config);
    // When
    MultiPaxosContext snapshot = ctx.snapshot(NullLogProvider.getInstance(), timeouts, executor, acceptorInstances, objStream, objStream, electionCredentials);
    // Then
    assertEquals(ctx, snapshot);
}
Also used : Executor(java.util.concurrent.Executor) ElectionCredentialsProvider(org.neo4j.cluster.protocol.election.ElectionCredentialsProvider) InstanceId(org.neo4j.cluster.InstanceId) Timeouts(org.neo4j.cluster.timeout.Timeouts) Config(org.neo4j.kernel.configuration.Config) MultiPaxosContext(org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.context.MultiPaxosContext) ClusterConfiguration(org.neo4j.cluster.protocol.cluster.ClusterConfiguration) ObjectStreamFactory(org.neo4j.cluster.protocol.atomicbroadcast.ObjectStreamFactory) Test(org.junit.Test)

Example 78 with Executor

use of java.util.concurrent.Executor in project zipkin by openzipkin.

the class LazyTest method get_memoizes.

@Test(timeout = 1000L)
public void get_memoizes() throws InterruptedException {
    int getCount = 1000;
    AtomicInteger value = new AtomicInteger();
    Lazy<Integer> lazyInt = new Lazy<Integer>() {

        final AtomicInteger val = new AtomicInteger();

        @Override
        protected Integer compute() {
            return val.incrementAndGet();
        }
    };
    CountDownLatch latch = new CountDownLatch(getCount);
    Executor exec = Executors.newFixedThreadPool(10);
    for (int i = 0; i < getCount; i++) {
        exec.execute(() -> {
            // if lazy computes multiple times, the result of lazyInt.get() > 1
            value.getAndAdd(lazyInt.get());
            latch.countDown();
        });
    }
    latch.await();
    assertThat(value.get()).isEqualTo(getCount);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Executor(java.util.concurrent.Executor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 79 with Executor

use of java.util.concurrent.Executor in project dev-summit-architecture-demo by yigit.

the class AutoCancelAsyncTaskTest method unregisterAfterCompletion.

@Test
public void unregisterAfterCompletion() {
    Executor mockExecutor = new Executor() {

        @Override
        public void execute(Runnable command) {
            command.run();
        }
    };
    LifecycleProvider provider = mock(LifecycleProvider.class);
    doNothing().when(provider).addLifecycleListener(any(LifecycleListener.class));
    AutoCancelAsyncTask<Void, Void> task = new AutoCancelAsyncTask<Void, Void>(provider) {

        @Override
        protected void onResult(Void aVoid) {
        }

        @Override
        protected Void onDoInBackground(Void... params) {
            return null;
        }
    };
    task.executeOnExecutor(mockExecutor);
    verify(provider).removeLifecycleListener(any(LifecycleListener.class));
}
Also used : Executor(java.util.concurrent.Executor) LifecycleProvider(com.android.example.devsummit.archdemo.util.LifecycleProvider) LifecycleListener(com.android.example.devsummit.archdemo.util.LifecycleListener) Test(org.junit.Test)

Example 80 with Executor

use of java.util.concurrent.Executor in project java-chassis by ServiceComb.

the class MockUtil method mockMicroserviceMetaManager.

public void mockMicroserviceMetaManager() {
    new MockUp<MicroserviceMetaManager>() {

        @Mock
        public SchemaMeta ensureFindSchemaMeta(String schemaId) {
            SchemaMeta schemaMeta = Mockito.mock(SchemaMeta.class);
            OperationMeta operationMeta = Mockito.mock(OperationMeta.class);
            Mockito.when(schemaMeta.ensureFindOperation(null)).thenReturn(operationMeta);
            Method method = this.getClass().getMethods()[0];
            Mockito.when(operationMeta.getMethod()).thenReturn(method);
            OperationProtobuf operationProtobuf = Mockito.mock(OperationProtobuf.class);
            Mockito.when(operationMeta.getExtData("protobuf")).thenReturn(operationProtobuf);
            Executor lExecutor = Mockito.mock(Executor.class);
            Mockito.when(operationMeta.getExecutor()).thenReturn(lExecutor);
            return schemaMeta;
        }
    };
}
Also used : Executor(java.util.concurrent.Executor) OperationProtobuf(io.servicecomb.codec.protobuf.definition.OperationProtobuf) SchemaMeta(io.servicecomb.core.definition.SchemaMeta) MockUp(mockit.MockUp) OperationMeta(io.servicecomb.core.definition.OperationMeta) Method(java.lang.reflect.Method)

Aggregations

Executor (java.util.concurrent.Executor)302 Test (org.junit.Test)127 ArrayList (java.util.ArrayList)35 CountDownLatch (java.util.concurrent.CountDownLatch)32 IOException (java.io.IOException)29 List (java.util.List)27 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)22 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)17 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)16 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)16 Map (java.util.Map)15 Timeouts (org.neo4j.cluster.timeout.Timeouts)15 ExecutorService (java.util.concurrent.ExecutorService)13 InstanceId (org.neo4j.cluster.InstanceId)13 Config (org.neo4j.kernel.configuration.Config)13 File (java.io.File)12 ObjectInputStreamFactory (org.neo4j.cluster.protocol.atomicbroadcast.ObjectInputStreamFactory)12 ObjectOutputStreamFactory (org.neo4j.cluster.protocol.atomicbroadcast.ObjectOutputStreamFactory)12 HeartbeatContext (org.neo4j.cluster.protocol.heartbeat.HeartbeatContext)12 InetSocketAddress (java.net.InetSocketAddress)10