You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

QueueComparatorsTest.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (c) 2006-2016 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.parser.irc.outputqueue;
  23. import com.dmdirc.parser.common.QueuePriority;
  24. import java.time.Clock;
  25. import java.time.Duration;
  26. import java.time.Instant;
  27. import java.time.ZoneId;
  28. import org.junit.Test;
  29. import static org.junit.Assert.assertEquals;
  30. import static org.junit.Assert.assertTrue;
  31. public class QueueComparatorsTest {
  32. private static final Clock NOW = Clock.fixed(Instant.ofEpochMilli(100 * 1000),
  33. ZoneId.systemDefault());
  34. private static final Clock NOW_MINUS_FIVE = Clock.fixed(Instant.ofEpochMilli(95 * 1000),
  35. ZoneId.systemDefault());
  36. private static final Clock NOW_MINUS_TEN = Clock.fixed(Instant.ofEpochMilli(90 * 1000),
  37. ZoneId.systemDefault());
  38. private final QueueItem currentLowPriority1 = QueueItem.create(
  39. NOW_MINUS_FIVE, "", QueuePriority.LOW);
  40. private final QueueItem currentLowPriority2 = QueueItem.create(
  41. NOW_MINUS_FIVE, "", QueuePriority.LOW);
  42. private final QueueItem currentNormalPriority = QueueItem.create(
  43. NOW_MINUS_FIVE, "", QueuePriority.NORMAL);
  44. private final QueueItem currentHighPriority = QueueItem.create(
  45. NOW_MINUS_FIVE, "", QueuePriority.HIGH);
  46. private final QueueItem oldLowPriority = QueueItem.create(
  47. NOW_MINUS_TEN, "", QueuePriority.LOW);
  48. @Test
  49. public void testComparesByPriority() {
  50. assertEquals(0, QueueComparators.byPriorityThenNumber().compare(
  51. currentLowPriority1,
  52. currentLowPriority1));
  53. assertTrue(QueueComparators.byPriorityThenNumber().compare(
  54. currentNormalPriority,
  55. currentLowPriority2) < 0);
  56. assertTrue(QueueComparators.byPriorityThenNumber().compare(
  57. currentHighPriority,
  58. oldLowPriority) < 0);
  59. assertTrue(QueueComparators.byPriorityThenNumber().compare(
  60. currentNormalPriority,
  61. currentHighPriority) > 0);
  62. }
  63. @Test
  64. public void testComparesByNumber() {
  65. // If the priority is the same, then the lowest number comes first.
  66. assertTrue(QueueComparators.byPriorityThenNumber().compare(
  67. currentLowPriority1,
  68. currentLowPriority2) < 0);
  69. assertTrue(QueueComparators.byPriorityThenNumber().compare(
  70. currentLowPriority2,
  71. currentLowPriority1) > 0);
  72. }
  73. @Test
  74. public void testStarvedItemsFirst() {
  75. // If an item is starved then it gets queued first
  76. assertTrue(QueueComparators.byPriorityThenNumber(NOW, Duration.ofSeconds(7)).compare(
  77. oldLowPriority,
  78. currentHighPriority) < 0);
  79. assertTrue(QueueComparators.byPriorityThenNumber(NOW, Duration.ofSeconds(7)).compare(
  80. currentLowPriority1,
  81. oldLowPriority) > 0);
  82. }
  83. }