-
Notifications
You must be signed in to change notification settings - Fork 254
Expand file tree
/
Copy pathAerospikeTimeTravelTest.java
More file actions
118 lines (90 loc) · 3.97 KB
/
AerospikeTimeTravelTest.java
File metadata and controls
118 lines (90 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package com.playtika.testcontainer.aerospike;
import com.aerospike.client.Bin;
import com.aerospike.client.Key;
import com.aerospike.client.policy.WritePolicy;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.temporal.ChronoUnit;
import java.util.concurrent.TimeUnit;
import static java.time.Duration.ofDays;
import static java.time.Duration.ofHours;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
public class AerospikeTimeTravelTest extends BaseAerospikeTest {
@Autowired
private ExpiredDocumentsCleaner expiredDocumentsCleaner;
@AfterEach
public void tearDown() {
aerospikeTestOperations.rollbackTime();
}
@Test
public void shouldRemoveExpired() throws Exception {
Key key = new Key(namespace, SET, "shouldRemoveExpired");
putBin(key, (int) TimeUnit.DAYS.toSeconds(1));
Instant plus23 = Instant.now().plus(23, ChronoUnit.HOURS);
expiredDocumentsCleaner.cleanExpiredDocumentsBefore(plus23);
assertThat(client.get(null, key)).isNotNull();
Instant plus25 = Instant.now().plus(25, ChronoUnit.HOURS);
expiredDocumentsCleaner.cleanExpiredDocumentsBefore(plus25);
assertThat(client.get(null, key)).isNull();
}
@Test
public void shouldNotRemoveNonExpiringDocuments() throws Exception {
Key key = new Key(namespace, SET, "shouldRemoveExpired");
putBin(key, 0);
Instant twoDays = Instant.now().plus(2, ChronoUnit.DAYS);
expiredDocumentsCleaner.cleanExpiredDocumentsBefore(twoDays);
assertThat(client.get(null, key)).isNotNull();
}
@Test
public void shouldAddHours() {
Key key = new Key(namespace, SET, "shouldTimeTravel");
putBin(key, (int) TimeUnit.HOURS.toSeconds(25));
aerospikeTestOperations.addDuration(ofHours(24));
assertThat(client.get(null, key)).isNotNull();
aerospikeTestOperations.addDuration(ofHours(2));
assertThat(client.get(null, key)).isNull();
}
@Test
public void shouldAddDays() {
Key key = new Key(namespace, SET, "shouldAddDays");
putBin(key, (int) TimeUnit.HOURS.toSeconds(25));
aerospikeTestOperations.addDuration(ofDays(1));
assertThat(client.get(null, key)).isNotNull();
aerospikeTestOperations.addDuration(ofDays(1));
assertThat(client.get(null, key)).isNull();
}
@Test
public void shouldSetFutureTime() {
Key key = new Key(namespace, SET, "shouldSetFutureTime");
putBin(key, (int) TimeUnit.HOURS.toSeconds(25));
aerospikeTestOperations.timeTravelTo(DateTimeUtils.now().plusHours(24));
assertThat(client.get(null, key)).isNotNull();
aerospikeTestOperations.timeTravelTo(DateTimeUtils.now().plusHours(2));
assertThat(client.get(null, key)).isNull();
}
@Test
public void shouldNotSetFutureTime() {
OffsetDateTime minusDay = DateTimeUtils.now().minusDays(1);
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> aerospikeTestOperations.timeTravelTo(minusDay));
}
@Test
public void shouldRollbackTime() {
Key key = new Key(namespace, SET, "shouldRollbackTime");
putBin(key, (int) TimeUnit.HOURS.toSeconds(3));
aerospikeTestOperations.addDuration(ofHours(2));
assertThat(client.get(null, key)).isNotNull();
aerospikeTestOperations.rollbackTime();
aerospikeTestOperations.addDuration(ofHours(2));
assertThat(client.get(null, key)).isNotNull();
}
private void putBin(Key key, int expiration) {
Bin bin = new Bin("mybin", "myvalue");
WritePolicy writePolicy = new WritePolicy(client.getWritePolicyDefault());
writePolicy.expiration = expiration;
client.put(writePolicy, key, bin);
}
}