Search in sources :

Example 1 with InMemoryZKServer

use of com.continuuity.weave.internal.zookeeper.InMemoryZKServer in project weave by continuuity.

the class ZKClientTest method testCreateParent.

@Test
public void testCreateParent() throws ExecutionException, InterruptedException {
    InMemoryZKServer zkServer = InMemoryZKServer.builder().setTickTime(1000).build();
    zkServer.startAndWait();
    try {
        ZKClientService client = ZKClientService.Builder.of(zkServer.getConnectionStr()).build();
        client.startAndWait();
        try {
            String path = client.create("/test1/test2/test3/test4/test5", "testing".getBytes(), CreateMode.PERSISTENT_SEQUENTIAL).get();
            Assert.assertTrue(path.startsWith("/test1/test2/test3/test4/test5"));
            String dataPath = "";
            for (int i = 1; i <= 4; i++) {
                dataPath = dataPath + "/test" + i;
                Assert.assertNull(client.getData(dataPath).get().getData());
            }
            Assert.assertTrue(Arrays.equals("testing".getBytes(), client.getData(path).get().getData()));
        } finally {
            client.stopAndWait();
        }
    } finally {
        zkServer.stopAndWait();
    }
}
Also used : InMemoryZKServer(com.continuuity.weave.internal.zookeeper.InMemoryZKServer) Test(org.junit.Test)

Example 2 with InMemoryZKServer

use of com.continuuity.weave.internal.zookeeper.InMemoryZKServer in project weave by continuuity.

the class ZKClientTest method testGetChildren.

@Test
public void testGetChildren() throws ExecutionException, InterruptedException {
    InMemoryZKServer zkServer = InMemoryZKServer.builder().setTickTime(1000).build();
    zkServer.startAndWait();
    try {
        ZKClientService client = ZKClientService.Builder.of(zkServer.getConnectionStr()).build();
        client.startAndWait();
        try {
            client.create("/test", null, CreateMode.PERSISTENT).get();
            Assert.assertTrue(client.getChildren("/test").get().getChildren().isEmpty());
            Futures.allAsList(ImmutableList.of(client.create("/test/c1", null, CreateMode.EPHEMERAL), client.create("/test/c2", null, CreateMode.EPHEMERAL))).get();
            NodeChildren nodeChildren = client.getChildren("/test").get();
            Assert.assertEquals(2, nodeChildren.getChildren().size());
            Assert.assertEquals(ImmutableSet.of("c1", "c2"), ImmutableSet.copyOf(nodeChildren.getChildren()));
        } finally {
            client.stopAndWait();
        }
    } finally {
        zkServer.stopAndWait();
    }
}
Also used : InMemoryZKServer(com.continuuity.weave.internal.zookeeper.InMemoryZKServer) Test(org.junit.Test)

Example 3 with InMemoryZKServer

use of com.continuuity.weave.internal.zookeeper.InMemoryZKServer in project weave by continuuity.

the class ZKClientTest method testSetData.

@Test
public void testSetData() throws ExecutionException, InterruptedException {
    InMemoryZKServer zkServer = InMemoryZKServer.builder().setTickTime(1000).build();
    zkServer.startAndWait();
    try {
        ZKClientService client = ZKClientService.Builder.of(zkServer.getConnectionStr()).build();
        client.startAndWait();
        client.create("/test", null, CreateMode.PERSISTENT).get();
        Assert.assertNull(client.getData("/test").get().getData());
        client.setData("/test", "testing".getBytes()).get();
        Assert.assertTrue(Arrays.equals("testing".getBytes(), client.getData("/test").get().getData()));
    } finally {
        zkServer.stopAndWait();
    }
}
Also used : InMemoryZKServer(com.continuuity.weave.internal.zookeeper.InMemoryZKServer) Test(org.junit.Test)

Example 4 with InMemoryZKServer

use of com.continuuity.weave.internal.zookeeper.InMemoryZKServer in project weave by continuuity.

the class ZKClientTest method testExpireRewatch.

