use of org.wildfly.clustering.singleton.election.Preference in project wildfly by wildfly.
the class ElectionPolicyServiceConfigurator method configure.
@Override
public ServiceConfigurator configure(OperationContext context, ModelNode model) throws OperationFailedException {
List<ModelNode> socketBindingPreferences = SOCKET_BINDING_PREFERENCES.resolveModelAttribute(context, model).asListOrEmpty();
List<ModelNode> namePreferences = NAME_PREFERENCES.resolveModelAttribute(context, model).asListOrEmpty();
List<Preference> preferences = new ArrayList<>(socketBindingPreferences.size() + namePreferences.size());
List<Dependency> dependencies = new ArrayList<>(socketBindingPreferences.size());
for (ModelNode preference : socketBindingPreferences) {
SupplierDependency<OutboundSocketBinding> binding = new ServiceSupplierDependency<>(CommonUnaryRequirement.OUTBOUND_SOCKET_BINDING.getServiceName(context, preference.asString()));
preferences.add(new OutboundSocketBindingPreference(binding));
dependencies.add(binding);
}
for (ModelNode preference : namePreferences) {
preferences.add(new NamePreference(preference.asString()));
}
this.dependencies = dependencies;
this.preferences = preferences;
return this;
}
use of org.wildfly.clustering.singleton.election.Preference in project wildfly by wildfly.
the class SocketAddressPreferenceTestCase method test.
@Test
public void test() throws UnknownHostException {
InetSocketAddress preferredAddress = new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 1);
InetSocketAddress otherAddress1 = new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 2);
InetSocketAddress otherAddress2 = new InetSocketAddress(InetAddress.getByName("127.0.0.2"), 1);
Preference preference = new SocketAddressPreference(preferredAddress);
Node preferredNode = mock(Node.class);
Node otherNode1 = mock(Node.class);
Node otherNode2 = mock(Node.class);
when(preferredNode.getSocketAddress()).thenReturn(preferredAddress);
when(otherNode1.getSocketAddress()).thenReturn(otherAddress1);
when(otherNode2.getSocketAddress()).thenReturn(otherAddress2);
assertTrue(preference.preferred(preferredNode));
assertFalse(preference.preferred(otherNode1));
assertFalse(preference.preferred(otherNode2));
}
use of org.wildfly.clustering.singleton.election.Preference in project wildfly by wildfly.
the class PreferredSingletonElectionPolicyTestCase method elect.
@Test
public void elect() {
SingletonElectionPolicy policy = mock(SingletonElectionPolicy.class);
Preference preference1 = mock(Preference.class);
Preference preference2 = mock(Preference.class);
Node node1 = mock(Node.class);
Node node2 = mock(Node.class);
Node node3 = mock(Node.class);
Node node4 = mock(Node.class);
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);
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);
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<Node> nodes = Arrays.asList(node3, node4);
when(policy.elect(nodes)).thenReturn(node3);
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(policy, preference1, preference2).elect(nodes));
}
use of org.wildfly.clustering.singleton.election.Preference in project wildfly by wildfly.
the class NamePreferenceTestCase method test.
@Test
public void test() {
Preference preference = new NamePreference("node1");
Node node1 = mock(Node.class);
Node node2 = mock(Node.class);
when(node1.getName()).thenReturn("node1");
when(node2.getName()).thenReturn("node2");
assertTrue(preference.preferred(node1));
assertFalse(preference.preferred(node2));
}
Aggregations