Search in sources :

Example 1 with KryoSerializer

use of com.nokia.dempsy.serialization.kryo.KryoSerializer in project Dempsy by Dempsy.

the class TestDefaultSerializer method testMultithreadedSerialization.

@Test
public void testMultithreadedSerialization() throws Throwable {
    Thread[] threads = new Thread[numThreads];
    final Object latch = new Object();
    final AtomicBoolean done = new AtomicBoolean(false);
    final AtomicBoolean failed = new AtomicBoolean(false);
    final KryoSerializer<MockClass> ser = new KryoSerializer<MockClass>(new Registration(MockClass.class.getName(), 10));
    final AtomicBoolean[] finished = new AtomicBoolean[numThreads];
    final AtomicLong[] counts = new AtomicLong[numThreads];
    final long maxSerialize = 100000;
    try {
        for (int i = 0; i < threads.length; i++) {
            finished[i] = new AtomicBoolean(true);
            counts[i] = new AtomicLong(0);
            final int curIndex = i;
            Thread t = new Thread(new Runnable() {

                int index = curIndex;

                @Override
                public void run() {
                    try {
                        synchronized (latch) {
                            finished[index].set(false);
                            latch.wait();
                        }
                        while (!done.get()) {
                            MockClass o = new MockClass(index, "Hello:" + index);
                            byte[] data = ser.serialize(o);
                            MockClass dser = ser.deserialize(data);
                            assertEquals(o, dser);
                            counts[index].incrementAndGet();
                        }
                    } catch (Throwable th) {
                        failed.set(true);
                    } finally {
                        finished[index].set(true);
                    }
                }
            }, "Kryo-Test-Thread-" + i);
            t.setDaemon(true);
            t.start();
            threads[i] = t;
        }
        // wait until all the threads have been started.
        assertTrue(TestUtils.poll(baseTimeoutMillis, finished, new TestUtils.Condition<AtomicBoolean[]>() {

            @Override
            public boolean conditionMet(AtomicBoolean[] o) throws Throwable {
                for (int i = 0; i < numThreads; i++) if (o[i].get())
                    return false;
                return true;
            }
        }));
        Thread.sleep(10);
        synchronized (latch) {
            latch.notifyAll();
        }
        // wait until so many message have been serialized
        //  This can be slow on cloudbees servers so we're going to double the wait time.
        assertTrue(TestUtils.poll(baseTimeoutMillis * 2, counts, new TestUtils.Condition<AtomicLong[]>() {

            @Override
            public boolean conditionMet(AtomicLong[] cnts) throws Throwable {
                for (int i = 0; i < numThreads; i++) if (cnts[i].get() < maxSerialize)
                    return false;
                return true;
            }
        }));
    } finally {
        done.set(true);
    }
    for (int i = 0; i < threads.length; i++) threads[i].join(baseTimeoutMillis);
    for (int i = 0; i < threads.length; i++) assertTrue(finished[i].get());
    assertTrue(!failed.get());
}
Also used : KryoSerializer(com.nokia.dempsy.serialization.kryo.KryoSerializer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) Registration(com.nokia.dempsy.serialization.kryo.Registration) Test(org.junit.Test)

Example 2 with KryoSerializer

use of com.nokia.dempsy.serialization.kryo.KryoSerializer in project Dempsy by Dempsy.

the class TestDefaultSerializer method testKryoSerializeDeserializeWithRegister.

@Test
public void testKryoSerializeDeserializeWithRegister() throws Throwable {
    KryoSerializer<MockClass> ser1 = new KryoSerializer<MockClass>();
    runSerializer(ser1);
    KryoSerializer<MockClass> ser2 = new KryoSerializer<MockClass>(new Registration(MockClass.class.getName(), 10));
    runSerializer(ser2);
    byte[] d1 = ser1.serialize(o1);
    byte[] d2 = ser2.serialize(o1);
    assertTrue(d2.length < d1.length);
}
Also used : Registration(com.nokia.dempsy.serialization.kryo.Registration) KryoSerializer(com.nokia.dempsy.serialization.kryo.KryoSerializer) Test(org.junit.Test)

