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