Example usage for java.util.concurrent.atomic AtomicInteger get

List of usage examples for java.util.concurrent.atomic AtomicInteger get

Introduction

In this page you can find the example usage for java.util.concurrent.atomic AtomicInteger get.

Prototype

public final int get() 

Source Link

Document

Returns the current value, with memory effects as specified by VarHandle#getVolatile .

Usage

From source file:com.netflix.curator.framework.recipes.locks.TestInterProcessSemaphoreCluster.java

@Test
public void testKilledServerWithEnsembleProvider() throws Exception {
    final int CLIENT_QTY = 10;
    final Timing timing = new Timing();
    final String PATH = "/foo/bar/lock";

    ExecutorService executorService = Executors.newFixedThreadPool(CLIENT_QTY);
    ExecutorCompletionService<Void> completionService = new ExecutorCompletionService<Void>(executorService);
    TestingCluster cluster = new TestingCluster(3);
    try {//from w  ww. jav  a  2 s . c  o  m
        cluster.start();

        final AtomicReference<String> connectionString = new AtomicReference<String>(
                cluster.getConnectString());
        final EnsembleProvider provider = new EnsembleProvider() {
            @Override
            public void start() throws Exception {
            }

            @Override
            public String getConnectionString() {
                return connectionString.get();
            }

            @Override
            public void close() throws IOException {
            }
        };

        final Semaphore acquiredSemaphore = new Semaphore(0);
        final AtomicInteger acquireCount = new AtomicInteger(0);
        final CountDownLatch suspendedLatch = new CountDownLatch(CLIENT_QTY);
        for (int i = 0; i < CLIENT_QTY; ++i) {
            completionService.submit(new Callable<Void>() {
                @Override
                public Void call() throws Exception {
                    CuratorFramework client = CuratorFrameworkFactory.builder().ensembleProvider(provider)
                            .sessionTimeoutMs(timing.session()).connectionTimeoutMs(timing.connection())
                            .retryPolicy(new ExponentialBackoffRetry(100, 3)).build();
                    try {
                        final Semaphore suspendedSemaphore = new Semaphore(0);
                        client.getConnectionStateListenable().addListener(new ConnectionStateListener() {
                            @Override
                            public void stateChanged(CuratorFramework client, ConnectionState newState) {
                                if ((newState == ConnectionState.SUSPENDED)
                                        || (newState == ConnectionState.LOST)) {
                                    suspendedLatch.countDown();
                                    suspendedSemaphore.release();
                                }
                            }
                        });

                        client.start();

                        InterProcessSemaphoreV2 semaphore = new InterProcessSemaphoreV2(client, PATH, 1);

                        while (!Thread.currentThread().isInterrupted()) {
                            Lease lease = null;
                            try {
                                lease = semaphore.acquire();
                                acquiredSemaphore.release();
                                acquireCount.incrementAndGet();
                                suspendedSemaphore.acquire();
                            } catch (Exception e) {
                                // just retry
                            } finally {
                                if (lease != null) {
                                    acquireCount.decrementAndGet();
                                    IOUtils.closeQuietly(lease);
                                }
                            }
                        }
                    } finally {
                        IOUtils.closeQuietly(client);
                    }
                    return null;
                }
            });
        }

        Assert.assertTrue(timing.acquireSemaphore(acquiredSemaphore));
        Assert.assertEquals(1, acquireCount.get());

        cluster.close();
        timing.awaitLatch(suspendedLatch);
        timing.forWaiting().sleepABit();
        Assert.assertEquals(0, acquireCount.get());

        cluster = new TestingCluster(3);
        cluster.start();

        connectionString.set(cluster.getConnectString());
        timing.forWaiting().sleepABit();

        Assert.assertTrue(timing.acquireSemaphore(acquiredSemaphore));
        timing.forWaiting().sleepABit();
        Assert.assertEquals(1, acquireCount.get());
    } finally {
        executorService.shutdown();
        executorService.awaitTermination(10, TimeUnit.SECONDS);
        executorService.shutdownNow();
        IOUtils.closeQuietly(cluster);
    }
}

