com.github.lukaszbudnik.dqueue.OrderedQueueClientIntegrationTest.java Source code

Java tutorial

Introduction

Here is the source code for com.github.lukaszbudnik.dqueue.OrderedQueueClientIntegrationTest.java

Source

/**
 * Copyright (C) 2015-2017 ?ukasz Budnik <lukasz.budnik@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 */
package com.github.lukaszbudnik.dqueue;

import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;
import com.codahale.metrics.health.HealthCheck;
import com.codahale.metrics.health.HealthCheckRegistry;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.utils.UUIDs;
import com.github.lukaszbudnik.cloudtag.CloudTagEnsembleProvider;
import com.github.lukaszbudnik.gpe.PropertiesElResolverModule;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.inject.Guice;
import com.google.inject.Injector;
import org.apache.commons.io.IOUtils;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Map;
import java.util.Optional;
import java.util.SortedMap;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;

public class OrderedQueueClientIntegrationTest {

    private static Injector injector;
    private static CuratorFramework zookeeperClient;

    private static String cassandraKeyspace = "test" + System.currentTimeMillis();
    private OrderedQueueClientImpl queueClient;
    private Session session;
    private MetricRegistry metricRegistry;
    private HealthCheckRegistry healthCheckRegistry;

    @BeforeClass
    public static void beforeClass() throws Exception {
        injector = Guice.createInjector(
                new PropertiesElResolverModule(Arrays.asList("/dqueue.properties", "/cloudtag.properties")));

        CloudTagEnsembleProvider cloudTagEnsembleProvider = injector.getInstance(CloudTagEnsembleProvider.class);
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        zookeeperClient = CuratorFrameworkFactory.builder().ensembleProvider(cloudTagEnsembleProvider)
                .retryPolicy(retryPolicy).build();
        zookeeperClient.start();
        // warm up zookeeper zookeeperClient - on my macbook pro takes even up to 10 seconds
        zookeeperClient.getChildren().forPath("/");
    }

    @Before
    public void before() throws Exception {
        QueueClientBuilder queueClientBuilder = injector.getInstance(QueueClientBuilder.class);

        metricRegistry = new MetricRegistry();
        healthCheckRegistry = new HealthCheckRegistry();

        queueClient = (OrderedQueueClientImpl) queueClientBuilder.withCassandraKeyspace(cassandraKeyspace)
                .withZookeeperClient(zookeeperClient).withMetricRegistry(metricRegistry)
                .withHealthMetricRegistry(healthCheckRegistry).buildOrdered();

        session = queueClient.getSession();
        session.execute("create keyspace " + cassandraKeyspace
                + " WITH replication = {'class':'SimpleStrategy', 'replication_factor':3}");
    }

    @After
    public void after() throws Exception {
        session.execute("drop keyspace " + cassandraKeyspace);

        SortedMap<String, Timer> timers = metricRegistry.getTimers();

        timers.keySet().stream().forEach(k -> {
            Timer t = timers.get(k);
            System.out.println(k);
            System.out.println("\ttimes called ==> " + t.getCount());
            System.out.println("\tmedian ==> " + t.getSnapshot().getMedian() / 1000 / 1000);
            System.out.println("\t75th percentile ==> " + t.getSnapshot().get75thPercentile() / 1000 / 1000);
            System.out.println("\t99th percentile ==> " + t.getSnapshot().get99thPercentile() / 1000 / 1000);
        });

        SortedMap<String, HealthCheck.Result> healthChecks = healthCheckRegistry.runHealthChecks();
        healthChecks.keySet().stream().forEach(k -> {
            HealthCheck.Result healthCheck = healthChecks.get(k);
            System.out.println(k + " healthy? ==> " + healthCheck.isHealthy());
        });

        queueClient.close();
    }

    @AfterClass
    public static void afterClass() {
        IOUtils.closeQuietly(zookeeperClient);
    }

    @Test
    public void shouldPublishSequential() throws ExecutionException, InterruptedException {
        Future<ImmutableList<UUID>> futures = queueClient.publishOrdered(
                new Item(UUIDs.timeBased(), ByteBuffer.wrap("A".getBytes())),
                new Item(UUIDs.timeBased(), ByteBuffer.wrap("B".getBytes())),
                new Item(UUIDs.timeBased(), ByteBuffer.wrap("C".getBytes())));
        futures.get();

        Future<Optional<OrderedItem>> optionalFutureA = queueClient.consumeOrdered();
        Optional<OrderedItem> itemOptionalA = optionalFutureA.get();
        OrderedItem itemA = itemOptionalA.get();

        assertNotNull(itemA);
        assertEquals("A", new String(itemA.getContents().array()));

        Future<Optional<OrderedItem>> optionalFutureB = queueClient.consumeOrdered();
        Optional<OrderedItem> itemOptionalB = optionalFutureB.get();

        assertFalse(itemOptionalB.isPresent());

        queueClient.deleteOrdered(itemA);

        optionalFutureB = queueClient.consumeOrdered();
        itemOptionalB = optionalFutureB.get();
        OrderedItem itemB = itemOptionalB.get();

        assertNotNull(itemB);
        assertEquals("B", new String(itemB.getContents().array()));

        Future<Optional<OrderedItem>> optionalFutureC = queueClient.consumeOrdered();
        Optional<OrderedItem> itemOptionalC = optionalFutureC.get();

        assertFalse(itemOptionalC.isPresent());

        queueClient.deleteOrdered(itemB);

        optionalFutureC = queueClient.consumeOrdered();
        itemOptionalC = optionalFutureC.get();
        OrderedItem itemC = itemOptionalC.get();

        assertNotNull(itemC);
        assertEquals("C", new String(itemC.getContents().array()));

        queueClient.deleteOrdered(itemC);

        Future<Optional<OrderedItem>> empty = queueClient.consumeOrdered();
        assertFalse(empty.get().isPresent());
    }

    @Test
    public void shouldPublishSequentialWithFilters() throws ExecutionException, InterruptedException {

        Map<String, String> filters = ImmutableMap.of("filter1", "1", "filter2", "two");

        Future<ImmutableList<UUID>> futures = queueClient.publishOrdered(
                new Item(UUIDs.timeBased(), ByteBuffer.wrap("A".getBytes()), filters),
                new Item(UUIDs.timeBased(), ByteBuffer.wrap("B".getBytes()), filters),
                new Item(UUIDs.timeBased(), ByteBuffer.wrap("C".getBytes()), filters));
        futures.get();

        Future<Optional<OrderedItem>> optionalFutureA = queueClient.consumeOrdered(filters);
        Optional<OrderedItem> itemOptionalA = optionalFutureA.get();
        OrderedItem itemA = itemOptionalA.get();

        assertNotNull(itemA);
        assertEquals("A", new String(itemA.getContents().array()));

        Future<Optional<OrderedItem>> optionalFutureB = queueClient.consumeOrdered(filters);
        Optional<OrderedItem> itemOptionalB = optionalFutureB.get();

        assertFalse(itemOptionalB.isPresent());

        queueClient.deleteOrdered(itemA);

        optionalFutureB = queueClient.consumeOrdered(filters);
        itemOptionalB = optionalFutureB.get();
        OrderedItem itemB = itemOptionalB.get();

        assertNotNull(itemB);
        assertEquals("B", new String(itemB.getContents().array()));

        Future<Optional<OrderedItem>> optionalFutureC = queueClient.consumeOrdered(filters);
        Optional<OrderedItem> itemOptionalC = optionalFutureC.get();

        assertFalse(itemOptionalC.isPresent());

        queueClient.deleteOrdered(itemB);

        optionalFutureC = queueClient.consumeOrdered(filters);
        itemOptionalC = optionalFutureC.get();
        OrderedItem itemC = itemOptionalC.get();

        assertNotNull(itemC);
        assertEquals("C", new String(itemC.getContents().array()));

        queueClient.deleteOrdered(itemC);

        Future<Optional<OrderedItem>> empty = queueClient.consumeOrdered(filters);
        assertFalse(empty.get().isPresent());
    }

}