Search in sources :

Example 1 with ConcurrentSet

use of io.netty.util.internal.ConcurrentSet in project core-java by SpineEventEngine.

the class StandPostShould method deliver_updates_through_several_threads.

@SuppressWarnings("MethodWithMultipleLoops")
@Test
public void deliver_updates_through_several_threads() throws InterruptedException {
    final int threadsCount = Given.THREADS_COUNT_IN_POOL_EXECUTOR;
    // Too long variable name
    @SuppressWarnings("LocalVariableNamingConvention") final int threadExecutionMaxAwaitSeconds = Given.AWAIT_SECONDS;
    final Set<String> threadInvocationRegistry = new ConcurrentSet<>();
    final Stand stand = Stand.newBuilder().build();
    final ExecutorService executor = Executors.newFixedThreadPool(threadsCount);
    final Runnable task = new Runnable() {

        @Override
        public void run() {
            final String threadName = Thread.currentThread().getName();
            Assert.assertFalse(threadInvocationRegistry.contains(threadName));
            final ProjectId enitityId = ProjectId.newBuilder().setId(Identifier.newUuid()).build();
            final Given.StandTestAggregate entity = Given.aggregateRepo().create(enitityId);
            stand.post(requestFactory.createCommandContext().getActorContext().getTenantId(), entity);
            threadInvocationRegistry.add(threadName);
        }
    };
    for (int i = 0; i < threadsCount; i++) {
        executor.execute(task);
    }
    executor.awaitTermination(threadExecutionMaxAwaitSeconds, TimeUnit.SECONDS);
    Assert.assertEquals(threadInvocationRegistry.size(), threadsCount);
}
Also used : ConcurrentSet(io.netty.util.internal.ConcurrentSet) ExecutorService(java.util.concurrent.ExecutorService) ProjectId(io.spine.test.projection.ProjectId) Test(org.junit.Test)

Example 2 with ConcurrentSet

use of io.netty.util.internal.ConcurrentSet in project ribbon by Netflix.

the class RxMovieServer method handleUpdateRecommendationsForUser.

private Observable<Void> handleUpdateRecommendationsForUser(HttpServerRequest<ByteBuf> request, final HttpServerResponse<ByteBuf> response) {
    System.out.println("HTTP request -> update recommendations for user: " + request.getPath());
    final String userId = userIdFromPath(request.getPath());
    if (userId == null) {
        response.setStatus(HttpResponseStatus.BAD_REQUEST);
        return response.close();
    }
    return request.getContent().flatMap(new Func1<ByteBuf, Observable<Void>>() {

        @Override
        public Observable<Void> call(ByteBuf byteBuf) {
            String movieId = byteBuf.toString(Charset.defaultCharset());
            System.out.println(format("    updating: {user=%s, movie=%s}", userId, movieId));
            synchronized (this) {
                Set<String> recommendations;
                if (userRecommendations.containsKey(userId)) {
                    recommendations = userRecommendations.get(userId);
                } else {
                    recommendations = new ConcurrentSet<String>();
                    userRecommendations.put(userId, recommendations);
                }
                recommendations.add(movieId);
            }
            response.setStatus(HttpResponseStatus.OK);
            return response.close();
        }
    });
}
Also used : Set(java.util.Set) ConcurrentSet(io.netty.util.internal.ConcurrentSet) ConcurrentSet(io.netty.util.internal.ConcurrentSet) String(java.lang.String) ByteBuf(io.netty.buffer.ByteBuf) Observable(rx.Observable)

Example 3 with ConcurrentSet

use of io.netty.util.internal.ConcurrentSet in project x-pipe by ctripcorp.

the class SenderManagerTest method testSenderManager.

@Test
public void testSenderManager() {
    HostPort hostPort = new HostPort("192.168.1.10", 6379);
    Map<ALERT_TYPE, Set<AlertEntity>> alerts = new ConcurrentHashMap<>();
    AlertEntity alert = new AlertEntity(hostPort, dcNames[0], "cluster-test", "shard-test", "", ALERT_TYPE.XREDIS_VERSION_NOT_VALID);
    Set<AlertEntity> set = new ConcurrentSet<>();
    set.add(alert);
    alerts.put(ALERT_TYPE.XREDIS_VERSION_NOT_VALID, set);
    new Thread(new Runnable() {

        @Override
        public void run() {
            alerts.get(ALERT_TYPE.XREDIS_VERSION_NOT_VALID).remove(alert);
        }
    }).start();
    List<Map<ALERT_TYPE, Set<AlertEntity>>> result = senderManager.getGroupedAlerts(alerts);
    logger.info("result: {}", result.get(0));
    if (!result.isEmpty()) {
        Set<AlertEntity> alertEntities = result.get(0).getOrDefault(alert.getAlertType(), null);
        if (alertEntities != null) {
            Assert.assertFalse(alertEntities.isEmpty());
        }
    }
}
Also used : ConcurrentSet(io.netty.util.internal.ConcurrentSet) ConcurrentSet(io.netty.util.internal.ConcurrentSet) HostPort(com.ctrip.xpipe.endpoint.HostPort) AlertEntity(com.ctrip.xpipe.redis.console.alert.AlertEntity) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ALERT_TYPE(com.ctrip.xpipe.redis.console.alert.ALERT_TYPE) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) AbstractConsoleIntegrationTest(com.ctrip.xpipe.redis.console.AbstractConsoleIntegrationTest) Test(org.junit.Test)

