Added support of the expression based durability levels. (#1721)

Closes #1063.
This commit is contained in:
Tigran Babloyan
2023-04-24 20:17:19 +04:00
committed by GitHub
parent e91322bd13
commit 7edc00e10c
20 changed files with 238 additions and 19 deletions

View File

@@ -46,6 +46,7 @@ import org.springframework.data.couchbase.domain.*;
import org.springframework.data.couchbase.util.ClusterType;
import org.springframework.data.couchbase.util.IgnoreWhen;
import org.springframework.data.couchbase.util.JavaIntegrationTests;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import com.couchbase.client.core.error.CouchbaseException;
@@ -63,6 +64,7 @@ import com.couchbase.client.java.query.QueryScanConsistency;
*/
@IgnoreWhen(clusterTypes = ClusterType.MOCKED)
@SpringJUnitConfig(Config.class)
@TestPropertySource(properties = { "valid.document.durability = MAJORITY" })
class CouchbaseTemplateKeyValueIntegrationTests extends JavaIntegrationTests {
@Autowired public CouchbaseTemplate couchbaseTemplate;
@@ -290,7 +292,8 @@ class CouchbaseTemplateKeyValueIntegrationTests extends JavaIntegrationTests {
@Test
void withDurability()
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
for (Class<?> clazz : new Class[] { User.class, UserAnnotatedDurability.class, UserAnnotatedPersistTo.class, UserAnnotatedReplicateTo.class }) {
for (Class<?> clazz : new Class[] { User.class, UserAnnotatedDurability.class, UserAnnotatedDurabilityExpression.class,
UserAnnotatedPersistTo.class, UserAnnotatedReplicateTo.class }) {
// insert, replace, upsert
for (OneAndAllEntity<User> operator : new OneAndAllEntity[]{couchbaseTemplate.insertById(clazz),
couchbaseTemplate.replaceById(clazz), couchbaseTemplate.upsertById(clazz)}) {
@@ -1132,6 +1135,31 @@ class CouchbaseTemplateKeyValueIntegrationTests extends JavaIntegrationTests {
couchbaseTemplate.removeById(UserAnnotatedDurability.class).one(user.getId());
}
@Test
void insertByIdWithAnnotatedDurabilityExpression() {
UserAnnotatedDurabilityExpression user = new UserAnnotatedDurabilityExpression(UUID.randomUUID().toString(), "firstname", "lastname");
UserAnnotatedDurabilityExpression inserted = null;
// occasionally gives "reactor.core.Exceptions$OverflowException: Could not emit value due to lack of requests"
for (int i = 1; i != 5; i++) {
try {
inserted = couchbaseTemplate.insertById(UserAnnotatedDurabilityExpression.class)
.one(user);
break;
} catch (Exception ofe) {
System.out.println("" + i + " caught: " + ofe);
couchbaseTemplate.removeByQuery(UserAnnotatedDurabilityExpression.class).withConsistency(QueryScanConsistency.REQUEST_PLUS).all();
if (i == 4) {
throw ofe;
}
sleepSecs(1);
}
}
assertEquals(user, inserted);
assertThrows(DuplicateKeyException.class, () -> couchbaseTemplate.insertById(UserAnnotatedDurabilityExpression.class).one(user));
couchbaseTemplate.removeById(UserAnnotatedDurabilityExpression.class).one(user.getId());
}
@Test
void existsById() {
String id = UUID.randomUUID().toString();

View File

@@ -23,6 +23,7 @@ import java.util.Calendar;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
import com.couchbase.client.core.msg.kv.DurabilityLevel;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
@@ -33,7 +34,8 @@ import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
@SpringJUnitConfig
@TestPropertySource(properties = { "valid.document.expiry = 10", "invalid.document.expiry = abc" })
@TestPropertySource(properties = { "valid.document.expiry = 10", "invalid.document.expiry = abc",
"invalid.document.durability = some", "valid.document.durability = MAJORITY" })
public class BasicCouchbasePersistentEntityTests {
@Autowired ConfigurableEnvironment environment;
@@ -180,6 +182,34 @@ public class BasicCouchbasePersistentEntityTests {
() -> getBasicCouchbasePersistentEntity(ExpiryAndExpression.class).getExpiry());
}
@Test
void doesNotAllowUseDurabilityAndExpressionSimultaneously() {
assertThrows(IllegalArgumentException.class,
() -> getBasicCouchbasePersistentEntity(DurabilityAndExpression.class).getDurabilityLevel());
}
@Test
void failsIfDurabilityExpressionMissesRequiredProperty() {
assertThrows(IllegalArgumentException.class,
() -> getBasicCouchbasePersistentEntity(DurabilityWithMissingProperty.class).getDurabilityLevel());
}
@Test
void doesNotAllowUseDurabilityFromInvalidExpression() {
assertThrows(IllegalArgumentException.class,
() -> getBasicCouchbasePersistentEntity(DurabilityWithInvalidExpression.class).getDurabilityLevel());
}
@Test
void usesGetDurabilityExpression() {
assertThat(getBasicCouchbasePersistentEntity(ConstantDurabilityExpression.class).getDurabilityLevel()).isEqualTo(DurabilityLevel.MAJORITY);
}
@Test
void usesGetDurabilityFromValidExpression() {
assertThat(getBasicCouchbasePersistentEntity(DurabilityWithValidExpression.class).getDurabilityLevel()).isEqualTo(DurabilityLevel.MAJORITY);
}
private BasicCouchbasePersistentEntity getBasicCouchbasePersistentEntity(Class<?> clazz) {
BasicCouchbasePersistentEntity basicCouchbasePersistentEntity = new BasicCouchbasePersistentEntity(
ClassTypeInformation.from(clazz));
@@ -264,4 +294,34 @@ public class BasicCouchbasePersistentEntityTests {
@Document(expiry = 10, expiryExpression = "10")
class ExpiryAndExpression {}
/**
* Simple POJO to test that durability and durability expression cannot be used simultaneously
*/
@Document(durabilityLevel = DurabilityLevel.MAJORITY, durabilityExpression = "MAJORITY")
class DurabilityAndExpression {}
/**
* Simple POJO to test durability expression logic failure to resolve property placeholder
*/
@Document(durabilityExpression = "${missing.durability}")
class DurabilityWithMissingProperty {}
/**
* Simple POJO to test invalid durability expression
*/
@Document(durabilityExpression = "${invalid.document.durability}")
class DurabilityWithInvalidExpression {}
/**
* Simple POJO to test constant expiry expression
*/
@Document(durabilityExpression = "MAJORITY")
class ConstantDurabilityExpression {}
/**
* Simple POJO to test valid expiry expression by resolving simple property from environment
*/
@Document(durabilityExpression = "${valid.document.durability}")
class DurabilityWithValidExpression {}
}

View File

@@ -0,0 +1,36 @@
/*
* Copyright 2020-2023 the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.couchbase.domain;
import com.couchbase.client.core.msg.kv.DurabilityLevel;
import org.springframework.data.couchbase.core.mapping.Document;
import java.io.Serializable;
/**
* Annotated User entity for tests
*
* @author Tigran Babloyan
*/
@Document(durabilityExpression = "${valid.document.durability}")
public class UserAnnotatedDurabilityExpression extends User implements Serializable {
public UserAnnotatedDurabilityExpression(String id, String firstname, String lastname) {
super(id, firstname, lastname);
}
}