From source file:com.asakusafw.lang.compiler.cli.BatchCompilerCliTest.java

/**
 * full args./*from w w  w .ja  va  2s  . c  om*/
 * @throws Exception if failed
 */
@Test
public void execute_full() throws Exception {
    File output = deployer.newFolder();
    File explore = prepareLibrary("explore");
    File external = prepareLibrary("external");
    File embed = prepareLibrary("embed");
    File attach = prepareLibrary("attach");
    String[] args = strings(new Object[] { "--explore",
            files(ResourceUtil.findLibraryByClass(DummyBatch.class), explore), "--output", output,
            "--classAnalyzer", classes(DummyClassAnalyzer.class), "--batchCompiler",
            classes(DelegateBatchCompiler.class), "--external", files(external), "--embed", files(embed),
            "--attach", files(attach), "--include", classes(DummyBatch.class), "--dataModelProcessors",
            classes(DummyDataModelProcessor.class), "--externalPortProcessors",
            classes(DummyExternalPortProcessor.class), "--batchProcessors", classes(DummyBatchProcessor.class),
            "--jobflowProcessors", classes(DummyJobflowProcessor.class), "--participants",
            classes(DummyCompilerParticipant.class), "--runtimeWorkingDirectory", "testRuntimeWorkingDirectory",
            "--failOnError", true, "--batchIdPrefix", "prefix.", "-P", "a=b", "-property", "c=d", });
    AtomicInteger count = new AtomicInteger();
    int status = execute(args, (context, batch) -> {
        count.incrementAndGet();
        assertThat(batch.getBatchId(), is("prefix.DummyBatch"));
        assertThat(batch.getDescriptionClass(), is(classOf(DummyBatch.class)));
        assertThat(context.getOutput().getBasePath(), is(new File(output, batch.getBatchId())));

        assertThat(context.getProject().getClassLoader().getResource("explore"), is(notNullValue()));
        assertThat(context.getProject().getClassLoader().getResource("embed"), is(notNullValue()));
        assertThat(context.getProject().getClassLoader().getResource("attach"), is(notNullValue()));
        assertThat(context.getProject().getClassLoader().getResource("external"), is(notNullValue()));

        assertThat(context.getProject().getProjectContents(), includes("explore"));
        assertThat(context.getProject().getProjectContents(), not(includes("embed")));
        assertThat(context.getProject().getProjectContents(), not(includes("attach")));
        assertThat(context.getProject().getProjectContents(), not(includes("external")));

        // --explore -> implicitly embedded
        assertThat(context.getProject().getEmbeddedContents(), includes("explore"));
        assertThat(context.getProject().getEmbeddedContents(), includes("embed"));
        assertThat(context.getProject().getEmbeddedContents(), not(includes("attach")));
        assertThat(context.getProject().getEmbeddedContents(), not(includes("external")));

        assertThat(context.getProject().getAttachedLibraries(), not(deepIncludes("explore")));
        assertThat(context.getProject().getAttachedLibraries(), not(deepIncludes("embed")));
        assertThat(context.getProject().getAttachedLibraries(), deepIncludes("attach"));
        assertThat(context.getProject().getAttachedLibraries(), not(deepIncludes("external")));

        assertThat(context.getTools().getDataModelProcessor(), is(consistsOf(DummyDataModelProcessor.class)));
        assertThat(context.getTools().getExternalPortProcessor(),
                is(consistsOf(DummyExternalPortProcessor.class)));
        assertThat(context.getTools().getBatchProcessor(), is(consistsOf(DummyBatchProcessor.class)));
        assertThat(context.getTools().getJobflowProcessor(), is(consistsOf(DummyJobflowProcessor.class)));
        assertThat(context.getTools().getParticipant(), is(consistsOf(DummyCompilerParticipant.class)));
    });
    assertThat(status, is(0));
    assertThat(count.get(), is(1));
}

From source file:byps.test.TestRemoteServerR.java

