1919import com .mongodb .client .result .UpdateResult ;
2020import com .mongodb .connection .ClusterSettings ;
2121import io .opentelemetry .instrumentation .mongo .testing .AbstractMongoClientTest ;
22+ import io .opentelemetry .instrumentation .testing .internal .AutoCleanupExtension ;
2223import io .opentelemetry .instrumentation .testing .junit .AgentInstrumentationExtension ;
2324import io .opentelemetry .instrumentation .testing .junit .InstrumentationExtension ;
2425import java .util .concurrent .CompletableFuture ;
2728import org .bson .BsonDocument ;
2829import org .bson .BsonString ;
2930import org .bson .Document ;
30- import org .junit .jupiter .api .AfterAll ;
3131import org .junit .jupiter .api .BeforeAll ;
3232import org .junit .jupiter .api .extension .RegisterExtension ;
3333
@@ -36,6 +36,8 @@ class MongoAsyncClientTest extends AbstractMongoClientTest<MongoCollection<Docum
3636 @ RegisterExtension
3737 static final InstrumentationExtension testing = AgentInstrumentationExtension .create ();
3838
39+ @ RegisterExtension static final AutoCleanupExtension cleanup = AutoCleanupExtension .create ();
40+
3941 private MongoClient client ;
4042
4143 @ BeforeAll
@@ -50,13 +52,7 @@ void setup() {
5052 new ConnectionString ("mongodb://" + host + ":" + port ))
5153 .build ())
5254 .build ());
53- }
54-
55- @ AfterAll
56- void cleanup () {
57- if (client != null ) {
58- client .close ();
59- }
55+ cleanup .deferAfterAll (client );
6056 }
6157
6258 @ Override
@@ -65,27 +61,30 @@ protected InstrumentationExtension testing() {
6561 }
6662
6763 @ Override
68- public void createCollection (String dbName , String collectionName ) {
64+ protected void createCollection (String dbName , String collectionName ) {
6965 MongoDatabase db = client .getDatabase (dbName );
7066 db .createCollection (collectionName , toCallback (result -> {}));
7167 }
7268
7369 @ Override
74- public void createCollectionNoDescription (String dbName , String collectionName ) {
75- MongoDatabase db = MongoClients .create ("mongodb://" + host + ":" + port ).getDatabase (dbName );
76- db .createCollection (collectionName , toCallback (result -> {}));
70+ protected void createCollectionNoDescription (String dbName , String collectionName ) {
71+ MongoClient mongoClient = MongoClients .create ("mongodb://" + host + ":" + port );
72+ cleanup .deferAfterAll (mongoClient );
73+ mongoClient .getDatabase (dbName ).createCollection (collectionName , toCallback (result -> {}));
7774 }
7875
7976 @ Override
80- public void createCollectionWithAlreadyBuiltClientOptions (String dbName , String collectionName ) {
77+ protected void createCollectionWithAlreadyBuiltClientOptions (
78+ String dbName , String collectionName ) {
8179 MongoClientSettings clientSettings = client .getSettings ();
8280 MongoClientSettings newClientSettings = MongoClientSettings .builder (clientSettings ).build ();
83- MongoDatabase db = MongoClients .create (newClientSettings ).getDatabase (dbName );
84- db .createCollection (collectionName , toCallback (result -> {}));
81+ MongoClient mongoClient = MongoClients .create (newClientSettings );
82+ cleanup .deferAfterAll (mongoClient );
83+ mongoClient .getDatabase (dbName ).createCollection (collectionName , toCallback (result -> {}));
8584 }
8685
8786 @ Override
88- public void createCollectionCallingBuildTwice (String dbName , String collectionName ) {
87+ protected void createCollectionCallingBuildTwice (String dbName , String collectionName ) {
8988 MongoClientSettings .Builder settings =
9089 MongoClientSettings .builder ()
9190 .clusterSettings (
@@ -94,20 +93,21 @@ public void createCollectionCallingBuildTwice(String dbName, String collectionNa
9493 .applyConnectionString (new ConnectionString ("mongodb://" + host + ":" + port ))
9594 .build ());
9695 settings .build ();
97- MongoDatabase db = MongoClients .create (settings .build ()).getDatabase (dbName );
98- db .createCollection (collectionName , toCallback (result -> {}));
96+ MongoClient mongoClient = MongoClients .create (settings .build ());
97+ cleanup .deferAfterAll (mongoClient );
98+ mongoClient .getDatabase (dbName ).createCollection (collectionName , toCallback (result -> {}));
9999 }
100100
101101 @ Override
102- public long getCollection (String dbName , String collectionName ) {
102+ protected long getCollection (String dbName , String collectionName ) {
103103 MongoDatabase db = client .getDatabase (dbName );
104104 CompletableFuture <Long > count = new CompletableFuture <>();
105105 db .getCollection (collectionName ).count (toCallback (o -> count .complete (((Long ) o ))));
106106 return count .join ();
107107 }
108108
109109 @ Override
110- public MongoCollection <Document > setupInsert (String dbName , String collectionName )
110+ protected MongoCollection <Document > setupInsert (String dbName , String collectionName )
111111 throws InterruptedException {
112112 MongoCollection <Document > collection =
113113 testing ()
@@ -125,7 +125,7 @@ public MongoCollection<Document> setupInsert(String dbName, String collectionNam
125125 }
126126
127127 @ Override
128- public long insert (MongoCollection <Document > collection ) {
128+ protected long insert (MongoCollection <Document > collection ) {
129129 CompletableFuture <Long > count = new CompletableFuture <>();
130130 collection .insertOne (
131131 new Document ("password" , "SECRET" ),
@@ -134,7 +134,7 @@ public long insert(MongoCollection<Document> collection) {
134134 }
135135
136136 @ Override
137- public MongoCollection <Document > setupUpdate (String dbName , String collectionName )
137+ protected MongoCollection <Document > setupUpdate (String dbName , String collectionName )
138138 throws InterruptedException {
139139 MongoCollection <Document > collection =
140140 testing ()
@@ -157,7 +157,7 @@ public MongoCollection<Document> setupUpdate(String dbName, String collectionNam
157157 }
158158
159159 @ Override
160- public long update (MongoCollection <Document > collection ) {
160+ protected long update (MongoCollection <Document > collection ) {
161161 CompletableFuture <UpdateResult > result = new CompletableFuture <>();
162162 CompletableFuture <Long > count = new CompletableFuture <>();
163163 collection .updateOne (
@@ -172,7 +172,7 @@ public long update(MongoCollection<Document> collection) {
172172 }
173173
174174 @ Override
175- public MongoCollection <Document > setupDelete (String dbName , String collectionName )
175+ protected MongoCollection <Document > setupDelete (String dbName , String collectionName )
176176 throws InterruptedException {
177177 MongoCollection <Document > collection =
178178 testing ()
@@ -195,7 +195,7 @@ public MongoCollection<Document> setupDelete(String dbName, String collectionNam
195195 }
196196
197197 @ Override
198- public long delete (MongoCollection <Document > collection ) {
198+ protected long delete (MongoCollection <Document > collection ) {
199199 CompletableFuture <DeleteResult > result = new CompletableFuture <>();
200200 CompletableFuture <Long > count = new CompletableFuture <>();
201201 collection .deleteOne (
@@ -209,17 +209,17 @@ public long delete(MongoCollection<Document> collection) {
209209 }
210210
211211 @ Override
212- public MongoCollection <Document > setupGetMore (String dbName , String collectionName ) {
212+ protected MongoCollection <Document > setupGetMore (String dbName , String collectionName ) {
213213 return abort ("not tested on async" );
214214 }
215215
216216 @ Override
217- public void getMore (MongoCollection <Document > collection ) {
217+ protected void getMore (MongoCollection <Document > collection ) {
218218 abort ("not tested on async" );
219219 }
220220
221221 @ Override
222- public void error (String dbName , String collectionName ) throws Throwable {
222+ protected void error (String dbName , String collectionName ) throws Throwable {
223223 MongoCollection <Document > collection =
224224 testing ()
225225 .runWithSpan (
@@ -240,7 +240,7 @@ public void error(String dbName, String collectionName) throws Throwable {
240240 throw result .join ();
241241 }
242242
243- <T > SingleResultCallback <T > toCallback (Consumer <Object > closure ) {
243+ private static <T > SingleResultCallback <T > toCallback (Consumer <Object > closure ) {
244244 return (result , t ) -> {
245245 if (t != null ) {
246246 closure .accept (t );
0 commit comments