16
16
17
17
package org .springframework .session .data .redis ;
18
18
19
+ import java .time .Duration ;
19
20
import java .time .Instant ;
20
21
22
+ import org .junit .jupiter .api .BeforeEach ;
21
23
import org .junit .jupiter .api .Test ;
22
24
import org .junit .jupiter .api .extension .ExtendWith ;
23
25
import reactor .core .publisher .Mono ;
24
26
25
27
import org .springframework .beans .factory .annotation .Autowired ;
26
28
import org .springframework .context .annotation .Configuration ;
29
+ import org .springframework .data .redis .core .ReactiveHashOperations ;
27
30
import org .springframework .data .redis .core .ReactiveRedisOperations ;
28
31
import org .springframework .session .Session ;
29
32
import org .springframework .session .data .redis .ReactiveRedisSessionRepository .RedisSession ;
35
38
36
39
import static org .assertj .core .api .Assertions .assertThat ;
37
40
import static org .assertj .core .api .Assertions .assertThatIllegalStateException ;
41
+ import static org .mockito .ArgumentMatchers .any ;
42
+ import static org .mockito .ArgumentMatchers .anyString ;
38
43
import static org .mockito .ArgumentMatchers .endsWith ;
39
44
import static org .mockito .BDDMockito .given ;
45
+ import static org .mockito .BDDMockito .willAnswer ;
40
46
import static org .mockito .Mockito .reset ;
41
47
import static org .mockito .Mockito .spy ;
42
48
@@ -53,6 +59,13 @@ class ReactiveRedisSessionRepositoryITests extends AbstractRedisITests {
53
59
@ Autowired
54
60
private ReactiveRedisSessionRepository repository ;
55
61
62
+ private ReactiveRedisOperations <String , Object > sessionRedisOperations ;
63
+
64
+ @ BeforeEach
65
+ void setup () {
66
+ this .sessionRedisOperations = this .repository .getSessionRedisOperations ();
67
+ }
68
+
56
69
@ Test
57
70
void saves () {
58
71
RedisSession toSave = this .repository .createSession ().block ();
@@ -75,7 +88,8 @@ void saves() {
75
88
assertThat (this .repository .findById (toSave .getId ()).block ()).isNull ();
76
89
}
77
90
78
- @ Test // gh-1399
91
+ @ Test
92
+ // gh-1399
79
93
void saveMultipleTimes () {
80
94
RedisSession session = this .repository .createSession ().block ();
81
95
session .setAttribute ("attribute1" , "value1" );
@@ -245,6 +259,30 @@ void saveChangeSessionIdAfterCheckWhenOriginalKeyDoesNotExistsThenIgnoreError()
245
259
reset (spyOperations );
246
260
}
247
261
262
+ // gh-2464
263
+ @ Test
264
+ void saveWhenPutAllIsDelayedThenExpireShouldBeSet () {
265
+ ReactiveRedisOperations <String , Object > spy = spy (this .sessionRedisOperations );
266
+ ReflectionTestUtils .setField (this .repository , "sessionRedisOperations" , spy );
267
+ ReactiveHashOperations <String , Object , Object > opsForHash = spy (this .sessionRedisOperations .opsForHash ());
268
+ given (spy .opsForHash ()).willReturn (opsForHash );
269
+ willAnswer ((invocation ) -> Mono .delay (Duration .ofSeconds (1 )).then ((Mono <Void >) invocation .callRealMethod ()))
270
+ .given (opsForHash ).putAll (anyString (), any ());
271
+ RedisSession toSave = this .repository .createSession ().block ();
272
+
273
+ String expectedAttributeName = "a" ;
274
+ String expectedAttributeValue = "b" ;
275
+
276
+ toSave .setAttribute (expectedAttributeName , expectedAttributeValue );
277
+ this .repository .save (toSave ).block ();
278
+
279
+ String id = toSave .getId ();
280
+ Duration expireDuration = this .sessionRedisOperations .getExpire ("spring:session:sessions:" + id ).block ();
281
+
282
+ assertThat (expireDuration ).isNotEqualTo (Duration .ZERO );
283
+ reset (spy );
284
+ }
285
+
248
286
@ Configuration
249
287
@ EnableRedisWebSession
250
288
static class Config extends BaseConfig {
0 commit comments