You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@curator.apache.org by ra...@apache.org on 2013/07/07 19:26:59 UTC
[4/9] git commit: Moving to a filter based implementation
Moving to a filter based implementation
Project: http://git-wip-us.apache.org/repos/asf/incubator-curator/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-curator/commit/05c6c4fc
Tree: http://git-wip-us.apache.org/repos/asf/incubator-curator/tree/05c6c4fc
Diff: http://git-wip-us.apache.org/repos/asf/incubator-curator/diff/05c6c4fc
Branch: refs/heads/master
Commit: 05c6c4fc42845b144fbe369c00a1b6ee57010815
Parents: 0f00e95
Author: randgalt <ra...@apache.org>
Authored: Tue Jun 25 08:09:52 2013 -0500
Committer: randgalt <ra...@apache.org>
Committed: Tue Jun 25 08:09:52 2013 -0500
----------------------------------------------------------------------
.../x/discovery/DownInstanceManager.java | 139 -------------------
.../curator/x/discovery/InstanceFilter.java | 25 ++++
.../x/discovery/ServiceProviderBuilder.java | 12 +-
.../discovery/details/DownInstanceManager.java | 94 +++++++++++++
.../details/FilteredInstanceProvider.java | 46 ++++++
.../details/ServiceProviderBuilderImpl.java | 30 ++--
.../discovery/details/ServiceProviderImpl.java | 34 +----
.../x/discovery/TestDownInstanceManager.java | 81 -----------
.../details/TestDownInstanceManager.java | 70 ++++++++++
9 files changed, 261 insertions(+), 270 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-curator/blob/05c6c4fc/curator-x-discovery/src/main/java/org/apache/curator/x/discovery/DownInstanceManager.java
----------------------------------------------------------------------
diff --git a/curator-x-discovery/src/main/java/org/apache/curator/x/discovery/DownInstanceManager.java b/curator-x-discovery/src/main/java/org/apache/curator/x/discovery/DownInstanceManager.java
deleted file mode 100644
index e14b9de..0000000
--- a/curator-x-discovery/src/main/java/org/apache/curator/x/discovery/DownInstanceManager.java
+++ /dev/null
@@ -1,139 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you 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 org.apache.curator.x.discovery;
-
-import java.util.concurrent.DelayQueue;
-import java.util.concurrent.Delayed;
-import java.util.concurrent.TimeUnit;
-
-/**
- * Container for marking instance as temporarily down. Instances
- * added to this container will not be selected by {@link ServiceProvider}. NOTE:
- * you must add an instance of this class via {@link ServiceProviderBuilder#downInstanceManager(DownInstanceManager)}
- * to get this behavior.
- */
-public class DownInstanceManager
-{
- private final long timeoutMs;
- private final DelayQueue<Entry> queue = new DelayQueue<Entry>();
-
- private class Entry implements Delayed
- {
- private final long startMs = System.currentTimeMillis();
- private final ServiceInstance<?> instance;
-
- private Entry(ServiceInstance<?> instance)
- {
- this.instance = instance;
- }
-
- public long getDelay(TimeUnit unit)
- {
- long elapsedMs = System.currentTimeMillis() - startMs;
- long remainingMs = timeoutMs - elapsedMs;
- return (remainingMs > 0) ? unit.convert(remainingMs, TimeUnit.MILLISECONDS) : 0;
- }
-
- @Override
- // note: note semantically the same as equals()/hashCode()
- public int compareTo(Delayed rhs)
- {
- long diff = getDelay(TimeUnit.MILLISECONDS) - rhs.getDelay(TimeUnit.MILLISECONDS);
- return (diff < 0) ? -1 :((diff > 0) ? 1 : 0);
- }
-
- @Override
- public boolean equals(Object o)
- {
- if ( this == o )
- {
- return true;
- }
- if ( o == null || getClass() != o.getClass() )
- {
- return false;
- }
-
- Entry entry = (Entry)o;
-
- //noinspection RedundantIfStatement
- if ( !instance.equals(entry.instance) )
- {
- return false;
- }
-
- return true;
- }
-
- @Override
- public int hashCode()
- {
- return instance.hashCode();
- }
- }
-
- /**
- * @param timeout amount of time for instances to be unavailable
- * @param unit time unit
- */
- public DownInstanceManager(long timeout, TimeUnit unit)
- {
- timeoutMs = unit.toMillis(timeout);
- }
-
- /**
- * This instance will be unavailable for the configured timeout
- *
- * @param instance instance to mark unavailable
- */
- public void add(ServiceInstance<?> instance)
- {
- purge();
- queue.add(new Entry(instance));
- }
-
- /**
- * Return true of the given instance is currently unavailable
- *
- * @param instance instance to check
- * @return true/false
- */
- public boolean contains(ServiceInstance<?> instance)
- {
- purge();
- return queue.contains(new Entry(instance));
- }
-
- /**
- * Return true if there are instances currently unavailable
- *
- * @return true/false
- */
- public boolean hasEntries()
- {
- purge();
- return !queue.isEmpty();
- }
-
- private void purge()
- {
- //noinspection StatementWithEmptyBody
- while ( queue.poll() != null ){} // pull out expired entries
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-curator/blob/05c6c4fc/curator-x-discovery/src/main/java/org/apache/curator/x/discovery/InstanceFilter.java
----------------------------------------------------------------------
diff --git a/curator-x-discovery/src/main/java/org/apache/curator/x/discovery/InstanceFilter.java b/curator-x-discovery/src/main/java/org/apache/curator/x/discovery/InstanceFilter.java
new file mode 100644
index 0000000..72cd410
--- /dev/null
+++ b/curator-x-discovery/src/main/java/org/apache/curator/x/discovery/InstanceFilter.java
@@ -0,0 +1,25 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.curator.x.discovery;
+
+import com.google.common.base.Predicate;
+
+public interface InstanceFilter<T> extends Predicate<ServiceInstance<T>>
+{
+}
http://git-wip-us.apache.org/repos/asf/incubator-curator/blob/05c6c4fc/curator-x-discovery/src/main/java/org/apache/curator/x/discovery/ServiceProviderBuilder.java
----------------------------------------------------------------------
diff --git a/curator-x-discovery/src/main/java/org/apache/curator/x/discovery/ServiceProviderBuilder.java b/curator-x-discovery/src/main/java/org/apache/curator/x/discovery/ServiceProviderBuilder.java
index 58ffc1b..59596a2 100644
--- a/curator-x-discovery/src/main/java/org/apache/curator/x/discovery/ServiceProviderBuilder.java
+++ b/curator-x-discovery/src/main/java/org/apache/curator/x/discovery/ServiceProviderBuilder.java
@@ -20,6 +20,7 @@ package org.apache.curator.x.discovery;
import org.apache.curator.x.discovery.strategies.RoundRobinStrategy;
import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.TimeUnit;
public interface ServiceProviderBuilder<T>
{
@@ -64,12 +65,7 @@ public interface ServiceProviderBuilder<T>
*/
public ServiceProviderBuilder<T> refreshPaddingMs(int refreshPaddingMs);
- /**
- * optional - add a down instance manager to this provider. The provider will ignore instances
- * that are currently unavailable as specified by the manager
- *
- * @param downInstanceManager manager
- * @return this
- */
- public ServiceProviderBuilder<T> downInstanceManager(DownInstanceManager downInstanceManager);
+ public ServiceProviderBuilder<T> downInstanceArguments(long timeout, TimeUnit unit, int threshold);
+
+ public ServiceProviderBuilder<T> additionalFilter(InstanceFilter<T> filter);
}
http://git-wip-us.apache.org/repos/asf/incubator-curator/blob/05c6c4fc/curator-x-discovery/src/main/java/org/apache/curator/x/discovery/details/DownInstanceManager.java
----------------------------------------------------------------------
diff --git a/curator-x-discovery/src/main/java/org/apache/curator/x/discovery/details/DownInstanceManager.java b/curator-x-discovery/src/main/java/org/apache/curator/x/discovery/details/DownInstanceManager.java
new file mode 100644
index 0000000..efda05c
--- /dev/null
+++ b/curator-x-discovery/src/main/java/org/apache/curator/x/discovery/details/DownInstanceManager.java
@@ -0,0 +1,94 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.curator.x.discovery.details;
+
+import com.google.common.collect.Maps;
+import org.apache.curator.x.discovery.InstanceFilter;
+import org.apache.curator.x.discovery.ServiceInstance;
+import java.util.Map;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+
+class DownInstanceManager<T> implements InstanceFilter<T>
+{
+ private final long timeoutMs;
+ private final int threshold;
+ private final ConcurrentMap<ServiceInstance<?>, Status> statuses = Maps.newConcurrentMap();
+
+ private static final long DEFAULT_TIMEOUT_MS = 30000;
+ private static final int DEFAULT_THRESHOLD = 2;
+
+ private static class Status
+ {
+ private final long startMs = System.currentTimeMillis();
+ private final AtomicInteger errorCount = new AtomicInteger(0);
+ }
+
+ DownInstanceManager()
+ {
+ this(DEFAULT_TIMEOUT_MS, TimeUnit.MILLISECONDS, DEFAULT_THRESHOLD);
+ }
+
+ /**
+ * @param timeout amount of time for instances to be unavailable
+ * @param unit time unit
+ */
+ DownInstanceManager(long timeout, TimeUnit unit, int threshold)
+ {
+ this.threshold = threshold;
+ timeoutMs = unit.toMillis(timeout);
+ }
+
+ /**
+ * This instance will be unavailable for the configured timeout
+ *
+ * @param instance instance to mark unavailable
+ */
+ void add(ServiceInstance<?> instance)
+ {
+ purge();
+
+ Status newStatus = new Status();
+ Status oldStatus = statuses.putIfAbsent(instance, newStatus);
+ Status useStatus = (oldStatus != null) ? oldStatus : newStatus;
+ useStatus.errorCount.incrementAndGet();
+ }
+
+ @Override
+ public boolean apply(ServiceInstance<T> instance)
+ {
+ purge();
+
+ Status status = statuses.get(instance);
+ return (status == null) || (status.errorCount.get() < threshold);
+ }
+
+ private void purge()
+ {
+ for ( Map.Entry<ServiceInstance<?>, Status> entry : statuses.entrySet() )
+ {
+ long elapsedMs = System.currentTimeMillis() - entry.getValue().startMs;
+ if ( elapsedMs >= timeoutMs )
+ {
+ statuses.remove(entry.getKey());
+ }
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-curator/blob/05c6c4fc/curator-x-discovery/src/main/java/org/apache/curator/x/discovery/details/FilteredInstanceProvider.java
----------------------------------------------------------------------
diff --git a/curator-x-discovery/src/main/java/org/apache/curator/x/discovery/details/FilteredInstanceProvider.java b/curator-x-discovery/src/main/java/org/apache/curator/x/discovery/details/FilteredInstanceProvider.java
new file mode 100644
index 0000000..acaae65
--- /dev/null
+++ b/curator-x-discovery/src/main/java/org/apache/curator/x/discovery/details/FilteredInstanceProvider.java
@@ -0,0 +1,46 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.curator.x.discovery.details;
+
+import com.google.common.base.Predicate;
+import com.google.common.base.Predicates;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Iterables;
+import org.apache.curator.x.discovery.InstanceFilter;
+import org.apache.curator.x.discovery.ServiceInstance;
+import java.util.List;
+
+class FilteredInstanceProvider<T> implements InstanceProvider<T>
+{
+ private final InstanceProvider<T> instanceProvider;
+ private final Predicate<ServiceInstance<T>> predicates;
+
+ FilteredInstanceProvider(InstanceProvider<T> instanceProvider, List<InstanceFilter<T>> filters)
+ {
+ this.instanceProvider = instanceProvider;
+ predicates = Predicates.and(filters);
+ }
+
+ @Override
+ public List<ServiceInstance<T>> getInstances() throws Exception
+ {
+ Iterable<ServiceInstance<T>> filtered = Iterables.filter(instanceProvider.getInstances(), predicates);
+ return ImmutableList.copyOf(filtered);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-curator/blob/05c6c4fc/curator-x-discovery/src/main/java/org/apache/curator/x/discovery/details/ServiceProviderBuilderImpl.java
----------------------------------------------------------------------
diff --git a/curator-x-discovery/src/main/java/org/apache/curator/x/discovery/details/ServiceProviderBuilderImpl.java b/curator-x-discovery/src/main/java/org/apache/curator/x/discovery/details/ServiceProviderBuilderImpl.java
index 2917270..200ac06 100644
--- a/curator-x-discovery/src/main/java/org/apache/curator/x/discovery/details/ServiceProviderBuilderImpl.java
+++ b/curator-x-discovery/src/main/java/org/apache/curator/x/discovery/details/ServiceProviderBuilderImpl.java
@@ -18,12 +18,16 @@
*/
package org.apache.curator.x.discovery.details;
-import org.apache.curator.x.discovery.DownInstanceManager;
+import com.google.common.collect.Lists;
+import org.apache.curator.x.discovery.InstanceFilter;
import org.apache.curator.x.discovery.ProviderStrategy;
import org.apache.curator.x.discovery.ServiceProvider;
import org.apache.curator.x.discovery.ServiceProviderBuilder;
import org.apache.curator.x.discovery.strategies.RoundRobinStrategy;
+import java.util.ArrayList;
+import java.util.List;
import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.TimeUnit;
/**
* Builder for service providers
@@ -35,17 +39,14 @@ class ServiceProviderBuilderImpl<T> implements ServiceProviderBuilder<T>
private ProviderStrategy<T> providerStrategy;
private ThreadFactory threadFactory;
private int refreshPaddingMs;
- private DownInstanceManager downInstanceManager;
+ private DownInstanceManager<T> downInstanceManager = new DownInstanceManager<T>();
+ private List<InstanceFilter<T>> filters = Lists.newArrayList();
- /**
- * Allocate a new service provider based on the current builder settings
- *
- * @return provider
- */
- @Override
public ServiceProvider<T> build()
{
- return new ServiceProviderImpl<T>(discovery, serviceName, providerStrategy, threadFactory, downInstanceManager);
+ ArrayList<InstanceFilter<T>> localFilters = Lists.newArrayList(filters);
+ localFilters.add(downInstanceManager);
+ return new ServiceProviderImpl<T>(discovery, serviceName, providerStrategy, threadFactory, filters);
}
ServiceProviderBuilderImpl(ServiceDiscoveryImpl<T> discovery)
@@ -108,9 +109,16 @@ class ServiceProviderBuilderImpl<T> implements ServiceProviderBuilder<T>
}
@Override
- public ServiceProviderBuilder<T> downInstanceManager(DownInstanceManager downInstanceManager)
+ public ServiceProviderBuilder<T> downInstanceArguments(long timeout, TimeUnit unit, int threshold)
+ {
+ downInstanceManager = new DownInstanceManager<T>(timeout, unit, threshold);
+ return this;
+ }
+
+ @Override
+ public ServiceProviderBuilder<T> additionalFilter(InstanceFilter<T> filter)
{
- this.downInstanceManager = downInstanceManager;
+ filters.add(filter);
return this;
}
}
http://git-wip-us.apache.org/repos/asf/incubator-curator/blob/05c6c4fc/curator-x-discovery/src/main/java/org/apache/curator/x/discovery/details/ServiceProviderImpl.java
----------------------------------------------------------------------
diff --git a/curator-x-discovery/src/main/java/org/apache/curator/x/discovery/details/ServiceProviderImpl.java b/curator-x-discovery/src/main/java/org/apache/curator/x/discovery/details/ServiceProviderImpl.java
index b7f237b..dac5890 100644
--- a/curator-x-discovery/src/main/java/org/apache/curator/x/discovery/details/ServiceProviderImpl.java
+++ b/curator-x-discovery/src/main/java/org/apache/curator/x/discovery/details/ServiceProviderImpl.java
@@ -18,10 +18,7 @@
*/
package org.apache.curator.x.discovery.details;
-import com.google.common.base.Predicate;
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.Iterables;
-import org.apache.curator.x.discovery.DownInstanceManager;
+import org.apache.curator.x.discovery.InstanceFilter;
import org.apache.curator.x.discovery.ProviderStrategy;
import org.apache.curator.x.discovery.ServiceCache;
import org.apache.curator.x.discovery.ServiceInstance;
@@ -41,37 +38,12 @@ public class ServiceProviderImpl<T> implements ServiceProvider<T>
private final ServiceDiscoveryImpl<T> discovery;
private final ProviderStrategy<T> providerStrategy;
- public ServiceProviderImpl(ServiceDiscoveryImpl<T> discovery, String serviceName, ProviderStrategy<T> providerStrategy, ThreadFactory threadFactory, final DownInstanceManager downInstanceManager)
+ public ServiceProviderImpl(ServiceDiscoveryImpl<T> discovery, String serviceName, ProviderStrategy<T> providerStrategy, ThreadFactory threadFactory, List<InstanceFilter<T>> filters)
{
this.discovery = discovery;
this.providerStrategy = providerStrategy;
cache = discovery.serviceCacheBuilder().name(serviceName).threadFactory(threadFactory).build();
-
- instanceProvider = new InstanceProvider<T>()
- {
- @Override
- public List<ServiceInstance<T>> getInstances() throws Exception
- {
- List<ServiceInstance<T>> instances = cache.getInstances();
- if ( (downInstanceManager != null) && downInstanceManager.hasEntries() )
- {
- Iterable<ServiceInstance<T>> filtered = Iterables.filter
- (
- instances,
- new Predicate<ServiceInstance<T>>()
- {
- @Override
- public boolean apply(ServiceInstance<T> instance)
- {
- return !downInstanceManager.contains(instance);
- }
- }
- );
- instances = ImmutableList.copyOf(filtered);
- }
- return instances;
- }
- };
+ instanceProvider = new FilteredInstanceProvider<T>(cache, filters);
}
/**
http://git-wip-us.apache.org/repos/asf/incubator-curator/blob/05c6c4fc/curator-x-discovery/src/test/java/org/apache/curator/x/discovery/TestDownInstanceManager.java
----------------------------------------------------------------------
diff --git a/curator-x-discovery/src/test/java/org/apache/curator/x/discovery/TestDownInstanceManager.java b/curator-x-discovery/src/test/java/org/apache/curator/x/discovery/TestDownInstanceManager.java
deleted file mode 100644
index 4633b9d..0000000
--- a/curator-x-discovery/src/test/java/org/apache/curator/x/discovery/TestDownInstanceManager.java
+++ /dev/null
@@ -1,81 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you 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 org.apache.curator.x.discovery;
-
-import org.apache.curator.x.discovery.details.ServiceDiscoveryImpl;
-import org.apache.curator.x.discovery.details.ServiceProviderImpl;
-import org.apache.curator.x.discovery.strategies.RandomStrategy;
-import org.testng.Assert;
-import org.testng.annotations.Test;
-import java.util.concurrent.TimeUnit;
-
-public class TestDownInstanceManager
-{
- @Test
- public void testBasic() throws Exception
- {
- ServiceInstance<Void> instance1 = ServiceInstance.<Void>builder().name("hey").id("1").build();
- ServiceInstance<Void> instance2 = ServiceInstance.<Void>builder().name("hey").id("2").build();
-
- DownInstanceManager downInstanceManager = new DownInstanceManager(10, TimeUnit.DAYS);
- Assert.assertFalse(downInstanceManager.hasEntries());
- Assert.assertFalse(downInstanceManager.contains(instance1));
- Assert.assertFalse(downInstanceManager.contains(instance2));
-
- downInstanceManager.add(instance1);
- Assert.assertTrue(downInstanceManager.contains(instance1));
- Assert.assertFalse(downInstanceManager.contains(instance2));
- }
-
- @Test
- public void testExpiration() throws Exception
- {
- ServiceInstance<Void> instance1 = ServiceInstance.<Void>builder().name("hey").id("1").build();
- ServiceInstance<Void> instance2 = ServiceInstance.<Void>builder().name("hey").id("2").build();
-
- DownInstanceManager downInstanceManager = new DownInstanceManager(1, TimeUnit.SECONDS);
-
- downInstanceManager.add(instance1);
- Assert.assertTrue(downInstanceManager.contains(instance1));
- Assert.assertFalse(downInstanceManager.contains(instance2));
-
- Thread.sleep(1000);
-
- Assert.assertFalse(downInstanceManager.contains(instance1));
- Assert.assertFalse(downInstanceManager.contains(instance2));
- }
-
- @Test
- public void testInProvider() throws Exception
- {
- ServiceInstance<Void> instance1 = ServiceInstance.<Void>builder().name("hey").id("1").build();
- ServiceInstance<Void> instance2 = ServiceInstance.<Void>builder().name("hey").id("2").build();
-
- DownInstanceManager downInstanceManager = new DownInstanceManager(1, TimeUnit.SECONDS);
- ServiceDiscoveryImpl<Void> discovery = new ServiceDiscoveryImpl<Void>(null, null, null, null);
- ServiceProviderImpl<Void> provider = new ServiceProviderImpl<Void>
- (
- discovery,
- "test",
- new RandomStrategy<Void>(),
- null,
- downInstanceManager
- );
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-curator/blob/05c6c4fc/curator-x-discovery/src/test/java/org/apache/curator/x/discovery/details/TestDownInstanceManager.java
----------------------------------------------------------------------
diff --git a/curator-x-discovery/src/test/java/org/apache/curator/x/discovery/details/TestDownInstanceManager.java b/curator-x-discovery/src/test/java/org/apache/curator/x/discovery/details/TestDownInstanceManager.java
new file mode 100644
index 0000000..bddae89
--- /dev/null
+++ b/curator-x-discovery/src/test/java/org/apache/curator/x/discovery/details/TestDownInstanceManager.java
@@ -0,0 +1,70 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.curator.x.discovery.details;
+
+import org.apache.curator.x.discovery.ServiceInstance;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+import java.util.concurrent.TimeUnit;
+
+public class TestDownInstanceManager
+{
+ @Test
+ public void testBasic() throws Exception
+ {
+ ServiceInstance<Void> instance1 = ServiceInstance.<Void>builder().name("hey").id("1").build();
+ ServiceInstance<Void> instance2 = ServiceInstance.<Void>builder().name("hey").id("2").build();
+
+ DownInstanceManager<Void> downInstanceManager = new DownInstanceManager<Void>(10, TimeUnit.DAYS, 1);
+ Assert.assertTrue(downInstanceManager.apply(instance1));
+ Assert.assertTrue(downInstanceManager.apply(instance2));
+
+ downInstanceManager.add(instance1);
+ Assert.assertFalse(downInstanceManager.apply(instance1));
+ Assert.assertTrue(downInstanceManager.apply(instance2));
+ }
+
+ @Test
+ public void testExpiration() throws Exception
+ {
+ ServiceInstance<Void> instance1 = ServiceInstance.<Void>builder().name("hey").id("1").build();
+ ServiceInstance<Void> instance2 = ServiceInstance.<Void>builder().name("hey").id("2").build();
+
+ DownInstanceManager<Void> downInstanceManager = new DownInstanceManager<Void>(1, TimeUnit.SECONDS, 1);
+
+ downInstanceManager.add(instance1);
+ Assert.assertFalse(downInstanceManager.apply(instance1));
+ Assert.assertTrue(downInstanceManager.apply(instance2));
+
+ Thread.sleep(1000);
+
+ Assert.assertTrue(downInstanceManager.apply(instance1));
+ Assert.assertTrue(downInstanceManager.apply(instance2));
+ }
+
+ //@Test
+ public void testInProvider() throws Exception
+ {
+ ServiceInstance<Void> instance1 = ServiceInstance.<Void>builder().name("hey").id("1").build();
+ ServiceInstance<Void> instance2 = ServiceInstance.<Void>builder().name("hey").id("2").build();
+
+ DownInstanceManager<Void> downInstanceManager = new DownInstanceManager<Void>(1, TimeUnit.SECONDS, 1);
+ ServiceDiscoveryImpl<Void> discovery = new ServiceDiscoveryImpl<Void>(null, null, null, null);
+ }
+}