use of com.hazelcast.internal.serialization.SerializationService in project hazelcast by hazelcast.
the class SetEqualsHashTest method testCollectionItem_equalsAndHash.
@Test
public void testCollectionItem_equalsAndHash() {
SerializationServiceBuilder serializationServiceBuilder = new DefaultSerializationServiceBuilder();
SerializationService build = serializationServiceBuilder.build();
Data value = build.toData(randomString());
CollectionItem firstItem = new CollectionItem(1, value);
CollectionItem secondItem = new CollectionItem(2, value);
assertTrue(firstItem.equals(secondItem));
assertEquals(firstItem.hashCode(), secondItem.hashCode());
}
use of com.hazelcast.internal.serialization.SerializationService in project hazelcast by hazelcast.
the class CollectionTestUtil method getBackupList.
/**
* Returns all backup items of an {@link IList} by a given list name.
* <p>
* Note: You have to provide the {@link HazelcastInstance} you want to retrieve the backups from.
* Use {@link getBackupInstance} to retrieve the backup instance for a given replica index.
*
* @param backupInstance the {@link HazelcastInstance} to retrieve the backups from
* @param listName the list name
* @return a {@link List} with the backup items
*/
public static <E> List<E> getBackupList(HazelcastInstance backupInstance, String listName) {
NodeEngineImpl nodeEngine = getNodeEngineImpl(backupInstance);
CollectionService service = nodeEngine.getService(ListService.SERVICE_NAME);
CollectionContainer collectionContainer = service.getContainerMap().get(listName);
if (collectionContainer == null) {
return emptyList();
}
// the replica items are retrieved via `getMap()`, the primary items via `getCollection()`
Map<Long, CollectionItem> map = collectionContainer.getMap();
List<E> backupList = new ArrayList<E>(map.size());
SerializationService serializationService = nodeEngine.getSerializationService();
for (CollectionItem collectionItem : map.values()) {
E value = serializationService.toObject(collectionItem.getValue());
backupList.add(value);
}
return backupList;
}
use of com.hazelcast.internal.serialization.SerializationService in project hazelcast by hazelcast.
the class WanReplicationRefTest method testSerialization.
@Test
public void testSerialization() {
ref.setName("myWanReplicationRef");
ref.setMergePolicyClassName("myMergePolicy");
ref.setRepublishingEnabled(true);
ref.addFilter("myFilter");
SerializationService serializationService = new DefaultSerializationServiceBuilder().build();
Data serialized = serializationService.toData(ref);
WanReplicationRef deserialized = serializationService.toObject(serialized);
assertEquals(ref.getName(), deserialized.getName());
assertEquals(ref.getMergePolicyClassName(), deserialized.getMergePolicyClassName());
assertEquals(ref.isRepublishingEnabled(), deserialized.isRepublishingEnabled());
assertEquals(ref.getFilters(), deserialized.getFilters());
assertEquals(ref.toString(), deserialized.toString());
}
use of com.hazelcast.internal.serialization.SerializationService in project hazelcast by hazelcast.
the class EvictionStrategyTest method evictionPolicySuccessfullyEvaluatedOnSamplingBasedEvictionStrategy.
@Test
public void evictionPolicySuccessfullyEvaluatedOnSamplingBasedEvictionStrategy() {
final int recordCount = 100;
final int expectedEvictedRecordValue = recordCount / 2;
Node node = getNode(instance);
SerializationService serializationService = node.getSerializationService();
ICacheService cacheService = node.getNodeEngine().getService(ICacheService.SERVICE_NAME);
CacheContext cacheContext = cacheService.getOrCreateCacheContext("MyCache");
SamplingEvictionStrategy<K, V, S> evictionStrategy = SamplingEvictionStrategy.INSTANCE;
CacheRecordHashMap cacheRecordMap = new CacheRecordHashMap(serializationService, 1000, cacheContext);
CacheObjectRecord expectedEvictedRecord = null;
Data expectedData = null;
for (int i = 0; i < recordCount; i++) {
CacheObjectRecord record = new CacheObjectRecord(i, System.currentTimeMillis(), Long.MAX_VALUE);
Data data = serializationService.toData(i);
cacheRecordMap.put(data, record);
if (i == expectedEvictedRecordValue) {
expectedEvictedRecord = record;
expectedData = data;
}
}
assertNotNull(expectedEvictedRecord);
assertNotNull(expectedData);
final SimpleEvictionCandidate evictionCandidate = new SimpleEvictionCandidate((K) expectedData, (V) expectedEvictedRecord);
// we are testing "EvictionStrategy" in this test, so we mock "EvictionPolicyEvaluator" (it's tested in another test)
EvictionPolicyEvaluator evictionPolicyEvaluator = mock(EvictionPolicyEvaluator.class);
when(evictionPolicyEvaluator.evaluate(Matchers.any(Iterable.class))).thenReturn(evictionCandidate);
when(evictionPolicyEvaluator.getEvictionPolicyComparator()).thenReturn(null);
assertEquals(recordCount, cacheRecordMap.size());
assertTrue(cacheRecordMap.containsKey(expectedData));
assertTrue(cacheRecordMap.containsValue(expectedEvictedRecord));
boolean evicted = evictionStrategy.evict((S) cacheRecordMap, evictionPolicyEvaluator, EVICT_ALWAYS, NO_LISTENER);
assertTrue(evicted);
assertEquals(recordCount - 1, cacheRecordMap.size());
assertFalse(cacheRecordMap.containsKey(expectedData));
assertFalse(cacheRecordMap.containsValue(expectedEvictedRecord));
}
use of com.hazelcast.internal.serialization.SerializationService in project hazelcast by hazelcast.
the class PortableVersionTest method test_nestedPortable_versionedSerializer.
// Test for issue https://github.com/hazelcast/hazelcast/issues/12733
@Test
public void test_nestedPortable_versionedSerializer() {
SerializationServiceBuilder builder1 = new DefaultSerializationServiceBuilder();
builder1.setPortableVersion(6);
builder1.addPortableFactory(1, new PortableFactory() {
@Override
public Portable create(int classId) {
if (classId == 1) {
return new Parent();
} else if (classId == 2) {
return new Child();
}
return null;
}
});
SerializationService ss1 = builder1.build();
SerializationServiceBuilder builder2 = new DefaultSerializationServiceBuilder();
builder2.setPortableVersion(6);
builder2.addPortableFactory(1, new PortableFactory() {
@Override
public Portable create(int classId) {
if (classId == 1) {
return new Parent();
} else if (classId == 2) {
return new Child();
}
return null;
}
});
SerializationService ss2 = builder2.build();
// make sure ss2 cached class definition of Child
ss2.toData(new Child("sancar"));
// serialized parent from ss1
Parent parent = new Parent(new Child("sancar"));
Data data = ss1.toData(parent);
// cached class definition of Child and the class definition from data coming from ss1 should be compatible
assertEquals(parent, ss2.toObject(data));
}
Aggregations