use of io.questdb.std.str.StringSink in project questdb by bluestreak01.
the class IODispatcherTest method assertColumn.
private void assertColumn(CharSequence expected, int index) {
final String baseDir = temp.getRoot().getAbsolutePath();
DefaultCairoConfiguration configuration = new DefaultCairoConfiguration(baseDir);
try (TableReader reader = new TableReader(configuration, "telemetry")) {
final StringSink sink = new StringSink();
sink.clear();
printer.printFullColumn(reader.getCursor(), reader.getMetadata(), index, false, sink);
TestUtils.assertEquals(expected, sink);
reader.getCursor().toTop();
sink.clear();
printer.printFullColumn(reader.getCursor(), reader.getMetadata(), index, false, sink);
TestUtils.assertEquals(expected, sink);
}
}
use of io.questdb.std.str.StringSink in project questdb by bluestreak01.
the class IODispatcherTest method testTwoThreadsSendTwoThreadsRead.
@Test
public // dispatcher or Http parser.
void testTwoThreadsSendTwoThreadsRead() throws Exception {
LOG.info().$("started testSendHttpGet").$();
final String request = "GET /status?x=1&a=%26b&c&d=x HTTP/1.1\r\n" + "Host: localhost:9000\r\n" + "Connection: keep-alive\r\n" + "Cache-Control: max-age=0\r\n" + "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\n" + "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.48 Safari/537.36\r\n" + "Accept-Encoding: gzip,deflate,sdch\r\n" + "Accept-Language: en-US,en;q=0.8\r\n" + "Cookie: textwrapon=false; textautoformat=false; wysiwyg=textarea\r\n" + "\r\n";
// the difference between request and expected is url encoding (and ':' padding, which can easily be fixed)
final String expected = "GET /status?x=1&a=&b&c&d=x HTTP/1.1\r\n" + "host:localhost:9000\r\n" + "connection:keep-alive\r\n" + "cache-control:max-age=0\r\n" + "accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\n" + "user-agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.48 Safari/537.36\r\n" + "accept-encoding:gzip,deflate,sdch\r\n" + "accept-language:en-US,en;q=0.8\r\n" + "cookie:textwrapon=false; textautoformat=false; wysiwyg=textarea\r\n" + "\r\n";
final int N = 100;
final int serverThreadCount = 2;
final int senderCount = 2;
assertMemoryLeak(() -> {
HttpServerConfiguration httpServerConfiguration = new DefaultHttpServerConfiguration();
final NetworkFacade nf = NetworkFacadeImpl.INSTANCE;
final AtomicInteger requestsReceived = new AtomicInteger();
final AtomicBoolean finished = new AtomicBoolean(false);
final SOCountDownLatch senderHalt = new SOCountDownLatch(senderCount);
try (IODispatcher<HttpConnectionContext> dispatcher = IODispatchers.create(new DefaultIODispatcherConfiguration(), (fd, dispatcher1) -> new HttpConnectionContext(httpServerConfiguration.getHttpContextConfiguration()).of(fd, dispatcher1))) {
// server will publish status of each request to this queue
final RingQueue<Status> queue = new RingQueue<>(Status::new, 1024);
final MPSequence pubSeq = new MPSequence(queue.getCycle());
SCSequence subSeq = new SCSequence();
pubSeq.then(subSeq).then(pubSeq);
final AtomicBoolean serverRunning = new AtomicBoolean(true);
final SOCountDownLatch serverHaltLatch = new SOCountDownLatch(serverThreadCount);
try {
for (int j = 0; j < serverThreadCount; j++) {
new Thread(() -> {
final StringSink sink = new StringSink();
final long responseBuf = Unsafe.malloc(32, MemoryTag.NATIVE_DEFAULT);
Unsafe.getUnsafe().putByte(responseBuf, (byte) 'A');
final HttpRequestProcessor processor = new HttpRequestProcessor() {
@Override
public void onHeadersReady(HttpConnectionContext context) {
HttpRequestHeader headers = context.getRequestHeader();
sink.clear();
sink.put(headers.getMethodLine());
sink.put("\r\n");
ObjList<CharSequence> headerNames = headers.getHeaderNames();
for (int i = 0, n = headerNames.size(); i < n; i++) {
sink.put(headerNames.getQuick(i)).put(':');
sink.put(headers.getHeader(headerNames.getQuick(i)));
sink.put("\r\n");
}
sink.put("\r\n");
boolean result;
try {
TestUtils.assertEquals(expected, sink);
result = true;
} catch (Exception e) {
result = false;
}
while (true) {
long cursor = pubSeq.next();
if (cursor < 0) {
continue;
}
queue.get(cursor).valid = result;
pubSeq.done(cursor);
break;
}
requestsReceived.incrementAndGet();
nf.send(context.getFd(), responseBuf, 1);
}
};
HttpRequestProcessorSelector selector = new HttpRequestProcessorSelector() {
@Override
public HttpRequestProcessor select(CharSequence url) {
return null;
}
@Override
public HttpRequestProcessor getDefaultProcessor() {
return processor;
}
@Override
public void close() {
}
};
while (serverRunning.get()) {
dispatcher.run(0);
dispatcher.processIOQueue((operation, context) -> context.handleClientOperation(operation, selector, EmptyRescheduleContext));
}
Unsafe.free(responseBuf, 32, MemoryTag.NATIVE_DEFAULT);
serverHaltLatch.countDown();
}).start();
}
AtomicInteger completedCount = new AtomicInteger();
for (int j = 0; j < senderCount; j++) {
int k = j;
new Thread(() -> {
long sockAddr = Net.sockaddr("127.0.0.1", 9001);
try {
for (int i = 0; i < N && !finished.get(); i++) {
long fd = Net.socketTcp(true);
try {
TestUtils.assertConnect(fd, sockAddr);
int len = request.length();
long buffer = TestUtils.toMemory(request);
try {
Assert.assertEquals(len, Net.send(fd, buffer, len));
Assert.assertEquals("fd=" + fd + ", i=" + i, 1, Net.recv(fd, buffer, 1));
LOG.info().$("i=").$(i).$(", j=").$(k).$();
Assert.assertEquals('A', Unsafe.getUnsafe().getByte(buffer));
} finally {
Unsafe.free(buffer, len, MemoryTag.NATIVE_DEFAULT);
}
} finally {
Net.close(fd);
}
}
} finally {
completedCount.incrementAndGet();
Net.freeSockAddr(sockAddr);
senderHalt.countDown();
}
}).start();
}
int receiveCount = 0;
while (receiveCount < N * senderCount) {
long cursor = subSeq.next();
if (cursor < 0) {
if (cursor == -1 && completedCount.get() == senderCount) {
Assert.fail("Not all requests successful, test failed, see previous failures");
break;
}
Thread.yield();
continue;
}
boolean valid = queue.get(cursor).valid;
subSeq.done(cursor);
Assert.assertTrue(valid);
receiveCount++;
}
} catch (Throwable e) {
e.printStackTrace();
throw e;
} finally {
serverRunning.set(false);
serverHaltLatch.await();
}
} catch (Throwable e) {
e.printStackTrace();
throw e;
} finally {
finished.set(true);
senderHalt.await();
}
Assert.assertEquals(N * senderCount, requestsReceived.get());
});
}
use of io.questdb.std.str.StringSink in project questdb by bluestreak01.
the class LineTcpParser2Test method parseMeasurement.
private boolean parseMeasurement(long bufHi) {
while (lineTcpParser.getBufferAddress() < bufHi) {
ParseResult rc;
if (!onErrorLine) {
rc = lineTcpParser.parseMeasurement(bufHi);
} else {
rc = lineTcpParser.skipMeasurement(bufHi);
}
switch(rc) {
case MEASUREMENT_COMPLETE:
startOfLineAddr = lineTcpParser.getBufferAddress() + 1;
if (!onErrorLine) {
assembleLine();
} else {
onErrorLine = false;
}
lineTcpParser.startNextMeasurement();
break;
case BUFFER_UNDERFLOW:
return false;
case ERROR:
Assert.assertFalse(onErrorLine);
onErrorLine = true;
StringSink tmpSink = new StringSink();
if (Chars.utf8Decode(startOfLineAddr, lineTcpParser.getBufferAddress(), tmpSink)) {
sink.put(tmpSink.toString());
}
sink.put("--ERROR=");
sink.put(lineTcpParser.getErrorCode().toString());
sink.put("--");
break;
}
}
return true;
}
use of io.questdb.std.str.StringSink in project questdb by bluestreak01.
the class ContinuousMemoryMTest method testLong256Append3.
@Test
public void testLong256Append3() throws Exception {
withMem((rwMem, roMem) -> {
final int N = 1_000_000;
StringSink sink = new StringSink();
for (int i = 0; i < N; i++) {
Numbers.appendLong256(rnd.nextLong(), rnd.nextLong(), rnd.nextLong(), rnd.nextLong(), sink);
rwMem.putLong256(sink);
sink.clear();
}
roMem.extend(rwMem.size());
// read these values back from
assertLong256(rwMem, N);
assertLong256(roMem, N);
});
}
use of io.questdb.std.str.StringSink in project questdb by bluestreak01.
the class MemoryCARWImplTest method testLong256Null.
@Test
public void testLong256Null() {
long pageSize = 64;
final int N = 1000;
Long256Impl long256 = new Long256Impl();
try (MemoryARW mem = new MemoryCARWImpl(pageSize, Integer.MAX_VALUE, MemoryTag.NATIVE_DEFAULT)) {
for (int i = 0; i < N; i++) {
mem.putLong256((CharSequence) null);
}
StringSink sink = new StringSink();
long offset = 0;
for (int i = 0; i < N; i++) {
mem.getLong256(offset, long256);
Assert.assertEquals(Numbers.LONG_NaN, long256.getLong0());
Assert.assertEquals(Numbers.LONG_NaN, long256.getLong1());
Assert.assertEquals(Numbers.LONG_NaN, long256.getLong2());
Assert.assertEquals(Numbers.LONG_NaN, long256.getLong3());
mem.getLong256(offset, sink);
Assert.assertEquals(0, sink.length());
offset += Long256.BYTES;
}
}
}
Aggregations