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

FMWK-69 Fix condition timeout and improve tests #442

Merged
merged 8 commits into from
Oct 20, 2022
9 changes: 0 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
<aerospike-reactor>6.1.2</aerospike-reactor>
<embedded-aerospike>2.2.9</embedded-aerospike>
<awaitility>4.2.0</awaitility>
<blockhound>1.0.6.RELEASE</blockhound>
<lombok>1.18.22</lombok>
</properties>

Expand Down Expand Up @@ -271,13 +270,6 @@
<version>${awaitility}</version>
<scope>test</scope>
</dependency>
<!--Needed for tracking blocking calls in reactive application https://github.com/reactor/BlockHound -->
<dependency>
<groupId>io.projectreactor.tools</groupId>
<artifactId>blockhound</artifactId>
<version>${blockhound}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -339,7 +331,6 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration combine.self="override"/>
</plugin>
</plugins>
</build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package org.springframework.data.aerospike;

import com.aerospike.client.Bin;
import com.aerospike.client.IAerospikeClient;
import com.aerospike.client.Key;
import com.aerospike.client.Record;
import com.aerospike.client.*;
import com.aerospike.client.policy.Policy;
import com.aerospike.client.policy.RecordExistsAction;
import com.aerospike.client.policy.WritePolicy;
import com.aerospike.client.query.IndexCollectionType;
import com.aerospike.client.query.IndexType;
import lombok.Value;
import lombok.*;
import org.awaitility.Awaitility;
import org.springframework.data.aerospike.core.WritePolicyBuilder;
Expand Down Expand Up @@ -76,21 +77,15 @@ public void killAllScans() {
.contains("OK");
}

