forked from spring-attic/spring-data-aerospike
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Throw IndexAlreadyExistsException, IndexNotFoundException for index related error codes - Add tests for AerospikeTemplate index create/delete operations
- Loading branch information
Showing
10 changed files
with
217 additions
and
11 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
src/main/java/org/springframework/data/aerospike/IndexAlreadyExistsException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package org.springframework.data.aerospike; | ||
|
||
import org.springframework.dao.InvalidDataAccessResourceUsageException; | ||
|
||
public class IndexAlreadyExistsException extends InvalidDataAccessResourceUsageException { | ||
|
||
public IndexAlreadyExistsException(String msg, Throwable cause) { | ||
super(msg, cause); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/org/springframework/data/aerospike/IndexNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package org.springframework.data.aerospike; | ||
|
||
import org.springframework.dao.InvalidDataAccessResourceUsageException; | ||
|
||
public class IndexNotFoundException extends InvalidDataAccessResourceUsageException { | ||
|
||
public IndexNotFoundException(String msg, Throwable cause) { | ||
super(msg, cause); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/test/java/org/springframework/data/aerospike/AwaitilityUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.springframework.data.aerospike; | ||
|
||
import org.awaitility.core.ThrowingRunnable; | ||
|
||
import static org.awaitility.Awaitility.await; | ||
import static org.awaitility.Durations.TEN_SECONDS; | ||
|
||
public class AwaitilityUtils { | ||
|
||
public static void awaitTenSecondsUntil(ThrowingRunnable runnable) { | ||
await().atMost(TEN_SECONDS) | ||
.untilAsserted(runnable); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
src/test/java/org/springframework/data/aerospike/core/AerospikeTemplateIndexTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package org.springframework.data.aerospike.core; | ||
|
||
import com.aerospike.client.query.IndexType; | ||
import lombok.Value; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.springframework.data.aerospike.AsyncUtils; | ||
import org.springframework.data.aerospike.BaseBlockingIntegrationTests; | ||
import org.springframework.data.aerospike.IndexAlreadyExistsException; | ||
import org.springframework.data.aerospike.IndexNotFoundException; | ||
import org.springframework.data.aerospike.mapping.Document; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.springframework.data.aerospike.AwaitilityUtils.awaitTenSecondsUntil; | ||
|
||
public class AerospikeTemplateIndexTests extends BaseBlockingIntegrationTests { | ||
|
||
private static final String INDEX_TEST_1 = "index-test-77777"; | ||
|
||
@Override | ||
@Before | ||
public void setUp() { | ||
blockingAerospikeTestOperations.dropIndexIfExists(IndexedDocument.class, INDEX_TEST_1); | ||
} | ||
|
||
@Test | ||
public void createIndex_createsIndexIfExecutedConcurrently() throws Exception { | ||
AtomicInteger errors = new AtomicInteger(); | ||
AsyncUtils.executeConcurrently(5, () -> { | ||
try { | ||
template.createIndex(IndexedDocument.class, INDEX_TEST_1, "stringField", IndexType.STRING); | ||
} catch (IndexAlreadyExistsException e) { | ||
errors.incrementAndGet(); | ||
} | ||
}); | ||
|
||
awaitTenSecondsUntil(() -> | ||
assertThat(blockingAerospikeTestOperations.indexExists(INDEX_TEST_1)).isTrue()); | ||
assertThat(errors.get()).isLessThanOrEqualTo(4);// depending on the timing all 5 requests can succeed on Aerospike Server | ||
} | ||
|
||
@Test | ||
public void createIndex_allCreateIndexConcurrentAttemptsFailIfIndexAlreadyExists() throws Exception { | ||
template.createIndex(IndexedDocument.class, INDEX_TEST_1, "stringField", IndexType.STRING); | ||
|
||
awaitTenSecondsUntil(() -> | ||
assertThat(blockingAerospikeTestOperations.indexExists(INDEX_TEST_1)).isTrue()); | ||
|
||
AtomicInteger errors = new AtomicInteger(); | ||
AsyncUtils.executeConcurrently(5, () -> { | ||
try { | ||
template.createIndex(IndexedDocument.class, INDEX_TEST_1, "stringField", IndexType.STRING); | ||
} catch (IndexAlreadyExistsException e) { | ||
errors.incrementAndGet(); | ||
} | ||
}); | ||
|
||
assertThat(errors.get()).isEqualTo(5); | ||
} | ||
|
||
@Test | ||
public void createIndex_createsIndex() { | ||
template.createIndex(IndexedDocument.class, INDEX_TEST_1, "stringField", IndexType.STRING); | ||
|
||
awaitTenSecondsUntil(() -> | ||
assertThat(blockingAerospikeTestOperations.indexExists(INDEX_TEST_1)).isTrue()); | ||
} | ||
|
||
@Test | ||
public void createIndex_throwsExceptionIfIndexAlreadyExists() { | ||
template.createIndex(IndexedDocument.class, INDEX_TEST_1, "stringField", IndexType.STRING); | ||
|
||
awaitTenSecondsUntil(() -> assertThat(blockingAerospikeTestOperations.indexExists(INDEX_TEST_1)).isTrue()); | ||
|
||
assertThatThrownBy(() -> template.createIndex(IndexedDocument.class, INDEX_TEST_1, "stringField", IndexType.STRING)) | ||
.isInstanceOf(IndexAlreadyExistsException.class); | ||
} | ||
|
||
@Test | ||
public void deleteIndex_throwsExceptionIfIndexDoesNotExist() { | ||
assertThatThrownBy(() -> template.deleteIndex(IndexedDocument.class, "not-existing-index")) | ||
.isInstanceOf(IndexNotFoundException.class); | ||
} | ||
|
||
@Test | ||
public void deleteIndex_deletesExistingIndex() { | ||
template.createIndex(IndexedDocument.class, INDEX_TEST_1, "stringField", IndexType.STRING); | ||
|
||
awaitTenSecondsUntil(() -> assertThat(blockingAerospikeTestOperations.indexExists(INDEX_TEST_1)).isTrue()); | ||
|
||
template.deleteIndex(IndexedDocument.class, INDEX_TEST_1); | ||
|
||
awaitTenSecondsUntil(() -> assertThat(blockingAerospikeTestOperations.indexExists(INDEX_TEST_1)).isFalse()); | ||
} | ||
|
||
@Value | ||
@Document | ||
public static class IndexedDocument { | ||
|
||
String stringField; | ||
int intField; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters