use of org.h2.test.store.TestConcurrent in project h2database by h2database.
the class TestFileSystem method testConcurrent.
private void testConcurrent(String fsBase) throws Exception {
String s = FileUtils.createTempFile(fsBase + "/tmp", ".tmp", false, false);
File file = new File(TestBase.BASE_TEST_DIR + "/tmp");
file.getParentFile().mkdirs();
file.delete();
RandomAccessFile ra = new RandomAccessFile(file, "rw");
FileUtils.delete(s);
final FileChannel f = FileUtils.open(s, "rw");
final int size = getSize(10, 50);
f.write(ByteBuffer.allocate(size * 64 * 1024));
Random random = new Random(1);
System.gc();
Task task = new Task() {
@Override
public void call() throws Exception {
ByteBuffer byteBuff = ByteBuffer.allocate(16);
while (!stop) {
for (int pos = 0; pos < size; pos++) {
byteBuff.clear();
f.read(byteBuff, pos * 64 * 1024);
byteBuff.position(0);
int x = byteBuff.getInt();
int y = byteBuff.getInt();
assertEquals(x, y);
Thread.yield();
}
}
}
};
task.execute();
int[] data = new int[size];
try {
ByteBuffer byteBuff = ByteBuffer.allocate(16);
int operations = 10000;
for (int i = 0; i < operations; i++) {
byteBuff.position(0);
byteBuff.putInt(i);
byteBuff.putInt(i);
byteBuff.flip();
int pos = random.nextInt(size);
f.write(byteBuff, pos * 64 * 1024);
data[pos] = i;
pos = random.nextInt(size);
byteBuff.clear();
f.read(byteBuff, pos * 64 * 1024);
byteBuff.limit(16);
byteBuff.position(0);
int x = byteBuff.getInt();
int y = byteBuff.getInt();
assertEquals(x, y);
assertEquals(data[pos], x);
}
} catch (Throwable e) {
e.printStackTrace();
fail("Exception: " + e);
} finally {
task.get();
f.close();
ra.close();
file.delete();
FileUtils.delete(s);
System.gc();
}
}
use of org.h2.test.store.TestConcurrent in project h2database by h2database.
the class TestAll method testUnit.
private void testUnit() {
// mv store
addTest(new TestCacheConcurrentLIRS());
addTest(new TestCacheLIRS());
addTest(new TestCacheLongKeyLIRS());
addTest(new TestConcurrentLinkedList());
addTest(new TestDataUtils());
addTest(new TestFreeSpace());
addTest(new TestKillProcessWhileWriting());
addTest(new TestMVRTree());
addTest(new TestMVStore());
addTest(new TestMVStoreBenchmark());
addTest(new TestMVStoreStopCompact());
addTest(new TestMVStoreTool());
addTest(new TestMVTableEngine());
addTest(new TestObjectDataType());
addTest(new TestRandomMapOps());
addTest(new TestSpinLock());
addTest(new TestStreamStore());
addTest(new TestTransactionStore());
// unit
addTest(new TestAnsCompression());
addTest(new TestAutoReconnect());
addTest(new TestBinaryArithmeticStream());
addTest(new TestBitStream());
addTest(new TestBnf());
addTest(new TestCache());
addTest(new TestCharsetCollator());
addTest(new TestClearReferences());
addTest(new TestCollation());
addTest(new TestCompress());
addTest(new TestConnectionInfo());
addTest(new TestDataPage());
addTest(new TestDateIso8601());
addTest(new TestExit());
addTest(new TestFile());
addTest(new TestFileLock());
addTest(new TestFtp());
addTest(new TestIntArray());
addTest(new TestIntIntHashMap());
addTest(new TestIntPerfectHash());
addTest(new TestJmx());
addTest(new TestMathUtils());
addTest(new TestMode());
addTest(new TestModifyOnWrite());
addTest(new TestOldVersion());
addTest(new TestObjectDeserialization());
addTest(new TestMultiThreadedKernel());
addTest(new TestOverflow());
addTest(new TestPageStore());
addTest(new TestPageStoreCoverage());
addTest(new TestPerfectHash());
addTest(new TestPgServer());
addTest(new TestReader());
addTest(new TestRecovery());
addTest(new TestScriptReader());
addTest(new RecoverLobTest());
addTest(createTest("org.h2.test.unit.TestServlet"));
addTest(new TestSecurity());
addTest(new TestShell());
addTest(new TestSort());
addTest(new TestStreams());
addTest(new TestStringUtils());
addTest(new TestTimeStampWithTimeZone());
addTest(new TestTraceSystem());
addTest(new TestUpgrade());
addTest(new TestUsingIndex());
addTest(new TestUtils());
addTest(new TestValue());
addTest(new TestValueHashMap());
addTest(new TestWeb());
runAddedTests();
// serial
addTest(new TestDate());
addTest(new TestDateTimeUtils());
addTest(new TestCluster());
addTest(new TestConcurrent());
addTest(new TestFileLockSerialized());
addTest(new TestFileLockProcess());
addTest(new TestFileSystem());
addTest(new TestNetUtils());
addTest(new TestPattern());
addTest(new TestTools());
addTest(new TestSampleApps());
addTest(new TestStringCache());
addTest(new TestValueMemory());
runAddedTests(1);
}
use of org.h2.test.store.TestConcurrent in project h2database by h2database.
the class TestConcurrentLinkedList method testConcurrent.
private void testConcurrent() {
final ConcurrentArrayList<Integer> test = new ConcurrentArrayList<>();
// final ConcurrentRing<Integer> test = new ConcurrentRing<Integer>();
final AtomicInteger counter = new AtomicInteger();
final AtomicInteger size = new AtomicInteger();
Task task = new Task() {
@Override
public void call() {
while (!stop) {
Thread.yield();
if (size.get() < 10) {
test.add(counter.getAndIncrement());
size.getAndIncrement();
}
}
}
};
task.execute();
for (int i = 0; i < 1000000; ) {
Thread.yield();
Integer x = test.peekFirst();
if (x == null) {
continue;
}
assertEquals(i, x.intValue());
if (test.removeFirst(x)) {
size.getAndDecrement();
i++;
}
}
task.get();
}
use of org.h2.test.store.TestConcurrent in project h2database by h2database.
the class TestCacheConcurrentLIRS method testConcurrent.
private void testConcurrent() {
CacheLongKeyLIRS.Config cc = new CacheLongKeyLIRS.Config();
cc.maxMemory = 100;
final CacheLongKeyLIRS<Integer> test = new CacheLongKeyLIRS<>(cc);
int threadCount = 8;
final CountDownLatch wait = new CountDownLatch(1);
final AtomicBoolean stopped = new AtomicBoolean();
Task[] tasks = new Task[threadCount];
final int[] getCounts = new int[threadCount];
final int offset = 1000000;
for (int i = 0; i < 100; i++) {
test.put(offset + i, i);
}
final int[] keys = new int[1000];
Random random = new Random(1);
for (int i = 0; i < keys.length; i++) {
int key;
do {
key = (int) Math.abs(random.nextGaussian() * 50);
} while (key > 100);
keys[i] = key;
}
for (int i = 0; i < threadCount; i++) {
final int x = i;
Task t = new Task() {
@Override
public void call() throws Exception {
Random random = new Random(x);
wait.await();
int i = 0;
for (; !stopped.get(); i++) {
int key = keys[random.nextInt(keys.length)];
test.get(offset + key);
if ((i & 127) == 0) {
test.put(offset + random.nextInt(100), random.nextInt());
}
}
getCounts[x] = i;
}
};
t.execute("t" + i);
tasks[i] = t;
}
wait.countDown();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
stopped.set(true);
for (Task t : tasks) {
t.get();
}
int totalCount = 0;
for (int x : getCounts) {
totalCount += x;
}
trace("requests: " + totalCount);
}
Aggregations