Example 3 with KryoSerializer

use of com.nokia.dempsy.serialization.kryo.KryoSerializer in project Dempsy by Dempsy.

the class TestDefaultSerializer method testChildClassSerializationWithRegistration.

@Test
public void testChildClassSerializationWithRegistration() throws Throwable {
    KryoSerializer<Object> ser = new KryoSerializer<Object>(defaultMock3Optimizer);
    JavaSerializer<Object> serJ = new JavaSerializer<Object>();
    KryoSerializer<Object> serR = new KryoSerializer<Object>(defaultMock3Optimizer, new Registration(MockClass.class.getName(), 10));
    KryoSerializer<Object> serRR = new KryoSerializer<Object>(defaultMock3Optimizer, new Registration(MockClass.class.getName(), 10), new Registration(Mock3.class.getName(), 11));
    KryoSerializer<Object> serRROb = new KryoSerializer<Object>(defaultMock3Optimizer, new Registration(MockClass.class.getName()), new Registration(Mock3.class.getName()), new Registration(UUID.class.getName()));
    Mock2 o = new Mock3(1, new MockClass(2, "Hello"));
    byte[] data = ser.serialize(o);
    byte[] dataJ = serJ.serialize(o);
    byte[] dataR = serR.serialize(o);
    byte[] dataRR = serRR.serialize(o);
    byte[] dataRROb = serRROb.serialize(o);
    assertTrue(dataJ.length > data.length);
    assertTrue(dataR.length < data.length);
    assertTrue(dataRR.length < dataR.length);
    assertTrue(dataRROb.length == dataRR.length);
    Mock2 o2 = (Mock2) ser.deserialize(data);
    assertEquals(1, o2.getInt());
    assertEquals(new MockClass(2, "Hello"), o2.getMockClass());
    assertTrue(o2 instanceof Mock3);
    assertEquals(1, ((Mock3) o2).myI);
    serRROb.deserialize(dataRROb);
}
Also used : Registration(com.nokia.dempsy.serialization.kryo.Registration) JavaSerializer(com.nokia.dempsy.serialization.java.JavaSerializer) KryoSerializer(com.nokia.dempsy.serialization.kryo.KryoSerializer) Test(org.junit.Test)

Example 4 with KryoSerializer

use of com.nokia.dempsy.serialization.kryo.KryoSerializer in project Dempsy by Dempsy.

the class TestDefaultSerializer method testEnumWithNoDefaultConstructor.

@Test
public void testEnumWithNoDefaultConstructor() throws Throwable {
    Mock4 o = new Mock4(MockEnum.BOTH);
    KryoSerializer<Mock4> ser = new KryoSerializer<Mock4>(new Registration(Mock4.class.getName()), new Registration(MockEnum.class.getName()));
    byte[] data = ser.serialize(o);
    Mock4 dser = ser.deserialize(data);
    assertTrue(o.get() == dser.get());
    assertEquals(o.get().toString(), dser.get().toString());
}
Also used : Registration(com.nokia.dempsy.serialization.kryo.Registration) KryoSerializer(com.nokia.dempsy.serialization.kryo.KryoSerializer) Test(org.junit.Test)

Example 5 with KryoSerializer

use of com.nokia.dempsy.serialization.kryo.KryoSerializer in project Dempsy by Dempsy.

the class TestDefaultSerializer method testChildClassSerializationWithRegistrationAndOptimization.

