您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

EsQueueDaoTest.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.db.es;
  21. import java.util.ArrayList;
  22. import java.util.List;
  23. import java.util.Random;
  24. import java.util.stream.IntStream;
  25. import org.junit.Rule;
  26. import org.junit.Test;
  27. import org.sonar.api.impl.utils.TestSystem2;
  28. import org.sonar.core.util.UuidFactoryFast;
  29. import org.sonar.db.DbSession;
  30. import org.sonar.db.DbTester;
  31. import static org.assertj.core.api.Assertions.assertThat;
  32. public class EsQueueDaoTest {
  33. private static final int LIMIT = 10;
  34. private static TestSystem2 system2 = new TestSystem2().setNow(1_000);
  35. @Rule
  36. public DbTester dbTester = DbTester.create(system2);
  37. private DbSession dbSession = dbTester.getSession();
  38. private EsQueueDao underTest = dbTester.getDbClient().esQueueDao();
  39. @Test
  40. public void insert_data() {
  41. int nbOfInsert = 10 + new Random().nextInt(20);
  42. List<EsQueueDto> esQueueDtos = new ArrayList<>();
  43. IntStream.rangeClosed(1, nbOfInsert).forEach(
  44. i -> esQueueDtos.add(EsQueueDto.create("foo", UuidFactoryFast.getInstance().create()))
  45. );
  46. underTest.insert(dbSession, esQueueDtos);
  47. assertThat(dbTester.countSql(dbSession, "select count(*) from es_queue")).isEqualTo(nbOfInsert);
  48. }
  49. @Test
  50. public void delete_unknown_EsQueueDto_does_not_throw_exception() {
  51. int nbOfInsert = 10 + new Random().nextInt(20);
  52. List<EsQueueDto> esQueueDtos = new ArrayList<>();
  53. IntStream.rangeClosed(1, nbOfInsert).forEach(
  54. i -> esQueueDtos.add(EsQueueDto.create("foo", UuidFactoryFast.getInstance().create()))
  55. );
  56. underTest.insert(dbSession, esQueueDtos);
  57. underTest.delete(dbSession, EsQueueDto.create("foo", UuidFactoryFast.getInstance().create()));
  58. assertThat(dbTester.countSql(dbSession, "select count(*) from es_queue")).isEqualTo(nbOfInsert);
  59. }
  60. @Test
  61. public void delete_EsQueueDto_does_not_throw_exception() {
  62. int nbOfInsert = 10 + new Random().nextInt(20);
  63. List<EsQueueDto> esQueueDtos = new ArrayList<>();
  64. IntStream.rangeClosed(1, nbOfInsert).forEach(
  65. i -> esQueueDtos.add(EsQueueDto.create("foo", UuidFactoryFast.getInstance().create()))
  66. );
  67. underTest.insert(dbSession, esQueueDtos);
  68. assertThat(dbTester.countSql(dbSession, "select count(*) from es_queue")).isEqualTo(nbOfInsert);
  69. underTest.delete(dbSession, esQueueDtos);
  70. assertThat(dbTester.countSql(dbSession, "select count(*) from es_queue")).isEqualTo(0);
  71. }
  72. @Test
  73. public void selectForRecovery_must_return_limit_when_there_are_more_rows() {
  74. system2.setNow(1_000L);
  75. EsQueueDto i1 = underTest.insert(dbSession, EsQueueDto.create("foo", UuidFactoryFast.getInstance().create()));
  76. system2.setNow(1_001L);
  77. EsQueueDto i2 = underTest.insert(dbSession, EsQueueDto.create("foo", UuidFactoryFast.getInstance().create()));
  78. system2.setNow(1_002L);
  79. EsQueueDto i3 = underTest.insert(dbSession, EsQueueDto.create("foo", UuidFactoryFast.getInstance().create()));
  80. assertThat(underTest.selectForRecovery(dbSession, 2_000, 1))
  81. .extracting(EsQueueDto::getUuid)
  82. .containsExactly(i3.getUuid());
  83. assertThat(underTest.selectForRecovery(dbSession, 2_000, 2))
  84. .extracting(EsQueueDto::getUuid)
  85. .containsExactly(i3.getUuid(), i2.getUuid());
  86. assertThat(underTest.selectForRecovery(dbSession, 2_000, 10))
  87. .extracting(EsQueueDto::getUuid)
  88. .containsExactly(i3.getUuid(), i2.getUuid(), i1.getUuid());
  89. }
  90. @Test
  91. public void selectForRecovery_returns_ordered_rows_created_before_date() {
  92. system2.setNow(1_000L);
  93. EsQueueDto i1 = underTest.insert(dbSession, EsQueueDto.create("foo", UuidFactoryFast.getInstance().create()));
  94. system2.setNow(1_001L);
  95. EsQueueDto i2 = underTest.insert(dbSession, EsQueueDto.create("foo", UuidFactoryFast.getInstance().create()));
  96. system2.setNow(1_002L);
  97. EsQueueDto i3 = underTest.insert(dbSession, EsQueueDto.create("foo", UuidFactoryFast.getInstance().create()));
  98. assertThat(underTest.selectForRecovery(dbSession, 999, LIMIT)).hasSize(0);
  99. assertThat(underTest.selectForRecovery(dbSession, 1_000, LIMIT))
  100. .extracting(EsQueueDto::getUuid)
  101. .containsExactly(i1.getUuid());
  102. assertThat(underTest.selectForRecovery(dbSession, 1_001, LIMIT))
  103. .extracting(EsQueueDto::getUuid)
  104. .containsExactly(i2.getUuid(), i1.getUuid());
  105. assertThat(underTest.selectForRecovery(dbSession, 2_000, LIMIT))
  106. .extracting(EsQueueDto::getUuid)
  107. .containsExactly(i3.getUuid(), i2.getUuid(), i1.getUuid());
  108. }
  109. }