1
1
package org .influxdb .impl ;
2
2
3
+ import org .influxdb .InfluxDB ;
4
+ import org .influxdb .dto .BatchPoints ;
5
+ import org .influxdb .dto .Point ;
6
+ import org .junit .Test ;
7
+ import org .mockito .ArgumentCaptor ;
8
+
9
+ import java .io .IOException ;
10
+ import java .util .concurrent .TimeUnit ;
11
+
12
+ import static org .assertj .core .api .Assertions .assertThat ;
3
13
import static org .mockito .Mockito .any ;
4
14
import static org .mockito .Mockito .doThrow ;
5
15
import static org .mockito .Mockito .mock ;
6
16
import static org .mockito .Mockito .times ;
7
17
import static org .mockito .Mockito .verify ;
8
18
import static org .mockito .Mockito .verifyNoMoreInteractions ;
9
19
10
- import java .io .IOException ;
11
- import java .util .concurrent .TimeUnit ;
12
-
13
- import org .influxdb .InfluxDB ;
14
- import org .influxdb .dto .BatchPoints ;
15
- import org .influxdb .dto .Point ;
16
- import org .junit .Test ;
17
-
18
20
public class BatchProcessorTest {
19
21
20
22
@ Test
21
23
public void testSchedulerExceptionHandling () throws InterruptedException , IOException {
22
24
InfluxDB mockInfluxDB = mock (InfluxDBImpl .class );
23
- BatchProcessor batchProcessor = BatchProcessor .builder (mockInfluxDB ).actions (Integer .MAX_VALUE )
24
- .interval (1 , TimeUnit .NANOSECONDS ).build ();
25
+ BatchProcessor batchProcessor = BatchProcessor .builder (mockInfluxDB )
26
+ .actions (Integer .MAX_VALUE )
27
+ .consistencyLevel (InfluxDB .ConsistencyLevel .ONE )
28
+ .interval (1 , TimeUnit .NANOSECONDS ).build ();
25
29
26
30
doThrow (new RuntimeException ()).when (mockInfluxDB ).write (any (BatchPoints .class ));
27
31
@@ -44,7 +48,9 @@ public void testSchedulerExceptionHandling() throws InterruptedException, IOExce
44
48
@ Test
45
49
public void testBatchWriteWithDifferenctRp () throws InterruptedException , IOException {
46
50
InfluxDB mockInfluxDB = mock (InfluxDBImpl .class );
47
- BatchProcessor batchProcessor = BatchProcessor .builder (mockInfluxDB ).actions (Integer .MAX_VALUE )
51
+ BatchProcessor batchProcessor = BatchProcessor .builder (mockInfluxDB )
52
+ .actions (Integer .MAX_VALUE )
53
+ .consistencyLevel (InfluxDB .ConsistencyLevel .ONE )
48
54
.interval (1 , TimeUnit .NANOSECONDS ).build ();
49
55
50
56
Point point = Point .measurement ("cpu" ).field ("6" , "" ).build ();
@@ -59,11 +65,37 @@ public void testBatchWriteWithDifferenctRp() throws InterruptedException, IOExce
59
65
verify (mockInfluxDB , times (2 )).write (any (BatchPoints .class ));
60
66
}
61
67
68
+ @ Test
69
+ public void testConsistencyLevelIsHonored () {
70
+ InfluxDB .ConsistencyLevel desiredConsistencyLevel = InfluxDB .ConsistencyLevel .QUORUM ;
71
+
72
+ InfluxDB mockInfluxDB = mock (InfluxDBImpl .class );
73
+ BatchProcessor batchProcessor = BatchProcessor .builder (mockInfluxDB )
74
+ .actions (Integer .MAX_VALUE )
75
+ .consistencyLevel (desiredConsistencyLevel )
76
+ .interval (1 , TimeUnit .DAYS ).build ();
77
+
78
+ Point point = Point .measurement ("test" ).addField ("region" , "a" ).build ();
79
+ BatchProcessor .HttpBatchEntry httpBatchEntry = new BatchProcessor .HttpBatchEntry (point , "http" , "http-rp" );
80
+
81
+ batchProcessor .put (httpBatchEntry );
82
+
83
+ batchProcessor .flush ();
84
+
85
+ ArgumentCaptor <BatchPoints > batchPoints = ArgumentCaptor .forClass (BatchPoints .class );
86
+
87
+ verify (mockInfluxDB , times (1 )).write (batchPoints .capture ());
88
+
89
+ assertThat (batchPoints .getAllValues ()).hasSize (1 );
90
+ assertThat (batchPoints .getValue ().getConsistency ()).isEqualTo (desiredConsistencyLevel );
91
+ }
92
+
62
93
@ Test
63
94
public void testFlushWritesBufferedPointsAndDoesNotShutdownScheduler () throws InterruptedException {
64
95
InfluxDB mockInfluxDB = mock (InfluxDBImpl .class );
65
96
BatchProcessor batchProcessor = BatchProcessor .builder (mockInfluxDB )
66
97
.actions (Integer .MAX_VALUE )
98
+ .consistencyLevel (InfluxDB .ConsistencyLevel .ONE )
67
99
.interval (1 , TimeUnit .NANOSECONDS ).build ();
68
100
69
101
Point point = Point .measurement ("test" ).addField ("region" , "a" ).build ();
@@ -88,21 +120,28 @@ public void testFlushWritesBufferedPointsAndDoesNotShutdownScheduler() throws In
88
120
@ Test (expected = IllegalArgumentException .class )
89
121
public void testActionsIsZero () throws InterruptedException , IOException {
90
122
InfluxDB mockInfluxDB = mock (InfluxDBImpl .class );
91
- BatchProcessor .builder (mockInfluxDB ).actions (0 )
123
+ BatchProcessor .builder (mockInfluxDB ).actions (0 ). consistencyLevel ( InfluxDB . ConsistencyLevel . ONE )
92
124
.interval (1 , TimeUnit .NANOSECONDS ).build ();
93
125
}
94
126
95
127
@ Test (expected = IllegalArgumentException .class )
96
128
public void testIntervalIsZero () throws InterruptedException , IOException {
97
129
InfluxDB mockInfluxDB = mock (InfluxDBImpl .class );
98
- BatchProcessor .builder (mockInfluxDB ).actions (1 )
130
+ BatchProcessor .builder (mockInfluxDB ).actions (1 ). consistencyLevel ( InfluxDB . ConsistencyLevel . ONE )
99
131
.interval (0 , TimeUnit .NANOSECONDS ).build ();
100
132
}
101
133
102
134
@ Test (expected = NullPointerException .class )
103
135
public void testInfluxDBIsNull () throws InterruptedException , IOException {
104
136
InfluxDB mockInfluxDB = null ;
105
- BatchProcessor .builder (mockInfluxDB ).actions (1 )
137
+ BatchProcessor .builder (mockInfluxDB ).actions (1 ). consistencyLevel ( InfluxDB . ConsistencyLevel . ONE )
106
138
.interval (1 , TimeUnit .NANOSECONDS ).build ();
107
139
}
140
+
141
+ @ Test (expected = NullPointerException .class )
142
+ public void testConsistencyLevelIsNull () {
143
+ InfluxDB mockInfluxDB = mock (InfluxDBImpl .class );
144
+ BatchProcessor .builder (mockInfluxDB ).actions (1 ).consistencyLevel (null )
145
+ .interval (1 , TimeUnit .NANOSECONDS ).build ();
146
+ }
108
147
}
0 commit comments