/**
 * This test simulates a pause time where the client has no active long-pool 
 * and the server tries to contact the client.
 * @throws RemoteException/*from  w w  w . jav a  2s  . c  om*/
 * @throws InterruptedException
 */
@Test
public void testServerCallsClientNoActiveLongPoll() throws RemoteException, InterruptedException {
    log.info("testServerCallsClientNoActiveLongPoll(");

    final boolean[] holdClient = new boolean[] { true };
    final AtomicInteger r1 = new AtomicInteger();
    final AtomicInteger r2 = new AtomicInteger();
    final AtomicInteger r3 = new AtomicInteger();
    final AtomicReference<Throwable> ex = new AtomicReference<Throwable>();
    final CountDownLatch countDown = new CountDownLatch(3);

    BClient_Testser client = TestUtilsHttp.createClient(1);
    ServerIFAsync remote = client.getServerIF();

    // Provide implementation for interface ClientIF
    client.addRemote(new BSkeleton_ClientIF() {
        @Override
        public int incrementInt(int a) throws RemoteException {
            log.info("incrementInt(" + a + ")");

            try {
                synchronized (holdClient) {
                    while (holdClient[0]) {
                        // (1) wait until (4)
                        holdClient.wait(100 * 1000);
                    }
                }
            } catch (Throwable e) {
            }

            return a + 1;
        }
    });

    // (2) Trigger one client invocation to allocate the current active long-poll.
    // The client will receive the request and will wait in (1).
    remote.callClientIncrementInt(0, new BAsyncResultIgnored<Integer>());

    // (3) Trigger server to invoke client.
    // Server will try to send requests to the client but there is no open long-poll.
    // Server has to wait until it receives a long-poll.
    internalCallClientAsync(remote, 5, r1, ex, countDown);
    internalCallClientAsync(remote, 6, r2, ex, countDown);
    internalCallClientAsync(remote, 7, r3, ex, countDown);

    // (4) release the long-pool hold in (1).
    synchronized (holdClient) {
        holdClient[0] = false;
        holdClient.notifyAll();
    }

    // Now the server can send the pending requests from (3) 
    // to the client.

    // Wait until all requests were processed.
    countDown.await(10, TimeUnit.SECONDS);

    TestUtils.assertEquals(log, "r1", 6, r1.get());
    TestUtils.assertEquals(log, "r2", 7, r2.get());
    TestUtils.assertEquals(log, "r3", 8, r3.get());

    log.info(")testServerCallsClientNoActiveLongPoll");
}

From source file:com.spectralogic.ds3client.integration.GetJobManagement_Test.java

private void testGetJobWithUserSuppliedChannelStrategy(
        final TransferStrategyBuilderModifiable transferStrategyBuilderModifiable)
        throws IOException, InterruptedException {
    final String tempPathPrefix = null;
    final Path tempDirectory = Files.createTempDirectory(Paths.get("."), tempPathPrefix);
    final String fileName = "beowulf.txt";

    try {//from   www . j a v a  2s  . com
        final List<Ds3Object> objects = Lists.newArrayList(new Ds3Object(fileName));

        final GetBulkJobSpectraS3Request getBulkJobSpectraS3Request = new GetBulkJobSpectraS3Request(
                BUCKET_NAME, objects);

        final GetBulkJobSpectraS3Response getBulkJobSpectraS3Response = client
                .getBulkJobSpectraS3(getBulkJobSpectraS3Request);

        final MasterObjectList masterObjectList = getBulkJobSpectraS3Response.getMasterObjectList();

        final AtomicInteger numTimesChannelOpened = new AtomicInteger(0);
        final AtomicInteger numTimesChannelClosed = new AtomicInteger(0);

        TransferStrategyBuilder transferStrategyBuilder = new TransferStrategyBuilder().withDs3Client(client)
                .withMasterObjectList(masterObjectList).withChannelBuilder(new FileObjectGetter(tempDirectory))
                .withRangesForBlobs(PartialObjectHelpers.mapRangesToBlob(masterObjectList.getObjects(),
                        PartialObjectHelpers.getPartialObjectsRanges(objects)))
                .withChannelStrategy(new UserSuppliedPutChannelStrategy(new FileObjectGetter(tempDirectory),
                        new ChannelMonitorable() {
                            @Override
                            public void acquired() {
                                numTimesChannelOpened.getAndIncrement();
                            }

                            @Override
                            public void released() {
                                numTimesChannelClosed.getAndIncrement();
                            }
                        }));

        transferStrategyBuilder = transferStrategyBuilderModifiable.modify(transferStrategyBuilder);

        final TransferStrategy transferStrategy = transferStrategyBuilder.makeGetTransferStrategy();

        transferStrategy.transfer();

        final Collection<File> filesInTempDirectory = FileUtils.listFiles(tempDirectory.toFile(), null, false);

        for (final File file : filesInTempDirectory) {
            assertEquals(fileName, file.getName());
        }

        assertEquals(1, numTimesChannelOpened.get());
        assertEquals(1, numTimesChannelClosed.get());
    } finally {
        FileUtils.deleteDirectory(tempDirectory.toFile());
    }
}

