use of io.questdb.std.datetime.millitime.MillisecondClock in project questdb by bluestreak01.
the class FunctionParserTest method testNoArgFunction.
@Test
public void testNoArgFunction() throws SqlException {
functions.add(new SysdateFunctionFactory());
functions.add(new ToStrDateFunctionFactory());
FunctionParser functionParser = new FunctionParser(new DefaultCairoConfiguration(root) {
@Override
public MillisecondClock getMillisecondClock() {
return () -> {
try {
return DateFormatUtils.parseUTCDate("2018-03-04T21:40:00.000Z");
} catch (NumericException e) {
Assert.fail();
}
return 0;
};
}
}, new FunctionFactoryCache(configuration, functions));
Function function = parseFunction("to_str(sysdate(), 'EE, dd-MMM-yyyy HH:mm:ss')", new GenericRecordMetadata(), functionParser);
TestUtils.assertEquals("Sunday, 04-Mar-2018 21:40:00", function.getStr(null));
}
use of io.questdb.std.datetime.millitime.MillisecondClock in project questdb by bluestreak01.
the class IODispatcherTest method testSendHttpGetAndSimpleResponse.
@Test
public void testSendHttpGetAndSimpleResponse() throws Exception {
LOG.info().$("started testSendHttpGetAndSimpleResponse").$();
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 String expectedResponse = "HTTP/1.1 200 OK\r\n" + "Server: questDB/1.0\r\n" + "Date: Thu, 1 Jan 1970 00:00:00 GMT\r\n" + "Transfer-Encoding: chunked\r\n" + "Content-Type: text/plain; charset=utf-8\r\n" + "\r\n" + "04\r\n" + "OK\r\n" + "\r\n" + "00\r\n" + "\r\n";
assertMemoryLeak(() -> {
HttpServerConfiguration httpServerConfiguration = new DefaultHttpServerConfiguration(new DefaultHttpContextConfiguration() {
@Override
public MillisecondClock getClock() {
return () -> 0;
}
});
SOCountDownLatch connectLatch = new SOCountDownLatch(1);
SOCountDownLatch contextClosedLatch = new SOCountDownLatch(1);
AtomicInteger closeCount = new AtomicInteger(0);
try (IODispatcher<HttpConnectionContext> dispatcher = IODispatchers.create(new DefaultIODispatcherConfiguration(), new IOContextFactory<HttpConnectionContext>() {
@Override
public HttpConnectionContext newInstance(long fd, IODispatcher<HttpConnectionContext> dispatcher1) {
connectLatch.countDown();
return new HttpConnectionContext(httpServerConfiguration.getHttpContextConfiguration()) {
@Override
public void close() {
// context is closed
if (closeCount.incrementAndGet() == 1) {
super.close();
contextClosedLatch.countDown();
}
}
}.of(fd, dispatcher1);
}
})) {
StringSink sink = new StringSink();
final HttpRequestProcessorSelector selector = new HttpRequestProcessorSelector() {
@Override
public HttpRequestProcessor select(CharSequence url) {
return null;
}
@Override
public HttpRequestProcessor getDefaultProcessor() {
return new HttpRequestProcessor() {
@Override
public void onHeadersReady(HttpConnectionContext context) {
HttpRequestHeader headers = context.getRequestHeader();
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");
}
@Override
public void onRequestComplete(HttpConnectionContext context) throws PeerDisconnectedException, PeerIsSlowToReadException {
context.simpleResponse().sendStatusWithDefaultMessage(200);
}
};
}
@Override
public void close() {
}
};
AtomicBoolean serverRunning = new AtomicBoolean(true);
SOCountDownLatch serverHaltLatch = new SOCountDownLatch(1);
new Thread(() -> {
while (serverRunning.get()) {
dispatcher.run(0);
dispatcher.processIOQueue((operation, context) -> context.handleClientOperation(operation, selector, EmptyRescheduleContext));
}
serverHaltLatch.countDown();
}).start();
long fd = Net.socketTcp(true);
try {
long sockAddr = Net.sockaddr("127.0.0.1", 9001);
try {
TestUtils.assertConnect(fd, sockAddr);
connectLatch.await();
int len = request.length();
long buffer = TestUtils.toMemory(request);
try {
Assert.assertEquals(len, Net.send(fd, buffer, len));
// read response we expect
StringSink sink2 = new StringSink();
final int expectedLen = expectedResponse.length();
int read = 0;
while (read < expectedLen) {
int n = Net.recv(fd, buffer, len);
Assert.assertTrue(n > 0);
for (int i = 0; i < n; i++) {
sink2.put((char) Unsafe.getUnsafe().getByte(buffer + i));
}
// copy response bytes to sink
read += n;
}
TestUtils.assertEquals(expectedResponse, sink2);
} finally {
Unsafe.free(buffer, len, MemoryTag.NATIVE_DEFAULT);
}
Assert.assertEquals(0, Net.close(fd));
LOG.info().$("closed [fd=").$(fd).$(']').$();
fd = -1;
contextClosedLatch.await();
serverRunning.set(false);
serverHaltLatch.await();
Assert.assertEquals(0, dispatcher.getConnectionCount());
TestUtils.assertEquals(expected, sink);
} finally {
Net.freeSockAddr(sockAddr);
}
} finally {
if (fd != -1) {
Net.close(fd);
}
}
Assert.assertEquals(1, closeCount.get());
}
});
}
Aggregations