46
46
import java .io .IOException ;
47
47
import java .io .InputStream ;
48
48
import java .nio .charset .StandardCharsets ;
49
+ import java .util .Arrays ;
49
50
import java .util .Collections ;
50
51
import java .util .Date ;
51
52
import java .util .List ;
@@ -228,6 +229,8 @@ public void testRealtimeRun() throws Exception {
228
229
flushJobResponse = new FlushJobAction .Response (true , new Date (2000 ));
229
230
Bucket bucket = mock (Bucket .class );
230
231
when (bucket .getTimestamp ()).thenReturn (new Date (2000 ));
232
+ when (bucket .getEpoch ()).thenReturn (2L );
233
+ when (bucket .getBucketSpan ()).thenReturn (4L );
231
234
when (flushJobFuture .actionGet ()).thenReturn (flushJobResponse );
232
235
when (client .execute (same (FlushJobAction .INSTANCE ), flushJobRequests .capture ())).thenReturn (flushJobFuture );
233
236
when (delayedDataDetector .detectMissingData (2000 ))
@@ -272,10 +275,10 @@ public void testRealtimeRun() throws Exception {
272
275
new Date (currentTime ),
273
276
SystemUser .NAME ,
274
277
bucket .getTimestamp (),
275
- bucket .getTimestamp ( ),
278
+ new Date (( bucket .getEpoch () + bucket . getBucketSpan ()) * 1000 ),
276
279
jobId ,
277
- null ,
278
- null ,
280
+ new Date ( currentTime ) ,
281
+ SystemUser . NAME ,
279
282
"annotation" );
280
283
281
284
IndexRequest request = new IndexRequest (AnnotationIndex .WRITE_ALIAS_NAME , ElasticsearchMappings .DOC_TYPE );
@@ -288,8 +291,13 @@ public void testRealtimeRun() throws Exception {
288
291
assertThat (request .source (), equalTo (indexRequestArgumentCaptor .getValue ().source ()));
289
292
290
293
// Execute a fourth time, this time we return a new delayedDataDetector response to verify annotation gets updated
294
+ Bucket bucket2 = mock (Bucket .class );
295
+ when (bucket2 .getTimestamp ()).thenReturn (new Date (6000 ));
296
+ when (bucket2 .getEpoch ()).thenReturn (6L );
297
+ when (bucket2 .getBucketSpan ()).thenReturn (4L );
291
298
when (delayedDataDetector .detectMissingData (2000 ))
292
- .thenReturn (Collections .singletonList (BucketWithMissingData .fromMissingAndBucket (15 , bucket )));
299
+ .thenReturn (Arrays .asList (BucketWithMissingData .fromMissingAndBucket (10 , bucket ),
300
+ BucketWithMissingData .fromMissingAndBucket (5 , bucket2 )));
293
301
currentTime = currentTime + DatafeedJob .MISSING_DATA_CHECK_INTERVAL_MS + 1 ;
294
302
inputStream = new ByteArrayInputStream (contentBytes );
295
303
when (dataExtractor .hasNext ()).thenReturn (true ).thenReturn (false );
@@ -299,14 +307,15 @@ public void testRealtimeRun() throws Exception {
299
307
300
308
msg = Messages .getMessage (Messages .JOB_AUDIT_DATAFEED_MISSING_DATA ,
301
309
15 ,
302
- XContentElasticsearchExtension .DEFAULT_DATE_PRINTER .print (2000 ));
310
+ XContentElasticsearchExtension .DEFAULT_DATE_PRINTER .print (6000 ));
303
311
// What we expect the updated annotation to be indexed as
304
312
IndexRequest indexRequest = new IndexRequest (AnnotationIndex .WRITE_ALIAS_NAME , ElasticsearchMappings .DOC_TYPE );
305
313
indexRequest .id (annotationDocId );
306
314
Annotation updatedAnnotation = new Annotation (expectedAnnotation );
307
315
updatedAnnotation .setAnnotation (msg );
308
316
updatedAnnotation .setModifiedTime (new Date (currentTime ));
309
317
updatedAnnotation .setModifiedUsername (SystemUser .NAME );
318
+ updatedAnnotation .setEndTimestamp (new Date ((bucket2 .getEpoch () + bucket2 .getBucketSpan ()) * 1000 ));
310
319
try (XContentBuilder xContentBuilder = updatedAnnotation .toXContent (XContentFactory .jsonBuilder (), ToXContent .EMPTY_PARAMS )) {
311
320
indexRequest .source (xContentBuilder );
312
321
}
@@ -319,6 +328,17 @@ public void testRealtimeRun() throws Exception {
319
328
assertThat (indexRequest .source ().utf8ToString (),
320
329
equalTo (updateRequestArgumentCaptor .getValue ().source ().utf8ToString ()));
321
330
assertThat (updateRequestArgumentCaptor .getValue ().opType (), equalTo (DocWriteRequest .OpType .INDEX ));
331
+
332
+ // Execute a fifth time, no changes should occur as annotation is the same
333
+ currentTime = currentTime + DatafeedJob .MISSING_DATA_CHECK_INTERVAL_MS + 1 ;
334
+ inputStream = new ByteArrayInputStream (contentBytes );
335
+ when (dataExtractor .hasNext ()).thenReturn (true ).thenReturn (false );
336
+ when (dataExtractor .next ()).thenReturn (Optional .of (inputStream ));
337
+ when (dataExtractorFactory .newExtractor (anyLong (), anyLong ())).thenReturn (dataExtractor );
338
+ datafeedJob .runRealtime ();
339
+
340
+ // We should not get 3 index requests for the annotations
341
+ verify (client , atMost (2 )).index (any ());
322
342
}
323
343
324
344
public void testEmptyDataCountGivenlookback () throws Exception {
0 commit comments