From source file:org.dasein.cloud.azure.tests.network.AzureVlanSupportTest.java

@Test
public void removeSubnetShouldPostCorrectRequest() throws CloudException, InternalException {
    final AtomicInteger putCount = new AtomicInteger(0);
    new MockUp<CloseableHttpClient>() {
        @Mock//from  w  w w . j a v a 2 s . c om
        public CloseableHttpResponse execute(Invocation inv, HttpUriRequest request) throws IOException {
            if (request.getMethod().equals("GET")
                    && VIRTUAL_NETWORK_SITES_URL.equals(request.getURI().toString())) {
                DaseinObjectToXmlEntity<VirtualNetworkSitesModel> daseinEntity = new DaseinObjectToXmlEntity<VirtualNetworkSitesModel>(
                        createVirtualNetworkSitesModelWithSubnet(ID, NAME, REGION, CIDR, "Created", SUBNET_NAME,
                                SUBNET_CIDR));
                return getHttpResponseMock(getStatusLineMock(HttpServletResponse.SC_OK), daseinEntity,
                        new Header[] { new BasicHeader("x-ms-request-id", UUID.randomUUID().toString()) });
            } else if ("GET".equals(request.getMethod())
                    && NETWORK_CONFIG_URL.equals(request.getURI().toString())) {
                DaseinObjectToXmlEntity<NetworkConfigurationModel> daseinEntity = new DaseinObjectToXmlEntity<NetworkConfigurationModel>(
                        createNetworkConfigurationModelWithSubnet(NAME, REGION, CIDR, SUBNET_NAME,
                                SUBNET_CIDR));
                return getHttpResponseMock(getStatusLineMock(HttpServletResponse.SC_OK), daseinEntity,
                        new Header[] { new BasicHeader("x-ms-request-id", UUID.randomUUID().toString()) });
            } else if ("PUT".equals(request.getMethod())) {
                putCount.incrementAndGet();
                NetworkConfigurationModel networkConfigurationModel = createNetworkConfigurationModelWithSubnet(
                        NAME, REGION, CIDR, null, null);
                assertPut(request, NETWORK_CONFIG_URL,
                        new Header[] { new BasicHeader("x-ms-version", "2012-03-01") },
                        networkConfigurationModel);
                return getHttpResponseMock(getStatusLineMock(HttpServletResponse.SC_OK), null,
                        new Header[] { new BasicHeader("x-ms-request-id", UUID.randomUUID().toString()) });
            } else {
                throw new IOException("Request is not mocked");
            }

        }
    };
    vlanSupport.removeSubnet(SUBNET_ID);
    assertEquals("removeVlan PUT network config should perform only 1 times", 1, putCount.get());
}

From source file:com.heliosapm.tsdblite.metric.MetricCache.java

