Package org.voltdb.messaging

Source Code of org.voltdb.messaging.FastSerializerTest

/* This file is part of VoltDB.
* Copyright (C) 2008-2010 VoltDB L.L.C.
*
* This file contains original code and/or modifications of original code.
* Any modifications made by VoltDB L.L.C. are licensed under the following
* terms and conditions:
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
/* Copyright (C) 2008
* Evan Jones
* Massachusetts Institute of Technology
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/

package org.voltdb.messaging;

import java.io.IOException;

import org.voltdb.messaging.FastSerializer;
import org.voltdb.utils.DBBPool;

import junit.framework.TestCase;

public class FastSerializerTest extends TestCase {
    FastSerializer heapOut;
    FastSerializer directOut;
    FastSerializer poolOut;
    DBBPool pool;
    public void setUp() {
        heapOut = new FastSerializer();
        directOut = new FastSerializer(true, true);
        pool = new DBBPool();
        poolOut = new FastSerializer(pool);
    }

    public void tearDown() {
        poolOut.getBBContainer().discard();
        pool.clear();
        pool = null;
        heapOut = null;
        directOut = null;
        poolOut = null;
        System.gc();
    }

    public void testHugeMessage() throws IOException {
        testHM(heapOut);
        testHM(directOut);
        testHM(poolOut);
    }

    private void testHM(FastSerializer fs) throws IOException {
        // 1 MB message
        byte[] huge = new byte[1024*1024];
        huge[huge.length-1] = 42;
        fs.write(huge);
        assertEquals(42, fs.getBytes()[huge.length-1]);
    }

    public void testHugeMessageBigEndian() throws IOException {
        testHMLE(heapOut);
        testHMLE(directOut);
        testHMLE(poolOut);
    }

    private void testHMLE(FastSerializer out) throws IOException {
        out = new FastSerializer(false, false);

        // 1 MB message
        byte[] huge = new byte[1024*1024];
        huge[huge.length-1] = 42;
        out.write(huge);
        out.writeInt(0x01020304);

        byte[] bytes = out.getBBContainer().b.array();
        assertEquals(0x01, bytes[huge.length]);
        assertEquals(0x02, bytes[huge.length+1]);
        assertEquals(0x03, bytes[huge.length+2]);
        assertEquals(0x04, bytes[huge.length+3]);
    }

    public void testClear() throws IOException {
        testClearP(heapOut);
        testClearP(directOut);
        testClearP(poolOut);
    }

    private void testClearP(FastSerializer out) throws IOException {
        out.writeInt(42);
        out.clear();
        out.writeInt(42);
        byte[] bytes = out.getBytes();
        assertEquals(4, bytes.length);
    }

    public void testDirect() throws IOException {
        directOut = new FastSerializer(false, true);
        assertTrue(directOut.getBBContainer().b.isDirect());
        testHugeMessage();
        // Should still be direct after resizing.
        assertTrue(directOut.getBBContainer().b.isDirect());
        directOut.getBBContainer().discard();
    }
}
TOP

Related Classes of org.voltdb.messaging.FastSerializerTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.