Skip to content

Commit 238f87f

Browse files
artembilangaryrussell
authored andcommitted
INT-4464 Check ZKMetadataStore.running before use
JIRA: https://jira.spring.io/browse/INT-4464 A `ZookeeperMetadataStore.get()` is based on the `this.cache` variable. This one is initialized in the `start()`. * Assert `isRunning()` in the `get()` before using. **Cherry-pick to 5.0.x and 4.3.x**
1 parent 5d5093f commit 238f87f

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

spring-integration-zookeeper/src/main/java/org/springframework/integration/zookeeper/metadata/ZookeeperMetadataStore.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2016 the original author or authors.
2+
* Copyright 2015-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -72,7 +72,7 @@ public class ZookeeperMetadataStore implements ListenableMetadataStore, SmartLif
7272

7373
private volatile int phase = Integer.MAX_VALUE;
7474

75-
public ZookeeperMetadataStore(CuratorFramework client) throws Exception {
75+
public ZookeeperMetadataStore(CuratorFramework client) {
7676
Assert.notNull(client, "Client cannot be null");
7777
this.client = client;
7878
}
@@ -203,6 +203,7 @@ public void put(String key, String value) {
203203
@Override
204204
public String get(String key) {
205205
Assert.notNull(key, "'key' must not be null.");
206+
Assert.state(isRunning(), "ZookeeperMetadataStore has to be started before using.");
206207
synchronized (this.updateMap) {
207208
ChildData currentData = this.cache.getCurrentData(getPath(key));
208209
if (currentData == null) {

spring-integration-zookeeper/src/test/java/org/springframework/integration/zookeeper/metadata/ZookeeperMetadataStoreTests.java

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2017 the original author or authors.
2+
* Copyright 2015-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -131,7 +131,10 @@ public String evaluate() {
131131
return metadataStore.get(testKey2);
132132
}
133133
})));
134+
135+
otherMetadataStore.stop();
134136
CloseableUtils.closeQuietly(otherClient);
137+
135138
}
136139

137140
@Test
@@ -164,6 +167,8 @@ public String evaluate() {
164167
}
165168
})));
166169
assertEquals("Integration-2", otherMetadataStore.get(testKey));
170+
171+
otherMetadataStore.stop();
167172
CloseableUtils.closeQuietly(otherClient);
168173
}
169174

@@ -223,7 +228,6 @@ public void testRemoveFromMetadataStore() throws Exception {
223228
String testValue = "Integration";
224229
metadataStore.put(testKey, testValue);
225230
assertEquals(testValue, metadataStore.remove(testKey));
226-
Thread.sleep(1000);
227231
assertNull(metadataStore.remove(testKey));
228232
}
229233

@@ -296,10 +300,6 @@ public void onUpdate(String key, String newValue) {
296300
waitAtBarrier("remove", barriers);
297301
assertThat(notifiedChanges, hasSize(4));
298302
assertThat(notifiedChanges.get(3), IsIterableContainingInOrder.contains("remove", testKey, "Integration-3"));
299-
300-
// sleep and try to see if there were any other updates
301-
Thread.sleep(1000);
302-
assertThat(notifiedChanges, hasSize(4));
303303
}
304304

305305
@Test
@@ -367,9 +367,8 @@ public void onUpdate(String key, String newValue) {
367367
assertThat(notifiedChanges, hasSize(4));
368368
assertThat(notifiedChanges.get(3), IsIterableContainingInOrder.contains("remove", testKey, "Integration-3"));
369369

370-
// sleep and try to see if there were any other updates - if there any pending updates, we should catch them by now
371-
Thread.sleep(1000);
372-
assertThat(notifiedChanges, hasSize(4));
370+
otherMetadataStore.stop();
371+
CloseableUtils.closeQuietly(otherClient);
373372
}
374373

375374
@Test
@@ -388,6 +387,19 @@ public void testAddRemoveListener() throws Exception {
388387
assertThat(listeners, hasSize(0));
389388
}
390389

390+
@Test
391+
public void testEnsureStarted() {
392+
ZookeeperMetadataStore zookeeperMetadataStore = new ZookeeperMetadataStore(this.client);
393+
394+
try {
395+
zookeeperMetadataStore.get("foo");
396+
}
397+
catch (Exception e) {
398+
assertThat(e, instanceOf(IllegalStateException.class));
399+
assertThat(e.getMessage(), containsString("ZookeeperMetadataStore has to be started before using."));
400+
}
401+
}
402+
391403
private void waitAtBarrier(String barrierName, Map<String, CyclicBarrier> barriers) {
392404
try {
393405
barriers.get(barrierName).await(10, TimeUnit.SECONDS);

0 commit comments

Comments
 (0)