/*
 * 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.runtime.rpc.akka;

import org.apache.flink.runtime.akka.AkkaUtils;
import org.apache.flink.runtime.rpc.RpcEndpoint;
import org.apache.flink.runtime.rpc.RpcGateway;
import org.apache.flink.runtime.rpc.RpcService;
import org.apache.flink.util.TestLogger;

import org.junit.Test;

import java.util.concurrent.CompletableFuture;

import static org.junit.Assert.assertTrue;

public class MainThreadValidationTest extends TestLogger {

	@Test
	public void failIfNotInMainThread() throws Exception {
		// test if assertions are activated. The test only works if assertions are loaded.
		try {
			assert false;
			// apparently they are not activated
			return;
		} catch (AssertionError ignored) {}

		// actual test
		AkkaRpcService akkaRpcService = new AkkaRpcService(
				AkkaUtils.createDefaultActorSystem(),
				AkkaRpcServiceConfiguration.defaultConfiguration());

		try {
			TestEndpoint testEndpoint = new TestEndpoint(akkaRpcService);
			testEndpoint.start();

			// this works, because it is executed as an RPC call
			testEndpoint.getSelfGateway(TestGateway.class).someConcurrencyCriticalFunction();

			// this fails, because it is executed directly
			boolean exceptionThrown;
			try {
				testEndpoint.someConcurrencyCriticalFunction();
				exceptionThrown = false;
			}
			catch (AssertionError e) {
				exceptionThrown = true;
			}
			assertTrue("should fail with an assertion error", exceptionThrown);

			testEndpoint.shutDown();
		}
		finally {
			akkaRpcService.stopService().get();
		}
	}

	// ------------------------------------------------------------------------
	//  test RPC endpoint
	// ------------------------------------------------------------------------

	interface TestGateway extends RpcGateway {

		void someConcurrencyCriticalFunction();
	}

	@SuppressWarnings("unused")
	public static class TestEndpoint extends RpcEndpoint implements TestGateway {

		public TestEndpoint(RpcService rpcService) {
			super(rpcService);
		}

		@Override
		public CompletableFuture<Void> postStop() {
			return CompletableFuture.completedFuture(null);
		}

		@Override
		public void someConcurrencyCriticalFunction() {
			validateRunsInMainThread();
		}
	}
}

@@ -1195,16 +1195,125 @@
 tTrue;%0A%0A
+/**%0A * Tests that the %7B@link AkkaRpcService%7D runs all RPCs in the %7B@link AkkaRpcActor%7D's%0A * main thread.%0A */%0A
 public c
@@ -2712,43 +2712,15 @@
 %7D%0A%0A%09
-@SuppressWarnings(%22unused%22)%0A%09public
+private
  sta
@@ -2791,21 +2791,22 @@
 y %7B%0A%0A%09%09p
-ublic
+rivate
  TestEnd

Fix checkstyle violations in MainThreadValidationTest