private MetricCache() {

    final String jmxDomain = ConfigurationHelper.getSystemThenEnvProperty(Constants.CONF_METRICS_MSERVER,
            Constants.DEFAULT_METRICS_MSERVER);
    if (!JMXHelper.getHeliosMBeanServer().getDefaultDomain().equals(jmxDomain)) {
        metricMBeanServer = JMXHelper.createMBeanServer(jmxDomain, true);
        final int port = ConfigurationHelper.getIntSystemThenEnvProperty(Constants.CONF_METRICS_JMXMP_PORT,
                Constants.DEFAULT_METRICS_JMXMP_PORT);
        if (port > -1) {
            final String iface = ConfigurationHelper.getSystemThenEnvProperty(
                    Constants.CONF_METRICS_JMXMP_IFACE, Constants.DEFAULT_METRICS_JMXMP_IFACE);
            final JMXServiceURL surl = JMXHelper.fireUpJMXMPServer(iface, port, metricMBeanServer);
            log.info("Metrics MBeanServer [{}] available at [{}]", jmxDomain, surl);
        }//from w  w w. j  a va 2 s  . c  o m
    } else {
        metricMBeanServer = JMXHelper.getHeliosMBeanServer();
    }
    expiry = ConfigurationHelper.getLongSystemThenEnvProperty(Constants.CONF_METRIC_EXPIRY,
            Constants.DEFAULT_METRIC_EXPIRY);
    expiryPeriod = ConfigurationHelper.getLongSystemThenEnvProperty(Constants.CONF_METRIC_EXPIRY_PERIOD,
            Constants.DEFAULT_METRIC_EXPIRY_PERIOD);
    expiryThread = new Thread(new Runnable() {
        @Override
        public void run() {
            while (true) {
                final AtomicInteger expiredMetricCount = new AtomicInteger(0);
                SystemClock.sleep(expiryPeriod);
                final long startTime = System.currentTimeMillis();
                final ObjectName[] metricObjectNames = JMXHelper.query(JMXHelper.ALL_MBEANS_FILTER,
                        metricBeanQuery);
                final Collection<Future<?>> taskFutures = new ArrayList<Future<?>>(metricObjectNames.length);
                for (final ObjectName on : metricObjectNames) {
                    taskFutures.add(expiryService.submit(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                final long now = System.currentTimeMillis();
                                final Map<String, Object> attrMap = JMXHelper.getAttributes(on,
                                        EXPIRY_ATTRIBUTES);
                                if (!attrMap.containsKey("LastActivity")) {
                                    log.warn("No LA for [{}], AttrMap: {}", on, attrMap);
                                    return;
                                }
                                final long lastActivity = (Long) attrMap.get("LastActivity");
                                final long age = now - lastActivity;
                                if (age > expiry) {
                                    expiredMetricCount.incrementAndGet();
                                    metricMBeanServer.unregisterMBean(on);
                                    expiredMetrics.increment();
                                    final long hc = (Long) attrMap.get("MetricHashCode");
                                    metricCache.remove(hc);
                                }
                            } catch (Exception x) {
                                log.error("Expiry Task Failure", x);
                            }
                        }
                    }));
                }
                final long dispatchElapsed = System.currentTimeMillis() - startTime;
                lastExpiryDispatchTime.set(dispatchElapsed);
                int fails = 0;
                for (Future<?> f : taskFutures) {
                    try {
                        f.get();
                    } catch (Exception x) {
                        fails++;
                    }
                }
                final long expiryElapsed = System.currentTimeMillis() - startTime;
                final int exp = expiredMetricCount.get();
                if (exp != 0) {
                    log.info("Expiry Dispatch for [{}] Metrics Completed in [{}] ms. Expired [{}] metrics.",
                            metricObjectNames.length, dispatchElapsed, exp);
                }

                lastExpiryTime.set(expiryElapsed);
                if (log.isDebugEnabled())
                    log.debug("Expiry Completed in [{}] ms. Tasks: {}, Fails: {}", expiryElapsed,
                            taskFutures.size(), fails);
            }
        }
    }, "MetricExpiryThread");
    expiryThread.setDaemon(true);
    expiryThread.setPriority(Thread.MAX_PRIORITY);
    expiryThread.start();
    JMXHelper.registerMBean(this, OBJECT_NAME);
}

