Commit 604dbb7b authored by fgorostiaga's avatar fgorostiaga
Browse files

Added test files

parent 4a0e4d6b
package com.hazelcast.instance;
/*
* Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
*
* Licensed 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.
*/
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.instance.HazelcastInstanceImpl;
import com.hazelcast.instance.HazelcastInstanceProxy;
import com.hazelcast.instance.Node;
import static java.lang.reflect.Proxy.isProxyClass;
@SuppressWarnings("WeakerAccess")
public final class TestUtil {
private TestUtil() {
}
public static Node getNode(HazelcastInstance hz) {
if (isProxyClass(hz.getClass())) {
return null;// HazelcastStarter.getNode(hz);
} else {
HazelcastInstanceImpl hazelcastInstanceImpl = getHazelcastInstanceImpl(hz);
return hazelcastInstanceImpl.node;
}
}
public static HazelcastInstanceImpl getHazelcastInstanceImpl(HazelcastInstance hz) {
if (hz instanceof HazelcastInstanceImpl) {
return (HazelcastInstanceImpl) hz;
} else if (hz instanceof HazelcastInstanceProxy) {
HazelcastInstanceProxy proxy = (HazelcastInstanceProxy) hz;
if (proxy.original != null) {
return proxy.original;
}
}
Class<? extends HazelcastInstance> clazz = hz.getClass();
if (isProxyClass(clazz)) {
return null;// HazelcastStarter.getHazelcastInstanceImpl(hz);
}
throw new IllegalArgumentException("The given HazelcastInstance is not an active HazelcastInstanceImpl: " + clazz);
}
}
package hazelcasttwoinstances;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import com.hazelcast.config.Config;
import com.hazelcast.config.MapConfig;
import com.hazelcast.config.MapIndexConfig;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.Member;
import com.hazelcast.instance.TestUtil;
import com.hazelcast.internal.cluster.impl.ClusterServiceImpl;
import com.hazelcast.nio.Address;
import com.hazelcast.query.Predicates;
import com.hazelcast.spi.properties.GroupProperty;
public class Main {
private static final List<String> POSSIBLE_VALUES = Arrays.asList("A", "B", "C", "D");
private static final String IP_ADDRESS = "192.168.97.198";
public static void main(String args[]) throws IOException {
Config config = config();
HazelcastInstance firstInstance = Hazelcast.newHazelcastInstance(config);
BufferedReader buffer=new BufferedReader(new InputStreamReader(System.in));
for (long key = 0; key < 10000; key++) {
String randomValue = POSSIBLE_VALUES.get(ThreadLocalRandom.current().nextInt(POSSIBLE_VALUES.size()));
firstInstance.getMap("test").put(key, new TestObject(key, randomValue));
}
buffer.readLine();
ClusterServiceImpl clusterService = TestUtil.getNode(firstInstance).getClusterService();
Member suspectedMember = clusterService.getMember(new Address(IP_ADDRESS, 5702));
clusterService.suspectMember(suspectedMember, null, true);
//TestUtil.getNode(firstInstance).getConnectionManager().getConnection(new Address(IP_ADDRESS, 5702)).close("reason", null);
while(true) {
int size = firstInstance.getMap("test").keySet(Predicates.in("value", "A", "B", "C", "D")).size();
System.out.println(size);
if (size!=10000) break;
}
}
protected static Config config() {
return new Config()
.setProperty(GroupProperty.MERGE_FIRST_RUN_DELAY_SECONDS.getName(), "5")
.setProperty(GroupProperty.WAIT_SECONDS_BEFORE_JOIN.getName(), "0")
.setInstanceName("main")
.addMapConfig(new MapConfig()
.setName("test")
.addMapIndexConfig(new MapIndexConfig()
.setAttribute("value").setOrdered(true)));
}
private static class TestObject implements Serializable {
private Long id;
private String value;
public TestObject() {}
public TestObject(Long id, String value) {
this.id = id;
this.value = value;
}
}
}
package hazelcasttwoinstances;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import com.hazelcast.config.Config;
import com.hazelcast.config.MapConfig;
import com.hazelcast.config.MapIndexConfig;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.query.Predicates;
import com.hazelcast.spi.properties.GroupProperty;
public class Main_two {
public static void main(String args[]) throws IOException {
Config config = config();
HazelcastInstance firstInstance = Hazelcast.newHazelcastInstance(config);
BufferedReader buffer=new BufferedReader(new InputStreamReader(System.in));
buffer.readLine();
Hazelcast.getAllHazelcastInstances();
//System.out.println(secondInstance.getMap("test").keySet(Predicates.in("value", "A", "B", "C", "D")).size());
//closeConnectionBetween(firstInstance, Hazelcast.getHazelcastInstanceByName("main"));
boolean fstinst;
while(true) {
fstinst=true;
int size = firstInstance.getMap("test").keySet(Predicates.in("value", "A", "B", "C", "D")).size();
System.out.println(size);
if (size!=10000) break;
}
System.out.println(fstinst);
}
protected static Config config() {
return new Config()
.setProperty(GroupProperty.MERGE_FIRST_RUN_DELAY_SECONDS.getName(), "5")
.setProperty(GroupProperty.WAIT_SECONDS_BEFORE_JOIN.getName(), "0")
.setInstanceName("main_two")
.addMapConfig(new MapConfig()
.setName("test")
.addMapIndexConfig(new MapIndexConfig()
.setAttribute("value").setOrdered(true)));
}
private static class TestObject implements Serializable {
public TestObject() {}
public TestObject(Long id, String value) {
}
}
/*public static void closeConnectionBetween(HazelcastInstance h1, HazelcastInstance h2) {
if (h1 == null || h2 == null) {
return;
}
Node n1 = getNode(h1);
Node n2 = getNode(h2);
suspectMember(n1, n2);
suspectMember(n2, n1);
}
public static Node getNode(HazelcastInstance hz) {
return TestUtil.getNode(hz);
}
public static void suspectMember(Node suspectingNode, Node suspectedNode) {
if (suspectingNode != null && suspectedNode != null) {
ClusterServiceImpl clusterService = suspectingNode.getClusterService();
Member suspectedMember = clusterService.getMember(suspectedNode.getLocalMember().getAddress());
if (suspectedMember != null) {
clusterService.suspectMember(suspectedMember, null, true);
}
}
}*/
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment