Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: clean HibernateSequentialNumberCounterStoreTest flaky tests #16202

Merged
merged 3 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
package org.hisp.dhis.reservedvalue.hibernate;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import javax.persistence.EntityManager;
import lombok.RequiredArgsConstructor;
Expand All @@ -54,9 +53,7 @@ public List<Integer> getNextValues(String uid, String key, int length) {
.setParameter("length", length)
.getSingleResult();

return IntStream.range(count - length, length + (count - length))
.boxed()
.collect(Collectors.toList());
return IntStream.range(count - length, length + (count - length)).boxed().toList();
}

@Override
Expand Down
5 changes: 5 additions & 0 deletions dhis-2/dhis-test-integration/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -43,36 +43,48 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import org.apache.commons.lang3.RandomStringUtils;
import java.util.stream.Stream;
import org.hisp.dhis.reservedvalue.SequentialNumberCounterStore;
import org.hisp.dhis.test.integration.TransactionalIntegrationTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

class HibernateSequentialNumberCounterStoreTest extends TransactionalIntegrationTest {

@Autowired private DummyService dummyService;
@Autowired private SequentialNumberCounterStore sequentialNumberCounterStore;

@Test
void getNextValues() {
List<Integer> result = dummyService.getNextValues("ABC", "ABC-#", 3);
List<Integer> result = nextSequentialValues("ABC", "ABC-#", 3);
assertEquals(3, result.size());
assertTrue(result.contains(1));
assertTrue(result.contains(2));
assertTrue(result.contains(3));
result = dummyService.getNextValues("ABC", "ABC-#", 50);
result = nextSequentialValues("ABC", "ABC-#", 50);
assertEquals(50, result.size());
assertTrue(result.contains(4));
assertTrue(result.contains(5));
assertTrue(result.contains(52));
assertTrue(result.contains(53));
}

private void test(final int threadCount) throws InterruptedException, ExecutionException {
final String uid = RandomStringUtils.randomAlphabetic(3).toUpperCase();
Callable<List<Integer>> task = () -> dummyService.getNextValues(uid, uid + "-#", 50);
private static Stream<Arguments> threadCounter() {
return Stream.of(
Arguments.of(1, "AAA"),
Arguments.of(4, "BBB"),
Arguments.of(8, "CCC"),
Arguments.of(16, "DDD"),
Arguments.of(32, "EEE"));
}

@ParameterizedTest
@MethodSource("threadCounter")
void shouldGenerateSequentialValueGivenThreadCounter(int threadCount, String uid)
throws InterruptedException, ExecutionException {
Callable<List<Integer>> task = () -> nextSequentialValues(uid, uid + "-#", 50);
List<Callable<List<Integer>>> tasks = Collections.nCopies(threadCount, task);
ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
List<Future<List<Integer>>> futures = executorService.invokeAll(tasks);
Expand All @@ -95,52 +107,24 @@ private void test(final int threadCount) throws InterruptedException, ExecutionE
assertThat(allIdList.get(allIdList.size() - 1), is(50 * threadCount));
}

@Test
void test1() throws InterruptedException, ExecutionException {
test(1);
}

@Test
void test4() throws InterruptedException, ExecutionException {
test(4);
}

@Test
void test8() throws InterruptedException, ExecutionException {
test(8);
}

@Test
void test16() throws InterruptedException, ExecutionException {
test(16);
}

@Test
void test32() throws InterruptedException, ExecutionException {
test(32);
}

@Test
void deleteCounter() {
assertTrue(dummyService.getNextValues("ABC", "ABC-#", 3).contains(1));
dummyService.deleteCounter("ABC");
assertTrue(dummyService.getNextValues("ABC", "ABC-#", 3).contains(1));
assertTrue(dummyService.getNextValues("ABC", "ABC-##", 3).contains(1));
assertTrue(dummyService.getNextValues("ABC", "ABC-###", 3).contains(1));
dummyService.deleteCounter("ABC");
assertTrue(dummyService.getNextValues("ABC", "ABC-#", 3).contains(1));
assertTrue(dummyService.getNextValues("ABC", "ABC-##", 3).contains(1));
assertTrue(dummyService.getNextValues("ABC", "ABC-###", 3).contains(1));
assertTrue(nextSequentialValues("ABC", "ABC-#", 3).contains(1));
deleteCounter("ABC");
assertTrue(nextSequentialValues("ABC", "ABC-#", 3).contains(1));
assertTrue(nextSequentialValues("ABC", "ABC-##", 3).contains(1));
assertTrue(nextSequentialValues("ABC", "ABC-###", 3).contains(1));
deleteCounter("ABC");
assertTrue(nextSequentialValues("ABC", "ABC-#", 3).contains(1));
assertTrue(nextSequentialValues("ABC", "ABC-##", 3).contains(1));
assertTrue(nextSequentialValues("ABC", "ABC-###", 3).contains(1));
}

@Configuration
static class TestConfig {

@Autowired private SequentialNumberCounterStore sequentialNumberCounterStore;
public List<Integer> nextSequentialValues(String uid, String key, int length) {
return sequentialNumberCounterStore.getNextValues(uid, key, length);
}

@Bean
public DummyService dummyService() {
return new DummyService(sequentialNumberCounterStore);
}
public void deleteCounter(String uid) {
sequentialNumberCounterStore.deleteCounter(uid);
}
}
Loading