From source file:org.glassfish.jersey.examples.sseitemstore.ItemStoreResourceTest.java

/**
 * Test the item addition, addition event broadcasting and item retrieval from {@link ItemStoreResource}.
 *
 * @throws Exception in case of a test failure.
 *//*w w w . j  a  v  a2s . co m*/
@Test
public void testItemsStore() throws Exception {
    final List<String> items = Collections.unmodifiableList(Arrays.asList("foo", "bar", "baz"));
    final WebTarget itemsTarget = target("items");
    final CountDownLatch latch = new CountDownLatch(items.size() * MAX_LISTENERS * 2); // countdown on all events
    final List<Queue<Integer>> indexQueues = new ArrayList<Queue<Integer>>(MAX_LISTENERS);
    final EventSource[] sources = new EventSource[MAX_LISTENERS];
    final AtomicInteger sizeEventsCount = new AtomicInteger(0);

    for (int i = 0; i < MAX_LISTENERS; i++) {
        final int id = i;
        final EventSource es = EventSource.target(itemsTarget.path("events")).named("SOURCE " + id).build();
        sources[id] = es;

        final Queue<Integer> indexes = new ConcurrentLinkedQueue<Integer>();
        indexQueues.add(indexes);

        es.register(new EventListener() {
            @SuppressWarnings("MagicNumber")
            @Override
            public void onEvent(InboundEvent inboundEvent) {
                try {
                    if (inboundEvent.getName() == null) {
                        final String data = inboundEvent.readData();
                        LOGGER.info("[-i-] SOURCE " + id + ": Received event id=" + inboundEvent.getId()
                                + " data=" + data);
                        indexes.add(items.indexOf(data));
                    } else if ("size".equals(inboundEvent.getName())) {
                        sizeEventsCount.incrementAndGet();
                    }
                } catch (Exception ex) {
                    LOGGER.log(Level.SEVERE, "[-x-] SOURCE " + id + ": Error getting event data.", ex);
                    indexes.add(-999);
                } finally {
                    latch.countDown();
                }
            }
        });
    }

    try {
        open(sources);

        for (String item : items) {
            postItem(itemsTarget, item);
        }

        assertTrue("Waiting to receive all events has timed out.",
                latch.await(
                        (1000 + MAX_LISTENERS * EventSource.RECONNECT_DEFAULT) * getAsyncTimeoutMultiplier(),
                        TimeUnit.MILLISECONDS));

        // need to force disconnect on server in order for EventSource.close(...) to succeed with HttpUrlConnection
        sendCommand(itemsTarget, "disconnect");
    } finally {
        close(sources);
    }

    String postedItems = itemsTarget.request().get(String.class);
    for (String item : items) {
        assertTrue("Item '" + item + "' not stored on server.", postedItems.contains(item));
    }

    int queueId = 0;
    for (Queue<Integer> indexes : indexQueues) {
        for (int i = 0; i < items.size(); i++) {
            assertTrue("Event for '" + items.get(i) + "' not received in queue " + queueId,
                    indexes.contains(i));
        }
        assertEquals("Not received the expected number of events in queue " + queueId, items.size(),
                indexes.size());
        queueId++;
    }

    assertEquals("Number of received 'size' events does not match.", items.size() * MAX_LISTENERS,
            sizeEventsCount.get());
}

From source file:com.alibaba.wasp.master.FMaster.java

/**
 * @see com.alibaba.wasp.protobuf.generated.MasterAdminProtos.MasterAdminService.BlockingInterface#fetchEntityGroupSize(com.google.protobuf.RpcController,
 *      com.alibaba.wasp.protobuf.generated.MasterAdminProtos.FetchEntityGroupSizeRequest)
 *//*  w w w .j a  va2s .c  o m*/
