27
27
import com .mongodb .event .CommandStartedEvent ;
28
28
import org .bson .BsonDocument ;
29
29
import org .bson .Document ;
30
+ import org .junit .jupiter .api .AfterAll ;
31
+ import org .junit .jupiter .api .BeforeAll ;
30
32
import org .junit .jupiter .api .Test ;
31
33
32
34
import java .io .File ;
55
57
public abstract class AbstractSessionsProseTest {
56
58
57
59
private static final int MONGOCRYPTD_PORT = 47017 ;
60
+ private static Process mongocryptdProcess ;
58
61
59
62
protected abstract MongoClient getMongoClient (MongoClientSettings settings );
60
63
64
+ @ BeforeAll
65
+ public static void beforeAll () throws IOException {
66
+ if (serverVersionAtLeast (4 , 2 )) {
67
+ mongocryptdProcess = startMongocryptdProcess ();
68
+ }
69
+ }
70
+
71
+ @ AfterAll
72
+ public static void afterAll () {
73
+ if (mongocryptdProcess != null ) {
74
+ mongocryptdProcess .destroy ();
75
+ mongocryptdProcess = null ;
76
+ }
77
+ }
78
+
61
79
// Test 13 from #13-existing-sessions-are-not-checked-into-a-cleared-pool-after-forking
62
80
@ Test
63
81
public void shouldCreateServerSessionOnlyAfterConnectionCheckout () throws InterruptedException {
@@ -119,78 +137,69 @@ public void commandStarted(final CommandStartedEvent event) {
119
137
@ Test
120
138
public void shouldIgnoreImplicitSessionIfConnectionDoesNotSupportSessions () throws IOException {
121
139
assumeTrue (serverVersionAtLeast (4 , 2 ));
122
- Process mongocryptdProcess = startMongocryptdProcess ("1" );
123
- try {
124
- // initialize to true in case the command listener is never actually called, in which case the assertFalse will fire
125
- AtomicBoolean containsLsid = new AtomicBoolean (true );
126
- try (MongoClient client = getMongoClient (
127
- getMongocryptdMongoClientSettingsBuilder ()
128
- .addCommandListener (new CommandListener () {
129
- @ Override
130
- public void commandStarted (final CommandStartedEvent event ) {
131
- containsLsid .set (event .getCommand ().containsKey ("lsid" ));
132
- }
133
- })
134
- .build ())) {
135
-
136
- Document helloResponse = client .getDatabase ("admin" ).runCommand (new Document ("hello" , 1 ));
137
- assertFalse ((helloResponse .containsKey ("logicalSessionTimeoutMinutes" )));
138
-
139
- MongoCollection <Document > collection = client .getDatabase (getDefaultDatabaseName ()).getCollection (getClass ().getName ());
140
- try {
141
- collection .find ().first ();
142
- } catch (MongoCommandException e ) {
143
- // ignore command errors from mongocryptd
144
- }
145
- assertFalse (containsLsid .get ());
146
140
147
- // reset
148
- containsLsid .set (true );
141
+ // initialize to true in case the command listener is never actually called, in which case the assertFalse will fire
142
+ AtomicBoolean containsLsid = new AtomicBoolean (true );
143
+ try (MongoClient client = getMongoClient (
144
+ getMongocryptdMongoClientSettingsBuilder ()
145
+ .addCommandListener (new CommandListener () {
146
+ @ Override
147
+ public void commandStarted (final CommandStartedEvent event ) {
148
+ containsLsid .set (event .getCommand ().containsKey ("lsid" ));
149
+ }
150
+ })
151
+ .build ())) {
152
+
153
+ Document helloResponse = client .getDatabase ("admin" ).runCommand (new Document ("hello" , 1 ));
154
+ assertFalse ((helloResponse .containsKey ("logicalSessionTimeoutMinutes" )));
149
155
150
- try {
151
- collection .insertOne (new Document ());
152
- } catch (MongoCommandException e ) {
153
- // ignore command errors from mongocryptd
154
- }
155
- assertFalse (containsLsid .get ());
156
+ MongoCollection <Document > collection = client .getDatabase (getDefaultDatabaseName ()).getCollection (getClass ().getName ());
157
+ try {
158
+ collection .find ().first ();
159
+ } catch (MongoCommandException e ) {
160
+ // ignore command errors from mongocryptd
156
161
}
157
- } finally {
158
- mongocryptdProcess .destroy ();
162
+ assertFalse (containsLsid .get ());
163
+
164
+ // reset
165
+ containsLsid .set (true );
166
+
167
+ try {
168
+ collection .insertOne (new Document ());
169
+ } catch (MongoCommandException e ) {
170
+ // ignore command errors from mongocryptd
171
+ }
172
+ assertFalse (containsLsid .get ());
159
173
}
160
174
}
161
175
162
176
// Test 19 from #19-explicit-session-raises-an-error-if-connection-does-not-support-sessions
163
177
@ Test
164
178
public void shouldThrowOnExplicitSessionIfConnectionDoesNotSupportSessions () throws IOException {
165
179
assumeTrue (serverVersionAtLeast (4 , 2 ));
166
- Process mongocryptdProcess = startMongocryptdProcess ("2" );
167
- try {
168
- try (MongoClient client = getMongoClient (getMongocryptdMongoClientSettingsBuilder ().build ())) {
169
- MongoCollection <Document > collection = client .getDatabase (getDefaultDatabaseName ()).getCollection (getClass ().getName ());
170
-
171
- Document helloResponse = client .getDatabase ("admin" ).runCommand (new Document ("hello" , 1 ));
172
- assertFalse ((helloResponse .containsKey ("logicalSessionTimeoutMinutes" )));
173
-
174
- try (ClientSession session = client .startSession ()) {
175
- String expectedClientExceptionMessage =
176
- "Attempting to use a ClientSession while connected to a server that doesn't support sessions" ;
177
- try {
178
- collection .find (session ).first ();
179
- fail ("Expected MongoClientException" );
180
- } catch (MongoClientException e ) {
181
- assertEquals (expectedClientExceptionMessage , e .getMessage ());
182
- }
183
-
184
- try {
185
- collection .insertOne (session , new Document ());
186
- fail ("Expected MongoClientException" );
187
- } catch (MongoClientException e ) {
188
- assertEquals (expectedClientExceptionMessage , e .getMessage ());
189
- }
180
+ try (MongoClient client = getMongoClient (getMongocryptdMongoClientSettingsBuilder ().build ())) {
181
+ MongoCollection <Document > collection = client .getDatabase (getDefaultDatabaseName ()).getCollection (getClass ().getName ());
182
+
183
+ Document helloResponse = client .getDatabase ("admin" ).runCommand (new Document ("hello" , 1 ));
184
+ assertFalse ((helloResponse .containsKey ("logicalSessionTimeoutMinutes" )));
185
+
186
+ try (ClientSession session = client .startSession ()) {
187
+ String expectedClientExceptionMessage =
188
+ "Attempting to use a ClientSession while connected to a server that doesn't support sessions" ;
189
+ try {
190
+ collection .find (session ).first ();
191
+ fail ("Expected MongoClientException" );
192
+ } catch (MongoClientException e ) {
193
+ assertEquals (expectedClientExceptionMessage , e .getMessage ());
194
+ }
195
+
196
+ try {
197
+ collection .insertOne (session , new Document ());
198
+ fail ("Expected MongoClientException" );
199
+ } catch (MongoClientException e ) {
200
+ assertEquals (expectedClientExceptionMessage , e .getMessage ());
190
201
}
191
202
}
192
- } finally {
193
- mongocryptdProcess .destroy ();
194
203
}
195
204
}
196
205
@@ -200,10 +209,11 @@ private static MongoClientSettings.Builder getMongocryptdMongoClientSettingsBuil
200
209
builder .hosts (singletonList (new ServerAddress ("localhost" , MONGOCRYPTD_PORT ))));
201
210
}
202
211
203
- private static Process startMongocryptdProcess (final String pidSuffix ) throws IOException {
212
+ private static Process startMongocryptdProcess () throws IOException {
213
+ String port = Integer .toString (MONGOCRYPTD_PORT );
204
214
ProcessBuilder processBuilder = new ProcessBuilder (asList ("mongocryptd" ,
205
- "--port" , Integer . toString ( MONGOCRYPTD_PORT ) ,
206
- "--pidfilepath" , "mongocryptd-" + pidSuffix + ".pid" ));
215
+ "--port" , port ,
216
+ "--pidfilepath" , "mongocryptd-" + port + ".pid" ));
207
217
processBuilder .redirectErrorStream (true );
208
218
processBuilder .redirectOutput (new File ("/tmp/mongocryptd.log" ));
209
219
return processBuilder .start ();
0 commit comments