33
33
import static com .mongodb .reactivestreams .client .internal .TimeoutHelper .databaseWithTimeout ;
34
34
import static com .mongodb .reactivestreams .client .internal .TimeoutHelper .databaseWithTimeoutDeferred ;
35
35
import static org .junit .jupiter .api .Assertions .assertEquals ;
36
- import static org .junit .jupiter .api .Assertions .assertNotEquals ;
37
36
import static org .junit .jupiter .api .Assertions .assertThrows ;
38
37
import static org .mockito .ArgumentMatchers .anyLong ;
39
38
import static org .mockito .ArgumentMatchers .eq ;
@@ -82,39 +81,49 @@ void shouldNotSetRemainingTimeoutDatabaseWhenTimeoutIsNull() {
82
81
@ Test
83
82
void shouldNotSetRemainingTimeoutOnCollectionWhenTimeoutIsInfinite () {
84
83
//given
85
- MongoCollection <Document > collection = mock (MongoCollection .class );
84
+ MongoCollection <Document > collectionWithTimeout = mock (MongoCollection .class );
85
+ MongoCollection <Document > collection = mock (MongoCollection .class , mongoCollection -> {
86
+ when (mongoCollection .withTimeout (anyLong (), eq (TimeUnit .MILLISECONDS ))).thenReturn (collectionWithTimeout );
87
+ });
86
88
87
89
//when
88
90
MongoCollection <Document > result = collectionWithTimeout (collection , Timeout .infinite ());
89
91
MongoCollection <Document > monoResult = collectionWithTimeoutMono (collection , Timeout .infinite ()).block ();
90
92
MongoCollection <Document > monoResultDeferred = collectionWithTimeoutDeferred (collection , Timeout .infinite ()).block ();
91
93
92
94
//then
93
- assertEquals (collection , result );
94
- assertEquals (collection , monoResult );
95
- assertEquals (collection , monoResultDeferred );
95
+ assertEquals (collectionWithTimeout , result );
96
+ assertEquals (collectionWithTimeout , monoResult );
97
+ assertEquals (collectionWithTimeout , monoResultDeferred );
98
+ verify (collection , times (3 ))
99
+ .withTimeout (0L , TimeUnit .MILLISECONDS );
96
100
}
97
101
98
102
@ Test
99
103
void shouldNotSetRemainingTimeoutOnDatabaseWhenTimeoutIsInfinite () {
100
104
//given
101
- MongoDatabase database = mock (MongoDatabase .class );
105
+ MongoDatabase databaseWithTimeout = mock (MongoDatabase .class );
106
+ MongoDatabase database = mock (MongoDatabase .class , mongoDatabase -> {
107
+ when (mongoDatabase .withTimeout (anyLong (), eq (TimeUnit .MILLISECONDS ))).thenReturn (databaseWithTimeout );
108
+ });
102
109
103
110
//when
104
111
MongoDatabase result = databaseWithTimeout (database , TIMEOUT_ERROR_MESSAGE , Timeout .infinite ());
105
112
MongoDatabase monoResultDeferred = databaseWithTimeoutDeferred (database , TIMEOUT_ERROR_MESSAGE , Timeout .infinite ()).block ();
106
113
107
114
//then
108
- assertEquals (database , result );
109
- assertEquals (database , monoResultDeferred );
115
+ assertEquals (databaseWithTimeout , result );
116
+ assertEquals (databaseWithTimeout , monoResultDeferred );
117
+ verify (database , times (2 ))
118
+ .withTimeout (0L , TimeUnit .MILLISECONDS );
110
119
}
111
120
112
121
@ Test
113
122
void shouldSetRemainingTimeoutOnCollectionWhenTimeout () {
114
123
//given
115
124
MongoCollection <Document > collectionWithTimeout = mock (MongoCollection .class );
116
125
MongoCollection <Document > collection = mock (MongoCollection .class , mongoCollection -> {
117
- when (mongoCollection .withTimeout (anyLong (), eq (TimeUnit .MILLISECONDS ))).thenReturn (mongoCollection );
126
+ when (mongoCollection .withTimeout (anyLong (), eq (TimeUnit .MILLISECONDS ))).thenReturn (collectionWithTimeout );
118
127
});
119
128
Timeout timeout = Timeout .expiresIn (1 , TimeUnit .DAYS , ZERO_DURATION_MEANS_EXPIRED );
120
129
@@ -126,17 +135,17 @@ void shouldSetRemainingTimeoutOnCollectionWhenTimeout() {
126
135
//then
127
136
verify (collection , times (3 ))
128
137
.withTimeout (longThat (remaining -> remaining > 0 ), eq (TimeUnit .MILLISECONDS ));
129
- assertNotEquals (collectionWithTimeout , result );
130
- assertNotEquals (collectionWithTimeout , monoResult );
131
- assertNotEquals (collectionWithTimeout , monoResultDeferred );
138
+ assertEquals (collectionWithTimeout , result );
139
+ assertEquals (collectionWithTimeout , monoResult );
140
+ assertEquals (collectionWithTimeout , monoResultDeferred );
132
141
}
133
142
134
143
@ Test
135
144
void shouldSetRemainingTimeoutOnDatabaseWhenTimeout () {
136
145
//given
137
146
MongoDatabase databaseWithTimeout = mock (MongoDatabase .class );
138
147
MongoDatabase database = mock (MongoDatabase .class , mongoDatabase -> {
139
- when (mongoDatabase .withTimeout (anyLong (), eq (TimeUnit .MILLISECONDS ))).thenReturn (mongoDatabase );
148
+ when (mongoDatabase .withTimeout (anyLong (), eq (TimeUnit .MILLISECONDS ))).thenReturn (databaseWithTimeout );
140
149
});
141
150
Timeout timeout = Timeout .expiresIn (1 , TimeUnit .DAYS , ZERO_DURATION_MEANS_EXPIRED );
142
151
@@ -147,8 +156,8 @@ void shouldSetRemainingTimeoutOnDatabaseWhenTimeout() {
147
156
//then
148
157
verify (database , times (2 ))
149
158
.withTimeout (longThat (remaining -> remaining > 0 ), eq (TimeUnit .MILLISECONDS ));
150
- assertNotEquals (databaseWithTimeout , result );
151
- assertNotEquals (databaseWithTimeout , monoResultDeferred );
159
+ assertEquals (databaseWithTimeout , result );
160
+ assertEquals (databaseWithTimeout , monoResultDeferred );
152
161
}
153
162
154
163
@ Test
0 commit comments