@Override
public FetchEntityGroupSizeResponse fetchEntityGroupSize(RpcController controller,
        FetchEntityGroupSizeRequest request) throws ServiceException {
    final byte[] tableNameBytes = request.getTableName().toByteArray();
    final AtomicInteger actualEgCount = new AtomicInteger(0);
    MetaScannerVisitor visitor = new MetaScannerVisitorBase() {
        @Override
        public boolean processRow(org.apache.hadoop.hbase.client.Result rowResult) throws IOException {
            EntityGroupInfo info = EntityGroupInfo.getEntityGroupInfo(rowResult);
            if (info == null) {
                LOG.warn("No serialized EntityGroupInfo in " + rowResult);
                return true;
            }
            if (!(Bytes.equals(info.getTableName(), tableNameBytes))) {
                return false;
            }
            ServerName serverName = EntityGroupInfo.getServerName(rowResult);
            // Make sure that entityGroups are assigned to server
            if (!(info.isOffline() || info.isSplit()) && serverName != null
                    && serverName.getHostAndPort() != null) {
                actualEgCount.incrementAndGet();
            }
            return true;
        }
    };
    try {
        FMetaScanner.metaScan(conf, visitor, tableNameBytes);
    } catch (IOException e) {
        LOG.error("Failed fetchEntityGroupSize.", e);
        throw new ServiceException(e);
    }
    FetchEntityGroupSizeResponse.Builder res = FetchEntityGroupSizeResponse.newBuilder();
    res.setEgSize(actualEgCount.get());
    return res.build();
}

From source file:org.dasein.cloud.azurepack.tests.compute.AzurePackVirtualMachineSupportTest.java

@Test
public void lauchTemplateVMShouldSendCorrectRequest() throws CloudException, InternalException {
    final AtomicInteger postCount = new AtomicInteger(0);
    new StartOrStopVirtualMachinesRequestExecutorMockUp("Start") {
        @Mock/*  ww  w  . j  a  va 2 s.  c  o m*/
        public void $init(CloudProvider provider, HttpClientBuilder clientBuilder, HttpUriRequest request,
                ResponseHandler handler) {
            String requestUri = request.getURI().toString();
            if (request.getMethod().equals("POST")
                    && requestUri.equals(String.format(LIST_VM_RESOURCES, ENDPOINT, ACCOUNT_NO))) {
                requestResourceType = 21;
                WAPVirtualMachineModel wapVirtualMachineModel = new WAPVirtualMachineModel();
                wapVirtualMachineModel.setName(VM_1_NAME);
                wapVirtualMachineModel.setCloudId(REGION);
                wapVirtualMachineModel.setStampId(DATACENTER_ID);
                wapVirtualMachineModel.setVmTemplateId(TPL_1_ID);
                wapVirtualMachineModel.setProductKey(VM_1_WINDOWS_SERIAL_NUMBER);
                wapVirtualMachineModel.setLocalAdminUserName(VM_1_BOOTSTRAP_USER);
                wapVirtualMachineModel.setLocalAdminPassword(VM_1_BOOTSTRAP_PASSWORD);
                List<WAPNewAdapterModel> adapters = new ArrayList<>();
                WAPNewAdapterModel newAdapterModel = new WAPNewAdapterModel();
                newAdapterModel.setVmNetworkName(VM_1_NETWORK_NAME);
                adapters.add(newAdapterModel);
                wapVirtualMachineModel.setNewVirtualNetworkAdapterInput(adapters);

                assertPost(request, String.format(LIST_VM_RESOURCES, ENDPOINT, ACCOUNT_NO), new Header[0],
                        wapVirtualMachineModel);
            } else {
                super.$init(provider, clientBuilder, request, handler);
            }
            responseHandler = handler;
        }

        @Mock
        public Object execute() {
            if (requestResourceType == 21) {
                postCount.incrementAndGet();
                return mapFromModel(this.responseHandler, createWAPVirtualMachineModel());
            } else {
                return super.execute();
            }
        }
    };

    VMLaunchOptions vmLaunchOptions = VMLaunchOptions.getInstance(HWP_1_ID, TPL_1_ID, VM_1_NAME,
            VM_1_DESCRIPTION);
    vmLaunchOptions.inVlan(null, DATACENTER_ID, VM_1_NETWORK_ID);
    vmLaunchOptions.withWinProductSerialNum(VM_1_WINDOWS_SERIAL_NUMBER);
    vmLaunchOptions.withBootstrapUser("dummy-user-name-to-be-replaced", VM_1_BOOTSTRAP_PASSWORD);
    VirtualMachine virtualMachine = azurePackVirtualMachineSupport.launch(vmLaunchOptions);
    assertEquals("terminate doesn't send DELETE request", 1, postCount.get());
    assertVirtualMachine(virtualMachine);
}

