Permalink
Browse files

AS7-5024 Simplify definition of chained PreferredSingletonElectionPol…

…icy preferences
  • Loading branch information...
1 parent 5500833 commit d1477df1035a9196231c27505119e6b04a415d61 @pferraro committed Jun 19, 2012
View
19 ...ain/java/org/jboss/as/clustering/singleton/election/PreferredSingletonElectionPolicy.java
@@ -21,6 +21,8 @@
*/
package org.jboss.as.clustering.singleton.election;
+import java.util.Arrays;
+import java.util.Collections;
import java.util.List;
import org.jboss.as.clustering.ClusterNode;
@@ -33,19 +35,26 @@
* @author Paul Ferraro
*/
public class PreferredSingletonElectionPolicy implements SingletonElectionPolicy {
- private final Preference preference;
+ private final List<Preference> preferences;
private final SingletonElectionPolicy policy;
+ @Deprecated
public PreferredSingletonElectionPolicy(Preference preference, SingletonElectionPolicy policy) {
- this.preference = preference;
+ this(policy, preference);
+ }
+
+ public PreferredSingletonElectionPolicy(SingletonElectionPolicy policy, Preference... preferences) {
this.policy = policy;
+ this.preferences = (preferences != null) ? Arrays.asList(preferences) : Collections.<Preference>emptyList();
}
@Override
public ClusterNode elect(List<ClusterNode> candidates) {
- for (ClusterNode candidate: candidates) {
- if (this.preference.preferred(candidate)) {
- return candidate;
+ for (Preference preference: this.preferences) {
+ for (ClusterNode candidate: candidates) {
+ if (preference.preferred(candidate)) {
+ return candidate;
+ }
}
}
return this.policy.elect(candidates);
View
34 .../org/jboss/as/clustering/singleton/election/PreferredSingletonElectionPolicyTestCase.java
@@ -38,30 +38,40 @@
@Test
public void elect() {
SingletonElectionPolicy policy = mock(SingletonElectionPolicy.class);
- Preference preference = mock(Preference.class);
+ Preference preference1 = mock(Preference.class);
+ Preference preference2 = mock(Preference.class);
ClusterNode node1 = mock(ClusterNode.class);
ClusterNode node2 = mock(ClusterNode.class);
ClusterNode node3 = mock(ClusterNode.class);
+ ClusterNode node4 = mock(ClusterNode.class);
- when(preference.preferred(same(node1))).thenReturn(true);
- when(preference.preferred(same(node2))).thenReturn(false);
- when(preference.preferred(same(node3))).thenReturn(false);
+ when(preference1.preferred(same(node1))).thenReturn(true);
+ when(preference1.preferred(same(node2))).thenReturn(false);
+ when(preference1.preferred(same(node3))).thenReturn(false);
+ when(preference1.preferred(same(node4))).thenReturn(false);
- assertSame(node1, new PreferredSingletonElectionPolicy(preference, policy).elect(Arrays.asList(node1, node2)));
- assertSame(node1, new PreferredSingletonElectionPolicy(preference, policy).elect(Arrays.asList(node2, node1)));
+ when(preference2.preferred(same(node1))).thenReturn(false);
+ when(preference2.preferred(same(node2))).thenReturn(true);
+ when(preference2.preferred(same(node3))).thenReturn(false);
+ when(preference2.preferred(same(node4))).thenReturn(false);
- List<ClusterNode> nodes = Arrays.asList(node2, node3);
- when(policy.elect(nodes)).thenReturn(node2);
-
- assertSame(node2, new PreferredSingletonElectionPolicy(preference, policy).elect(nodes));
+ assertSame(node1, new PreferredSingletonElectionPolicy(policy, preference1, preference2).elect(Arrays.asList(node1, node2, node3, node4)));
+ assertSame(node1, new PreferredSingletonElectionPolicy(policy, preference1, preference2).elect(Arrays.asList(node4, node3, node2, node1)));
+ assertSame(node2, new PreferredSingletonElectionPolicy(policy, preference1, preference2).elect(Arrays.asList(node2, node3, node4)));
+ assertSame(node2, new PreferredSingletonElectionPolicy(policy, preference1, preference2).elect(Arrays.asList(node4, node3, node2)));
+ List<ClusterNode> nodes = Arrays.asList(node3, node4);
when(policy.elect(nodes)).thenReturn(node3);
- assertSame(node3, new PreferredSingletonElectionPolicy(preference, policy).elect(nodes));
+ assertSame(node3, new PreferredSingletonElectionPolicy(policy, preference1, preference2).elect(nodes));
+
+ when(policy.elect(nodes)).thenReturn(node4);
+
+ assertSame(node4, new PreferredSingletonElectionPolicy(policy, preference1, preference2).elect(nodes));
when(policy.elect(nodes)).thenReturn(null);
- assertNull(new PreferredSingletonElectionPolicy(preference, policy).elect(nodes));
+ assertNull(new PreferredSingletonElectionPolicy(policy, preference1, preference2).elect(nodes));
}
}

0 comments on commit d1477df

Please sign in to comment.