3838import io .opentelemetry .instrumentation .testing .junit .InstrumentationExtension ;
3939import java .util .concurrent .atomic .AtomicReference ;
4040import java .util .stream .Stream ;
41+ import org .junit .jupiter .api .BeforeAll ;
4142import org .junit .jupiter .api .Test ;
4243import org .junit .jupiter .api .extension .RegisterExtension ;
4344import org .junit .jupiter .params .ParameterizedTest ;
@@ -52,27 +53,43 @@ public abstract class AbstractCouchbaseClientTest extends AbstractCouchbaseTest
5253
5354 @ RegisterExtension static final AutoCleanupExtension cleanup = AutoCleanupExtension .create ();
5455
56+ private CouchbaseEnvironment environmentCouchbase ;
57+ private CouchbaseEnvironment environmentMemcache ;
58+ private CouchbaseCluster clusterCouchbase ;
59+ private CouchbaseCluster clusterMemcache ;
60+
5561 private static Stream <Arguments > bucketSettings () {
5662 return Stream .of (
5763 Arguments .of (named (bucketCouchbase .type ().name (), bucketCouchbase )),
5864 Arguments .of (named (bucketMemcache .type ().name (), bucketMemcache )));
5965 }
6066
61- protected CouchbaseCluster prepareCluster (BucketSettings bucketSettings ) {
62- CouchbaseEnvironment environment = envBuilder (bucketSettings ).build ();
63- CouchbaseCluster cluster = CouchbaseCluster .create (environment , singletonList ("127.0.0.1" ));
64- // AutoCleanupExtension runs in LIFO order; clear spans emitted while closing the client.
65- cleanup .deferCleanup (testing ::clearData );
66- cleanup .deferCleanup (environment ::shutdown );
67- cleanup .deferCleanup (cluster ::disconnect );
67+ @ BeforeAll
68+ void setUpClusters () {
69+ environmentCouchbase = envBuilder (bucketCouchbase ).build ();
70+ clusterCouchbase = CouchbaseCluster .create (environmentCouchbase , singletonList ("127.0.0.1" ));
71+ cleanup .deferAfterAll (environmentCouchbase ::shutdown );
72+ cleanup .deferAfterAll (clusterCouchbase ::disconnect );
73+
74+ environmentMemcache = envBuilder (bucketMemcache ).build ();
75+ clusterMemcache = CouchbaseCluster .create (environmentMemcache , singletonList ("127.0.0.1" ));
76+ cleanup .deferAfterAll (environmentMemcache ::shutdown );
77+ cleanup .deferAfterAll (clusterMemcache ::disconnect );
78+ }
6879
69- return cluster ;
80+ protected CouchbaseCluster getCluster (BucketSettings bucketSettings ) {
81+ if (bucketSettings == bucketCouchbase ) {
82+ return clusterCouchbase ;
83+ } else if (bucketSettings == bucketMemcache ) {
84+ return clusterMemcache ;
85+ }
86+ throw new IllegalArgumentException ("unknown setting " + bucketSettings .name ());
7087 }
7188
7289 @ ParameterizedTest
7390 @ MethodSource ("bucketSettings" )
7491 void hasBucket (BucketSettings bucketSettings ) {
75- CouchbaseCluster cluster = prepareCluster (bucketSettings );
92+ CouchbaseCluster cluster = getCluster (bucketSettings );
7693 ClusterManager manager = cluster .clusterManager (USERNAME , PASSWORD );
7794
7895 testing .waitForTraces (1 );
@@ -101,10 +118,11 @@ void hasBucket(BucketSettings bucketSettings) {
101118 @ ParameterizedTest
102119 @ MethodSource ("bucketSettings" )
103120 void upsertAndGet (BucketSettings bucketSettings ) {
104- CouchbaseCluster cluster = prepareCluster (bucketSettings );
121+ CouchbaseCluster cluster = getCluster (bucketSettings );
105122
106123 // Connect to the bucket and open it
107124 Bucket bucket = cluster .openBucket (bucketSettings .name (), bucketSettings .password ());
125+ cleanup .deferCleanup (bucket ::close );
108126
109127 // Create a JSON document and store it with the ID "helloworld"
110128 JsonObject content = JsonObject .create ().put ("hello" , "world" );
@@ -170,8 +188,9 @@ void upsertAndGet(BucketSettings bucketSettings) {
170188 @ Test
171189 void query () {
172190 // Only couchbase buckets support queries.
173- CouchbaseCluster cluster = prepareCluster (bucketCouchbase );
191+ CouchbaseCluster cluster = getCluster (bucketCouchbase );
174192 Bucket bucket = cluster .openBucket (bucketCouchbase .name (), bucketCouchbase .password ());
193+ cleanup .deferCleanup (bucket ::close );
175194
176195 // Mock expects this specific query.
177196 // See com.couchbase.mock.http.query.QueryServer.handleString.
0 commit comments