|
| 1 | +package com.playtika.test.aerospike; |
| 2 | + |
| 3 | +import com.aerospike.client.AerospikeClient; |
| 4 | +import com.aerospike.client.Bin; |
| 5 | +import com.aerospike.client.Key; |
| 6 | +import com.aerospike.client.policy.ClientPolicy; |
| 7 | +import com.aerospike.client.policy.WritePolicy; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | +import org.springframework.beans.factory.annotation.Autowired; |
| 10 | +import org.springframework.beans.factory.annotation.Value; |
| 11 | +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
| 12 | +import org.springframework.boot.test.context.SpringBootTest; |
| 13 | +import org.springframework.context.annotation.Bean; |
| 14 | +import org.springframework.context.annotation.Configuration; |
| 15 | +import org.springframework.context.annotation.Primary; |
| 16 | + |
| 17 | +import java.time.Instant; |
| 18 | +import java.time.temporal.ChronoUnit; |
| 19 | +import java.util.concurrent.TimeUnit; |
| 20 | + |
| 21 | +import static org.assertj.core.api.Assertions.assertThat; |
| 22 | + |
| 23 | +@SpringBootTest( |
| 24 | + classes = AerospikeExpiredDocumentsCleanerWithDurableDeleteTest.TestConfiguration.class |
| 25 | +) |
| 26 | +class AerospikeExpiredDocumentsCleanerWithDurableDeleteTest { |
| 27 | + |
| 28 | + static final String SET_NAME = "some-set"; |
| 29 | + |
| 30 | + @Value("${embedded.aerospike.namespace}") |
| 31 | + String namespace; |
| 32 | + @Autowired |
| 33 | + ExpiredDocumentsCleaner durableDeleteExpiredDocumentsCleaner; |
| 34 | + @Autowired |
| 35 | + AerospikeClient aerospikeClient; |
| 36 | + |
| 37 | + @Test |
| 38 | + public void shouldNotRemoveExpiredWithDurableDeleteFlagBecauseOfAerospikeCommunityEdition() { |
| 39 | + Key key = new Key(namespace, SET_NAME, "shouldRemoveExpired"); |
| 40 | + putBin(key, (int) TimeUnit.DAYS.toSeconds(1)); |
| 41 | + |
| 42 | + Instant plus23 = Instant.now().plus(23, ChronoUnit.HOURS); |
| 43 | + durableDeleteExpiredDocumentsCleaner.cleanExpiredDocumentsBefore(plus23); |
| 44 | + assertThat(aerospikeClient.get(null, key)).isNotNull(); |
| 45 | + |
| 46 | + Instant plus25 = Instant.now().plus(25, ChronoUnit.HOURS); |
| 47 | + durableDeleteExpiredDocumentsCleaner.cleanExpiredDocumentsBefore(plus25); |
| 48 | + assertThat(aerospikeClient.get(null, key)).isNotNull(); |
| 49 | + } |
| 50 | + private void putBin(Key key, int expiration) { |
| 51 | + Bin bin = new Bin("mybin", "myvalue"); |
| 52 | + |
| 53 | + WritePolicy writePolicy = new WritePolicy(aerospikeClient.getWritePolicyDefault()); |
| 54 | + writePolicy.expiration = expiration; |
| 55 | + aerospikeClient.put(writePolicy, key, bin); |
| 56 | + } |
| 57 | + |
| 58 | + @EnableAutoConfiguration |
| 59 | + @Configuration |
| 60 | + static class TestConfiguration { |
| 61 | + |
| 62 | + @Primary |
| 63 | + @Bean(destroyMethod = "close") |
| 64 | + public AerospikeClient aerospikeClient(@Value("${embedded.aerospike.host}") String host, |
| 65 | + @Value("${embedded.aerospike.port}") int port) { |
| 66 | + ClientPolicy clientPolicy = new ClientPolicy(); |
| 67 | + clientPolicy.timeout = 10_000;//in millis |
| 68 | + return new AerospikeClient(clientPolicy, host, port); |
| 69 | + } |
| 70 | + |
| 71 | + @Bean |
| 72 | + public ExpiredDocumentsCleaner durableDeleteExpiredDocumentsCleaner(AerospikeClient client, |
| 73 | + AerospikeProperties properties) { |
| 74 | + return new AerospikeExpiredDocumentsCleaner(client, properties.getNamespace(), true); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | +} |
0 commit comments