You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by ch...@apache.org on 2020/05/28 15:46:39 UTC

[flink] 03/07: [FLINK-17558][tests] Add TestExecutorResource

This is an automated email from the ASF dual-hosted git repository.

chesnay pushed a commit to branch release-1.10
in repository https://gitbox.apache.org/repos/asf/flink.git

commit cbb8cb65db71a600d551ba0ceb9bd413f47a5c3a
Author: Chesnay Schepler <ch...@apache.org>
AuthorDate: Thu May 28 14:45:13 2020 +0200

    [FLINK-17558][tests] Add TestExecutorResource
---
 .../testutils/executor/TestExecutorResource.java   | 55 ++++++++++++++++++++++
 1 file changed, 55 insertions(+)

diff --git a/flink-test-utils-parent/flink-test-utils-junit/src/main/java/org/apache/flink/testutils/executor/TestExecutorResource.java b/flink-test-utils-parent/flink-test-utils-junit/src/main/java/org/apache/flink/testutils/executor/TestExecutorResource.java
new file mode 100644
index 0000000..b7619fc
--- /dev/null
+++ b/flink-test-utils-parent/flink-test-utils-junit/src/main/java/org/apache/flink/testutils/executor/TestExecutorResource.java
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.testutils.executor;
+
+import org.junit.rules.ExternalResource;
+
+import java.util.concurrent.Executor;
+import java.util.concurrent.ExecutorService;
+import java.util.function.Supplier;
+
+/**
+ * Resource which starts/stops an {@link ExecutorService} for testing purposes.
+ */
+public class TestExecutorResource extends ExternalResource {
+
+	private final Supplier<ExecutorService> serviceFactory;
+
+	private ExecutorService executorService;
+
+	public TestExecutorResource(Supplier<ExecutorService> serviceFactory) {
+		this.serviceFactory = serviceFactory;
+	}
+
+	@Override
+	protected void before() throws Throwable {
+		executorService = serviceFactory.get();
+	}
+
+	public Executor getExecutor() {
+		// only return an Executor since this resource is in charge of the life cycle
+		return executorService;
+	}
+
+	@Override
+	protected void after() {
+		if (executorService != null) {
+			executorService.shutdown();
+		}
+	}
+}