@Test
public void testExpireRewatch() throws InterruptedException, IOException, ExecutionException {
    InMemoryZKServer zkServer = InMemoryZKServer.builder().setTickTime(1000).build();
    zkServer.startAndWait();
    try {
        final CountDownLatch expireReconnectLatch = new CountDownLatch(1);
        final AtomicBoolean expired = new AtomicBoolean(false);
        final ZKClientService client = ZKClientServices.delegate(ZKClients.reWatchOnExpire(ZKClientService.Builder.of(zkServer.getConnectionStr()).setSessionTimeout(2000).setConnectionWatcher(new Watcher() {

            @Override
            public void process(WatchedEvent event) {
                if (event.getState() == Event.KeeperState.Expired) {
                    expired.set(true);
                } else if (event.getState() == Event.KeeperState.SyncConnected && expired.compareAndSet(true, true)) {
                    expireReconnectLatch.countDown();
                }
            }
        }).build()));
        client.startAndWait();
        try {
            final BlockingQueue<Watcher.Event.EventType> events = new LinkedBlockingQueue<Watcher.Event.EventType>();
            client.exists("/expireRewatch", new Watcher() {

                @Override
                public void process(WatchedEvent event) {
                    client.exists("/expireRewatch", this);
                    events.add(event.getType());
                }
            });
            client.create("/expireRewatch", null, CreateMode.PERSISTENT);
            Assert.assertEquals(Watcher.Event.EventType.NodeCreated, events.poll(2, TimeUnit.SECONDS));
            KillZKSession.kill(client.getZooKeeperSupplier().get(), zkServer.getConnectionStr(), 1000);
            Assert.assertTrue(expireReconnectLatch.await(5, TimeUnit.SECONDS));
            client.delete("/expireRewatch");
            Assert.assertEquals(Watcher.Event.EventType.NodeDeleted, events.poll(4, TimeUnit.SECONDS));
        } finally {
            client.stopAndWait();
        }
    } finally {
        zkServer.stopAndWait();
    }
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Watcher(org.apache.zookeeper.Watcher) WatchedEvent(org.apache.zookeeper.WatchedEvent) CountDownLatch(java.util.concurrent.CountDownLatch) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) InMemoryZKServer(com.continuuity.weave.internal.zookeeper.InMemoryZKServer) Test(org.junit.Test)

Example 5 with InMemoryZKServer

use of com.continuuity.weave.internal.zookeeper.InMemoryZKServer in project weave by continuuity.

the class ControllerTest method testController.

@Test
public void testController() throws ExecutionException, InterruptedException, TimeoutException {
    InMemoryZKServer zkServer = InMemoryZKServer.builder().build();
    zkServer.startAndWait();
    LOG.info("ZKServer: " + zkServer.getConnectionStr());
    try {
        RunId runId = RunIds.generate();
        ZKClientService zkClientService = ZKClientService.Builder.of(zkServer.getConnectionStr()).build();
        zkClientService.startAndWait();
        Service service = createService(zkClientService, runId);
        service.startAndWait();
        WeaveController controller = getController(zkClientService, runId);
        controller.sendCommand(Command.Builder.of("test").build()).get(2, TimeUnit.SECONDS);
        controller.stop().get(2, TimeUnit.SECONDS);
        Assert.assertEquals(ServiceController.State.TERMINATED, controller.state());
        final CountDownLatch terminateLatch = new CountDownLatch(1);
        service.addListener(new ServiceListenerAdapter() {

            @Override
            public void terminated(Service.State from) {
                terminateLatch.countDown();
            }
        }, Threads.SAME_THREAD_EXECUTOR);
        Assert.assertTrue(service.state() == Service.State.TERMINATED || terminateLatch.await(2, TimeUnit.SECONDS));
        zkClientService.stopAndWait();
    } finally {
        zkServer.stopAndWait();
    }
}
Also used : ZKClientService(com.continuuity.weave.zookeeper.ZKClientService) AbstractIdleService(com.google.common.util.concurrent.AbstractIdleService) ZKClientService(com.continuuity.weave.zookeeper.ZKClientService) Service(com.google.common.util.concurrent.Service) WeaveController(com.continuuity.weave.api.WeaveController) ServiceListenerAdapter(com.continuuity.weave.common.ServiceListenerAdapter) RunId(com.continuuity.weave.api.RunId) CountDownLatch(java.util.concurrent.CountDownLatch) InMemoryZKServer(com.continuuity.weave.internal.zookeeper.InMemoryZKServer) Test(org.junit.Test)

Aggregations

InMemoryZKServer (com.continuuity.weave.internal.zookeeper.InMemoryZKServer)11 Test (org.junit.Test)11 CountDownLatch (java.util.concurrent.CountDownLatch)5 RunId (com.continuuity.weave.api.RunId)4 ZKClientService (com.continuuity.weave.zookeeper.ZKClientService)4 AbstractIdleService (com.google.common.util.concurrent.AbstractIdleService)4 WeaveController (com.continuuity.weave.api.WeaveController)3 ServiceListenerAdapter (com.continuuity.weave.common.ServiceListenerAdapter)3 Service (com.google.common.util.concurrent.Service)3 WatchedEvent (org.apache.zookeeper.WatchedEvent)2 Watcher (org.apache.zookeeper.Watcher)2 ZKServiceDecorator (com.continuuity.weave.internal.ZKServiceDecorator)1 JsonObject (com.google.gson.JsonObject)1 File (java.io.File)1 ExecutionException (java.util.concurrent.ExecutionException)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 Semaphore (java.util.concurrent.Semaphore)1 TimeoutException (java.util.concurrent.TimeoutException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1