Example 4 with ConcurrentSet

use of io.netty.util.internal.ConcurrentSet in project rocketmq-externals by apache.

the class PositionManagementServiceImplTest method testSendNeedSynchronizePosition.

@Test
public void testSendNeedSynchronizePosition() throws Exception {
    positionManagementService.putPosition(positions);
    ByteBuffer sourcePartitionTmp = ByteBuffer.wrap("127.0.0.2:3306".getBytes("UTF-8"));
    JSONObject jsonObject = new JSONObject();
    ByteBuffer sourcePositionTmp = ByteBuffer.wrap(jsonObject.toJSONString().getBytes());
    positionStore.put(sourcePartitionTmp, sourcePositionTmp);
    Set<ByteBuffer> needSyncPartitionTmp = needSyncPartition;
    needSyncPartition = new ConcurrentSet<>();
    Map<ByteBuffer, ByteBuffer> needSyncPosition = positionStore.getKVMap().entrySet().stream().filter(entry -> needSyncPartitionTmp.contains(entry.getKey())).collect(Collectors.toMap(entry -> entry.getKey(), entry -> entry.getValue()));
    assertTrue(needSyncPartition.size() == 0);
    ByteBuffer bytes = needSyncPosition.get(sourcePartition);
    assertNotNull(bytes);
    ByteBuffer tmpBytes = needSyncPosition.get(sourcePartitionTmp);
    assertNull(tmpBytes);
    List<ByteBuffer> sourcePartitions = new ArrayList<ByteBuffer>(8) {

        {
            add(sourcePartition);
            add(sourcePartitionTmp);
        }
    };
    needSyncPartition = needSyncPartitionTmp;
    needSyncPartition.addAll(sourcePartitions);
    positionManagementService.removePosition(sourcePartitions);
    assertTrue(needSyncPartition.size() == 0);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) SendCallback(org.apache.rocketmq.client.producer.SendCallback) Message(org.apache.rocketmq.common.message.Message) DefaultMQPushConsumer(org.apache.rocketmq.client.consumer.DefaultMQPushConsumer) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) ConnectConfig(org.apache.rocketmq.connect.runtime.config.ConnectConfig) DataSynchronizerCallback(org.apache.rocketmq.connect.runtime.utils.datasync.DataSynchronizerCallback) ConcurrentSet(io.netty.util.internal.ConcurrentSet) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) ConnAndTaskConfigs(org.apache.rocketmq.connect.runtime.common.ConnAndTaskConfigs) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Map(java.util.Map) After(org.junit.After) Mockito.doAnswer(org.mockito.Mockito.doAnswer) TestUtils(org.apache.rocketmq.connect.runtime.utils.TestUtils) BrokerBasedLog(org.apache.rocketmq.connect.runtime.utils.datasync.BrokerBasedLog) Method(java.lang.reflect.Method) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) Before(org.junit.Before) Assert.assertNotNull(org.junit.Assert.assertNotNull) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Field(java.lang.reflect.Field) Collectors(java.util.stream.Collectors) File(java.io.File) SendResult(io.openmessaging.producer.SendResult) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) KeyValueStore(org.apache.rocketmq.connect.runtime.store.KeyValueStore) Assert.assertFalse(org.junit.Assert.assertFalse) Future(io.openmessaging.Future) JSONObject(com.alibaba.fastjson.JSONObject) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) JSONObject(com.alibaba.fastjson.JSONObject) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Aggregations

ConcurrentSet (io.netty.util.internal.ConcurrentSet)4 Test (org.junit.Test)3 Set (java.util.Set)2 JSONObject (com.alibaba.fastjson.JSONObject)1 HostPort (com.ctrip.xpipe.endpoint.HostPort)1 AbstractConsoleIntegrationTest (com.ctrip.xpipe.redis.console.AbstractConsoleIntegrationTest)1 ALERT_TYPE (com.ctrip.xpipe.redis.console.alert.ALERT_TYPE)1 AlertEntity (com.ctrip.xpipe.redis.console.alert.AlertEntity)1 ByteBuf (io.netty.buffer.ByteBuf)1 Future (io.openmessaging.Future)1 SendResult (io.openmessaging.producer.SendResult)1 ProjectId (io.spine.test.projection.ProjectId)1 File (java.io.File)1 String (java.lang.String)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1