use of com.hazelcast.internal.serialization.DataSerializerHook in project hazelcast by hazelcast.
the class SplitBrainProtectionOperationTest method assertThatInternalOperationsAreNotSplitBrainProtectionDependent.
@Test
public void assertThatInternalOperationsAreNotSplitBrainProtectionDependent() throws Exception {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
Iterator<DataSerializerHook> hooks = ServiceLoader.iterator(DataSerializerHook.class, FACTORY_ID, classLoader);
while (hooks.hasNext()) {
DataSerializerHook hook = hooks.next();
String simpleClassName = hook.getClass().getName();
LOGGER.info("Testing " + simpleClassName + "...");
DataSerializableFactory factory = hook.createFactory();
int typeId = 0;
while (true) {
IdentifiedDataSerializable ids = createIDS(factory, typeId++);
if (ids == null) {
break;
}
Class<? extends IdentifiedDataSerializable> clazz = ids.getClass();
String className = clazz.getName();
String name = lowerCaseInternal(clazz.getSimpleName());
LOGGER.info(clazz.getSimpleName());
if (matches(NO_SPLIT_BRAIN_PROTECTION_PACKAGES, className)) {
continue;
}
boolean shouldBeMutatingOperation = false;
boolean shouldBeReadonlyOperation = false;
if (!(ids instanceof Operation) || ids instanceof UrgentSystemOperation || matches(INTERNAL_CLASS_NAMES, name) || matches(INTERNAL_PACKAGES, className)) {
// no, urgent, internal or no split brain protection operations
shouldBeMutatingOperation = false;
shouldBeReadonlyOperation = false;
} else if (ids instanceof AbstractLockOperation || matches(MUTATING_CLASS_NAMES, name)) {
// mutating operations
if (isForcedReadOnly(className)) {
shouldBeReadonlyOperation = true;
} else {
shouldBeMutatingOperation = true;
}
} else if (matches(READONLY_CLASS_NAMES, name)) {
// read-only operations
shouldBeReadonlyOperation = true;
} else {
fail(className + " doesn't match any criteria!");
}
// asserts
if (ids instanceof MutatingOperation) {
if (!shouldBeMutatingOperation) {
fail(className + " implements " + MUTATING_OP_NAME);
}
} else if (shouldBeMutatingOperation) {
fail(className + " should implement " + MUTATING_OP_NAME);
}
if (ids instanceof ReadonlyOperation) {
if (!shouldBeReadonlyOperation) {
fail(className + " implements " + READ_ONLY_OP_NAME);
}
} else if (shouldBeReadonlyOperation) {
fail(className + " should implement " + READ_ONLY_OP_NAME);
}
}
}
}
use of com.hazelcast.internal.serialization.DataSerializerHook in project hazelcast by hazelcast.
the class ServiceLoaderTest method testSkipHookLoadedByDifferentClassloader.
@Test
public void testSkipHookLoadedByDifferentClassloader() {
Class<?> otherInterface = newInterface(SpiDataSerializerHook.class.getName());
ClassLoader otherClassloader = otherInterface.getClassLoader();
Class<?> otherHook = newClassImplementingInterface("com.hazelcast.internal.serialization.DifferentHook", otherInterface, otherClassloader);
// otherHook is loaded by other classloader -> it should be skipped
ServiceLoader.ServiceDefinition definition1 = new ServiceLoader.ServiceDefinition(otherHook.getName(), otherClassloader);
// this hook should be loaded
ServiceLoader.ServiceDefinition definition2 = new ServiceLoader.ServiceDefinition(SpiDataSerializerHook.class.getName(), SpiDataSerializerHook.class.getClassLoader());
Set<ServiceLoader.ServiceDefinition> definitions = setOf(definition1, definition2);
ServiceLoader.ClassIterator<DataSerializerHook> iterator = new ServiceLoader.ClassIterator<>(definitions, DataSerializerHook.class);
assertTrue(iterator.hasNext());
Class<DataSerializerHook> hook = iterator.next();
assertEquals(SpiDataSerializerHook.class, hook);
assertFalse(iterator.hasNext());
}
use of com.hazelcast.internal.serialization.DataSerializerHook in project hazelcast by hazelcast.
the class DataSerializableConventionsTest method test_identifiedDataSerializables_areInstancesOfSameClass_whenConstructedFromFactory.
/**
* Locates {@link IdentifiedDataSerializable} classes via reflection, iterates over them and asserts an instance created by
* a factory is of the same classes as an instance created via reflection.
*/
@Test
public void test_identifiedDataSerializables_areInstancesOfSameClass_whenConstructedFromFactory() throws Exception {
Set<Class<? extends DataSerializerHook>> dsHooks = REFLECTIONS.getSubTypesOf(DataSerializerHook.class);
Map<Integer, DataSerializableFactory> factories = new HashMap<Integer, DataSerializableFactory>();
for (Class<? extends DataSerializerHook> hookClass : dsHooks) {
DataSerializerHook dsHook = hookClass.newInstance();
DataSerializableFactory factory = dsHook.createFactory();
factories.put(dsHook.getFactoryId(), factory);
}
Set<Class<? extends IdentifiedDataSerializable>> identifiedDataSerializables = getIDSConcreteClasses();
for (Class<? extends IdentifiedDataSerializable> klass : identifiedDataSerializables) {
if (AbstractLocalOperation.class.isAssignableFrom(klass)) {
continue;
}
if (isReadOnlyConfig(klass)) {
continue;
}
// wrap all of this in try-catch, as it is legitimate for some classes to throw UnsupportedOperationException
try {
Constructor<? extends IdentifiedDataSerializable> ctor = klass.getDeclaredConstructor();
ctor.setAccessible(true);
IdentifiedDataSerializable instance = ctor.newInstance();
int factoryId = instance.getFactoryId();
int typeId = instance.getClassId();
if (!factories.containsKey(factoryId)) {
fail("Factory with ID " + factoryId + " declared in " + klass + " not found." + " Is such a factory ID registered?");
}
IdentifiedDataSerializable instanceFromFactory = factories.get(factoryId).create(typeId);
assertNotNull("Factory with ID " + factoryId + " returned null for type with ID " + typeId, instanceFromFactory);
assertTrue("Factory with ID " + factoryId + " instantiated an object of " + instanceFromFactory.getClass() + " while expected type was " + instance.getClass(), instanceFromFactory.getClass().equals(instance.getClass()));
} catch (UnsupportedOperationException ignored) {
// expected from local operation classes not meant for serialization
}
}
}
Aggregations