com.opentable.etcd.SmokeTest.java Source code

Java tutorial

Introduction

Here is the source code for com.opentable.etcd.SmokeTest.java

Source

/*
 * 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.opentable.etcd;

import java.net.URI;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import javax.ws.rs.client.Client;

import com.fasterxml.jackson.databind.ObjectMapper;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;

import com.opentable.config.Config;
import com.opentable.jaxrs.JaxRsClientFactory;
import com.opentable.jaxrs.JsonClientFeature;
import com.opentable.jaxrs.StandardFeatureGroup;

public class SmokeTest {

    @Rule
    public EtcdServerRule server = EtcdServerRule.singleNode();

    ExecutorService backgroundThread;
    String prefix;

    Client jaxrsClient;
    EtcdClient client;

    @Before
    public void initialize() {
        backgroundThread = Executors.newSingleThreadExecutor();
        this.prefix = "/unittest-" + UUID.randomUUID().toString();

        jaxrsClient = new JaxRsClientFactory(Config.getEmptyConfig())
                .addFeatureToAllClients(JsonClientFeature.forMapper(new ObjectMapper()))
                .newClient("etcd", StandardFeatureGroup.PUBLIC);

        final EtcdClientFactory factory = new EtcdClientFactory(jaxrsClient, new ObjectMapper());
        client = factory.createClient(URI.create(server.getConnectString()));
    }

    @After
    public void tearDown() throws Exception {
        backgroundThread.shutdown();
        jaxrsClient.close();
    }

    @Test
    public void setAndGet() throws Exception {
        String key = prefix + "/message";

        EtcdResult result;

        result = this.client.set(key, "hello");
        Assert.assertEquals("set", result.action);
        Assert.assertEquals("hello", result.node.value);
        Assert.assertNull(result.prevNode);

        result = this.client.get(key);
        Assert.assertEquals("get", result.action);
        Assert.assertEquals("hello", result.node.value);
        Assert.assertNull(result.prevNode);

        result = this.client.set(key, "world");
        Assert.assertEquals("set", result.action);
        Assert.assertEquals("world", result.node.value);
        Assert.assertNotNull(result.prevNode);
        Assert.assertEquals("hello", result.prevNode.value);

        result = this.client.get(key);
        Assert.assertEquals("get", result.action);
        Assert.assertEquals("world", result.node.value);
        Assert.assertNull(result.prevNode);
    }

    @Test
    public void getNonExistentKey() throws Exception {
        String key = prefix + "/doesnotexist";

        EtcdResult result;

        result = this.client.get(key);
        Assert.assertNull(result);
    }

    @Test
    public void testDelete() throws Exception {
        String key = prefix + "/testDelete";

        EtcdResult result;

        result = this.client.set(key, "hello");

        result = this.client.get(key);
        Assert.assertEquals("hello", result.node.value);

        result = this.client.delete(key);
        Assert.assertEquals("delete", result.action);
        Assert.assertEquals(null, result.node.value);
        Assert.assertNotNull(result.prevNode);
        Assert.assertEquals("hello", result.prevNode.value);

        result = this.client.get(key);
        Assert.assertNull(result);
    }

    @Test
    public void deleteNonExistentKey() throws Exception {
        String key = prefix + "/doesnotexist";
        Assert.assertNull(this.client.delete(key));
    }

    @Test
    public void testTtl() throws Exception {
        String key = prefix + "/ttl";

        EtcdResult result;

        result = this.client.set(key, "hello", 2);
        Assert.assertNotNull(result.node.expiration);
        Assert.assertTrue(result.node.ttl == 2 || result.node.ttl == 1);

        result = this.client.get(key);
        Assert.assertEquals("hello", result.node.value);

        // TTL was redefined to mean TTL + 0.5s (Issue #306)
        Thread.sleep(3000);

        result = this.client.get(key);
        Assert.assertNull(result);
    }

    @Test
    @Ignore("not implemented!")
    public void testCAS() throws Exception {
        String key = prefix + "/cas";

        EtcdResult result;

        result = this.client.set(key, "hello");
        result = this.client.get(key);
        Assert.assertEquals("hello", result.node.value);

        result = this.client.cas(key, "world", "world");
        Assert.assertEquals(true, result.isError());
        result = this.client.get(key);
        Assert.assertEquals("hello", result.node.value);

        result = this.client.cas(key, "hello", "world");
        Assert.assertEquals(false, result.isError());
        result = this.client.get(key);
        Assert.assertEquals("world", result.node.value);
    }

    @Test
    public void testWatchPrefix() throws Exception {
        String key = prefix + "/watch";

        EtcdResult result = this.client.set(key + "/f2", "f2");
        Assert.assertTrue(!result.isError());
        Assert.assertNotNull(result.node);
        Assert.assertEquals("f2", result.node.value);

        final long watchIndex = result.node.modifiedIndex + 1;
        Future<EtcdResult> watchFuture = backgroundThread.submit(() -> {
            return this.client.watch(key, watchIndex, true);
        });
        try {
            EtcdResult watchResult = watchFuture.get(100, TimeUnit.MILLISECONDS);
            Assert.fail("Subtree watch fired unexpectedly: " + watchResult);
        } catch (TimeoutException e) {
            // Expected
        }

        Assert.assertFalse(watchFuture.isDone());

        result = this.client.set(key + "/f1", "f1");
        Assert.assertTrue(!result.isError());
        Assert.assertNotNull(result.node);
        Assert.assertEquals("f1", result.node.value);

        EtcdResult watchResult = watchFuture.get(100, TimeUnit.MILLISECONDS);

        Assert.assertNotNull(watchResult);
        Assert.assertTrue(!watchResult.isError());
        Assert.assertNotNull(watchResult.node);

        {
            Assert.assertEquals(key + "/f1", watchResult.node.key);
            Assert.assertEquals("f1", watchResult.node.value);
            Assert.assertEquals("set", watchResult.action);
            Assert.assertNull(result.prevNode);
            Assert.assertEquals(result.node.modifiedIndex, watchResult.node.modifiedIndex);
        }
    }

    @Test
    public void testList() throws Exception {
        String key = prefix + "/dir";

        EtcdResult result;

        result = this.client.set(key + "/f1", "f1");
        Assert.assertEquals("f1", result.node.value);
        result = this.client.set(key + "/f2", "f2");
        Assert.assertEquals("f2", result.node.value);
        result = this.client.set(key + "/f3", "f3");
        Assert.assertEquals("f3", result.node.value);
        result = this.client.set(key + "/subdir1/f", "f");
        Assert.assertEquals("f", result.node.value);

        EtcdResult listing = this.client.get(key);
        Assert.assertEquals(4, listing.node.nodes.size());
        Assert.assertEquals("get", listing.action);

        {
            EtcdNode child = listing.node.nodes.get(0);
            Assert.assertEquals(key + "/f1", child.key);
            Assert.assertEquals("f1", child.value);
            Assert.assertEquals(false, child.dir);
        }
        {
            EtcdNode child = listing.node.nodes.get(1);
            Assert.assertEquals(key + "/f2", child.key);
            Assert.assertEquals("f2", child.value);
            Assert.assertEquals(false, child.dir);
        }
        {
            EtcdNode child = listing.node.nodes.get(2);
            Assert.assertEquals(key + "/f3", child.key);
            Assert.assertEquals("f3", child.value);
            Assert.assertEquals(false, child.dir);
        }
        {
            EtcdNode child = listing.node.nodes.get(3);
            Assert.assertEquals(key + "/subdir1", child.key);
            Assert.assertEquals(null, child.value);
            Assert.assertEquals(true, child.dir);
        }
    }

    @Test
    public void testGetVersion() throws Exception {
        String version = this.client.getVersion();
        Assert.assertTrue(version.startsWith("etcd"));
    }

    @Test
    public void testResponseMetadata() throws Exception {
        String key = prefix + "/message";

        long etcdIndex, raftIndex, raftTerm;

        EtcdResult result;

        result = this.client.set(key, "hello");

        etcdIndex = result.etcdIndex;
        raftIndex = result.raftIndex;
        raftTerm = result.raftTerm;

        Assert.assertTrue(etcdIndex > 0);
        Assert.assertTrue(raftIndex > 0);
        Assert.assertTrue(raftTerm >= 0);

        result = this.client.get(key);

        Assert.assertEquals(etcdIndex, result.etcdIndex);
        Assert.assertTrue(raftIndex <= result.raftIndex);
        Assert.assertTrue(raftTerm <= result.raftTerm);
        raftIndex = result.raftIndex;
        raftTerm = result.raftTerm;

        result = this.client.set(key, "world");

        Assert.assertTrue(etcdIndex < result.etcdIndex);
        Assert.assertTrue(raftIndex < result.raftIndex);
        Assert.assertTrue(raftTerm <= result.raftTerm);
        etcdIndex = result.etcdIndex;
        raftIndex = result.raftIndex;
        raftTerm = result.raftTerm;

        result = this.client.get(key);
        Assert.assertEquals(etcdIndex, result.etcdIndex);
        Assert.assertTrue(raftIndex <= result.raftIndex);
        Assert.assertTrue(raftTerm <= result.raftTerm);
    }
}