@Test
public void testChildClassSerializationWithRegistrationAndOptimization() throws Throwable {
    KryoSerializer<Object> ser = new KryoSerializer<Object>(defaultMock3Optimizer);
    JavaSerializer<Object> serJ = new JavaSerializer<Object>();
    KryoSerializer<Object> serR = new KryoSerializer<Object>(defaultMock3Optimizer, new Registration(MockClass.class.getName(), 10));
    KryoSerializer<Object> serRR = new KryoSerializer<Object>(defaultMock3Optimizer, new Registration(MockClass.class.getName(), 10), new Registration(Mock3.class.getName(), 11));
    KryoSerializer<Object> serRROb = new KryoSerializer<Object>(defaultMock3Optimizer, new Registration(MockClass.class.getName()), new Registration(Mock3.class.getName()));
    KryoSerializer<Object> serRRO = new KryoSerializer<Object>(new Registration(MockClass.class.getName(), 10), new Registration(Mock3.class.getName(), 11), new Registration(UUID.class.getName(), 12));
    serRRO.setKryoOptimizer(new KryoOptimizer() {

        @Override
        public void preRegister(Kryo kryo) {
            kryo.setRegistrationRequired(true);
            @SuppressWarnings("unchecked") FieldSerializer<MockClass> mockClassSer = (FieldSerializer<MockClass>) kryo.getSerializer(MockClass.class);
            mockClassSer.setFieldsCanBeNull(false);
            @SuppressWarnings("unchecked") FieldSerializer<Mock2> mock2Ser = (FieldSerializer<Mock2>) kryo.getSerializer(MockClass.class);
            mock2Ser.setFixedFieldTypes(true);
            mock2Ser.setFieldsCanBeNull(false);
        }

        @Override
        public void postRegister(Kryo kryo) {
            com.esotericsoftware.kryo.Registration reg = kryo.getRegistration(UUID.class);
            reg.setSerializer(uuidSerializer);
        }
    });
    Mock2 o = new Mock3(1, new MockClass(2, "Hello"));
    byte[] data = ser.serialize(o);
    byte[] dataJ = serJ.serialize(o);
    byte[] dataR = serR.serialize(o);
    byte[] dataRR = serRR.serialize(o);
    byte[] dataRROb = serRROb.serialize(o);
    byte[] dataRRO = serRRO.serialize(o);
    assertTrue(dataJ.length > data.length);
    assertTrue(dataR.length < data.length);
    assertTrue(dataRR.length < dataR.length);
    assertTrue(dataRROb.length == dataRR.length);
    assertTrue(dataRRO.length <= dataRR.length);
    Mock2 o2 = (Mock2) ser.deserialize(data);
    assertEquals(1, o2.getInt());
    assertEquals(new MockClass(2, "Hello"), o2.getMockClass());
    assertTrue(o2 instanceof Mock3);
    assertEquals(1, ((Mock3) o2).myI);
    assertEquals(o, serR.deserialize(dataR));
    assertEquals(o, serRR.deserialize(dataRR));
    assertEquals(o, serRRO.deserialize(dataRRO));
}
Also used : JavaSerializer(com.nokia.dempsy.serialization.java.JavaSerializer) KryoSerializer(com.nokia.dempsy.serialization.kryo.KryoSerializer) FieldSerializer(com.esotericsoftware.kryo.serializers.FieldSerializer) Registration(com.nokia.dempsy.serialization.kryo.Registration) KryoOptimizer(com.nokia.dempsy.serialization.kryo.KryoOptimizer) UUID(java.util.UUID) Kryo(com.esotericsoftware.kryo.Kryo) Test(org.junit.Test)

Aggregations

KryoSerializer (com.nokia.dempsy.serialization.kryo.KryoSerializer)5 Registration (com.nokia.dempsy.serialization.kryo.Registration)5 Test (org.junit.Test)5 JavaSerializer (com.nokia.dempsy.serialization.java.JavaSerializer)2 Kryo (com.esotericsoftware.kryo.Kryo)1 FieldSerializer (com.esotericsoftware.kryo.serializers.FieldSerializer)1 KryoOptimizer (com.nokia.dempsy.serialization.kryo.KryoOptimizer)1 UUID (java.util.UUID)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1