Imported Upstream version 1.5.1
This commit is contained in:
@@ -0,0 +1,155 @@
|
||||
/*
|
||||
* Copyright (c) 2014 by Michael Berlin, Zuse Institute Berlin
|
||||
*
|
||||
* Licensed under the BSD License, see LICENSE file for details.
|
||||
*
|
||||
*/
|
||||
|
||||
package org.xtreemfs.foundation.buffer;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Tests different sequences of {@link BufferPool#allocate(int)} and {@link BufferPool#free(ReusableBuffer)}
|
||||
* operations, some also regarding view buffers.
|
||||
*
|
||||
* Please note that {@link BufferPool} is a singleton and therefore the pool size increases as the number of
|
||||
* run tests does. Therefore, each test has to evaluate changes in the pool size relative to the pool size at
|
||||
* the start of test.
|
||||
*
|
||||
*/
|
||||
public class BufferPoolTest {
|
||||
public static final int TEST_BUFFER_SIZE = 8192;
|
||||
|
||||
@Test
|
||||
public final void testSimpleAllocateAndFree() {
|
||||
ReusableBuffer buf = null;
|
||||
// There may be already a buffer pooled. If not, the pool size will stay 0 after an allocate().
|
||||
int currentPoolSize = Math.max(0, BufferPool.getPoolSize(TEST_BUFFER_SIZE) - 1);
|
||||
|
||||
buf = BufferPool.allocate(TEST_BUFFER_SIZE);
|
||||
assertEquals("BufferPool must be empty because a buffer was only allocated but not returned so far.",
|
||||
currentPoolSize,
|
||||
BufferPool.getPoolSize(TEST_BUFFER_SIZE));
|
||||
|
||||
BufferPool.free(buf);
|
||||
assertEquals("One buffer must have been returned and pooled.", currentPoolSize + 1,
|
||||
BufferPool.getPoolSize(TEST_BUFFER_SIZE));
|
||||
|
||||
buf = BufferPool.allocate(TEST_BUFFER_SIZE);
|
||||
assertEquals("Pooled buffer must have been re-allocated.", currentPoolSize,
|
||||
BufferPool.getPoolSize(TEST_BUFFER_SIZE));
|
||||
|
||||
BufferPool.free(buf);
|
||||
assertEquals("One buffer must have been returned and pooled again.", currentPoolSize + 1,
|
||||
BufferPool.getPoolSize(TEST_BUFFER_SIZE));
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void testReusableViewBuffers() {
|
||||
ReusableBuffer buf = null;
|
||||
// There may be already a buffer pooled. If not, the pool size will stay 0 after an allocate().
|
||||
int currentPoolSize = Math.max(0, BufferPool.getPoolSize(TEST_BUFFER_SIZE) - 1);
|
||||
|
||||
buf = BufferPool.allocate(TEST_BUFFER_SIZE);
|
||||
assertEquals(currentPoolSize, BufferPool.getPoolSize(TEST_BUFFER_SIZE));
|
||||
|
||||
BufferPool.free(buf);
|
||||
assertEquals(currentPoolSize + 1, BufferPool.getPoolSize(TEST_BUFFER_SIZE));
|
||||
|
||||
buf = BufferPool.allocate(TEST_BUFFER_SIZE);
|
||||
assertEquals(currentPoolSize, BufferPool.getPoolSize(TEST_BUFFER_SIZE));
|
||||
ReusableBuffer viewBuffer = buf.createViewBuffer();
|
||||
|
||||
BufferPool.free(viewBuffer);
|
||||
assertEquals("Buffer not returned to pool yet since one reference is left.", currentPoolSize,
|
||||
BufferPool.getPoolSize(TEST_BUFFER_SIZE));
|
||||
BufferPool.free(buf);
|
||||
assertEquals("Buffer must have been returned to pool since no reference is left.", currentPoolSize + 1,
|
||||
BufferPool.getPoolSize(TEST_BUFFER_SIZE));
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void testReusableViewBuffersOfReusableViewBuffers() {
|
||||
ReusableBuffer buf = null;
|
||||
// There may be already a buffer pooled. If not, the pool size will stay 0 after an allocate().
|
||||
int currentPoolSize = Math.max(0, BufferPool.getPoolSize(TEST_BUFFER_SIZE) - 1);
|
||||
|
||||
buf = BufferPool.allocate(TEST_BUFFER_SIZE);
|
||||
assertEquals(currentPoolSize, BufferPool.getPoolSize(TEST_BUFFER_SIZE));
|
||||
|
||||
BufferPool.free(buf);
|
||||
assertEquals(currentPoolSize + 1, BufferPool.getPoolSize(TEST_BUFFER_SIZE));
|
||||
|
||||
buf = BufferPool.allocate(TEST_BUFFER_SIZE);
|
||||
assertEquals(currentPoolSize, BufferPool.getPoolSize(TEST_BUFFER_SIZE));
|
||||
ReusableBuffer viewBuffer = buf.createViewBuffer();
|
||||
// Create a view buffer of a view buffer.
|
||||
ReusableBuffer viewBuffer2 = viewBuffer.createViewBuffer();
|
||||
|
||||
BufferPool.free(viewBuffer2);
|
||||
assertEquals("Buffer not returned to pool yet since two references are left.", currentPoolSize,
|
||||
BufferPool.getPoolSize(TEST_BUFFER_SIZE));
|
||||
BufferPool.free(viewBuffer);
|
||||
assertEquals("Buffer not returned to pool yet since one reference is left.", currentPoolSize,
|
||||
BufferPool.getPoolSize(TEST_BUFFER_SIZE));
|
||||
BufferPool.free(buf);
|
||||
assertEquals("Buffer must have been returned to pool since no reference is left.", currentPoolSize + 1,
|
||||
BufferPool.getPoolSize(TEST_BUFFER_SIZE));
|
||||
}
|
||||
|
||||
private void assertThatAssertionsAreEnabled() {
|
||||
boolean assertOn = false;
|
||||
// *assigns* true if assertions are on.
|
||||
assert assertOn = true;
|
||||
assertTrue("Enable assertions or this test won't work correctly.", assertOn);
|
||||
}
|
||||
|
||||
@Test(expected = AssertionError.class)
|
||||
public final void testDoubleFreeThrows() {
|
||||
assertThatAssertionsAreEnabled();
|
||||
|
||||
ReusableBuffer buf = null;
|
||||
// There may be already a buffer pooled. If not, the pool size will stay 0 after an allocate().
|
||||
int currentPoolSize = Math.max(0, BufferPool.getPoolSize(TEST_BUFFER_SIZE) - 1);
|
||||
|
||||
buf = BufferPool.allocate(TEST_BUFFER_SIZE);
|
||||
assertEquals(currentPoolSize, BufferPool.getPoolSize(TEST_BUFFER_SIZE));
|
||||
|
||||
BufferPool.free(buf);
|
||||
assertEquals(currentPoolSize + 1, BufferPool.getPoolSize(TEST_BUFFER_SIZE));
|
||||
|
||||
// Double free will trigger assertion.
|
||||
BufferPool.free(buf);
|
||||
}
|
||||
|
||||
@Test(expected = AssertionError.class)
|
||||
public final void testDoubleFreeOfRecursiveViewBuffersThrows() {
|
||||
assertThatAssertionsAreEnabled();
|
||||
|
||||
ReusableBuffer buf = null;
|
||||
// There may be already a buffer pooled. If not, the pool size will stay 0 after an allocate().
|
||||
int currentPoolSize = Math.max(0, BufferPool.getPoolSize(TEST_BUFFER_SIZE) - 1);
|
||||
|
||||
buf = BufferPool.allocate(TEST_BUFFER_SIZE);
|
||||
assertEquals(currentPoolSize, BufferPool.getPoolSize(TEST_BUFFER_SIZE));
|
||||
|
||||
BufferPool.free(buf);
|
||||
assertEquals(currentPoolSize + 1, BufferPool.getPoolSize(TEST_BUFFER_SIZE));
|
||||
|
||||
buf = BufferPool.allocate(TEST_BUFFER_SIZE);
|
||||
assertEquals(currentPoolSize, BufferPool.getPoolSize(TEST_BUFFER_SIZE));
|
||||
ReusableBuffer viewBuffer = buf.createViewBuffer();
|
||||
// Create a view buffer of a view buffer.
|
||||
ReusableBuffer viewBuffer2 = viewBuffer.createViewBuffer();
|
||||
|
||||
BufferPool.free(viewBuffer2);
|
||||
BufferPool.free(viewBuffer);
|
||||
BufferPool.free(buf);
|
||||
|
||||
// Double free will trigger assertion.
|
||||
BufferPool.free(viewBuffer2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,176 @@
|
||||
/*
|
||||
* Copyright (c) 2010 by Bjoern Kolbeck, Zuse Institute Berlin
|
||||
* 2014 by Michael Berlin, Zuse Institute Berlin
|
||||
*
|
||||
* Licensed under the BSD License, see LICENSE file for details.
|
||||
*
|
||||
*/
|
||||
|
||||
package org.xtreemfs.foundation.buffer;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class ReusableBufferTest {
|
||||
|
||||
@Test
|
||||
public final void testArray() {
|
||||
ReusableBuffer rb = ReusableBuffer.wrap("Yagga Yagga".getBytes());
|
||||
ReusableBuffer vb = rb.createViewBuffer();
|
||||
|
||||
vb.position(0);
|
||||
vb.limit(5);
|
||||
String result = new String(vb.array());
|
||||
|
||||
assertEquals("Yagga", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void testRecursiveNonResuableViewBuffer() {
|
||||
ReusableBuffer rb = ReusableBuffer.wrap("Yagga Yagga".getBytes());
|
||||
assertFalse(rb.isReusable());
|
||||
|
||||
ReusableBuffer viewBuffer = rb.createViewBuffer();
|
||||
assertFalse(viewBuffer.isReusable());
|
||||
|
||||
ReusableBuffer viewBuffer2 = viewBuffer.createViewBuffer();
|
||||
assertFalse(viewBuffer2.isReusable());
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void testNonResuableViewBufferHasIndependentPositions() {
|
||||
ReusableBuffer rb = ReusableBuffer.wrap("Yagga Yagga".getBytes());
|
||||
assertFalse(rb.isReusable());
|
||||
rb.position(0);
|
||||
rb.limit(5);
|
||||
|
||||
ReusableBuffer viewBuffer = rb.createViewBuffer();
|
||||
assertFalse(viewBuffer.isReusable());
|
||||
viewBuffer.position(1);
|
||||
viewBuffer.limit(4);
|
||||
|
||||
assertEquals(0, rb.position());
|
||||
assertEquals(5, rb.limit());
|
||||
assertEquals(1, viewBuffer.position());
|
||||
assertEquals(4, viewBuffer.limit());
|
||||
|
||||
ReusableBuffer viewBufferOfViewBuffer = viewBuffer.createViewBuffer();
|
||||
assertFalse(viewBufferOfViewBuffer.isReusable());
|
||||
viewBufferOfViewBuffer.position(2);
|
||||
viewBufferOfViewBuffer.limit(3);
|
||||
|
||||
assertEquals(0, rb.position());
|
||||
assertEquals(5, rb.limit());
|
||||
assertEquals(1, viewBuffer.position());
|
||||
assertEquals(4, viewBuffer.limit());
|
||||
assertEquals(2, viewBufferOfViewBuffer.position());
|
||||
assertEquals(3, viewBufferOfViewBuffer.limit());
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void testRange() {
|
||||
// Test with a view buffer and with a reusable buffer.
|
||||
ReusableBuffer[] buffers = new ReusableBuffer[] { ReusableBuffer.wrap("Yagga Yagga".getBytes()),
|
||||
BufferPool.allocate(12) };
|
||||
for (ReusableBuffer buf : buffers) {
|
||||
buf.range(1, 4);
|
||||
assertEquals(0, buf.position());
|
||||
assertEquals(4, buf.limit());
|
||||
assertEquals(4, buf.capacity());
|
||||
assertEquals(4, buf.remaining());
|
||||
|
||||
// Now create a view buffer which will have a different range.
|
||||
ReusableBuffer viewBuf = buf.createViewBuffer();
|
||||
viewBuf.range(2, 2);
|
||||
assertEquals(0, viewBuf.position());
|
||||
assertEquals(2, viewBuf.limit());
|
||||
assertEquals(2, viewBuf.capacity());
|
||||
assertEquals(2, viewBuf.remaining());
|
||||
|
||||
// The range of the original buffer was not affected.
|
||||
assertEquals(0, buf.position());
|
||||
assertEquals(4, buf.limit());
|
||||
assertEquals(4, buf.capacity());
|
||||
assertEquals(4, buf.remaining());
|
||||
|
||||
if (buf.isReusable()) {
|
||||
BufferPool.free(buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void testShrink() {
|
||||
// Test with a view buffer and with a reusable buffer.
|
||||
ReusableBuffer[] buffers = new ReusableBuffer[] { ReusableBuffer.wrap("Yagga Yagga".getBytes()),
|
||||
BufferPool.allocate(12) };
|
||||
for (ReusableBuffer buf : buffers) {
|
||||
buf.shrink(4);
|
||||
assertEquals(0, buf.position());
|
||||
assertEquals(4, buf.limit());
|
||||
assertEquals(4, buf.capacity());
|
||||
assertEquals(4, buf.remaining());
|
||||
|
||||
// Now create a view buffer which will have a different range.
|
||||
ReusableBuffer viewBuf = buf.createViewBuffer();
|
||||
viewBuf.shrink(2);
|
||||
assertEquals(0, viewBuf.position());
|
||||
assertEquals(2, viewBuf.limit());
|
||||
assertEquals(2, viewBuf.capacity());
|
||||
assertEquals(2, viewBuf.remaining());
|
||||
|
||||
// The range of the original buffer was not affected.
|
||||
assertEquals(0, buf.position());
|
||||
assertEquals(4, buf.limit());
|
||||
assertEquals(4, buf.capacity());
|
||||
assertEquals(4, buf.remaining());
|
||||
|
||||
if (buf.isReusable()) {
|
||||
BufferPool.free(buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void testEnlarge() {
|
||||
// Test with a view buffer and with a reusable buffer.
|
||||
ReusableBuffer[] buffers = new ReusableBuffer[] { ReusableBuffer.wrap("Yagga Yagga".getBytes()),
|
||||
BufferPool.allocate(11) };
|
||||
for (ReusableBuffer buf : buffers) {
|
||||
int originalBufSize = buf.capacity();
|
||||
|
||||
// When there is no underlying, larger buffer, enlarge won't have an effect (the capacity of the
|
||||
// underlying buffer can be higher than the current buffer size.)
|
||||
assertFalse(buf.enlarge(buf.capacityUnderlying() + 1));
|
||||
// Enlarging to the current size should always work.
|
||||
assertTrue(buf.enlarge(originalBufSize));
|
||||
assertEquals(originalBufSize, buf.capacity());
|
||||
|
||||
// Create a view buffer first which is smaller than the original one.
|
||||
buf.limit(originalBufSize / 2);
|
||||
ReusableBuffer smallerBuf = buf.createViewBuffer();
|
||||
|
||||
// Enlarge it, but not larger than the underlying buffer.
|
||||
int newBufSize = smallerBuf.capacity() * 2;
|
||||
assertTrue(smallerBuf.enlarge(newBufSize));
|
||||
assertEquals(0, buf.position());
|
||||
assertEquals(newBufSize, smallerBuf.limit());
|
||||
assertEquals(newBufSize, smallerBuf.capacity());
|
||||
assertEquals(newBufSize, smallerBuf.remaining());
|
||||
|
||||
// You cannot enlarge it over the capacity of the original buffer.
|
||||
assertFalse(smallerBuf.enlarge(buf.capacityUnderlying() + 1));
|
||||
|
||||
// The range of the original buffer was not affected.
|
||||
assertEquals(0, buf.position());
|
||||
assertEquals(originalBufSize / 2, buf.limit());
|
||||
assertEquals(originalBufSize, buf.capacity());
|
||||
assertEquals(originalBufSize / 2, buf.remaining());
|
||||
|
||||
if (buf.isReusable()) {
|
||||
BufferPool.free(buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,261 @@
|
||||
/*
|
||||
* Copyright (c) 2008-2010 by Christian Lorenz,
|
||||
* Zuse Institute Berlin
|
||||
*
|
||||
* Licensed under the BSD License, see LICENSE file for details.
|
||||
*
|
||||
*/
|
||||
|
||||
package org.xtreemfs.test.foundation.checksums;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.zip.Adler32;
|
||||
import java.util.zip.Checksum;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.xtreemfs.foundation.checksums.ChecksumAlgorithm;
|
||||
import org.xtreemfs.foundation.checksums.ChecksumFactory;
|
||||
import org.xtreemfs.foundation.checksums.ChecksumProvider;
|
||||
import org.xtreemfs.foundation.checksums.provider.JavaChecksumProvider;
|
||||
import org.xtreemfs.foundation.logging.Logging;
|
||||
|
||||
/**
|
||||
* tests the checksum factory and some checksums
|
||||
*
|
||||
* 19.08.2008
|
||||
*
|
||||
* @author clorenz
|
||||
*/
|
||||
public class ChecksumFactoryTest {
|
||||
private ChecksumFactory factory;
|
||||
private ByteBuffer data;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
Logging.start(Logging.LEVEL_ERROR);
|
||||
|
||||
this.factory = ChecksumFactory.getInstance();
|
||||
|
||||
ChecksumProvider provider = new JavaChecksumProvider();
|
||||
this.factory.addProvider(provider);
|
||||
|
||||
this.data = ByteBuffer.wrap(generateRandomBytes(1024 * 128));
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
}
|
||||
|
||||
/**
|
||||
* generates randomly filled byte-array
|
||||
*
|
||||
* @param length
|
||||
* of the byte-array
|
||||
*/
|
||||
public static byte[] generateRandomBytes(int length) {
|
||||
Random r = new Random();
|
||||
byte[] bytes = new byte[length];
|
||||
|
||||
r.nextBytes(bytes);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* tests the internal java checksum algorithms
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
@Test
|
||||
public void testJavaChecksumAlgorithm() throws Exception {
|
||||
// compute checksum with xtreemfs ChecksumFactory
|
||||
long xtreemfsValue = computeXtreemfsChecksum("Adler32", true);
|
||||
|
||||
// compute checksum with java API
|
||||
Checksum javaAlgorithm = new Adler32();
|
||||
javaAlgorithm.update(data.array(), 0, data.array().length);
|
||||
long javaValue = javaAlgorithm.getValue();
|
||||
|
||||
// System.out.println(javaValue);
|
||||
// System.out.println(xtreemfsValue);
|
||||
|
||||
assertEquals(javaValue, xtreemfsValue);
|
||||
}
|
||||
|
||||
// /**
|
||||
// * tests the internal java message digest algorithms
|
||||
// * @throws Exception
|
||||
// */
|
||||
// public void testJavaMessageDigestAlgorithm() throws Exception {
|
||||
// // compute checksum with xtreemfs ChecksumFactory
|
||||
// String xtreemfsValue = computeXtreemfsChecksum("MD5", true);
|
||||
//
|
||||
// // compute checksum with java API
|
||||
// String javaValue = computeJavaMessageDigest("MD5");
|
||||
//
|
||||
// // System.out.println("java: "+xtreemfsValue);
|
||||
// // System.out.println("xtreemfs: "+javaValue.toString());
|
||||
//
|
||||
// assertEquals(javaValue.toString(), xtreemfsValue);
|
||||
// }
|
||||
|
||||
/**
|
||||
* @param algorithm
|
||||
* @param returnAlgorithm
|
||||
* @return
|
||||
* @throws NoSuchAlgorithmException
|
||||
*/
|
||||
private long computeXtreemfsChecksum(String algorithm, boolean returnAlgorithm) throws NoSuchAlgorithmException {
|
||||
// compute checksum with xtreemfs ChecksumFactory
|
||||
ChecksumAlgorithm xtreemfsAlgorithm = factory.getAlgorithm(algorithm);
|
||||
xtreemfsAlgorithm.update(data);
|
||||
long xtreemfsValue = xtreemfsAlgorithm.getValue();
|
||||
if (returnAlgorithm)
|
||||
this.factory.returnAlgorithm(xtreemfsAlgorithm);
|
||||
return xtreemfsValue;
|
||||
}
|
||||
|
||||
private long computeJavaCheckSum(String algorithm) throws NoSuchAlgorithmException {
|
||||
// compute checksum with java API
|
||||
Adler32 adler = new Adler32();
|
||||
adler.update(data.array());
|
||||
return adler.getValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* tests, if the internal buffer of the checksums is working correctly, if the checksum is used more than once
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
@Test
|
||||
public void testIfChecksumIsAlwaysTheSame() throws Exception {
|
||||
ChecksumAlgorithm algorithm = factory.getAlgorithm("Adler32");
|
||||
algorithm.update(data);
|
||||
long oldValue = algorithm.getValue();
|
||||
|
||||
for (int i = 0; i < 32; i++) {
|
||||
algorithm.update(data);
|
||||
long newValue = algorithm.getValue();
|
||||
|
||||
assertEquals(oldValue, newValue);
|
||||
oldValue = newValue;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* tests, if the ChecksumFactory delivers only "thread-safe" instances (cache-pool)
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
@Test
|
||||
public void testThreadSafety() throws Exception {
|
||||
final int THREADS = 8;
|
||||
this.data = ByteBuffer.wrap(generateRandomBytes(1024 * 1024 * 32));
|
||||
|
||||
// compute correct checksum with java API
|
||||
Long javaValue = computeJavaCheckSum("Adler32");
|
||||
|
||||
Callable<Long> computation = new Callable<Long>() {
|
||||
@Override
|
||||
public Long call() {
|
||||
try {
|
||||
// compute checksum with xtreemfs ChecksumFactory
|
||||
long xtreemfsValue = computeXtreemfsChecksum("Adler32", true);
|
||||
return xtreemfsValue;
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
e.printStackTrace();
|
||||
return 0l;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return 0l;
|
||||
}
|
||||
}
|
||||
};
|
||||
LinkedList<Future<Long>> results = useMultipleThreads(THREADS, computation);
|
||||
|
||||
// compare correct java checksum with xtreemfs checksums
|
||||
for (Future<Long> result : results) {
|
||||
assertEquals(javaValue, result.get());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* tests, if the ChecksumFactory cache-pool works correctly
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
@Test
|
||||
public void testChecksumFactoryCache() throws Exception {
|
||||
// FIXME: use bigger values for more comprehensive testing, but this will slow down the test
|
||||
final int THREADS = 8;
|
||||
final int ROUNDS = 50;
|
||||
this.data = ByteBuffer.wrap(generateRandomBytes(1024 * 1024));
|
||||
|
||||
// compute correct checksum with java API
|
||||
Long javaValue = computeJavaCheckSum("Adler32");
|
||||
|
||||
Callable<LinkedList<Long>> computation = new Callable<LinkedList<Long>>() {
|
||||
@Override
|
||||
public LinkedList<Long> call() {
|
||||
try {
|
||||
LinkedList<Long> values = new LinkedList<Long>();
|
||||
boolean returning = false;
|
||||
for (int i = 0; i < ROUNDS; i++) {
|
||||
// compute checksum with xtreemfs ChecksumFactory
|
||||
long xtreemfsValue = computeXtreemfsChecksum("Adler32", returning);
|
||||
values.add(xtreemfsValue);
|
||||
returning = !returning;
|
||||
}
|
||||
return values;
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
};
|
||||
LinkedList<Future<LinkedList<Long>>> results = useMultipleThreads(THREADS, computation);
|
||||
|
||||
// compare correct java checksum with xtreemfs checksums
|
||||
for (Future<LinkedList<Long>> result : results) {
|
||||
for (Long value : result.get()) {
|
||||
assertEquals(javaValue, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* executes a given computation in a couple of threads and returns the results of the computations
|
||||
*
|
||||
* @param THREADS
|
||||
* @param computation
|
||||
* @return a list of futures, which contain the results of the computations
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
private <E> LinkedList<Future<E>> useMultipleThreads(final int THREADS, Callable<E> computation)
|
||||
throws InterruptedException {
|
||||
LinkedList<Future<E>> results = new LinkedList<Future<E>>();
|
||||
// compute xtreemfs checksums with multiple threads
|
||||
ExecutorService executor = Executors.newFixedThreadPool(THREADS);
|
||||
for (int i = 0; i < THREADS; i++) {
|
||||
Future<E> tmp = executor.submit(computation);
|
||||
results.add(tmp);
|
||||
}
|
||||
executor.shutdown();
|
||||
executor.awaitTermination(60, TimeUnit.SECONDS);
|
||||
return results;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright (c) 2008-2010 by Christian Lorenz,
|
||||
* Zuse Institute Berlin
|
||||
*
|
||||
* Licensed under the BSD License, see LICENSE file for details.
|
||||
*
|
||||
*/
|
||||
|
||||
package org.xtreemfs.test.foundation.checksums;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.xtreemfs.foundation.checksums.StringChecksumAlgorithm;
|
||||
import org.xtreemfs.foundation.checksums.algorithms.SDBM;
|
||||
import org.xtreemfs.foundation.logging.Logging;
|
||||
|
||||
/**
|
||||
* some tests for the checksum algorithms, which are based on strings
|
||||
*
|
||||
* 02.09.2008
|
||||
*
|
||||
* @author clorenz
|
||||
*/
|
||||
public class StringChecksumAlgorithmTest {
|
||||
private ByteBuffer bufferData;
|
||||
private String stringData;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
Logging.start(Logging.LEVEL_ERROR);
|
||||
|
||||
this.stringData = "";
|
||||
for (int i = 0; i < 1024; i++) {
|
||||
this.stringData += "Test, ";
|
||||
}
|
||||
this.bufferData = ByteBuffer.wrap(stringData.getBytes());
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
}
|
||||
|
||||
/**
|
||||
* tests, if the SDBM algorithm generates the same checksum with a String-input and ByteBuffer-input
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
@Test
|
||||
public void testSDBMStringBufferEquality() throws Exception {
|
||||
// compute checksum with xtreemfs ChecksumFactory
|
||||
StringChecksumAlgorithm algorithm = new SDBM();
|
||||
|
||||
// string
|
||||
algorithm.digest(stringData);
|
||||
long stringValue = algorithm.getValue();
|
||||
|
||||
// buffer
|
||||
algorithm.update(bufferData);
|
||||
long bufferValue = algorithm.getValue();
|
||||
|
||||
// System.out.println(stringValue);
|
||||
// System.out.println(bufferValue);
|
||||
|
||||
assertEquals(stringValue, bufferValue);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,363 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2011 by Bjoern Kolbeck,
|
||||
* Zuse Institute Berlin
|
||||
*
|
||||
* Licensed under the BSD License, see LICENSE file for details.
|
||||
*
|
||||
*/
|
||||
|
||||
package org.xtreemfs.test.foundation.pbrpc;
|
||||
|
||||
import org.xtreemfs.foundation.buffer.ReusableBuffer;
|
||||
import org.junit.Test;
|
||||
import org.xtreemfs.foundation.pbrpc.Schemes;
|
||||
import java.net.InetSocketAddress;
|
||||
import org.xtreemfs.foundation.pbrpc.client.RPCAuthentication;
|
||||
import org.xtreemfs.foundation.pbrpc.client.RPCNIOSocketClient;
|
||||
import org.xtreemfs.foundation.pbrpc.client.RPCResponse;
|
||||
import org.xtreemfs.foundation.pbrpc.generatedinterfaces.Ping.PingResponse;
|
||||
import org.xtreemfs.foundation.pbrpc.generatedinterfaces.PingServiceClient;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.xtreemfs.foundation.SSLOptions;
|
||||
import org.xtreemfs.foundation.TimeSync;
|
||||
import org.xtreemfs.foundation.logging.Logging;
|
||||
import org.xtreemfs.foundation.pbrpc.client.PBRPCException;
|
||||
import org.xtreemfs.foundation.pbrpc.generatedinterfaces.Ping;
|
||||
import org.xtreemfs.foundation.pbrpc.generatedinterfaces.Ping.PingRequest;
|
||||
import org.xtreemfs.foundation.pbrpc.generatedinterfaces.RPC;
|
||||
import org.xtreemfs.foundation.pbrpc.server.RPCNIOSocketServer;
|
||||
import org.xtreemfs.foundation.pbrpc.server.RPCServerRequest;
|
||||
import org.xtreemfs.foundation.pbrpc.server.RPCServerRequestListener;
|
||||
import org.xtreemfs.foundation.pbrpc.utils.ReusableBufferInputStream;
|
||||
import org.xtreemfs.foundation.util.OutputUtils;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author bjko
|
||||
*/
|
||||
public class PBRPCClientServerTest {
|
||||
private final int TEST_PORT = 12999;
|
||||
|
||||
private static final String[] schemes = new String[]{Schemes.SCHEME_PBRPC, Schemes.SCHEME_PBRPCS, Schemes.SCHEME_PBRPCG};
|
||||
|
||||
private static TimeSync ts = null;
|
||||
|
||||
public PBRPCClientServerTest() {
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpClass() throws Exception {
|
||||
Logging.start(Logging.LEVEL_WARN, Logging.Category.all);
|
||||
ts = TimeSync.initializeLocal(50);
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void tearDownClass() throws Exception {
|
||||
ts.close();
|
||||
}
|
||||
|
||||
// TODO add test methods here.
|
||||
// The methods must be annotated with annotation @Test. For example:
|
||||
//
|
||||
// @Test
|
||||
// public void hello() {}\
|
||||
@Test
|
||||
public void testRegularRPC() throws Exception {
|
||||
ResponseCreator creator = new ResponseCreator() {
|
||||
@Override
|
||||
public void answer(RPCServerRequest rq, PingRequest pRq) throws Exception {
|
||||
Ping.PingResponse.PingResult result = Ping.PingResponse.PingResult.newBuilder().setText(pRq.getText()).build();
|
||||
Ping.PingResponse resp = Ping.PingResponse.newBuilder().setResult(result).build();
|
||||
|
||||
rq.sendResponse(resp, null);
|
||||
}
|
||||
};
|
||||
|
||||
TestExecutor exec = new TestExecutor() {
|
||||
|
||||
@Override
|
||||
public void execTest(RPCNIOSocketClient client) throws Exception {
|
||||
PingServiceClient psClient = new PingServiceClient(client,null);
|
||||
|
||||
RPC.UserCredentials userCred = RPC.UserCredentials.newBuilder().setUsername("test").addGroups("tester").build();
|
||||
RPCResponse<PingResponse> response = psClient.doPing(new InetSocketAddress("localhost", TEST_PORT), RPCAuthentication.authNone, userCred, "Hello World!", false, null);
|
||||
assertEquals(response.get().getResult().getText(),"Hello World!");
|
||||
response.freeBuffers();
|
||||
|
||||
response = psClient.doPing(new InetSocketAddress("localhost", TEST_PORT), RPCAuthentication.authNone, userCred, "Murpel", false, null);
|
||||
assertEquals(response.get().getResult().getText(),"Murpel");
|
||||
response.freeBuffers();
|
||||
}
|
||||
};
|
||||
for (String scheme: schemes)
|
||||
runTest(scheme, creator, exec);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testErrorResponse() throws Exception {
|
||||
ResponseCreator creator = new ResponseCreator() {
|
||||
@Override
|
||||
public void answer(RPCServerRequest rq, PingRequest pRq) throws Exception {
|
||||
rq.sendError(RPC.ErrorType.ERRNO, RPC.POSIXErrno.POSIX_ERROR_EIO, "YaggYagga");
|
||||
}
|
||||
};
|
||||
|
||||
TestExecutor exec = new TestExecutor() {
|
||||
|
||||
@Override
|
||||
public void execTest(RPCNIOSocketClient client) throws Exception {
|
||||
PingServiceClient psClient = new PingServiceClient(client,null);
|
||||
|
||||
RPC.UserCredentials userCred = RPC.UserCredentials.newBuilder().setUsername("test").addGroups("tester").build();
|
||||
RPCResponse<PingResponse> response = psClient.doPing(new InetSocketAddress("localhost", TEST_PORT), RPCAuthentication.authNone, userCred, "Hello World!", false, null);
|
||||
|
||||
try {
|
||||
response.get();
|
||||
fail("expected error response");
|
||||
} catch (PBRPCException ex) {
|
||||
assertEquals(ex.getErrorType(),RPC.ErrorType.ERRNO);
|
||||
assertEquals(ex.getPOSIXErrno(),RPC.POSIXErrno.POSIX_ERROR_EIO);
|
||||
}
|
||||
response.freeBuffers();
|
||||
|
||||
|
||||
}
|
||||
};
|
||||
for (String scheme: schemes)
|
||||
runTest(scheme, creator, exec);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInternalServerError() throws Exception {
|
||||
ResponseCreator creator = new ResponseCreator() {
|
||||
@Override
|
||||
public void answer(RPCServerRequest rq, PingRequest pRq) throws Exception {
|
||||
rq.sendRedirect("redirtome");
|
||||
}
|
||||
};
|
||||
|
||||
TestExecutor exec = new TestExecutor() {
|
||||
|
||||
@Override
|
||||
public void execTest(RPCNIOSocketClient client) throws Exception {
|
||||
PingServiceClient psClient = new PingServiceClient(client,null);
|
||||
|
||||
RPC.UserCredentials userCred = RPC.UserCredentials.newBuilder().setUsername("test").addGroups("tester").build();
|
||||
RPCResponse<PingResponse> response = psClient.doPing(new InetSocketAddress("localhost", TEST_PORT), RPCAuthentication.authNone, userCred, "Hello World!", false, null);
|
||||
|
||||
try {
|
||||
response.get();
|
||||
fail("expected error response");
|
||||
} catch (PBRPCException ex) {
|
||||
assertEquals(ex.getErrorType(),RPC.ErrorType.REDIRECT);
|
||||
assertEquals(ex.getRedirectToServerUUID(),"redirtome");
|
||||
}
|
||||
response.freeBuffers();
|
||||
|
||||
|
||||
}
|
||||
};
|
||||
for (String scheme: schemes)
|
||||
runTest(scheme, creator, exec);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDataPing() throws Exception {
|
||||
ResponseCreator creator = new ResponseCreator() {
|
||||
@Override
|
||||
public void answer(RPCServerRequest rq, PingRequest pRq) throws Exception {
|
||||
Ping.PingResponse.PingResult result = Ping.PingResponse.PingResult.newBuilder().setText(pRq.getText()).build();
|
||||
Ping.PingResponse resp = Ping.PingResponse.newBuilder().setResult(result).build();
|
||||
|
||||
ReusableBuffer data = null;
|
||||
if (rq.getData() != null) {
|
||||
data = rq.getData().createViewBuffer();
|
||||
data.limit(data.capacity());
|
||||
data.position(data.capacity());
|
||||
}
|
||||
|
||||
rq.sendResponse(resp, data);
|
||||
}
|
||||
};
|
||||
|
||||
TestExecutor exec = new TestExecutor() {
|
||||
|
||||
@Override
|
||||
public void execTest(RPCNIOSocketClient client) throws Exception {
|
||||
PingServiceClient psClient = new PingServiceClient(client,null);
|
||||
byte[] arr = new byte[2065];
|
||||
for (int i= 0; i < arr.length; i++)
|
||||
arr[i] = 'x';
|
||||
ReusableBuffer sendData = ReusableBuffer.wrap(arr);
|
||||
// System.out.println("data: "+sendData);
|
||||
|
||||
RPC.UserCredentials userCred = RPC.UserCredentials.newBuilder().setUsername("test").addGroups("tester").build();
|
||||
RPCResponse<PingResponse> response = psClient.doPing(new InetSocketAddress("localhost", TEST_PORT), RPCAuthentication.authNone, userCred, "Hello World!", false, sendData);
|
||||
assertEquals(response.get().getResult().getText(),"Hello World!");
|
||||
|
||||
ReusableBuffer recdata = response.getData();
|
||||
assertTrue(recdata.hasRemaining());
|
||||
while (recdata.hasRemaining()) {
|
||||
assertEquals(recdata.get(),(byte)'x');
|
||||
}
|
||||
response.freeBuffers();
|
||||
|
||||
}
|
||||
};
|
||||
for (String scheme: schemes)
|
||||
runTest(scheme, creator, exec);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTimeout() throws Exception {
|
||||
ResponseCreator creator = new ResponseCreator() {
|
||||
@Override
|
||||
public void answer(RPCServerRequest rq, PingRequest pRq) throws Exception {
|
||||
//don't do anything
|
||||
}
|
||||
};
|
||||
|
||||
TestExecutor exec = new TestExecutor() {
|
||||
|
||||
@Override
|
||||
public void execTest(RPCNIOSocketClient client) throws Exception {
|
||||
PingServiceClient psClient = new PingServiceClient(client,null);
|
||||
|
||||
RPC.UserCredentials userCred = RPC.UserCredentials.newBuilder().setUsername("test").addGroups("tester").build();
|
||||
RPCResponse<PingResponse> response = psClient.doPing(new InetSocketAddress("localhost", TEST_PORT), RPCAuthentication.authNone, userCred, "Hello World!", false, null);
|
||||
|
||||
try {
|
||||
response.get();
|
||||
fail("expected error response");
|
||||
} catch (IOException ex) {
|
||||
}
|
||||
response.freeBuffers();
|
||||
|
||||
|
||||
}
|
||||
};
|
||||
for (String scheme: schemes)
|
||||
runTest(scheme, creator, exec);
|
||||
|
||||
}
|
||||
|
||||
public void runTest(String pbrpcScheme, ResponseCreator creator, TestExecutor exec) throws Exception {
|
||||
RPCNIOSocketClient client = null;
|
||||
RPCNIOSocketServer server = null;
|
||||
|
||||
// System.out.println("loading ssl context");
|
||||
|
||||
SSLOptions srvSSL = null;
|
||||
SSLOptions clientSSL = null;
|
||||
if (pbrpcScheme.equals(Schemes.SCHEME_PBRPCS) || pbrpcScheme.equals(Schemes.SCHEME_PBRPCG)) {
|
||||
srvSSL = createSSLOptions("DIR.p12", "passphrase", SSLOptions.PKCS12_CONTAINER,
|
||||
"trusted.jks", "passphrase", SSLOptions.JKS_CONTAINER, pbrpcScheme.equals(Schemes.SCHEME_PBRPCG), null);
|
||||
|
||||
clientSSL= createSSLOptions("Client.p12", "passphrase",
|
||||
SSLOptions.PKCS12_CONTAINER, "trusted.jks", "passphrase", SSLOptions.JKS_CONTAINER, pbrpcScheme.equals(Schemes.SCHEME_PBRPCG), null);
|
||||
}
|
||||
|
||||
// System.out.println("setup done");
|
||||
|
||||
try {
|
||||
|
||||
server = getServer(creator,srvSSL);
|
||||
|
||||
server.start();
|
||||
server.waitForStartup();
|
||||
|
||||
client = new RPCNIOSocketClient(clientSSL, 5000, 5*60*1000, "runTest");
|
||||
client.start();
|
||||
client.waitForStartup();
|
||||
|
||||
exec.execTest(client);
|
||||
|
||||
} finally {
|
||||
//clean up
|
||||
if (client != null) {
|
||||
client.shutdown();
|
||||
client.waitForShutdown();
|
||||
}
|
||||
if (server != null) {
|
||||
server.shutdown();
|
||||
server.waitForShutdown();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private RPCNIOSocketServer getServer(final ResponseCreator creator, SSLOptions sslOpt) throws IOException {
|
||||
return new RPCNIOSocketServer(TEST_PORT, null, new RPCServerRequestListener() {
|
||||
|
||||
@Override
|
||||
public void receiveRecord(RPCServerRequest rq) {
|
||||
// System.out.println("received request");
|
||||
try {
|
||||
ReusableBufferInputStream is = new ReusableBufferInputStream(rq.getMessage());
|
||||
Ping.PingRequest pingRq = Ping.PingRequest.parseFrom(is);
|
||||
|
||||
creator.answer(rq, pingRq);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
rq.sendError(RPC.RPCHeader.ErrorResponse.newBuilder().setErrorType(RPC.ErrorType.GARBAGE_ARGS).setErrorMessage(ex.getMessage()).setDebugInfo(OutputUtils.stackTraceToString(ex)).build());
|
||||
fail(ex.toString());
|
||||
|
||||
}
|
||||
}
|
||||
}, sslOpt);
|
||||
}
|
||||
|
||||
private SSLOptions createSSLOptions(String keyStoreName, String ksPassphrase,
|
||||
String ksContainerType, String trustStoreName, String tsPassphrase, String tsContainerType, boolean gridSSL, String sslProtocolString)
|
||||
throws IOException {
|
||||
|
||||
ClassLoader cl = this.getClass().getClassLoader();
|
||||
|
||||
InputStream ks = cl.getResourceAsStream(keyStoreName);
|
||||
if (ks == null) {
|
||||
// Assume the working directory is "java/servers".
|
||||
String testCert = "../../tests/certs/" + keyStoreName;
|
||||
if (new File(testCert).isFile()) {
|
||||
ks = new FileInputStream(testCert);
|
||||
} else {
|
||||
// Assume the working directory is the root of the project.
|
||||
ks = new FileInputStream("tests/certs/" + keyStoreName);
|
||||
}
|
||||
}
|
||||
|
||||
InputStream ts = cl.getResourceAsStream(trustStoreName);
|
||||
if (ts == null) {
|
||||
// Assume the working directory is "java/servers".
|
||||
String testCert = "../../tests/certs/" + trustStoreName;
|
||||
if (new File(testCert).isFile()) {
|
||||
ts = new FileInputStream(testCert);
|
||||
} else {
|
||||
// Assume the working directory is the root of the project.
|
||||
ts = new FileInputStream("tests/certs/" + trustStoreName);
|
||||
}
|
||||
}
|
||||
|
||||
return new SSLOptions(ks, ksPassphrase, ksContainerType, ts, tsPassphrase, tsContainerType, false, gridSSL, sslProtocolString, null);
|
||||
}
|
||||
|
||||
private static interface ResponseCreator {
|
||||
public void answer(RPCServerRequest rq, PingRequest pRq) throws Exception;
|
||||
}
|
||||
|
||||
private static interface TestExecutor {
|
||||
public void execTest(RPCNIOSocketClient client) throws Exception;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2011 by Bjoern Kolbeck,
|
||||
* Zuse Institute Berlin
|
||||
*
|
||||
* Licensed under the BSD License, see LICENSE file for details.
|
||||
*
|
||||
*/
|
||||
|
||||
package org.xtreemfs.test.foundation.pbrpc;
|
||||
|
||||
import org.xtreemfs.foundation.buffer.ReusableBuffer;
|
||||
import org.xtreemfs.foundation.pbrpc.utils.PBRPCDatagramPacket;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.xtreemfs.foundation.pbrpc.client.RPCAuthentication;
|
||||
import org.xtreemfs.foundation.pbrpc.generatedinterfaces.Ping.PingRequest;
|
||||
import org.xtreemfs.foundation.pbrpc.generatedinterfaces.RPC.MessageType;
|
||||
import org.xtreemfs.foundation.pbrpc.generatedinterfaces.RPC.RPCHeader;
|
||||
import org.xtreemfs.foundation.pbrpc.generatedinterfaces.RPC.UserCredentials;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author bjko
|
||||
*/
|
||||
public class PBRPCDatagramPacketTest {
|
||||
|
||||
public PBRPCDatagramPacketTest() {
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpClass() throws Exception {
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void tearDownClass() throws Exception {
|
||||
}
|
||||
|
||||
// TODO add test methods here.
|
||||
// The methods must be annotated with annotation @Test. For example:
|
||||
//
|
||||
// @Test
|
||||
// public void hello() {}
|
||||
|
||||
@Test
|
||||
public void testPacket() throws Exception {
|
||||
|
||||
RPCHeader.RequestHeader rqHdr = RPCHeader.RequestHeader.newBuilder().setAuthData(RPCAuthentication.authNone).setUserCreds(RPCAuthentication.userService).setInterfaceId(1).setProcId(2).build();
|
||||
RPCHeader hdr = RPCHeader.newBuilder().setCallId(12345).setMessageType(MessageType.RPC_REQUEST).setRequestHeader(rqHdr).build();
|
||||
|
||||
PingRequest pRq = PingRequest.newBuilder().setText("YAGGA!").setSendError(false).build();
|
||||
|
||||
PBRPCDatagramPacket dp = new PBRPCDatagramPacket(hdr, pRq);
|
||||
ReusableBuffer data = dp.assembleDatagramPacket();
|
||||
|
||||
dp = new PBRPCDatagramPacket(data, PingRequest.getDefaultInstance());
|
||||
|
||||
PingRequest response = (PingRequest)dp.getMessage();
|
||||
|
||||
assertEquals(dp.getHeader().getCallId(),12345);
|
||||
assertEquals(dp.getHeader().getMessageType(),MessageType.RPC_REQUEST);
|
||||
assertEquals(response.getText(),pRq.getText());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,255 @@
|
||||
/*
|
||||
* Copyright (c) 2009-2011 by Bjoern Kolbeck,
|
||||
* Zuse Institute Berlin
|
||||
*
|
||||
* Licensed under the BSD License, see LICENSE file for details.
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
package org.xtreemfs.test.foundation.pbrpc;
|
||||
|
||||
import org.xtreemfs.foundation.buffer.ReusableBuffer;
|
||||
import org.xtreemfs.foundation.TimeSync;
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import org.xtreemfs.foundation.logging.Logging;
|
||||
import org.xtreemfs.foundation.pbrpc.server.RPCServerRequest;
|
||||
import org.xtreemfs.foundation.util.OutputUtils;
|
||||
import org.xtreemfs.foundation.pbrpc.generatedinterfaces.Ping.PingResponse;
|
||||
import org.xtreemfs.foundation.pbrpc.generatedinterfaces.RPC;
|
||||
import org.xtreemfs.foundation.pbrpc.server.RPCServerRequestListener;
|
||||
import org.xtreemfs.foundation.pbrpc.server.RPCNIOSocketServer;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.xtreemfs.foundation.pbrpc.client.RPCAuthentication;
|
||||
import org.xtreemfs.foundation.pbrpc.client.RPCNIOSocketClient;
|
||||
import org.xtreemfs.foundation.pbrpc.client.RPCResponse;
|
||||
import org.xtreemfs.foundation.pbrpc.utils.ReusableBufferInputStream;
|
||||
import org.xtreemfs.foundation.pbrpc.generatedinterfaces.Ping;
|
||||
import org.xtreemfs.foundation.pbrpc.generatedinterfaces.PingServiceClient;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author bjko
|
||||
*/
|
||||
public class PBRPCTest {
|
||||
private final int TEST_PORT = 12999;
|
||||
|
||||
private static TimeSync ts = null;
|
||||
|
||||
public PBRPCTest() {
|
||||
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpClass() throws Exception {
|
||||
Logging.start(Logging.LEVEL_WARN, Logging.Category.all);
|
||||
ts = TimeSync.initializeLocal(50);
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void tearDownClass() throws Exception {
|
||||
ts.close();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testRPCClient() throws Exception {
|
||||
RPCNIOSocketClient client = null;
|
||||
RPCNIOSocketServer server = null;
|
||||
|
||||
try {
|
||||
|
||||
server = new RPCNIOSocketServer(TEST_PORT, null, new RPCServerRequestListener() {
|
||||
|
||||
@Override
|
||||
public void receiveRecord(RPCServerRequest rq) {
|
||||
// System.out.println("received request");
|
||||
try {
|
||||
ReusableBufferInputStream is = new ReusableBufferInputStream(rq.getMessage());
|
||||
Ping.PingRequest pingRq = Ping.PingRequest.parseFrom(is);
|
||||
|
||||
Ping.PingResponse.PingResult result = Ping.PingResponse.PingResult.newBuilder().setText(pingRq.getText()).build();
|
||||
Ping.PingResponse resp = Ping.PingResponse.newBuilder().setResult(result).build();
|
||||
|
||||
rq.sendResponse(resp, null);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
rq.sendError(RPC.RPCHeader.ErrorResponse.newBuilder().setErrorType(RPC.ErrorType.GARBAGE_ARGS).setErrorMessage(ex.getMessage()).setDebugInfo(OutputUtils.stackTraceToString(ex)).build());
|
||||
fail(ex.toString());
|
||||
|
||||
}
|
||||
}
|
||||
}, null);
|
||||
|
||||
server.start();
|
||||
server.waitForStartup();
|
||||
|
||||
client = new RPCNIOSocketClient(null, 15000, 5*60*1000, "testRPCClient");
|
||||
client.start();
|
||||
client.waitForStartup();
|
||||
|
||||
PingServiceClient psClient = new PingServiceClient(client,null);
|
||||
|
||||
RPC.UserCredentials userCred = RPC.UserCredentials.newBuilder().setUsername("test").addGroups("tester").build();
|
||||
RPCResponse<PingResponse> response = psClient.doPing(new InetSocketAddress("localhost", TEST_PORT), RPCAuthentication.authNone, userCred, "Hello World!", false, null);
|
||||
assertEquals(response.get().getResult().getText(),"Hello World!");
|
||||
|
||||
response = psClient.doPing(new InetSocketAddress("localhost", TEST_PORT), RPCAuthentication.authNone, userCred, "Murpel", false, null);
|
||||
assertEquals(response.get().getResult().getText(),"Murpel");
|
||||
|
||||
} finally {
|
||||
//clean up
|
||||
if (client != null) {
|
||||
client.shutdown();
|
||||
client.waitForShutdown();
|
||||
}
|
||||
if (server != null) {
|
||||
server.shutdown();
|
||||
server.waitForShutdown();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testRPCWithData() throws Exception {
|
||||
RPCNIOSocketClient client = null;
|
||||
RPCNIOSocketServer server = null;
|
||||
|
||||
try {
|
||||
|
||||
server = new RPCNIOSocketServer(TEST_PORT, null, new RPCServerRequestListener() {
|
||||
|
||||
@Override
|
||||
public void receiveRecord(RPCServerRequest rq) {
|
||||
// System.out.println("received request");
|
||||
try {
|
||||
ReusableBufferInputStream is = new ReusableBufferInputStream(rq.getMessage());
|
||||
Ping.PingRequest pingRq = Ping.PingRequest.parseFrom(is);
|
||||
|
||||
Ping.PingResponse.PingResult result = Ping.PingResponse.PingResult.newBuilder().setText(pingRq.getText()).build();
|
||||
Ping.PingResponse resp = Ping.PingResponse.newBuilder().setResult(result).build();
|
||||
|
||||
ReusableBuffer data = null;
|
||||
if (rq.getData() != null) {
|
||||
data = rq.getData().createViewBuffer();
|
||||
data.limit(data.capacity());
|
||||
data.position(data.capacity());
|
||||
}
|
||||
|
||||
rq.sendResponse(resp, data);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
rq.sendError(RPC.RPCHeader.ErrorResponse.newBuilder().setErrorType(RPC.ErrorType.GARBAGE_ARGS).setErrorMessage(ex.getMessage()).setDebugInfo(OutputUtils.stackTraceToString(ex)).build());
|
||||
fail(ex.toString());
|
||||
|
||||
}
|
||||
}
|
||||
}, null);
|
||||
|
||||
server.start();
|
||||
server.waitForStartup();
|
||||
|
||||
client = new RPCNIOSocketClient(null, 15000, 5*60*1000, "testRPCWithData");
|
||||
client.start();
|
||||
client.waitForStartup();
|
||||
|
||||
PingServiceClient psClient = new PingServiceClient(client,null);
|
||||
|
||||
byte[] arr = new byte[2065];
|
||||
for (int i= 0; i < arr.length; i++)
|
||||
arr[i] = 'x';
|
||||
ReusableBuffer sendData = ReusableBuffer.wrap(arr);
|
||||
// System.out.println("data: "+sendData);
|
||||
|
||||
RPC.UserCredentials userCred = RPC.UserCredentials.newBuilder().setUsername("test").addGroups("tester").build();
|
||||
RPCResponse<PingResponse> response = psClient.doPing(new InetSocketAddress("localhost", TEST_PORT), RPCAuthentication.authNone, userCred, "Hello World!", false, sendData);
|
||||
assertEquals(response.get().getResult().getText(),"Hello World!");
|
||||
|
||||
ReusableBuffer recdata = response.getData();
|
||||
assertTrue(recdata.hasRemaining());
|
||||
while (recdata.hasRemaining()) {
|
||||
assertEquals(recdata.get(),(byte)'x');
|
||||
}
|
||||
|
||||
response = psClient.doPing(new InetSocketAddress("localhost", TEST_PORT), RPCAuthentication.authNone, userCred, "Murpel", false, null);
|
||||
assertEquals(response.get().getResult().getText(),"Murpel");
|
||||
|
||||
} finally {
|
||||
//clean up
|
||||
if (client != null) {
|
||||
client.shutdown();
|
||||
client.waitForShutdown();
|
||||
}
|
||||
if (server != null) {
|
||||
server.shutdown();
|
||||
server.waitForShutdown();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testEmptyMessages() throws Exception {
|
||||
RPCNIOSocketClient client = null;
|
||||
RPCNIOSocketServer server = null;
|
||||
|
||||
try {
|
||||
|
||||
server = new RPCNIOSocketServer(TEST_PORT, null, new RPCServerRequestListener() {
|
||||
|
||||
@Override
|
||||
public void receiveRecord(RPCServerRequest rq) {
|
||||
// System.out.println("received request");
|
||||
try {
|
||||
assertNull(rq.getMessage());
|
||||
rq.sendResponse(null, null);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
rq.sendError(RPC.RPCHeader.ErrorResponse.newBuilder().setErrorType(RPC.ErrorType.GARBAGE_ARGS).setErrorMessage(ex.getMessage()).setDebugInfo(OutputUtils.stackTraceToString(ex)).build());
|
||||
fail(ex.toString());
|
||||
|
||||
}
|
||||
}
|
||||
}, null);
|
||||
|
||||
server.start();
|
||||
server.waitForStartup();
|
||||
|
||||
client = new RPCNIOSocketClient(null, 15000, 5*60*1000, "PBRPCTest::testEmptyMessages()");
|
||||
client.start();
|
||||
client.waitForStartup();
|
||||
|
||||
PingServiceClient psClient = new PingServiceClient(client,null);
|
||||
|
||||
RPC.UserCredentials userCred = RPC.UserCredentials.newBuilder().setUsername("test").addGroups("tester").build();
|
||||
RPCResponse response = psClient.emptyPing(new InetSocketAddress("localhost", TEST_PORT), RPCAuthentication.authNone, userCred);
|
||||
assertNull(response.get());
|
||||
|
||||
response = psClient.emptyPing(new InetSocketAddress("localhost", TEST_PORT), RPCAuthentication.authNone, userCred);
|
||||
assertNull(response.get());
|
||||
|
||||
response = psClient.emptyPing(new InetSocketAddress("localhost", TEST_PORT), RPCAuthentication.authNone, userCred);
|
||||
assertNull(response.get());
|
||||
|
||||
} finally {
|
||||
//clean up
|
||||
if (client != null) {
|
||||
client.shutdown();
|
||||
client.waitForShutdown();
|
||||
}
|
||||
if (server != null) {
|
||||
server.shutdown();
|
||||
server.waitForShutdown();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,377 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2011 by Bjoern Kolbeck,
|
||||
* Zuse Institute Berlin
|
||||
*
|
||||
* Licensed under the BSD License, see LICENSE file for details.
|
||||
*
|
||||
*/
|
||||
|
||||
package org.xtreemfs.test.foundation.pbrpc;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.xtreemfs.foundation.util.OutputUtils;
|
||||
import org.xtreemfs.foundation.buffer.ReusableBuffer;
|
||||
import org.xtreemfs.foundation.logging.Logging;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.Socket;
|
||||
import java.nio.ByteBuffer;
|
||||
import org.xtreemfs.foundation.pbrpc.generatedinterfaces.RPC;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.xtreemfs.foundation.pbrpc.server.RPCNIOSocketServer;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.xtreemfs.foundation.buffer.BufferPool;
|
||||
import org.xtreemfs.foundation.pbrpc.server.RPCServerRequest;
|
||||
import org.xtreemfs.foundation.pbrpc.server.RPCServerRequestListener;
|
||||
import org.xtreemfs.foundation.pbrpc.utils.RecordMarker;
|
||||
import org.xtreemfs.foundation.pbrpc.utils.ReusableBufferInputStream;
|
||||
import org.xtreemfs.foundation.pbrpc.utils.ReusableBufferOutputStream;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author bjko
|
||||
*/
|
||||
public class RPCNIOSocketServerTest {
|
||||
|
||||
private RPCNIOSocketServer server;
|
||||
|
||||
public RPCNIOSocketServerTest() {
|
||||
Logging.start(Logging.LEVEL_WARN, Logging.Category.all);
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpClass() throws Exception {
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void tearDownClass() throws Exception {
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
}
|
||||
|
||||
// TODO add test methods here.
|
||||
// The methods must be annotated with annotation @Test. For example:
|
||||
//
|
||||
// @Test
|
||||
// public void hello() {}
|
||||
|
||||
@Test
|
||||
public void testSerialization() throws Exception {
|
||||
final int CALLID = 5464566;
|
||||
|
||||
RPC.Auth auth = RPC.Auth.newBuilder().setAuthType(RPC.AuthType.AUTH_NONE).build();
|
||||
RPC.UserCredentials ucred = RPC.UserCredentials.newBuilder().setUsername("test").addGroups("user").build();
|
||||
RPC.RPCHeader.RequestHeader rqHdr = RPC.RPCHeader.RequestHeader.newBuilder().setAuthData(auth).setUserCreds(ucred).setProcId(2).setInterfaceId(2).build();
|
||||
RPC.RPCHeader header = RPC.RPCHeader.newBuilder().setCallId(CALLID).setMessageType(RPC.MessageType.RPC_REQUEST).setRequestHeader(rqHdr).build();
|
||||
|
||||
ReusableBufferOutputStream ois = new ReusableBufferOutputStream(ReusableBufferOutputStream.BUFF_SIZE);
|
||||
header.writeTo(ois);
|
||||
ois.flip();
|
||||
|
||||
byte[] data = new byte[ois.getBuffers()[0].remaining()];
|
||||
ois.getBuffers()[0].get(data);
|
||||
|
||||
ReusableBufferInputStream is = new ReusableBufferInputStream(ReusableBuffer.wrap(data));
|
||||
|
||||
RPC.RPCHeader deser = RPC.RPCHeader.parseFrom(is);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSimpleRPC() throws Exception {
|
||||
|
||||
final int CALLID = 5464566;
|
||||
final int TEST_PORT = 9991;
|
||||
final String USERID = "yaggaYagga";
|
||||
|
||||
server = new RPCNIOSocketServer(TEST_PORT, null, new RPCServerRequestListener() {
|
||||
|
||||
@Override
|
||||
public void receiveRecord(RPCServerRequest rq) {
|
||||
// System.out.println("received request");
|
||||
try {
|
||||
assertEquals(CALLID,rq.getHeader().getCallId());
|
||||
assertEquals(RPC.MessageType.RPC_REQUEST, rq.getHeader().getMessageType());
|
||||
|
||||
//send a dummy message
|
||||
RPC.UserCredentials msg = RPC.UserCredentials.newBuilder().setUsername(USERID).build();
|
||||
|
||||
ReusableBuffer data = BufferPool.allocate(2);
|
||||
data.put((byte)15);
|
||||
data.put((byte)20);
|
||||
|
||||
rq.sendResponse(msg, data);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
rq.sendError(RPC.RPCHeader.ErrorResponse.newBuilder().setErrorType(RPC.ErrorType.GARBAGE_ARGS).setErrorMessage(ex.getMessage()).setDebugInfo(OutputUtils.stackTraceToString(ex)).build());
|
||||
fail(ex.toString());
|
||||
|
||||
}
|
||||
}
|
||||
}, null);
|
||||
|
||||
server.start();
|
||||
server.waitForStartup();
|
||||
|
||||
Socket sock = new Socket("localhost", TEST_PORT);
|
||||
OutputStream out = sock.getOutputStream();
|
||||
InputStream in = sock.getInputStream();
|
||||
|
||||
RPC.Auth auth = RPC.Auth.newBuilder().setAuthType(RPC.AuthType.AUTH_NONE).build();
|
||||
RPC.UserCredentials ucred = RPC.UserCredentials.newBuilder().setUsername("test").addGroups("user").build();
|
||||
RPC.RPCHeader.RequestHeader rqHdr = RPC.RPCHeader.RequestHeader.newBuilder().setAuthData(auth).setUserCreds(ucred).setProcId(2).setInterfaceId(2).build();
|
||||
RPC.RPCHeader header = RPC.RPCHeader.newBuilder().setCallId(CALLID).setMessageType(RPC.MessageType.RPC_REQUEST).setRequestHeader(rqHdr).build();
|
||||
|
||||
ReusableBufferOutputStream ois = new ReusableBufferOutputStream(ReusableBufferOutputStream.BUFF_SIZE);
|
||||
header.writeTo(ois);
|
||||
int hdrLen = ois.length();
|
||||
|
||||
ByteBuffer recordMarker = ByteBuffer.allocate(RecordMarker.HDR_SIZE);
|
||||
recordMarker.putInt(hdrLen);
|
||||
recordMarker.putInt(0);
|
||||
recordMarker.putInt(0);
|
||||
recordMarker.flip();
|
||||
|
||||
ois.flip();
|
||||
|
||||
out.write(recordMarker.array());
|
||||
byte[] data = new byte[ois.getBuffers()[0].remaining()];
|
||||
ois.getBuffers()[0].get(data);
|
||||
out.write(data);
|
||||
|
||||
byte[] markerIn = new byte[RecordMarker.HDR_SIZE];
|
||||
in.read(markerIn);
|
||||
ReusableBuffer marker = ReusableBuffer.wrap(markerIn);
|
||||
|
||||
hdrLen = marker.getInt();
|
||||
int msgLen = marker.getInt();
|
||||
int dataLen = marker.getInt();
|
||||
|
||||
// System.out.println("header: "+hdrLen+"/"+msgLen+"/"+dataLen);
|
||||
|
||||
byte[] hdrIn = new byte[hdrLen];
|
||||
byte[] msgIn = new byte[msgLen];
|
||||
byte[] dataIn = new byte[dataLen];
|
||||
|
||||
in.read(hdrIn);
|
||||
in.read(msgIn);
|
||||
in.read(dataIn);
|
||||
|
||||
// System.out.println("read data");
|
||||
|
||||
RPC.RPCHeader respHdr = RPC.RPCHeader.parseFrom(hdrIn);
|
||||
|
||||
RPC.UserCredentials uc = RPC.UserCredentials.parseFrom(msgIn);
|
||||
|
||||
assertEquals(RPC.MessageType.RPC_RESPONSE_SUCCESS,respHdr.getMessageType());
|
||||
assertEquals(header.getCallId(),respHdr.getCallId());
|
||||
|
||||
assertEquals(USERID,uc.getUsername());
|
||||
|
||||
assertEquals(15,dataIn[0]);
|
||||
assertEquals(20,dataIn[1]);
|
||||
|
||||
sock.close();
|
||||
server.shutdown();
|
||||
server.waitForShutdown();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRPCWithoutMessage() throws Exception {
|
||||
|
||||
final int CALLID = 5464566;
|
||||
final int TEST_PORT = 9991;
|
||||
final String USERID = "yaggaYagga";
|
||||
|
||||
server = new RPCNIOSocketServer(TEST_PORT, null, new RPCServerRequestListener() {
|
||||
|
||||
@Override
|
||||
public void receiveRecord(RPCServerRequest rq) {
|
||||
// System.out.println("received request");
|
||||
try {
|
||||
assertNotNull(rq.getData());
|
||||
assertNull(rq.getMessage());
|
||||
assertEquals(CALLID,rq.getHeader().getCallId());
|
||||
assertEquals(RPC.MessageType.RPC_REQUEST, rq.getHeader().getMessageType());
|
||||
|
||||
//send a dummy message
|
||||
RPC.UserCredentials msg = RPC.UserCredentials.newBuilder().setUsername(USERID).build();
|
||||
|
||||
ReusableBuffer data = BufferPool.allocate(2);
|
||||
data.put((byte)15);
|
||||
data.put((byte)20);
|
||||
|
||||
rq.sendResponse(msg, data);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
rq.sendError(RPC.RPCHeader.ErrorResponse.newBuilder().setErrorType(RPC.ErrorType.GARBAGE_ARGS).setErrorMessage(ex.getMessage()).setDebugInfo(OutputUtils.stackTraceToString(ex)).build());
|
||||
fail(ex.toString());
|
||||
|
||||
}
|
||||
}
|
||||
}, null);
|
||||
|
||||
server.start();
|
||||
server.waitForStartup();
|
||||
|
||||
Socket sock = new Socket("localhost", TEST_PORT);
|
||||
OutputStream out = sock.getOutputStream();
|
||||
InputStream in = sock.getInputStream();
|
||||
|
||||
RPC.Auth auth = RPC.Auth.newBuilder().setAuthType(RPC.AuthType.AUTH_NONE).build();
|
||||
RPC.UserCredentials ucred = RPC.UserCredentials.newBuilder().setUsername("test").addGroups("user").build();
|
||||
RPC.RPCHeader.RequestHeader rqHdr = RPC.RPCHeader.RequestHeader.newBuilder().setAuthData(auth).setUserCreds(ucred).setProcId(2).setInterfaceId(2).build();
|
||||
RPC.RPCHeader header = RPC.RPCHeader.newBuilder().setCallId(CALLID).setMessageType(RPC.MessageType.RPC_REQUEST).setRequestHeader(rqHdr).build();
|
||||
|
||||
ReusableBufferOutputStream ois = new ReusableBufferOutputStream(ReusableBufferOutputStream.BUFF_SIZE);
|
||||
header.writeTo(ois);
|
||||
int hdrLen = ois.length();
|
||||
|
||||
ByteBuffer recordMarker = ByteBuffer.allocate(RecordMarker.HDR_SIZE);
|
||||
recordMarker.putInt(hdrLen);
|
||||
recordMarker.putInt(0);
|
||||
recordMarker.putInt(16);
|
||||
recordMarker.flip();
|
||||
|
||||
ois.flip();
|
||||
|
||||
out.write(recordMarker.array());
|
||||
byte[] data = new byte[ois.getBuffers()[0].remaining()];
|
||||
ois.getBuffers()[0].get(data);
|
||||
out.write(data);
|
||||
|
||||
for (int i = 0; i < 16; i++) {
|
||||
out.write('a');
|
||||
}
|
||||
|
||||
byte[] markerIn = new byte[RecordMarker.HDR_SIZE];
|
||||
in.read(markerIn);
|
||||
ReusableBuffer marker = ReusableBuffer.wrap(markerIn);
|
||||
|
||||
hdrLen = marker.getInt();
|
||||
int msgLen = marker.getInt();
|
||||
int dataLen = marker.getInt();
|
||||
|
||||
// System.out.println("header: "+hdrLen+"/"+msgLen+"/"+dataLen);
|
||||
|
||||
byte[] hdrIn = new byte[hdrLen];
|
||||
byte[] msgIn = new byte[msgLen];
|
||||
byte[] dataIn = new byte[dataLen];
|
||||
|
||||
in.read(hdrIn);
|
||||
in.read(msgIn);
|
||||
in.read(dataIn);
|
||||
|
||||
// System.out.println("read data");
|
||||
|
||||
RPC.RPCHeader respHdr = RPC.RPCHeader.parseFrom(hdrIn);
|
||||
|
||||
RPC.UserCredentials uc = RPC.UserCredentials.parseFrom(msgIn);
|
||||
|
||||
assertEquals(RPC.MessageType.RPC_RESPONSE_SUCCESS,respHdr.getMessageType());
|
||||
assertEquals(header.getCallId(),respHdr.getCallId());
|
||||
|
||||
assertEquals(USERID,uc.getUsername());
|
||||
|
||||
assertEquals(15,dataIn[0]);
|
||||
assertEquals(20,dataIn[1]);
|
||||
|
||||
sock.close();
|
||||
server.shutdown();
|
||||
server.waitForShutdown();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testErrorResponse() throws Exception {
|
||||
|
||||
final int CALLID = 5464566;
|
||||
final int TEST_PORT = 9991;
|
||||
final String USERID = "yaggaYagga";
|
||||
|
||||
server = new RPCNIOSocketServer(TEST_PORT, null, new RPCServerRequestListener() {
|
||||
|
||||
@Override
|
||||
public void receiveRecord(RPCServerRequest rq) {
|
||||
// System.out.println("received request");
|
||||
try {
|
||||
assertEquals(CALLID,rq.getHeader().getCallId());
|
||||
assertEquals(RPC.MessageType.RPC_REQUEST, rq.getHeader().getMessageType());
|
||||
|
||||
rq.sendError(RPC.RPCHeader.ErrorResponse.newBuilder().setErrorType(RPC.ErrorType.AUTH_FAILED).setErrorMessage("dummy error").setDebugInfo("no info here").build());
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
rq.sendError(RPC.RPCHeader.ErrorResponse.newBuilder().setErrorType(RPC.ErrorType.GARBAGE_ARGS).setErrorMessage(ex.getMessage()).setDebugInfo(OutputUtils.stackTraceToString(ex)).build());
|
||||
rq.freeBuffers();
|
||||
fail(ex.toString());
|
||||
}
|
||||
}
|
||||
}, null);
|
||||
|
||||
server.start();
|
||||
server.waitForStartup();
|
||||
|
||||
Socket sock = new Socket("localhost", TEST_PORT);
|
||||
OutputStream out = sock.getOutputStream();
|
||||
InputStream in = sock.getInputStream();
|
||||
|
||||
RPC.Auth auth = RPC.Auth.newBuilder().setAuthType(RPC.AuthType.AUTH_NONE).build();
|
||||
RPC.UserCredentials ucred = RPC.UserCredentials.newBuilder().setUsername("test").addGroups("user").build();
|
||||
RPC.RPCHeader.RequestHeader rqHdr = RPC.RPCHeader.RequestHeader.newBuilder().setAuthData(auth).setUserCreds(ucred).setProcId(2).setInterfaceId(2).build();
|
||||
RPC.RPCHeader header = RPC.RPCHeader.newBuilder().setCallId(CALLID).setMessageType(RPC.MessageType.RPC_REQUEST).setRequestHeader(rqHdr).build();
|
||||
|
||||
ReusableBufferOutputStream ois = new ReusableBufferOutputStream(ReusableBufferOutputStream.BUFF_SIZE);
|
||||
header.writeTo(ois);
|
||||
int hdrLen = ois.length();
|
||||
|
||||
ByteBuffer recordMarker = ByteBuffer.allocate(RecordMarker.HDR_SIZE);
|
||||
recordMarker.putInt(hdrLen);
|
||||
recordMarker.putInt(0);
|
||||
recordMarker.putInt(0);
|
||||
recordMarker.flip();
|
||||
|
||||
ois.flip();
|
||||
|
||||
out.write(recordMarker.array());
|
||||
byte[] data = new byte[ois.getBuffers()[0].remaining()];
|
||||
ois.getBuffers()[0].get(data);
|
||||
out.write(data);
|
||||
|
||||
byte[] markerIn = new byte[RecordMarker.HDR_SIZE];
|
||||
in.read(markerIn);
|
||||
ReusableBuffer marker = ReusableBuffer.wrap(markerIn);
|
||||
|
||||
hdrLen = marker.getInt();
|
||||
int msgLen = marker.getInt();
|
||||
int dataLen = marker.getInt();
|
||||
|
||||
assertEquals(0,msgLen);
|
||||
assertEquals(0,dataLen);
|
||||
|
||||
// System.out.println("header: "+hdrLen+"/"+msgLen+"/"+dataLen);
|
||||
|
||||
byte[] hdrIn = new byte[hdrLen];
|
||||
|
||||
in.read(hdrIn);
|
||||
|
||||
RPC.RPCHeader respHdr = RPC.RPCHeader.parseFrom(hdrIn);
|
||||
|
||||
assertEquals(RPC.MessageType.RPC_RESPONSE_ERROR,respHdr.getMessageType());
|
||||
assertEquals(header.getCallId(),respHdr.getCallId());
|
||||
assertEquals(RPC.ErrorType.AUTH_FAILED,respHdr.getErrorResponse().getErrorType());
|
||||
|
||||
sock.close();
|
||||
server.shutdown();
|
||||
server.waitForShutdown();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2011 by Bjoern Kolbeck,
|
||||
* Zuse Institute Berlin
|
||||
*
|
||||
* Licensed under the BSD License, see LICENSE file for details.
|
||||
*
|
||||
*/
|
||||
|
||||
package org.xtreemfs.test.foundation.pbrpc;
|
||||
|
||||
import org.xtreemfs.foundation.logging.Logging;
|
||||
import org.xtreemfs.foundation.pbrpc.generatedinterfaces.RPC.RPCHeader;
|
||||
import java.net.InetSocketAddress;
|
||||
import org.xtreemfs.foundation.pbrpc.server.RPCServerRequest;
|
||||
import org.xtreemfs.foundation.pbrpc.server.RPCUDPSocketServer;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.xtreemfs.foundation.pbrpc.client.RPCAuthentication;
|
||||
import org.xtreemfs.foundation.pbrpc.generatedinterfaces.Ping.PingRequest;
|
||||
import org.xtreemfs.foundation.pbrpc.generatedinterfaces.RPC.ErrorType;
|
||||
import org.xtreemfs.foundation.pbrpc.generatedinterfaces.RPC.MessageType;
|
||||
import org.xtreemfs.foundation.pbrpc.generatedinterfaces.RPC.POSIXErrno;
|
||||
import org.xtreemfs.foundation.pbrpc.server.RPCServerRequestListener;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author bjko
|
||||
*/
|
||||
public class RPCUDPSocketServerTest {
|
||||
|
||||
RPCUDPSocketServer server1, server2;
|
||||
|
||||
final static int PORT_1 = 33333;
|
||||
final static int PORT_2 = 33334;
|
||||
|
||||
public RPCUDPSocketServerTest() {
|
||||
Logging.start(Logging.LEVEL_WARN, Logging.Category.all);
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpClass() throws Exception {
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void tearDownClass() throws Exception {
|
||||
}
|
||||
|
||||
// TODO add test methods here.
|
||||
// The methods must be annotated with annotation @Test. For example:
|
||||
//
|
||||
// @Test
|
||||
// public void hello() {}
|
||||
|
||||
@Test
|
||||
public void testUDP() throws Exception {
|
||||
server1 = new RPCUDPSocketServer(PORT_1, new RPCServerRequestListener() {
|
||||
|
||||
@Override
|
||||
public void receiveRecord(RPCServerRequest rq) {
|
||||
// System.out.println("srv1: "+rq);
|
||||
}
|
||||
});
|
||||
|
||||
server2 = new RPCUDPSocketServer(PORT_2, new RPCServerRequestListener() {
|
||||
|
||||
@Override
|
||||
public void receiveRecord(RPCServerRequest rq) {
|
||||
// System.out.println("srv2: "+rq);
|
||||
rq.sendError(ErrorType.ERRNO, POSIXErrno.POSIX_ERROR_EIO, "yagga");
|
||||
}
|
||||
});
|
||||
|
||||
server1.start();
|
||||
server2.start();
|
||||
|
||||
server1.waitForStartup();
|
||||
server2.waitForStartup();
|
||||
|
||||
RPCHeader.RequestHeader rqHdr = RPCHeader.RequestHeader.newBuilder().setAuthData(RPCAuthentication.authNone).setUserCreds(RPCAuthentication.userService).setInterfaceId(1).setProcId(5).build();
|
||||
RPCHeader hdr = RPCHeader.newBuilder().setCallId(555).setMessageType(MessageType.RPC_REQUEST).setRequestHeader(rqHdr).build();
|
||||
PingRequest pRq = PingRequest.newBuilder().setSendError(false).setText("yagga").build();
|
||||
server1.sendRequest(hdr, pRq, new InetSocketAddress("localhost",PORT_2));
|
||||
|
||||
Thread.sleep(100);
|
||||
|
||||
server1.shutdown();
|
||||
server2.shutdown();
|
||||
|
||||
server1.waitForShutdown();
|
||||
server2.waitForShutdown();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright (c) 2008-2011 by Jan Stender, Bjoern Kolbeck,
|
||||
* Zuse Institute Berlin
|
||||
*
|
||||
* Licensed under the BSD License, see LICENSE file for details.
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
package org.xtreemfs.test.foundation.util;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.xtreemfs.foundation.util.OutputUtils;
|
||||
|
||||
public class OutputUtilsTest {
|
||||
|
||||
@Test
|
||||
public void testLongHexEncoding() throws Exception {
|
||||
|
||||
final long[] values = { 805306368000L, Integer.MIN_VALUE, Integer.MAX_VALUE, Long.MIN_VALUE,
|
||||
Long.MAX_VALUE, 1, 0, -1 };
|
||||
|
||||
for (long value : values) {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
OutputUtils.writeHexLong(sb, value);
|
||||
|
||||
assertEquals(value, OutputUtils.readHexLong(sb.toString(), 0));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadWriteHex() {
|
||||
final int objno = 129;
|
||||
final int objver = 459435;
|
||||
final int trepo = 1;
|
||||
final long checksum = 843349439598l;
|
||||
|
||||
final StringBuffer sb = new StringBuffer(Integer.SIZE/8*3+2*Long.SIZE/8);
|
||||
OutputUtils.writeHexInt(sb,objno);
|
||||
OutputUtils.writeHexInt(sb,objver);
|
||||
OutputUtils.writeHexInt(sb,trepo);
|
||||
OutputUtils.writeHexInt(sb,(int) (checksum >> 32));
|
||||
OutputUtils.writeHexInt(sb,(int) (checksum & 0xFFFFFFFF));
|
||||
OutputUtils.writeHexLong(sb, checksum);
|
||||
final String result = sb.toString();
|
||||
// System.out.println("result: "+result);
|
||||
|
||||
int tmp = OutputUtils.readHexInt(result, 0);
|
||||
assertEquals(objno,tmp);
|
||||
tmp = OutputUtils.readHexInt(result, 8);
|
||||
assertEquals(objver,tmp);
|
||||
tmp = OutputUtils.readHexInt(result, 16);
|
||||
assertEquals(trepo,tmp);
|
||||
tmp = OutputUtils.readHexInt(result, 24);
|
||||
long tmp2 = ((long)tmp)<< 32;
|
||||
tmp = OutputUtils.readHexInt(result, 32);
|
||||
tmp2 += tmp;
|
||||
assertEquals(checksum,tmp2);
|
||||
tmp2 = OutputUtils.readHexLong(result, 40);
|
||||
assertEquals(checksum,tmp2);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2011 by Bjoern Kolbeck, Felix Langner,
|
||||
* Zuse Institute Berlin
|
||||
*
|
||||
* Licensed under the BSD License, see LICENSE file for details.
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
package org.xtreemfs.test.foundation.util;
|
||||
|
||||
import org.junit.*;
|
||||
import org.xtreemfs.foundation.pbrpc.Schemes;
|
||||
import org.xtreemfs.foundation.util.PBRPCServiceURL;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author bjko
|
||||
*/
|
||||
public class PBRPCServiceURLTest {
|
||||
|
||||
public PBRPCServiceURLTest() {
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpClass() throws Exception {
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void tearDownClass() throws Exception {
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of getProtocol method, of class ONCRPCServiceURL.
|
||||
*/
|
||||
@Test
|
||||
public void testURLParse() throws Exception {
|
||||
String host = "yagga";
|
||||
int port = 1254;
|
||||
PBRPCServiceURL u = new PBRPCServiceURL("pbrpcg://"+host+":"+port+"/",
|
||||
Schemes.SCHEME_PBRPC,12345);
|
||||
assertEquals(host, u.getHost());
|
||||
assertEquals(port, u.getPort());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user