use of com.questdb.store.JournalWriter in project questdb by bluestreak01.
the class CachingWriterFactoryTest method testTwoThreadsRaceToAllocate.
@Test
public void testTwoThreadsRaceToAllocate() throws Exception {
final JournalMetadata<?> m = new JournalStructure("x").$date("ts").$().build();
int n = 2;
final CyclicBarrier barrier = new CyclicBarrier(n);
final CountDownLatch halt = new CountDownLatch(n);
final AtomicInteger errors = new AtomicInteger();
final AtomicInteger writerCount = new AtomicInteger();
for (int i = 0; i < n; i++) {
new Thread(() -> {
try {
barrier.await();
try (JournalWriter ignored = wf.writer(m)) {
writerCount.incrementAndGet();
} catch (WriterBusyException ignored) {
}
} catch (Exception e) {
e.printStackTrace();
errors.incrementAndGet();
} finally {
halt.countDown();
}
}).start();
}
halt.await();
// this check is unreliable on slow build servers
// it is very often the case that there are limited number of cores
// available and threads execute sequentially rather than
// simultaneously. We should check that none of the threads
// receive error.
// Assert.assertEquals(1, writerCount.get());
Assert.assertEquals(0, errors.get());
Assert.assertEquals(1, wf.countFreeWriters());
}
use of com.questdb.store.JournalWriter in project questdb by bluestreak01.
the class CachingWriterFactoryTest method testLockNonExisting.
@Test
public void testLockNonExisting() throws Exception {
final JournalMetadata<?> x = new JournalStructure("x").$date("ts").$().build();
wf.lock(x.getName());
try {
wf.writer(x);
Assert.fail();
} catch (JournalLockedException ignored) {
}
wf.unlock(x.getName());
try (JournalWriter wx = wf.writer(x)) {
Assert.assertNotNull(wx);
}
}
use of com.questdb.store.JournalWriter in project questdb by bluestreak01.
the class CachingWriterFactoryTest method testOneThreadGetRelease.
@Test
public void testOneThreadGetRelease() throws Exception {
final JournalMetadata<?> m = new JournalStructure("x").$date("ts").$().build();
JournalWriter x;
JournalWriter y;
x = wf.writer(m);
try {
Assert.assertEquals(0, wf.countFreeWriters());
Assert.assertNotNull(x);
Assert.assertTrue(x.isOpen());
Assert.assertTrue(x == wf.writer(m));
} finally {
x.close();
}
Assert.assertEquals(1, wf.countFreeWriters());
y = wf.writer(m);
try {
Assert.assertNotNull(y);
Assert.assertTrue(y.isOpen());
Assert.assertTrue(y == x);
} finally {
y.close();
}
Assert.assertEquals(1, wf.countFreeWriters());
}
use of com.questdb.store.JournalWriter in project questdb by bluestreak01.
the class CachingWriterFactoryTest method testAllocateAndClear.
@Test
public void testAllocateAndClear() throws Exception {
final JournalMetadata<?> m = new JournalStructure("z").$date("ts").$().build();
int n = 2;
final CyclicBarrier barrier = new CyclicBarrier(n);
final CountDownLatch halt = new CountDownLatch(n);
final AtomicInteger errors = new AtomicInteger();
final AtomicInteger writerCount = new AtomicInteger();
new Thread(() -> {
try {
for (int i = 0; i < 1000; i++) {
try (JournalWriter ignored = wf.writer(m)) {
writerCount.incrementAndGet();
} catch (WriterBusyException ignored) {
}
if (i == 1) {
barrier.await();
}
LockSupport.parkNanos(10L);
}
} catch (Exception e) {
e.printStackTrace();
errors.incrementAndGet();
} finally {
halt.countDown();
}
}).start();
new Thread(() -> {
try {
barrier.await();
for (int i = 0; i < 1000; i++) {
wf.releaseInactive();
LockSupport.parkNanos(10L);
}
} catch (Exception e) {
e.printStackTrace();
errors.incrementAndGet();
} finally {
halt.countDown();
}
}).start();
halt.await();
Assert.assertTrue(writerCount.get() > 0);
Assert.assertEquals(0, errors.get());
}
use of com.questdb.store.JournalWriter in project questdb by bluestreak01.
the class CachingWriterFactoryTest method testFactoryCloseBeforeRelease.
@Test
public void testFactoryCloseBeforeRelease() throws Exception {
final JournalMetadata<?> m = new JournalStructure("x").$date("ts").$().build();
JournalWriter x;
x = wf.writer(m);
try {
Assert.assertEquals(0, wf.countFreeWriters());
Assert.assertNotNull(x);
Assert.assertTrue(x.isOpen());
Assert.assertTrue(x == wf.writer(m));
wf.close();
} finally {
x.close();
}
Assert.assertFalse(x.isOpen());
try {
wf.writer(m);
} catch (FactoryClosedException ignored) {
}
}
Aggregations