diff --git a/spring-integration-zookeeper/src/main/java/org/springframework/integration/zookeeper/metadata/ZookeeperMetadataStore.java b/spring-integration-zookeeper/src/main/java/org/springframework/integration/zookeeper/metadata/ZookeeperMetadataStore.java index 2794395cf4..554cbd54a1 100644 --- a/spring-integration-zookeeper/src/main/java/org/springframework/integration/zookeeper/metadata/ZookeeperMetadataStore.java +++ b/spring-integration-zookeeper/src/main/java/org/springframework/integration/zookeeper/metadata/ZookeeperMetadataStore.java @@ -1,5 +1,5 @@ /* - * Copyright 2015-2016 the original author or authors. + * Copyright 2015-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -72,7 +72,7 @@ public class ZookeeperMetadataStore implements ListenableMetadataStore, SmartLif private volatile int phase = Integer.MAX_VALUE; - public ZookeeperMetadataStore(CuratorFramework client) throws Exception { + public ZookeeperMetadataStore(CuratorFramework client) { Assert.notNull(client, "Client cannot be null"); this.client = client; } @@ -203,6 +203,7 @@ public class ZookeeperMetadataStore implements ListenableMetadataStore, SmartLif @Override public String get(String key) { Assert.notNull(key, "'key' must not be null."); + Assert.state(isRunning(), "ZookeeperMetadataStore has to be started before using."); synchronized (this.updateMap) { ChildData currentData = this.cache.getCurrentData(getPath(key)); if (currentData == null) { diff --git a/spring-integration-zookeeper/src/test/java/org/springframework/integration/zookeeper/metadata/ZookeeperMetadataStoreTests.java b/spring-integration-zookeeper/src/test/java/org/springframework/integration/zookeeper/metadata/ZookeeperMetadataStoreTests.java index 680505d2c6..4d6c700bea 100644 --- a/spring-integration-zookeeper/src/test/java/org/springframework/integration/zookeeper/metadata/ZookeeperMetadataStoreTests.java +++ b/spring-integration-zookeeper/src/test/java/org/springframework/integration/zookeeper/metadata/ZookeeperMetadataStoreTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2015-2017 the original author or authors. + * Copyright 2015-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -131,7 +131,10 @@ public class ZookeeperMetadataStoreTests extends ZookeeperTestSupport { return metadataStore.get(testKey2); } }))); + + otherMetadataStore.stop(); CloseableUtils.closeQuietly(otherClient); + } @Test @@ -164,6 +167,8 @@ public class ZookeeperMetadataStoreTests extends ZookeeperTestSupport { } }))); assertEquals("Integration-2", otherMetadataStore.get(testKey)); + + otherMetadataStore.stop(); CloseableUtils.closeQuietly(otherClient); } @@ -223,7 +228,6 @@ public class ZookeeperMetadataStoreTests extends ZookeeperTestSupport { String testValue = "Integration"; metadataStore.put(testKey, testValue); assertEquals(testValue, metadataStore.remove(testKey)); - Thread.sleep(1000); assertNull(metadataStore.remove(testKey)); } @@ -296,10 +300,6 @@ public class ZookeeperMetadataStoreTests extends ZookeeperTestSupport { waitAtBarrier("remove", barriers); assertThat(notifiedChanges, hasSize(4)); assertThat(notifiedChanges.get(3), IsIterableContainingInOrder.contains("remove", testKey, "Integration-3")); - - // sleep and try to see if there were any other updates - Thread.sleep(1000); - assertThat(notifiedChanges, hasSize(4)); } @Test @@ -367,9 +367,8 @@ public class ZookeeperMetadataStoreTests extends ZookeeperTestSupport { assertThat(notifiedChanges, hasSize(4)); assertThat(notifiedChanges.get(3), IsIterableContainingInOrder.contains("remove", testKey, "Integration-3")); - // sleep and try to see if there were any other updates - if there any pending updates, we should catch them by now - Thread.sleep(1000); - assertThat(notifiedChanges, hasSize(4)); + otherMetadataStore.stop(); + CloseableUtils.closeQuietly(otherClient); } @Test @@ -388,6 +387,19 @@ public class ZookeeperMetadataStoreTests extends ZookeeperTestSupport { assertThat(listeners, hasSize(0)); } + @Test + public void testEnsureStarted() { + ZookeeperMetadataStore zookeeperMetadataStore = new ZookeeperMetadataStore(this.client); + + try { + zookeeperMetadataStore.get("foo"); + } + catch (Exception e) { + assertThat(e, instanceOf(IllegalStateException.class)); + assertThat(e.getMessage(), containsString("ZookeeperMetadataStore has to be started before using.")); + } + } + private void waitAtBarrier(String barrierName, Map barriers) { try { barriers.get(barrierName).await(10, TimeUnit.SECONDS);