5014 only positive ints (#5050)
* limit ints, add test and changelog * changelog
This commit is contained in:
parent
2c6cd60526
commit
a73b74869d
|
@ -0,0 +1,4 @@
|
|||
---
|
||||
type: add
|
||||
issue: 5014
|
||||
title: "There was a bug with automatic partition ID selection where it was not bound to positive integers and could return a value that was out of range. This has been fixed."
|
|
@ -123,14 +123,14 @@ public class PartitionLookupSvcImpl implements IPartitionLookupSvc {
|
|||
}
|
||||
|
||||
/**
|
||||
* Generate a random int which is guaranteed to be unused by an existing partition.
|
||||
* Generate a random postive integer between 1 and Integer.MAX_VALUE, which is guaranteed to be unused by an existing partition.
|
||||
* @return an integer representing a partition ID that is not currently in use by the system.
|
||||
*/
|
||||
public int generateRandomUnusedPartitionId() {
|
||||
int candidate = ThreadLocalRandom.current().nextInt();
|
||||
int candidate = ThreadLocalRandom.current().nextInt(1, Integer.MAX_VALUE);
|
||||
Optional<PartitionEntity> partition = myPartitionDao.findById(candidate);
|
||||
while (partition.isPresent()) {
|
||||
candidate = ThreadLocalRandom.current().nextInt();
|
||||
candidate = ThreadLocalRandom.current().nextInt(1, Integer.MAX_VALUE);
|
||||
partition = myPartitionDao.findById(candidate);
|
||||
}
|
||||
return candidate;
|
||||
|
|
|
@ -0,0 +1,55 @@
|
|||
package ca.uhn.fhir.jpa.partition;
|
||||
|
||||
import ca.uhn.fhir.context.FhirContext;
|
||||
import ca.uhn.fhir.interceptor.api.IInterceptorService;
|
||||
import ca.uhn.fhir.jpa.cache.ResourceChangeListenerRegistryInterceptor;
|
||||
import ca.uhn.fhir.jpa.dao.data.IPartitionDao;
|
||||
import ca.uhn.fhir.jpa.model.config.PartitionSettings;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(SpringExtension.class)
|
||||
class PartitionLookupSvcImplTest {
|
||||
|
||||
@Autowired
|
||||
private PartitionLookupSvcImpl myPartitionLookupSvc;
|
||||
@MockBean
|
||||
PartitionSettings myPartitionSettings;
|
||||
@MockBean
|
||||
IInterceptorService myInterceptorBroadcaster;
|
||||
@MockBean
|
||||
IPartitionDao myPartitionDao;
|
||||
@MockBean
|
||||
private FhirContext myFhirCtx;
|
||||
@MockBean
|
||||
private PlatformTransactionManager myTxManager;
|
||||
|
||||
@Configuration
|
||||
static class SpringContext {
|
||||
@Bean
|
||||
public PartitionLookupSvcImpl partitionLookupSvcImplTest() {
|
||||
return new PartitionLookupSvcImpl();
|
||||
}
|
||||
}
|
||||
@Test
|
||||
void generateRandomUnusedPartitionId() {
|
||||
when(myPartitionDao.findById(any())).thenReturn(Optional.empty());
|
||||
for (int i = 0; i<10000; i++) {
|
||||
int randomUnusedPartitionId = myPartitionLookupSvc.generateRandomUnusedPartitionId();
|
||||
assertTrue(randomUnusedPartitionId >= 1);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue