11package org .influxdb ;
22
3- import org .influxdb .InfluxDB .LogLevel ;
4- import org .influxdb .dto .*;
5- import org .influxdb .impl .InfluxDBImpl ;
6- import org .junit .Assert ;
7- import org .junit .Before ;
8- import org .junit .Test ;
9-
103import java .io .IOException ;
114import java .util .ArrayList ;
125import java .util .Arrays ;
136import java .util .List ;
147import java .util .Set ;
8+ import java .util .concurrent .BlockingQueue ;
9+ import java .util .concurrent .CountDownLatch ;
10+ import java .util .concurrent .LinkedBlockingQueue ;
1511import java .util .concurrent .ThreadFactory ;
1612import java .util .concurrent .TimeUnit ;
13+ import java .util .function .Consumer ;
14+
15+ import org .influxdb .InfluxDB .LogLevel ;
16+ import org .influxdb .dto .BatchPoints ;
17+ import org .influxdb .dto .Point ;
18+ import org .influxdb .dto .Pong ;
19+ import org .influxdb .dto .Query ;
20+ import org .influxdb .dto .QueryResult ;
21+ import org .influxdb .impl .InfluxDBImpl ;
22+ import org .junit .Assert ;
23+ import org .junit .Before ;
24+ import org .junit .Rule ;
25+ import org .junit .Test ;
26+ import org .junit .rules .ExpectedException ;
1727
1828import com .google .common .util .concurrent .Uninterruptibles ;
1929
@@ -29,6 +39,7 @@ public class InfluxDBTest {
2939 private final static int UDP_PORT = 8089 ;
3040 private final static String UDP_DATABASE = "udp" ;
3141
42+ @ Rule public final ExpectedException exception = ExpectedException .none ();
3243 /**
3344 * Create a influxDB connection before all tests start.
3445 *
@@ -57,7 +68,7 @@ public void setUp() throws InterruptedException, IOException {
5768 this .influxDB .createDatabase (UDP_DATABASE );
5869 // String logs = CharStreams.toString(new InputStreamReader(containerLogsStream,
5970 // Charsets.UTF_8));
60- System .out .println ("##################################################################################" );
71+ System .out .println ("################################################################################## " );
6172 // System.out.println("Container Logs: \n" + logs);
6273 System .out .println ("# Connected to InfluxDB Version: " + this .influxDB .version () + " #" );
6374 System .out .println ("##################################################################################" );
@@ -219,8 +230,8 @@ public void testWriteStringDataThroughUDP() {
219230 @ Test
220231 public void testWriteMultipleStringDataThroughUDP () {
221232 String measurement = TestUtils .getRandomMeasurement ();
222- this .influxDB .write (UDP_PORT , measurement + ",atag=test1 idle=100,usertime=10,system=1\n " +
223- measurement + ",atag=test2 idle=200,usertime=20,system=2\n " +
233+ this .influxDB .write (UDP_PORT , measurement + ",atag=test1 idle=100,usertime=10,system=1\n " +
234+ measurement + ",atag=test2 idle=200,usertime=20,system=2\n " +
224235 measurement + ",atag=test3 idle=300,usertime=30,system=3" );
225236 Uninterruptibles .sleepUninterruptibly (2 , TimeUnit .SECONDS );
226237 Query query = new Query ("SELECT * FROM " + measurement + " GROUP BY *" , UDP_DATABASE );
@@ -250,12 +261,12 @@ public void testWriteMultipleStringDataLinesThroughUDP() {
250261 Assert .assertEquals (3 , result .getResults ().get (0 ).getSeries ().size ());
251262 Assert .assertEquals (result .getResults ().get (0 ).getSeries ().get (0 ).getTags ().get ("atag" ), "test1" );
252263 Assert .assertEquals (result .getResults ().get (0 ).getSeries ().get (1 ).getTags ().get ("atag" ), "test2" );
253- Assert .assertEquals (result .getResults ().get (0 ).getSeries ().get (2 ).getTags ().get ("atag" ), "test3" );
264+ Assert .assertEquals (result .getResults ().get (0 ).getSeries ().get (2 ).getTags ().get ("atag" ), "test3" );
254265 }
255266
256267 /**
257268 * When batch of points' size is over UDP limit, the expected exception
258- * is java.lang.RuntimeException: java.net.SocketException:
269+ * is java.lang.RuntimeException: java.net.SocketException:
259270 * The message is larger than the maximum supported by the underlying transport: Datagram send failed
260271 * @throws Exception
261272 */
@@ -487,4 +498,93 @@ public void testWriteEnableGzipAndDisableGzip() {
487498 }
488499 }
489500
501+ /**
502+ * Test chunking.
503+ * @throws InterruptedException
504+ */
505+ @ Test
506+ public void testChunking () throws InterruptedException {
507+ if (this .influxDB .version ().startsWith ("0." ) || this .influxDB .version ().startsWith ("1.0" )) {
508+ // do not test version 0.13 and 1.0
509+ return ;
510+ }
511+ String dbName = "write_unittest_" + System .currentTimeMillis ();
512+ this .influxDB .createDatabase (dbName );
513+ String rp = TestUtils .defaultRetentionPolicy (this .influxDB .version ());
514+ BatchPoints batchPoints = BatchPoints .database (dbName ).retentionPolicy (rp ).build ();
515+ Point point1 = Point .measurement ("disk" ).tag ("atag" , "a" ).addField ("used" , 60L ).addField ("free" , 1L ).build ();
516+ Point point2 = Point .measurement ("disk" ).tag ("atag" , "b" ).addField ("used" , 70L ).addField ("free" , 2L ).build ();
517+ Point point3 = Point .measurement ("disk" ).tag ("atag" , "c" ).addField ("used" , 80L ).addField ("free" , 3L ).build ();
518+ batchPoints .point (point1 );
519+ batchPoints .point (point2 );
520+ batchPoints .point (point3 );
521+ this .influxDB .write (batchPoints );
522+
523+ Uninterruptibles .sleepUninterruptibly (2 , TimeUnit .SECONDS );
524+ final BlockingQueue <QueryResult > queue = new LinkedBlockingQueue <>();
525+ Query query = new Query ("SELECT * FROM disk" , dbName );
526+ this .influxDB .query (query , 2 , new Consumer <QueryResult >() {
527+ @ Override
528+ public void accept (QueryResult result ) {
529+ queue .add (result );
530+ }});
531+
532+ Uninterruptibles .sleepUninterruptibly (2 , TimeUnit .SECONDS );
533+ this .influxDB .deleteDatabase (dbName );
534+
535+ QueryResult result = queue .poll (20 , TimeUnit .SECONDS );
536+ Assert .assertNotNull (result );
537+ System .out .println (result );
538+ Assert .assertEquals (2 , result .getResults ().get (0 ).getSeries ().get (0 ).getValues ().size ());
539+
540+ result = queue .poll (20 , TimeUnit .SECONDS );
541+ Assert .assertNotNull (result );
542+ System .out .println (result );
543+ Assert .assertEquals (1 , result .getResults ().get (0 ).getSeries ().get (0 ).getValues ().size ());
544+ }
545+
546+ /**
547+ * Test chunking edge case.
548+ * @throws InterruptedException
549+ */
550+ @ Test
551+ public void testChunkingFail () throws InterruptedException {
552+ if (this .influxDB .version ().startsWith ("0." ) || this .influxDB .version ().startsWith ("1.0" )) {
553+ // do not test version 0.13 and 1.0
554+ return ;
555+ }
556+ String dbName = "write_unittest_" + System .currentTimeMillis ();
557+ this .influxDB .createDatabase (dbName );
558+ final CountDownLatch countDownLatch = new CountDownLatch (1 );
559+ Query query = new Query ("XXX" , dbName );
560+ this .influxDB .query (query , 10 , new Consumer <QueryResult >() {
561+ @ Override
562+ public void accept (QueryResult result ) {
563+ countDownLatch .countDown ();
564+ }
565+ });
566+ this .influxDB .deleteDatabase (dbName );
567+ Assert .assertFalse (countDownLatch .await (10 , TimeUnit .SECONDS ));
568+ }
569+
570+ /**
571+ * Test chunking on 0.13 and 1.0.
572+ * @throws InterruptedException
573+ */
574+ @ Test ()
575+ public void testChunkingOldVersion () throws InterruptedException {
576+
577+ if (this .influxDB .version ().startsWith ("0." ) || this .influxDB .version ().startsWith ("1.0" )) {
578+
579+ this .exception .expect (RuntimeException .class );
580+ String dbName = "write_unittest_" + System .currentTimeMillis ();
581+ Query query = new Query ("SELECT * FROM cpu GROUP BY *" , dbName );
582+ this .influxDB .query (query , 10 , new Consumer <QueryResult >() {
583+ @ Override
584+ public void accept (QueryResult result ) {
585+ }
586+ });
587+ }
588+ }
589+
490590}
0 commit comments