public void deleteAll(Class<?>... entityClasses) {
Arrays.asList(entityClasses).forEach(this::delete);
public void deleteAllAndVerify(Class<?>... entityClasses) {
Arrays.asList(entityClasses).forEach(this::truncateSetOfEntityClass);
Arrays.asList(entityClasses).forEach(this::awaitUntilSetIsEmpty);
}

private void awaitUntilSetIsEmpty(Class<?> entityClass) {
Awaitility.await()
.atMost(Duration.ofSeconds(10))
.until(() -> isEmptySet(client, getNamespace(), entityClass));
}

public <T> boolean isEmptySet(IAerospikeClient client, String namespace, Class<T> entityClass) {
String answer = Info.request(client.getNodes()[0], "sets/" + namespace + "/" + getSetName(entityClass));
return answer.isEmpty()
|| Stream.of(answer.split(";")).allMatch(s -> s.contains("objects=0"));
.until(() -> isEntityClassSetEmpty(entityClass));
}

public <T> void createIndexIfNotExists(Class<T> entityClass, String indexName, String binName, IndexType indexType, IndexCollectionType indexCollectionType) {
Expand Down Expand Up @@ -138,7 +133,9 @@ public void addNewFieldToSavedDataInAerospike(Key key) {
assertThat(updated.bins.get("notPresent")).isEqualTo("cats");
}

protected abstract void delete(Class<?> clazz);
protected abstract boolean isEntityClassSetEmpty(Class<?> clazz);

protected abstract void truncateSetOfEntityClass(Class<?> clazz);

protected abstract String getNamespace();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
package org.springframework.data.aerospike;

import com.aerospike.client.reactor.AerospikeReactorClient;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.aerospike.config.CommonTestConfig;
import org.springframework.data.aerospike.config.ReactiveTestConfig;
import org.springframework.data.aerospike.core.ReactiveAerospikeTemplate;
import reactor.blockhound.BlockHound;
import reactor.blockhound.BlockingOperationError;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

import java.io.Serializable;
import java.time.Duration;

@SpringBootTest(
classes = {ReactiveTestConfig.class, CommonTestConfig.class},
Expand All @@ -34,23 +27,4 @@ public abstract class BaseReactiveIntegrationTests extends BaseIntegrationTests
protected <T> T findById(Serializable id, Class<T> type) {
return reactiveTemplate.findById(id, type).block();
}

@BeforeAll
public static void installBlockHound() {
BlockHound.install();
}

@Test
public void shouldFailAsBlocking() {
StepVerifier.create(Mono.delay(Duration.ofSeconds(1))
.doOnNext(it -> {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}))
.expectError(BlockingOperationError.class)
.verify();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ public BlockingAerospikeTestOperations(IndexInfoParser indexInfoParser,
}

@Override
protected void delete(Class<?> clazz) {
protected boolean isEntityClassSetEmpty(Class<?> clazz) {
return !template.findAll(clazz).findAny().isPresent();
}

@Override
protected void truncateSetOfEntityClass(Class<?> clazz) {
template.delete(clazz);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@ public class ReactiveBlockingAerospikeTestOperations extends AdditionalAerospike
private final ReactiveAerospikeTemplate template;

public ReactiveBlockingAerospikeTestOperations(IndexInfoParser indexInfoParser,
IAerospikeClient client, GenericContainer aerospike,
IAerospikeClient client, GenericContainer<?> aerospike,
ReactiveAerospikeTemplate reactiveAerospikeTemplate) {
super(indexInfoParser, client, aerospike);
this.template = reactiveAerospikeTemplate;
}

@Override
protected void delete(Class<?> clazz) {
template.findAll(clazz)
.flatMap(template::delete)
.then().block();
protected boolean isEntityClassSetEmpty(Class<?> clazz) {
return Boolean.FALSE.equals(template.findAll(clazz).hasElements().block());
}

@Override
protected void truncateSetOfEntityClass(Class<?> clazz) {
template.delete(clazz).block();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.springframework.data.aerospike.core;

import org.awaitility.Awaitility;
import com.aerospike.client.Value;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -25,6 +26,9 @@
import org.springframework.data.aerospike.repository.query.Query;
import org.springframework.data.aerospike.sample.Person;

import java.time.Duration;
import java.util.Objects;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

Expand All @@ -34,7 +38,7 @@ public class AerospikeTemplateCountTests extends BaseBlockingIntegrationTests {
@BeforeEach
public void setUp() {
super.setUp();
additionalAerospikeTestOperations.deleteAll(Person.class);
additionalAerospikeTestOperations.deleteAllAndVerify(Person.class);
}

@Test
Expand Down Expand Up @@ -161,8 +165,12 @@ void countForObjects() {
template.insert(new Person(nextId(), "vasili", 52));
template.insert(new Person(nextId(), "petya", 52));

long count = template.count(Person.class);
Awaitility.await()
.atMost(Duration.ofSeconds(15))
.until(() -> isCountExactlyNum(4L));
}

assertThat(count).isEqualTo(4);
private boolean isCountExactlyNum(Long num) {
return Objects.equals(template.count(Person.class), num);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,4 @@ public void exists_shouldReturnTrueIfValueIsPresent() {
public void exists_shouldReturnFalseIfValueIsAbsent() {
assertThat(template.exists(id, Person.class)).isFalse();
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.springframework.data.aerospike.core;

import com.aerospike.client.query.IndexType;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.springframework.data.aerospike.BaseBlockingIntegrationTests;
import org.springframework.data.aerospike.QueryUtils;
import org.springframework.data.aerospike.repository.query.Query;
Expand All @@ -17,6 +19,7 @@

import static org.assertj.core.api.Assertions.assertThat;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class AerospikeTemplateFindByQueryProjectionTests extends BaseBlockingIntegrationTests {

Person jean = Person.builder()
Expand All @@ -42,11 +45,9 @@ public class AerospikeTemplateFindByQueryProjectionTests extends BaseBlockingInt

List<Person> all = Arrays.asList(jean, ashley, beatrice, dave, zaipper, knowlen, xylophone, mitch, alister, aabbot);

@Override
@BeforeEach
public void setUp() {
super.setUp();
additionalAerospikeTestOperations.deleteAll(Person.class);
@BeforeAll
public void beforeAllSetUp() {
additionalAerospikeTestOperations.deleteAllAndVerify(Person.class);

template.insertAll(all);

Expand All @@ -55,6 +56,12 @@ public void setUp() {
additionalAerospikeTestOperations.createIndexIfNotExists(Person.class, "person_last_name_index", "lastName", IndexType.STRING);
}

@Override
@BeforeEach
public void setUp() {
super.setUp();
}

@Test
public void findWithFilterEqualProjection() {
Query query = QueryUtils.createQueryForMethodWithArgs("findPersonByFirstName", "Dave");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
import com.aerospike.client.query.IndexType;
import com.aerospike.client.query.RecordSet;
import com.aerospike.client.query.Statement;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.springframework.data.aerospike.BaseBlockingIntegrationTests;
import org.springframework.data.aerospike.CollectionUtils;
import org.springframework.data.aerospike.QueryUtils;
Expand All @@ -39,6 +41,7 @@
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.entry;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class AerospikeTemplateFindByQueryTests extends BaseBlockingIntegrationTests {

Person jean = Person.builder().id(nextId()).firstName("Jean").lastName("Matthews").age(21).ints(Collections.singletonList(100))
Expand All @@ -63,11 +66,9 @@ public class AerospikeTemplateFindByQueryTests extends BaseBlockingIntegrationTe
.stringMap(Collections.singletonMap("key4", "val5")).build();
List<Person> all = Arrays.asList(jean, ashley, beatrice, dave, zaipper, knowlen, xylophone, mitch, alister, aabbot);

@Override
@BeforeEach
public void setUp() {
super.setUp();
additionalAerospikeTestOperations.deleteAll(Person.class);
@BeforeAll
public void beforeAllSetUp() {
additionalAerospikeTestOperations.deleteAllAndVerify(Person.class);

template.insertAll(all);

Expand All @@ -76,6 +77,12 @@ public void setUp() {
additionalAerospikeTestOperations.createIndexIfNotExists(Person.class, "person_last_name_index", "lastName", IndexType.STRING);
}

@Override
@BeforeEach
public void setUp() {
super.setUp();
}

@Test
public void findWithFilterEqual() {
Query query = QueryUtils.createQueryForMethodWithArgs("findPersonByFirstName", "Dave");
Expand Down Expand Up @@ -184,11 +191,13 @@ public void findAll_findAllExistingDocuments() {

@Test
public void findAll_findNothing() {
additionalAerospikeTestOperations.deleteAll(Person.class);
additionalAerospikeTestOperations.deleteAllAndVerify(Person.class);

Stream<Person> result = template.findAll(Person.class);

assertThat(result).isEmpty();

template.insertAll(all);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void setUp() {
LuaConfig.SourceDirectory = "src/test/resources/udf";

// Clean existing data
additionalAerospikeTestOperations.deleteAll(Person.class);
additionalAerospikeTestOperations.deleteAllAndVerify(Person.class);

// Insert data
Person firstPerson = Person.builder()
Expand Down Expand Up @@ -76,7 +76,7 @@ public void cleanUp() {
LuaConfig.SourceDirectory = System.getProperty("lua.dir", "udf");

// Clean existing data
additionalAerospikeTestOperations.deleteAll(Person.class);
additionalAerospikeTestOperations.deleteAllAndVerify(Person.class);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.springframework.data.aerospike.core.reactive;

import org.awaitility.Awaitility;
import com.aerospike.client.Value;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -12,6 +13,9 @@
import org.springframework.data.aerospike.sample.Person;
import reactor.core.scheduler.Schedulers;

import java.time.Duration;
import java.util.Objects;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

Expand All @@ -26,7 +30,7 @@ public class ReactiveAerospikeTemplateCountRelatedTests extends BaseReactiveInte
@BeforeEach
public void setUp() {
super.setUp();
additionalAerospikeTestOperations.deleteAll(Person.class);
additionalAerospikeTestOperations.deleteAllAndVerify(Person.class);
}

@Test
Expand Down Expand Up @@ -130,7 +134,12 @@ public void count_shouldCountAllByPassingEntityClass() {
reactiveTemplate.insert(new Person(nextId(), "vasili", 52)).subscribeOn(Schedulers.parallel()).block();
reactiveTemplate.insert(new Person(nextId(), "petya", 52)).subscribeOn(Schedulers.parallel()).block();

Long count = reactiveTemplate.count(Person.class).block();
assertThat(count).isEqualTo(4);
Awaitility.await()
.atMost(Duration.ofSeconds(15))
.until(() -> isCountExactlyNum(4L));
}

private boolean isCountExactlyNum(Long num) {
return Objects.equals(reactiveTemplate.count(Person.class).block(), num);
}
}
Loading