Skip to content

Commit 4bf8379

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 b8d4e6b commit 4bf8379

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-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: 22 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.
@@ -120,7 +120,10 @@ public void testPutIfAbsent() throws Exception {
120120
IntegrationUtils.bytesToString(client.getData().forPath(metadataStore.getPath(testKey2)), "UTF-8"));
121121
assertEquals("Integration-2", otherMetadataStore.get(testKey2));
122122
assertThat("Integration-2", eventually(equalsResult(() -> otherMetadataStore.get(testKey2))));
123+
124+
otherMetadataStore.stop();
123125
CloseableUtils.closeQuietly(otherClient);
126+
124127
}
125128

126129
@Test
@@ -143,6 +146,8 @@ public void testReplace() throws Exception {
143146
IntegrationUtils.bytesToString(client.getData().forPath(metadataStore.getPath(testKey)), "UTF-8"));
144147
assertThat("Integration-2", eventually(equalsResult(() -> metadataStore.get(testKey))));
145148
assertEquals("Integration-2", otherMetadataStore.get(testKey));
149+
150+
otherMetadataStore.stop();
146151
CloseableUtils.closeQuietly(otherClient);
147152
}
148153

@@ -202,7 +207,6 @@ public void testRemoveFromMetadataStore() throws Exception {
202207
String testValue = "Integration";
203208
metadataStore.put(testKey, testValue);
204209
assertEquals(testValue, metadataStore.remove(testKey));
205-
Thread.sleep(1000);
206210
assertNull(metadataStore.remove(testKey));
207211
}
208212

@@ -276,10 +280,6 @@ public void onUpdate(String key, String newValue) {
276280
waitAtBarrier("remove", barriers);
277281
assertThat(notifiedChanges, hasSize(4));
278282
assertThat(notifiedChanges.get(3), IsIterableContainingInOrder.contains("remove", testKey, "Integration-3"));
279-
280-
// sleep and try to see if there were any other updates
281-
Thread.sleep(1000);
282-
assertThat(notifiedChanges, hasSize(4));
283283
}
284284

285285
@Test
@@ -348,9 +348,9 @@ public void onUpdate(String key, String newValue) {
348348
assertThat(notifiedChanges, hasSize(4));
349349
assertThat(notifiedChanges.get(3), IsIterableContainingInOrder.contains("remove", testKey, "Integration-3"));
350350

351-
// sleep and try to see if there were any other updates - if there any pending updates, we should catch them by now
352-
Thread.sleep(1000);
353-
assertThat(notifiedChanges, hasSize(4));
351+
otherMetadataStore.stop();
352+
CloseableUtils.closeQuietly(otherClient);
353+
354354
}
355355

356356
@Test
@@ -369,6 +369,19 @@ public void testAddRemoveListener() throws Exception {
369369
assertThat(listeners, hasSize(0));
370370
}
371371

372+
@Test
373+
public void testEnsureStarted() {
374+
ZookeeperMetadataStore zookeeperMetadataStore = new ZookeeperMetadataStore(this.client);
375+
376+
try {
377+
zookeeperMetadataStore.get("foo");
378+
}
379+
catch (Exception e) {
380+
assertThat(e, instanceOf(IllegalStateException.class));
381+
assertThat(e.getMessage(), containsString("ZookeeperMetadataStore has to be started before using."));
382+
}
383+
}
384+
372385
private void waitAtBarrier(String barrierName, Map<String, CyclicBarrier> barriers) {
373386
try {
374387
barriers.get(barrierName).await(10, TimeUnit.SECONDS);

0 commit comments

Comments
 (0)