From source file:com.yahoo.pulsar.broker.service.PersistentQueueE2ETest.java

@Test(enabled = false)
public void testRoundRobinBatchDistribution() throws Exception {
    final String topicName = "persistent://prop/use/ns-abc/shared-topic5";
    final String subName = "sub5";
    final int numMsgs = 137; /* some random number different than default batch size of 100 */

    final AtomicInteger counter1 = new AtomicInteger(0);
    final AtomicInteger counter2 = new AtomicInteger(0);
    final AtomicInteger counter3 = new AtomicInteger(0);

    final CountDownLatch latch = new CountDownLatch(numMsgs * 3);

    ConsumerConfiguration conf1 = new ConsumerConfiguration();
    conf1.setSubscriptionType(SubscriptionType.Shared);
    conf1.setReceiverQueueSize(10);/*from ww  w  .  j av  a 2  s.  co m*/
    conf1.setMessageListener((consumer, msg) -> {
        try {
            counter1.incrementAndGet();
            consumer.acknowledge(msg);
            latch.countDown();
        } catch (Exception e) {
            fail("Should not fail");
        }
    });

    ConsumerConfiguration conf2 = new ConsumerConfiguration();
    conf2.setSubscriptionType(SubscriptionType.Shared);
    conf2.setReceiverQueueSize(10);
    conf2.setMessageListener((consumer, msg) -> {
        try {
            counter2.incrementAndGet();
            consumer.acknowledge(msg);
            latch.countDown();
        } catch (Exception e) {
            fail("Should not fail");
        }
    });

    ConsumerConfiguration conf3 = new ConsumerConfiguration();
    conf3.setSubscriptionType(SubscriptionType.Shared);
    conf3.setReceiverQueueSize(10);
    conf3.setMessageListener((consumer, msg) -> {
        try {
            counter3.incrementAndGet();
            consumer.acknowledge(msg);
            latch.countDown();
        } catch (Exception e) {
            fail("Should not fail");
        }
    });

    // subscribe and close, so that distribution can be checked after
    // all messages are published
    Consumer consumer1 = pulsarClient.subscribe(topicName, subName, conf1);
    Consumer consumer2 = pulsarClient.subscribe(topicName, subName, conf2);
    Consumer consumer3 = pulsarClient.subscribe(topicName, subName, conf3);

    List<CompletableFuture<MessageId>> futures = Lists.newArrayListWithCapacity(numMsgs);
    Producer producer = pulsarClient.createProducer(topicName);
    for (int i = 0; i < numMsgs * 3; i++) {
        String message = "msg-" + i;
        futures.add(producer.sendAsync(message.getBytes()));
    }
    FutureUtil.waitForAll(futures).get();
    producer.close();

    latch.await(1, TimeUnit.SECONDS);

    /*
     * total messages = 137 * 3 = 411 Each consumer has 10 permits. There will be 411 / 3*10 = 13 full distributions
     * i.e. each consumer will get 130 messages. In the 14th round, the balance is 411 - 130*3 = 21. Two consumers
     * will get another batch of 10 messages (Total: 140) and the 3rd one will get the last one (Total: 131)
     */
    assertTrue(CollectionUtils.subtract(Lists.newArrayList(140, 140, 131),
            Lists.newArrayList(counter1.get(), counter2.get(), counter3.get())).isEmpty());

    consumer1.close();
    consumer2.close();
    consumer3.close();
    admin.persistentTopics().delete(topicName);
}