/*
 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.source.coordinator;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

/**
 * Unit tests for ExecutorNotifier.
 */
public class ExecutorNotifierTest {
	private ScheduledExecutorService workerExecutor;
	private ExecutorService executorToNotify;
	private ExecutorNotifier notifier;
	private Throwable exceptionInHandler;

	@Before
	public void setup() {
		this.exceptionInHandler = null;
		this.workerExecutor = Executors.newSingleThreadScheduledExecutor(r ->
				new Thread(r, "worker-thread"));
		this.executorToNotify = Executors.newSingleThreadExecutor(r -> {
			Thread t = new Thread(r, "main-thread");
			t.setUncaughtExceptionHandler((thread, e) -> exceptionInHandler = e);
			return t;
		});
		this.notifier = new ExecutorNotifier(
				this.workerExecutor,
				this.executorToNotify);
	}

	@After
	public void tearDown() throws InterruptedException {
		notifier.close();
		closeExecutorToNotify();
	}

	@Test
	public void testBasic() throws InterruptedException {
		CountDownLatch latch = new CountDownLatch(1);
		AtomicInteger result = new AtomicInteger(0);
		notifier.notifyReadyAsync(() -> 1234, (v, e) -> {
			result.set(v);
			latch.countDown();
		});
		latch.await();
		closeExecutorToNotify();
		assertEquals(1234, result.get());
	}

	@Test
	public void testExceptionInCallable() {
		Exception exception = new Exception("Expected exception.");
		notifier.notifyReadyAsync(
				() -> {
					throw exception;
				},
				(v, e) -> {
					assertEquals(exception, e);
					assertNull(v);
				});
	}

	@Test
	public void testExceptionInHandler() throws InterruptedException {
		CountDownLatch latch = new CountDownLatch(1);
		RuntimeException exception =  new RuntimeException("Expected exception.");
		notifier.notifyReadyAsync(() -> 1234, (v, e) -> {
			latch.countDown();
			throw exception;
		});
		latch.await();
		closeExecutorToNotify();
		assertEquals(exception, exceptionInHandler);
	}

	private void closeExecutorToNotify() throws InterruptedException {
		executorToNotify.shutdown();
		executorToNotify.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
	}
}

@@ -1506,16 +1506,65 @@
 Handler;
+%0A%09private CountDownLatch exceptionInHandlerLatch;
 %0A%0A%09@Befo
@@ -1623,16 +1623,72 @@
 = null;%0A
+%09%09this.exceptionInHandlerLatch = new CountDownLatch(1);%0A
 %09%09this.w
@@ -1947,16 +1947,22 @@
 , e) -%3E 
+%7B%0A%09%09%09%09
 exceptio
@@ -1975,16 +1975,63 @@
 dler = e
+;%0A%09%09%09%09exceptionInHandlerLatch.countDown();%0A%09%09%09%7D
 );%0A%09%09%09re
@@ -3190,32 +3190,224 @@
 utorToNotify();%0A
+%09%09// The uncaught exception handler may fire after the executor has shutdown.%0A%09%09// We need to wait on the countdown latch here.%0A%09%09exceptionInHandlerLatch.await(10000L, TimeUnit.MILLISECONDS);%0A
 %09%09assertEquals(e

Fix unstable ExecutorNotifierTest#testExceptionInHandler (#12676)