/*
 * 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.beam.runners.dataflow.worker.fn;

import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;

import java.net.InetAddress;
import java.net.ServerSocket;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.apache.beam.model.fnexecution.v1.BeamFnApi;
import org.apache.beam.model.fnexecution.v1.BeamFnControlGrpc;
import org.apache.beam.model.pipeline.v1.Endpoints;
import org.apache.beam.runners.dataflow.worker.fn.stream.ServerStreamObserverFactory;
import org.apache.beam.runners.fnexecution.GrpcContextHeaderAccessorProvider;
import org.apache.beam.runners.fnexecution.ServerFactory;
import org.apache.beam.runners.fnexecution.control.FnApiControlClient;
import org.apache.beam.sdk.options.PipelineOptions;
import org.apache.beam.sdk.options.PipelineOptionsFactory;
import org.apache.beam.vendor.grpc.v1p13p1.io.grpc.ManagedChannelBuilder;
import org.apache.beam.vendor.grpc.v1p13p1.io.grpc.Server;
import org.apache.beam.vendor.grpc.v1p13p1.io.grpc.stub.StreamObserver;
import org.apache.beam.vendor.guava.v20_0.com.google.common.collect.ImmutableList;
import org.apache.beam.vendor.guava.v20_0.com.google.common.net.HostAndPort;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

/** Tests for {@link BeamFnControlService}. */
@RunWith(JUnit4.class)
public class BeamFnControlServiceTest {
  @Mock private StreamObserver<BeamFnApi.InstructionRequest> requestObserver;
  @Mock private StreamObserver<BeamFnApi.InstructionRequest> anotherRequestObserver;

  @Before
  public void setUp() {
    MockitoAnnotations.initMocks(this);
  }

  private Endpoints.ApiServiceDescriptor findOpenPort() throws Exception {
    InetAddress address = InetAddress.getLoopbackAddress();
    try (ServerSocket socket = new ServerSocket(0, -1, address)) {
      return Endpoints.ApiServiceDescriptor.newBuilder()
          .setUrl(HostAndPort.fromParts(address.getHostAddress(), socket.getLocalPort()).toString())
          .build();
    }
  }

  @Test(timeout = 2000)
  public void testClientConnecting() throws Exception {
    CountDownLatch requestCompleted = new CountDownLatch(1);
    doAnswer(
            invocation -> {
              requestCompleted.countDown();
              return null;
            })
        .when(requestObserver)
        .onCompleted();

    PipelineOptions options = PipelineOptionsFactory.create();
    Endpoints.ApiServiceDescriptor descriptor = findOpenPort();
    BeamFnControlService service =
        new BeamFnControlService(
            descriptor,
            ServerStreamObserverFactory.fromOptions(options)::from,
            GrpcContextHeaderAccessorProvider.getHeaderAccessor());
    Server server = ServerFactory.createDefault().create(ImmutableList.of(service), descriptor);
    String url = service.getApiServiceDescriptor().getUrl();
    BeamFnControlGrpc.BeamFnControlStub clientStub =
        BeamFnControlGrpc.newStub(ManagedChannelBuilder.forTarget(url).usePlaintext(true).build());

    // Connect from the client.
    clientStub.control(requestObserver);
    try (FnApiControlClient client = service.get()) {
      assertNotNull(client);
    }
    server.shutdown();
    server.awaitTermination(1, TimeUnit.SECONDS);
    server.shutdownNow();

    requestCompleted.await(); // Wait until request streams have been closed.

    verify(requestObserver).onCompleted();
    verifyNoMoreInteractions(requestObserver);
  }

  @Test(timeout = 2000)
  public void testMultipleClientsConnecting() throws Exception {
    CountDownLatch requestCompleted = new CountDownLatch(2);
    doAnswer(
            invocation -> {
              requestCompleted.countDown();
              return null;
            })
        .when(requestObserver)
        .onCompleted();
    doAnswer(
            invocation -> {
              requestCompleted.countDown();
              return null;
            })
        .when(anotherRequestObserver)
        .onCompleted();

    PipelineOptions options = PipelineOptionsFactory.create();
    Endpoints.ApiServiceDescriptor descriptor = findOpenPort();
    BeamFnControlService service =
        new BeamFnControlService(
            descriptor,
            ServerStreamObserverFactory.fromOptions(options)::from,
            GrpcContextHeaderAccessorProvider.getHeaderAccessor());
    Server server = ServerFactory.createDefault().create(ImmutableList.of(service), descriptor);

    String url = service.getApiServiceDescriptor().getUrl();
    BeamFnControlGrpc.BeamFnControlStub clientStub =
        BeamFnControlGrpc.newStub(ManagedChannelBuilder.forTarget(url).usePlaintext(true).build());
    BeamFnControlGrpc.BeamFnControlStub anotherClientStub =
        BeamFnControlGrpc.newStub(ManagedChannelBuilder.forTarget(url).usePlaintext(true).build());

    // Connect from the client.
    clientStub.control(requestObserver);

    // Connect again from the client.
    anotherClientStub.control(anotherRequestObserver);

    try (FnApiControlClient client = service.get()) {
      assertNotNull(client);
      try (FnApiControlClient anotherClient = service.get()) {
        assertNotNull(anotherClient);
      }
    }

    server.shutdown();
    server.awaitTermination(1, TimeUnit.SECONDS);
    server.shutdownNow();

    requestCompleted.await(); // Wait until request streams have been closed.

    verify(requestObserver).onCompleted();
    verifyNoMoreInteractions(requestObserver);
    verify(anotherRequestObserver).onCompleted();
    verifyNoMoreInteractions(anotherRequestObserver);
  }
}

@@ -902,52 +902,8 @@
 ll;%0A
-import static org.mockito.Mockito.doAnswer;%0A
 impo
@@ -940,16 +940,16 @@
 verify;%0A
+
 import s
@@ -1064,52 +1064,8 @@
 et;%0A
-import java.util.concurrent.CountDownLatch;%0A
 impo
@@ -2967,32 +2967,16 @@
 %0A  @Test
-(timeout = 2000)
 %0A  publi
@@ -3028,253 +3028,8 @@
 n %7B%0A
-    CountDownLatch requestCompleted = new CountDownLatch(1);%0A    doAnswer(%0A            invocation -%3E %7B%0A              requestCompleted.countDown();%0A              return null;%0A            %7D)%0A        .when(requestObserver)%0A        .onCompleted();%0A%0A
     
@@ -3957,85 +3957,64 @@
 ();%0A
-%0A
     
-requestCompleted.await(); // Wait until request streams have been closed
+Thread.sleep(1000); // Wait for stub to close stream
 .%0A%0A 
@@ -4118,24 +4118,8 @@
 Test
-(timeout = 2000)
 %0A  p
@@ -4184,443 +4184,8 @@
 n %7B%0A
-    CountDownLatch requestCompleted = new CountDownLatch(2);%0A    doAnswer(%0A            invocation -%3E %7B%0A              requestCompleted.countDown();%0A              return null;%0A            %7D)%0A        .when(requestObserver)%0A        .onCompleted();%0A    doAnswer(%0A            invocation -%3E %7B%0A              requestCompleted.countDown();%0A              return null;%0A            %7D)%0A        .when(anotherRequestObserver)%0A        .onCompleted();%0A%0A
     
@@ -5479,85 +5479,64 @@
 ();%0A
-%0A
     
-requestCompleted.await(); // Wait until request streams have been closed
+Thread.sleep(1000); // Wait for stub to close stream
 .%0A%0A 

Revert "[BEAM-5709] Changing sleeps to CountdownLatch in test"