我正在编写一些JUnit测试,涉及打开Couchbase,做一些事情,然后关闭Couchbase。测试的工作原理是建立连接并创建存储桶,然后在集群断开连接时发生异常。同样,一切似乎都有效,但我不确定连接是否实际上已关闭,我想了解问题发生的原因。
如果我在Thread.sleep上切换几秒钟,则不会发生错误。此外,如果我注释掉桶的打开,则不会发生错误。所以我认为它与桶需要时间打开并在集群关闭之前做它的事情有关(我还试图在关闭集群之前关闭存储桶,但这似乎没有什么区别)。
我试图理解为什么会发生这种情况,并且如果有一种比使用Thread.sleep更优雅的解决方法。
以下是代码:
static Cluster cluster;
static Bucket bucket;
@BeforeClass
public static void setUp() {
cluster = CouchbaseCluster.create();
}
@AfterClass
public static void tearDown() {
cluster.disconnect();
}
@Test
public void test() {
ClusterManager clusterManager = cluster.clusterManager("Administrator", "***");
BucketSettings bucketSettings = new DefaultBucketSettings.Builder()
.type(BucketType.COUCHBASE)
.name("test_db")
.password("")
.quota(100)
.indexReplicas(true)
.enableFlush(true)
.build();
clusterManager.insertBucket(bucketSettings);
bucket = cluster.openBucket("test_db");
//DO DB Operations
//Thread.sleep(4000);
}
以下是错误日志:
16:55:53.859 [globalEventExecutor-2-3] INFO com.couchbase.client.core.env.CoreEnvironment - Shutdown IoPool: success
16:55:54.705 [RxComputationScheduler-3] ERROR com.couchbase.client.deps.io.netty.util.concurrent.DefaultPromise.rejectedExecution - Failed to submit a listener notification task. Event loop shut down?
java.util.concurrent.RejectedExecutionException: event executor terminated
at com.couchbase.client.deps.io.netty.util.concurrent.SingleThreadEventExecutor.reject(SingleThreadEventExecutor.java:796)
at com.couchbase.client.deps.io.netty.util.concurrent.SingleThreadEventExecutor.offerTask(SingleThreadEventExecutor.java:336)
at com.couchbase.client.deps.io.netty.util.concurrent.SingleThreadEventExecutor.addTask(SingleThreadEventExecutor.java:329)
at com.couchbase.client.deps.io.netty.util.concurrent.SingleThreadEventExecutor.execute(SingleThreadEventExecutor.java:739)
at com.couchbase.client.deps.io.netty.util.concurrent.DefaultPromise.safeExecute(DefaultPromise.java:760)
at com.couchbase.client.deps.io.netty.util.concurrent.DefaultPromise.notifyListeners(DefaultPromise.java:428)
at com.couchbase.client.deps.io.netty.util.concurrent.DefaultPromise.setFailure(DefaultPromise.java:113)
at com.couchbase.client.deps.io.netty.channel.DefaultChannelPromise.setFailure(DefaultChannelPromise.java:87)
at com.couchbase.client.core.endpoint.AbstractEndpoint$3.call(AbstractEndpoint.java:337)
at com.couchbase.client.core.endpoint.AbstractEndpoint$3.call(AbstractEndpoint.java:330)
at rx.internal.operators.SingleOperatorOnErrorResumeNext$2.onError(SingleOperatorOnErrorResumeNext.java:69)
at rx.internal.operators.SingleTimeout$TimeoutSingleSubscriber$OtherSubscriber.onError(SingleTimeout.java:133)
at rx.Single$1.call(Single.java:460)
at rx.Single$1.call(Single.java:456)
at rx.internal.operators.SingleTimeout$TimeoutSingleSubscriber.call(SingleTimeout.java:110)
at rx.internal.schedulers.EventLoopsScheduler$EventLoopWorker$2.call(EventLoopsScheduler.java:189)
at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
16:55:54.706 [RxComputationScheduler-3] DEBUG com.couchbase.client.core.endpoint.Endpoint - [null][KeyValueEndpoint]: Endpoint connect completed, but got instructed to disconnect in the meantime.
16:55:54.821 [Thread-5] INFO com.couchbase.client.core.env.CoreEnvironment - Shutdown Netty: failure
16:55:54.821 [Thread-5] INFO com.couchbase.client.core.env.CoreEnvironment - Netty shutdown is best effort, ignoring failure
感谢您的任何帮助/建议!