|
33 | 33 | import java.util.Iterator;
|
34 | 34 | import java.util.List;
|
35 | 35 | import java.util.Set;
|
| 36 | +import java.util.Collections; |
| 37 | + |
36 | 38 | import java.util.concurrent.ThreadLocalRandom;
|
37 | 39 |
|
38 | 40 | import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
@@ -309,6 +311,58 @@ public void testComparatorDoesNotValidateGeneralContract() {
|
309 | 311 | assertDoesNotThrow(() -> policy.getAssignmentIterator(partition));
|
310 | 312 | }
|
311 | 313 |
|
| 314 | + @Test |
| 315 | + public void testComparatorClassDoesNotViolateTimSortContract() { |
| 316 | + String partition = "testPartition"; |
| 317 | + |
| 318 | + List<PriorityUtilizationQueueOrderingPolicy. |
| 319 | + PriorityQueueResourcesForSorting> queues = new ArrayList<>(); |
| 320 | + for (int i = 0; i < 1000; i++) { // 1000 queues to have enough queues so the exception occur |
| 321 | + queues.add(createMockPriorityQueueResourcesForSorting(partition)); |
| 322 | + } |
| 323 | + |
| 324 | + Collections.shuffle(queues); |
| 325 | + // java.lang.IllegalArgumentException: Comparison method violates its general contract! |
| 326 | + assertDoesNotThrow(() -> queues.sort(new PriorityUtilizationQueueOrderingPolicy(true) |
| 327 | + .new PriorityQueueComparator(partition))); |
| 328 | + |
| 329 | + } |
| 330 | + |
| 331 | + private PriorityUtilizationQueueOrderingPolicy. |
| 332 | + PriorityQueueResourcesForSorting createMockPriorityQueueResourcesForSorting( |
| 333 | + String partition) { |
| 334 | + QueueResourceQuotas resourceQuotas = randomResourceQuotas(partition); |
| 335 | + |
| 336 | + boolean isZeroResource = ThreadLocalRandom.current().nextBoolean(); |
| 337 | + if (isZeroResource) { |
| 338 | + resourceQuotas.setConfiguredMinResource(partition, Resource.newInstance(0, 0)); |
| 339 | + } |
| 340 | + |
| 341 | + QueueCapacities mockQueueCapacities = mock(QueueCapacities.class); |
| 342 | + when(mockQueueCapacities.getAbsoluteUsedCapacity(partition)) |
| 343 | + .thenReturn(4.2f); // could be any specific number, so that there are equal values |
| 344 | + when(mockQueueCapacities.getUsedCapacity(partition)) |
| 345 | + .thenReturn(1.0f); // could be any specific number, so that there are equal values |
| 346 | + when(mockQueueCapacities.getAbsoluteCapacity(partition)) |
| 347 | + .thenReturn(6.2f); // could be any specific number, so that there are equal values |
| 348 | + |
| 349 | + CSQueue mockQueue = mock(CSQueue.class); |
| 350 | + when(mockQueue.getQueueCapacities()) |
| 351 | + .thenReturn(mockQueueCapacities); |
| 352 | + when(mockQueue.getPriority()) |
| 353 | + .thenReturn(Priority.newInstance(7)); // could be any specific number, |
| 354 | + // so that there are equal values |
| 355 | + when(mockQueue.getAccessibleNodeLabels()) |
| 356 | + .thenReturn(Collections.singleton(partition)); |
| 357 | + when(mockQueue.getQueueResourceQuotas()) |
| 358 | + .thenReturn(resourceQuotas); |
| 359 | + |
| 360 | + return new PriorityUtilizationQueueOrderingPolicy.PriorityQueueResourcesForSorting( |
| 361 | + mockQueue, partition |
| 362 | + ); |
| 363 | + |
| 364 | + } |
| 365 | + |
312 | 366 | private QueueCapacities randomQueueCapacities(String partition) {
|
313 | 367 | QueueCapacities qc = new QueueCapacities(false);
|
314 | 368 | qc.setAbsoluteCapacity(partition, (float) randFloat(0.0d, 100.0d));
|
|
0 commit comments