-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
InternalCeQueueImplTest.java
747 lines (622 loc) · 31.2 KB
/
InternalCeQueueImplTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
/*
* SonarQube
* Copyright (C) 2009-2018 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.ce.queue;
import com.google.common.collect.ImmutableSet;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.List;
import java.util.Optional;
import java.util.Random;
import javax.annotation.Nullable;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.api.utils.System2;
import org.sonar.api.utils.internal.AlwaysIncreasingSystem2;
import org.sonar.ce.container.ComputeEngineStatus;
import org.sonar.ce.monitoring.CEQueueStatus;
import org.sonar.ce.monitoring.CEQueueStatusImpl;
import org.sonar.core.util.UuidFactory;
import org.sonar.core.util.UuidFactoryImpl;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.db.ce.CeActivityDto;
import org.sonar.db.ce.CeQueueDto;
import org.sonar.db.ce.CeTaskTypes;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.ComponentTesting;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.server.computation.task.step.TypedException;
import org.sonar.server.organization.DefaultOrganization;
import org.sonar.server.organization.DefaultOrganizationProvider;
import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.sonar.ce.container.ComputeEngineStatus.Status.STARTED;
import static org.sonar.ce.container.ComputeEngineStatus.Status.STOPPING;
public class InternalCeQueueImplTest {
private static final String AN_ANALYSIS_UUID = "U1";
private static final String WORKER_UUID_1 = "worker uuid 1";
private static final String WORKER_UUID_2 = "worker uuid 2";
private System2 system2 = new AlwaysIncreasingSystem2();
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Rule
public DbTester db = DbTester.create(system2);
private DbSession session = db.getSession();
private UuidFactory uuidFactory = UuidFactoryImpl.INSTANCE;
private CEQueueStatus queueStatus = new CEQueueStatusImpl(db.getDbClient());
private DefaultOrganizationProvider defaultOrganizationProvider = mock(DefaultOrganizationProvider.class);
private ComputeEngineStatus computeEngineStatus = mock(ComputeEngineStatus.class);
private InternalCeQueue underTest = new InternalCeQueueImpl(system2, db.getDbClient(), uuidFactory, queueStatus, defaultOrganizationProvider, computeEngineStatus);
@Before
public void setUp() {
OrganizationDto defaultOrganization = db.getDefaultOrganization();
when(defaultOrganizationProvider.get()).thenReturn(DefaultOrganization.newBuilder()
.setUuid(defaultOrganization.getUuid())
.setKey(defaultOrganization.getKey())
.setName(defaultOrganization.getName())
.setCreatedAt(defaultOrganization.getCreatedAt())
.setUpdatedAt(defaultOrganization.getUpdatedAt())
.build());
when(computeEngineStatus.getStatus()).thenReturn(STARTED);
}
@Test
public void submit_returns_task_populated_from_CeTaskSubmit_and_creates_CeQueue_row() {
CeTaskSubmit taskSubmit = createTaskSubmit(CeTaskTypes.REPORT, "PROJECT_1", "rob");
CeTask task = underTest.submit(taskSubmit);
verifyCeTask(taskSubmit, task, null);
verifyCeQueueDtoForTaskSubmit(taskSubmit);
}
@Test
public void submit_populates_component_name_and_key_of_CeTask_if_component_exists() {
ComponentDto componentDto = insertComponent(newComponentDto("PROJECT_1"));
CeTaskSubmit taskSubmit = createTaskSubmit(CeTaskTypes.REPORT, componentDto.uuid(), null);
CeTask task = underTest.submit(taskSubmit);
verifyCeTask(taskSubmit, task, componentDto);
}
@Test
public void submit_returns_task_without_component_info_when_submit_has_none() {
CeTaskSubmit taskSubmit = createTaskSubmit("not cpt related");
CeTask task = underTest.submit(taskSubmit);
verifyCeTask(taskSubmit, task, null);
}
@Test
public void massSubmit_returns_tasks_for_each_CeTaskSubmit_populated_from_CeTaskSubmit_and_creates_CeQueue_row_for_each() {
CeTaskSubmit taskSubmit1 = createTaskSubmit(CeTaskTypes.REPORT, "PROJECT_1", "rob");
CeTaskSubmit taskSubmit2 = createTaskSubmit("some type");
List<CeTask> tasks = underTest.massSubmit(asList(taskSubmit1, taskSubmit2));
assertThat(tasks).hasSize(2);
verifyCeTask(taskSubmit1, tasks.get(0), null);
verifyCeTask(taskSubmit2, tasks.get(1), null);
verifyCeQueueDtoForTaskSubmit(taskSubmit1);
verifyCeQueueDtoForTaskSubmit(taskSubmit2);
}
@Test
public void massSubmit_populates_component_name_and_key_of_CeTask_if_component_exists() {
ComponentDto componentDto1 = insertComponent(newComponentDto("PROJECT_1"));
CeTaskSubmit taskSubmit1 = createTaskSubmit(CeTaskTypes.REPORT, componentDto1.uuid(), null);
CeTaskSubmit taskSubmit2 = createTaskSubmit("something", "non existing component uuid", null);
List<CeTask> tasks = underTest.massSubmit(asList(taskSubmit1, taskSubmit2));
assertThat(tasks).hasSize(2);
verifyCeTask(taskSubmit1, tasks.get(0), componentDto1);
verifyCeTask(taskSubmit2, tasks.get(1), null);
}
@Test
public void peek_throws_NPE_if_workerUUid_is_null() {
expectedException.expect(NullPointerException.class);
expectedException.expectMessage("workerUuid can't be null");
underTest.peek(null);
}
@Test
public void test_remove() {
CeTask task = submit(CeTaskTypes.REPORT, "PROJECT_1");
Optional<CeTask> peek = underTest.peek(WORKER_UUID_1);
underTest.remove(peek.get(), CeActivityDto.Status.SUCCESS, null, null);
// queue is empty
assertThat(db.getDbClient().ceQueueDao().selectByUuid(db.getSession(), task.getUuid()).isPresent()).isFalse();
assertThat(underTest.peek(WORKER_UUID_2).isPresent()).isFalse();
// available in history
Optional<CeActivityDto> history = db.getDbClient().ceActivityDao().selectByUuid(db.getSession(), task.getUuid());
assertThat(history.isPresent()).isTrue();
assertThat(history.get().getStatus()).isEqualTo(CeActivityDto.Status.SUCCESS);
assertThat(history.get().getIsLast()).isTrue();
assertThat(history.get().getAnalysisUuid()).isNull();
}
@Test
public void remove_throws_IAE_if_exception_is_provided_but_status_is_SUCCESS() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("Error can be provided only when status is FAILED");
underTest.remove(mock(CeTask.class), CeActivityDto.Status.SUCCESS, null, new RuntimeException("Some error"));
}
@Test
public void remove_throws_IAE_if_exception_is_provided_but_status_is_CANCELED() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("Error can be provided only when status is FAILED");
underTest.remove(mock(CeTask.class), CeActivityDto.Status.CANCELED, null, new RuntimeException("Some error"));
}
@Test
public void remove_does_not_set_analysisUuid_in_CeActivity_when_CeTaskResult_has_no_analysis_uuid() {
CeTask task = submit(CeTaskTypes.REPORT, "PROJECT_1");
Optional<CeTask> peek = underTest.peek(WORKER_UUID_1);
underTest.remove(peek.get(), CeActivityDto.Status.SUCCESS, newTaskResult(null), null);
// available in history
Optional<CeActivityDto> history = db.getDbClient().ceActivityDao().selectByUuid(db.getSession(), task.getUuid());
assertThat(history.isPresent()).isTrue();
assertThat(history.get().getAnalysisUuid()).isNull();
}
@Test
public void remove_sets_analysisUuid_in_CeActivity_when_CeTaskResult_has_analysis_uuid() {
CeTask task = submit(CeTaskTypes.REPORT, "PROJECT_1");
Optional<CeTask> peek = underTest.peek(WORKER_UUID_2);
underTest.remove(peek.get(), CeActivityDto.Status.SUCCESS, newTaskResult(AN_ANALYSIS_UUID), null);
// available in history
Optional<CeActivityDto> history = db.getDbClient().ceActivityDao().selectByUuid(db.getSession(), task.getUuid());
assertThat(history.isPresent()).isTrue();
assertThat(history.get().getAnalysisUuid()).isEqualTo("U1");
}
@Test
public void remove_saves_error_message_and_stacktrace_when_exception_is_provided() {
Throwable error = new NullPointerException("Fake NPE to test persistence to DB");
CeTask task = submit(CeTaskTypes.REPORT, "PROJECT_1");
Optional<CeTask> peek = underTest.peek(WORKER_UUID_1);
underTest.remove(peek.get(), CeActivityDto.Status.FAILED, null, error);
Optional<CeActivityDto> activityDto = db.getDbClient().ceActivityDao().selectByUuid(session, task.getUuid());
assertThat(activityDto).isPresent();
assertThat(activityDto.get().getErrorMessage()).isEqualTo(error.getMessage());
assertThat(activityDto.get().getErrorStacktrace()).isEqualToIgnoringWhitespace(stacktraceToString(error));
assertThat(activityDto.get().getErrorType()).isNull();
}
@Test
public void remove_saves_error_when_TypedMessageException_is_provided() {
Throwable error = new TypedExceptionImpl("aType", "aMessage");
CeTask task = submit(CeTaskTypes.REPORT, "PROJECT_1");
Optional<CeTask> peek = underTest.peek(WORKER_UUID_1);
underTest.remove(peek.get(), CeActivityDto.Status.FAILED, null, error);
CeActivityDto activityDto = db.getDbClient().ceActivityDao().selectByUuid(session, task.getUuid()).get();
assertThat(activityDto.getErrorType()).isEqualTo("aType");
assertThat(activityDto.getErrorMessage()).isEqualTo("aMessage");
assertThat(activityDto.getErrorStacktrace()).isEqualToIgnoringWhitespace(stacktraceToString(error));
}
private static class TypedExceptionImpl extends RuntimeException implements TypedException {
private final String type;
private TypedExceptionImpl(String type, String message) {
super(message);
this.type = type;
}
@Override
public String getType() {
return type;
}
}
@Test
public void remove_copies_executionCount_and_workerUuid() {
db.getDbClient().ceQueueDao().insert(session, new CeQueueDto()
.setUuid("uuid")
.setTaskType("foo")
.setStatus(CeQueueDto.Status.PENDING)
.setWorkerUuid("Dustin")
.setExecutionCount(2));
db.commit();
underTest.remove(new CeTask.Builder()
.setOrganizationUuid("foo")
.setUuid("uuid")
.setType("bar")
.build(), CeActivityDto.Status.SUCCESS, null, null);
CeActivityDto dto = db.getDbClient().ceActivityDao().selectByUuid(db.getSession(), "uuid").get();
assertThat(dto.getExecutionCount()).isEqualTo(2);
assertThat(dto.getWorkerUuid()).isEqualTo("Dustin");
}
@Test
public void fail_to_remove_if_not_in_queue() {
CeTask task = submit(CeTaskTypes.REPORT, "PROJECT_1");
underTest.remove(task, CeActivityDto.Status.SUCCESS, null, null);
expectedException.expect(IllegalStateException.class);
underTest.remove(task, CeActivityDto.Status.SUCCESS, null, null);
}
@Test
public void test_peek() {
CeTask task = submit(CeTaskTypes.REPORT, "PROJECT_1");
Optional<CeTask> peek = underTest.peek(WORKER_UUID_1);
assertThat(peek.isPresent()).isTrue();
assertThat(peek.get().getUuid()).isEqualTo(task.getUuid());
assertThat(peek.get().getType()).isEqualTo(CeTaskTypes.REPORT);
assertThat(peek.get().getComponentUuid()).isEqualTo("PROJECT_1");
// no more pending tasks
peek = underTest.peek(WORKER_UUID_2);
assertThat(peek.isPresent()).isFalse();
}
@Test
public void peek_overrides_workerUuid_to_argument() {
db.getDbClient().ceQueueDao().insert(session, new CeQueueDto()
.setUuid("uuid")
.setTaskType("foo")
.setStatus(CeQueueDto.Status.PENDING)
.setWorkerUuid("must be overriden"));
db.commit();
underTest.peek(WORKER_UUID_1);
CeQueueDto ceQueueDto = db.getDbClient().ceQueueDao().selectByUuid(session, "uuid").get();
assertThat(ceQueueDto.getWorkerUuid()).isEqualTo(WORKER_UUID_1);
}
@Test
public void peek_nothing_if_application_status_stopping() {
submit(CeTaskTypes.REPORT, "PROJECT_1");
when(computeEngineStatus.getStatus()).thenReturn(STOPPING);
Optional<CeTask> peek = underTest.peek(WORKER_UUID_1);
assertThat(peek.isPresent()).isFalse();
}
@Test
public void peek_peeks_pending_tasks_with_executionCount_equal_to_0_and_increases_it() {
db.getDbClient().ceQueueDao().insert(session, new CeQueueDto()
.setUuid("uuid")
.setTaskType("foo")
.setStatus(CeQueueDto.Status.PENDING)
.setExecutionCount(0));
db.commit();
assertThat(underTest.peek(WORKER_UUID_1).get().getUuid()).isEqualTo("uuid");
assertThat(db.getDbClient().ceQueueDao().selectByUuid(session, "uuid").get().getExecutionCount()).isEqualTo(1);
}
@Test
public void peek_ignores_pending_tasks_with_executionCount_equal_to_1() {
db.getDbClient().ceQueueDao().insert(session, new CeQueueDto()
.setUuid("uuid")
.setTaskType("foo")
.setStatus(CeQueueDto.Status.PENDING)
.setExecutionCount(1));
db.commit();
assertThat(underTest.peek(WORKER_UUID_1).isPresent()).isFalse();
}
@Test
public void peek_ignores_pending_tasks_with_executionCount_equal_to_2() {
db.getDbClient().ceQueueDao().insert(session, new CeQueueDto()
.setUuid("uuid")
.setTaskType("foo")
.setStatus(CeQueueDto.Status.PENDING)
.setExecutionCount(2));
db.commit();
assertThat(underTest.peek(WORKER_UUID_1).isPresent()).isFalse();
}
@Test
public void peek_ignores_pending_tasks_with_executionCount_greater_than_2() {
db.getDbClient().ceQueueDao().insert(session, new CeQueueDto()
.setUuid("uuid")
.setTaskType("foo")
.setStatus(CeQueueDto.Status.PENDING)
.setExecutionCount(2 + Math.abs(new Random().nextInt(100))));
db.commit();
assertThat(underTest.peek(WORKER_UUID_1).isPresent()).isFalse();
}
@Test
public void peek_resets_to_pending_any_task_in_progress_for_specified_worker_uuid_and_updates_updatedAt_no_matter_execution_count() {
insertPending("u0", "doesn't matter", 0); // add a pending one that will be picked so that u1 isn't peek and status reset is visible in DB
CeQueueDto u1 = insertPending("u1", WORKER_UUID_1, 2);// won't be peeked because it's worn-out
CeQueueDto u2 = insertInProgress("u2", WORKER_UUID_1, 3);// will be reset but won't be picked because it's worn-out
CeQueueDto u3 = insertPending("u3", WORKER_UUID_1, 1);// will be picked-because older than any of the reset ones
CeQueueDto u4 = insertInProgress("u4", WORKER_UUID_1, 1);// will be reset
assertThat(underTest.peek(WORKER_UUID_1).get().getUuid()).isEqualTo("u0");
verifyUnmodifiedTask(u1);
verifyResetTask(u2);
verifyUnmodifiedTask(u3);
verifyResetTask(u4);
}
@Test
public void peek_resets_to_pending_any_task_in_progress_for_specified_worker_uuid_and_only_this_uuid() {
insertPending("u0", "doesn't matter", 0); // add a pending one that will be picked so that u1 isn't peek and status reset is visible in DB
CeQueueDto u1 = insertInProgress("u1", WORKER_UUID_1, 3);
CeQueueDto u2 = insertInProgress("u2", WORKER_UUID_2, 3);
CeQueueDto u3 = insertInProgress("u3", WORKER_UUID_1, 3);
CeQueueDto u4 = insertInProgress("u4", WORKER_UUID_2, 1);
assertThat(underTest.peek(WORKER_UUID_1).get().getUuid()).isEqualTo("u0");
verifyResetTask(u1);
verifyUnmodifiedTask(u2);
verifyResetTask(u3);
verifyUnmodifiedTask(u4);
}
@Test
public void peek_resets_to_pending_any_task_in_progress_for_specified_worker_uuid_and_peeks_the_oldest_non_worn_out_no_matter_if_it_has_been_reset_or_not() {
insertPending("u1", WORKER_UUID_1, 3); // won't be picked because worn out
insertInProgress("u2", WORKER_UUID_1, 3); // will be reset but won't be picked because worn out
insertInProgress("u3", WORKER_UUID_1, 1); // will be reset but won't be picked because worn out
insertPending("u4", WORKER_UUID_1, 0); // will be picked
Optional<CeTask> ceTask = underTest.peek(WORKER_UUID_1);
assertThat(ceTask.get().getUuid()).isEqualTo("u4");
}
@Test
public void peek_resets_to_pending_any_task_in_progress_for_specified_worker_uuid_and_peeks_reset_tasks_if_is_the_oldest_non_worn_out() {
insertPending("u1", WORKER_UUID_1, 3); // won't be picked because worn out
insertInProgress("u2", WORKER_UUID_1, 3); // will be reset but won't be picked because worn out
insertInProgress("u3", WORKER_UUID_1, 1); // won't be picked because worn out
insertPending("u4", WORKER_UUID_1, 0); // will be picked second
Optional<CeTask> ceTask = underTest.peek(WORKER_UUID_1);
assertThat(ceTask.get().getUuid()).isEqualTo("u4");
}
private void verifyResetTask(CeQueueDto originalDto) {
CeQueueDto dto = db.getDbClient().ceQueueDao().selectByUuid(session, originalDto.getUuid()).get();
assertThat(dto.getStatus()).isEqualTo(CeQueueDto.Status.PENDING);
assertThat(dto.getExecutionCount()).isEqualTo(originalDto.getExecutionCount());
assertThat(dto.getCreatedAt()).isEqualTo(originalDto.getCreatedAt());
assertThat(dto.getUpdatedAt()).isGreaterThan(originalDto.getUpdatedAt());
}
private void verifyUnmodifiedTask(CeQueueDto originalDto) {
CeQueueDto dto = db.getDbClient().ceQueueDao().selectByUuid(session, originalDto.getUuid()).get();
assertThat(dto.getStatus()).isEqualTo(originalDto.getStatus());
assertThat(dto.getExecutionCount()).isEqualTo(originalDto.getExecutionCount());
assertThat(dto.getCreatedAt()).isEqualTo(originalDto.getCreatedAt());
assertThat(dto.getUpdatedAt()).isEqualTo(originalDto.getUpdatedAt());
}
private CeQueueDto insertInProgress(String uuid, String workerUuid, int executionCount) {
checkArgument(executionCount > 0, "execution count less than 1 does not make sense for an in progress task");
CeQueueDto dto = new CeQueueDto()
.setUuid(uuid)
.setTaskType("foo")
.setStatus(CeQueueDto.Status.IN_PROGRESS)
.setWorkerUuid(workerUuid)
.setExecutionCount(executionCount);
db.getDbClient().ceQueueDao().insert(session, dto);
db.commit();
return dto;
}
private CeQueueDto insertPending(String uuid, String workerUuid, int executionCount) {
checkArgument(executionCount > -1, "execution count less than 0 does not make sense for a pending task");
CeQueueDto dto = new CeQueueDto()
.setUuid(uuid)
.setTaskType("foo")
.setStatus(CeQueueDto.Status.PENDING)
.setWorkerUuid(workerUuid)
.setExecutionCount(executionCount);
db.getDbClient().ceQueueDao().insert(session, dto);
db.commit();
return dto;
}
@Test
public void cancel_pending() {
CeTask task = submit(CeTaskTypes.REPORT, "PROJECT_1");
CeQueueDto queueDto = db.getDbClient().ceQueueDao().selectByUuid(db.getSession(), task.getUuid()).get();
underTest.cancel(db.getSession(), queueDto);
Optional<CeActivityDto> activity = db.getDbClient().ceActivityDao().selectByUuid(db.getSession(), task.getUuid());
assertThat(activity.isPresent()).isTrue();
assertThat(activity.get().getStatus()).isEqualTo(CeActivityDto.Status.CANCELED);
}
@Test
public void cancel_copies_executionCount_and_workerUuid() {
CeQueueDto ceQueueDto = db.getDbClient().ceQueueDao().insert(session, new CeQueueDto()
.setUuid("uuid")
.setTaskType("foo")
.setStatus(CeQueueDto.Status.PENDING)
.setWorkerUuid("Dustin")
.setExecutionCount(2));
db.commit();
underTest.cancel(db.getSession(), ceQueueDto);
CeActivityDto dto = db.getDbClient().ceActivityDao().selectByUuid(db.getSession(), "uuid").get();
assertThat(dto.getExecutionCount()).isEqualTo(2);
assertThat(dto.getWorkerUuid()).isEqualTo("Dustin");
}
@Test
public void fail_to_cancel_if_in_progress() {
CeTask task = submit(CeTaskTypes.REPORT, "PROJECT_1");
underTest.peek(WORKER_UUID_2);
CeQueueDto queueDto = db.getDbClient().ceQueueDao().selectByUuid(db.getSession(), task.getUuid()).get();
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("Task is in progress and can't be canceled");
underTest.cancel(db.getSession(), queueDto);
}
@Test
public void cancelAll_pendings_but_not_in_progress() {
CeTask inProgressTask = submit(CeTaskTypes.REPORT, "PROJECT_1");
CeTask pendingTask1 = submit(CeTaskTypes.REPORT, "PROJECT_2");
CeTask pendingTask2 = submit(CeTaskTypes.REPORT, "PROJECT_3");
underTest.peek(WORKER_UUID_2);
int canceledCount = underTest.cancelAll();
assertThat(canceledCount).isEqualTo(2);
Optional<CeActivityDto> history = db.getDbClient().ceActivityDao().selectByUuid(db.getSession(), pendingTask1.getUuid());
assertThat(history.get().getStatus()).isEqualTo(CeActivityDto.Status.CANCELED);
history = db.getDbClient().ceActivityDao().selectByUuid(db.getSession(), pendingTask2.getUuid());
assertThat(history.get().getStatus()).isEqualTo(CeActivityDto.Status.CANCELED);
history = db.getDbClient().ceActivityDao().selectByUuid(db.getSession(), inProgressTask.getUuid());
assertThat(history.isPresent()).isFalse();
}
@Test
public void cancelWornOuts_cancels_pending_tasks_with_executionCount_greater_or_equal_to_1() {
CeQueueDto u1 = insertCeQueueDto("u1", CeQueueDto.Status.PENDING, 0, "worker1");
CeQueueDto u2 = insertCeQueueDto("u2", CeQueueDto.Status.PENDING, 1, "worker1");
CeQueueDto u3 = insertCeQueueDto("u3", CeQueueDto.Status.PENDING, 2, "worker1");
CeQueueDto u4 = insertCeQueueDto("u4", CeQueueDto.Status.PENDING, 3, "worker1");
CeQueueDto u5 = insertCeQueueDto("u5", CeQueueDto.Status.IN_PROGRESS, 0, "worker1");
CeQueueDto u6 = insertCeQueueDto("u6", CeQueueDto.Status.IN_PROGRESS, 1, "worker1");
CeQueueDto u7 = insertCeQueueDto("u7", CeQueueDto.Status.IN_PROGRESS, 2, "worker1");
CeQueueDto u8 = insertCeQueueDto("u8", CeQueueDto.Status.IN_PROGRESS, 3, "worker1");
underTest.cancelWornOuts();
verifyUnmodified(u1);
verifyCanceled(u2);
verifyCanceled(u3);
verifyCanceled(u4);
verifyUnmodified(u5);
verifyUnmodified(u6);
verifyUnmodified(u7);
verifyUnmodified(u8);
}
@Test
public void resetTasksWithUnknownWorkerUUIDs_reset_only_in_progress_tasks() {
CeQueueDto u1 = insertCeQueueDto("u1", CeQueueDto.Status.PENDING, 0, null);
CeQueueDto u2 = insertCeQueueDto("u2", CeQueueDto.Status.PENDING, 1, "worker1");
CeQueueDto u3 = insertCeQueueDto("u3", CeQueueDto.Status.PENDING, 2, null);
CeQueueDto u4 = insertCeQueueDto("u4", CeQueueDto.Status.PENDING, 3, "worker2");
CeQueueDto u5 = insertCeQueueDto("u5", CeQueueDto.Status.IN_PROGRESS, 0, null);
CeQueueDto u6 = insertCeQueueDto("u6", CeQueueDto.Status.IN_PROGRESS, 1, "worker1");
CeQueueDto u7 = insertCeQueueDto("u7", CeQueueDto.Status.IN_PROGRESS, 2, "worker2");
CeQueueDto u8 = insertCeQueueDto("u8", CeQueueDto.Status.IN_PROGRESS, 3, "worker3");
underTest.resetTasksWithUnknownWorkerUUIDs(ImmutableSet.of("worker2", "worker3"));
// Pending tasks must not be modified even if a workerUUID is not present
verifyUnmodified(u1);
verifyUnmodified(u2);
verifyUnmodified(u3);
verifyUnmodified(u4);
// Unknown worker : null, "worker1"
verifyReset(u5);
verifyReset(u6);
// Known workers : "worker2", "worker3"
verifyUnmodified(u7);
verifyUnmodified(u8);
}
@Test
public void resetTasksWithUnknownWorkerUUIDs_with_empty_set_will_reset_all_in_progress_tasks() {
CeQueueDto u1 = insertCeQueueDto("u1", CeQueueDto.Status.PENDING, 0, null);
CeQueueDto u2 = insertCeQueueDto("u2", CeQueueDto.Status.PENDING, 1, "worker1");
CeQueueDto u3 = insertCeQueueDto("u3", CeQueueDto.Status.PENDING, 2, null);
CeQueueDto u4 = insertCeQueueDto("u4", CeQueueDto.Status.PENDING, 3, "worker2");
CeQueueDto u5 = insertCeQueueDto("u5", CeQueueDto.Status.IN_PROGRESS, 0, null);
CeQueueDto u6 = insertCeQueueDto("u6", CeQueueDto.Status.IN_PROGRESS, 1, "worker1");
CeQueueDto u7 = insertCeQueueDto("u7", CeQueueDto.Status.IN_PROGRESS, 2, "worker2");
CeQueueDto u8 = insertCeQueueDto("u8", CeQueueDto.Status.IN_PROGRESS, 3, "worker3");
underTest.resetTasksWithUnknownWorkerUUIDs(ImmutableSet.of());
// Pending tasks must not be modified even if a workerUUID is not present
verifyUnmodified(u1);
verifyUnmodified(u2);
verifyUnmodified(u3);
verifyUnmodified(u4);
// Unknown worker : null, "worker1"
verifyReset(u5);
verifyReset(u6);
verifyReset(u7);
verifyReset(u8);
}
@Test
public void resetTasksWithUnknownWorkerUUIDs_with_worker_without_tasks_will_reset_all_in_progress_tasks() {
CeQueueDto u1 = insertCeQueueDto("u1", CeQueueDto.Status.PENDING, 0, null);
CeQueueDto u2 = insertCeQueueDto("u2", CeQueueDto.Status.PENDING, 1, "worker1");
CeQueueDto u3 = insertCeQueueDto("u3", CeQueueDto.Status.PENDING, 2, null);
CeQueueDto u4 = insertCeQueueDto("u4", CeQueueDto.Status.PENDING, 3, "worker2");
CeQueueDto u5 = insertCeQueueDto("u5", CeQueueDto.Status.IN_PROGRESS, 0, null);
CeQueueDto u6 = insertCeQueueDto("u6", CeQueueDto.Status.IN_PROGRESS, 1, "worker1");
CeQueueDto u7 = insertCeQueueDto("u7", CeQueueDto.Status.IN_PROGRESS, 2, "worker2");
CeQueueDto u8 = insertCeQueueDto("u8", CeQueueDto.Status.IN_PROGRESS, 3, "worker3");
underTest.resetTasksWithUnknownWorkerUUIDs(ImmutableSet.of("worker1000", "worker1001"));
// Pending tasks must not be modified even if a workerUUID is not present
verifyUnmodified(u1);
verifyUnmodified(u2);
verifyUnmodified(u3);
verifyUnmodified(u4);
// Unknown worker : null, "worker1"
verifyReset(u5);
verifyReset(u6);
verifyReset(u7);
verifyReset(u8);
}
private void verifyReset(CeQueueDto original) {
CeQueueDto dto = db.getDbClient().ceQueueDao().selectByUuid(db.getSession(), original.getUuid()).get();
// We do not touch ExecutionCount nor CreatedAt
assertThat(dto.getExecutionCount()).isEqualTo(original.getExecutionCount());
assertThat(dto.getCreatedAt()).isEqualTo(original.getCreatedAt());
// Status must have changed to PENDING and must not be equal to previous status
assertThat(dto.getStatus()).isEqualTo(CeQueueDto.Status.PENDING).isNotEqualTo(original.getStatus());
// UpdatedAt must have been updated
assertThat(dto.getUpdatedAt()).isNotEqualTo(original.getUpdatedAt());
// StartedAt must be null
assertThat(dto.getStartedAt()).isNull();
// WorkerUuid must be null
assertThat(dto.getWorkerUuid()).isNull();
}
private void verifyUnmodified(CeQueueDto original) {
CeQueueDto dto = db.getDbClient().ceQueueDao().selectByUuid(db.getSession(), original.getUuid()).get();
assertThat(dto.getStatus()).isEqualTo(original.getStatus());
assertThat(dto.getExecutionCount()).isEqualTo(original.getExecutionCount());
assertThat(dto.getCreatedAt()).isEqualTo(original.getCreatedAt());
assertThat(dto.getUpdatedAt()).isEqualTo(original.getUpdatedAt());
}
private void verifyCanceled(CeQueueDto original) {
assertThat(db.getDbClient().ceQueueDao().selectByUuid(db.getSession(), original.getUuid())).isEmpty();
CeActivityDto dto = db.getDbClient().ceActivityDao().selectByUuid(db.getSession(), original.getUuid()).get();
assertThat(dto.getStatus()).isEqualTo(CeActivityDto.Status.CANCELED);
assertThat(dto.getExecutionCount()).isEqualTo(original.getExecutionCount());
assertThat(dto.getWorkerUuid()).isEqualTo(original.getWorkerUuid());
}
private CeQueueDto insertCeQueueDto(String uuid, CeQueueDto.Status status, int executionCount, String workerUuid) {
CeQueueDto dto = new CeQueueDto()
.setUuid(uuid)
.setTaskType("foo")
.setStatus(status)
.setExecutionCount(executionCount)
.setWorkerUuid(workerUuid);
db.getDbClient().ceQueueDao().insert(db.getSession(), dto);
db.commit();
return dto;
}
private void verifyCeTask(CeTaskSubmit taskSubmit, CeTask task, @Nullable ComponentDto componentDto) {
if (componentDto == null) {
assertThat(task.getOrganizationUuid()).isEqualTo(defaultOrganizationProvider.get().getUuid());
} else {
assertThat(task.getOrganizationUuid()).isEqualTo(componentDto.getOrganizationUuid());
}
assertThat(task.getUuid()).isEqualTo(taskSubmit.getUuid());
assertThat(task.getComponentUuid()).isEqualTo(task.getComponentUuid());
assertThat(task.getType()).isEqualTo(taskSubmit.getType());
if (componentDto == null) {
assertThat(task.getComponentKey()).isNull();
assertThat(task.getComponentName()).isNull();
} else {
assertThat(task.getComponentKey()).isEqualTo(componentDto.getDbKey());
assertThat(task.getComponentName()).isEqualTo(componentDto.name());
}
assertThat(task.getSubmitterLogin()).isEqualTo(taskSubmit.getSubmitterLogin());
}
private void verifyCeQueueDtoForTaskSubmit(CeTaskSubmit taskSubmit) {
Optional<CeQueueDto> queueDto = db.getDbClient().ceQueueDao().selectByUuid(db.getSession(), taskSubmit.getUuid());
assertThat(queueDto.isPresent()).isTrue();
CeQueueDto dto = queueDto.get();
assertThat(dto.getTaskType()).isEqualTo(taskSubmit.getType());
assertThat(dto.getComponentUuid()).isEqualTo(taskSubmit.getComponentUuid());
assertThat(dto.getSubmitterLogin()).isEqualTo(taskSubmit.getSubmitterLogin());
assertThat(dto.getCreatedAt()).isEqualTo(dto.getUpdatedAt()).isNotNull();
}
private ComponentDto newComponentDto(String uuid) {
return ComponentTesting.newPublicProjectDto(db.getDefaultOrganization(), uuid).setName("name_" + uuid).setDbKey("key_" + uuid);
}
private CeTask submit(String reportType, String componentUuid) {
return underTest.submit(createTaskSubmit(reportType, componentUuid, null));
}
private CeTaskSubmit createTaskSubmit(String type) {
return createTaskSubmit(type, null, null);
}
private CeTaskSubmit createTaskSubmit(String type, @Nullable String componentUuid, @Nullable String submitterLogin) {
CeTaskSubmit.Builder submission = underTest.prepareSubmit();
submission.setType(type);
submission.setComponentUuid(componentUuid);
submission.setSubmitterLogin(submitterLogin);
return submission.build();
}
private CeTaskResult newTaskResult(@Nullable String analysisUuid) {
CeTaskResult taskResult = mock(CeTaskResult.class);
when(taskResult.getAnalysisUuid()).thenReturn(java.util.Optional.ofNullable(analysisUuid));
return taskResult;
}
private ComponentDto insertComponent(ComponentDto componentDto) {
db.getDbClient().componentDao().insert(session, componentDto);
session.commit();
return componentDto;
}
private static String stacktraceToString(Throwable error) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
error.printStackTrace(new PrintStream(out));
return out.toString();
}
}