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

import static java.util.Collections.emptyMap;
import static java.util.Collections.emptySet;

import com.google.common.collect.LinkedListMultimap;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.apache.beam.sdk.options.PipelineOptionsFactory;
import org.apache.beam.sdk.test.ThreadLeakTracker;
import org.joda.time.Instant;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.junit.rules.TestRule;

/**
 * Validates some basic behavior for the ExecutorServiceParallelExecutor.
 */
public class ExecutorServiceParallelExecutorTest {

  @Rule
  public final TestRule threadTracker = new ThreadLeakTracker();

  @Rule
  public final TestName testName = new TestName();

  @Test
  public void ensureMetricsThreadDoesntLeak() {
    final DirectGraph graph = DirectGraph.create(
      emptyMap(), emptyMap(), LinkedListMultimap.create(),
      emptySet(), emptyMap());
    final ExecutorService executorService = Executors.newSingleThreadExecutor(
      new ThreadFactoryBuilder()
        .setDaemon(false)
        .setNameFormat("dontleak_" + getClass().getName() + "#" + testName.getMethodName())
        .build());

    // fake a metrics usage
    executorService.submit(() -> {});

    final EvaluationContext context = EvaluationContext.create(
      MockClock.fromInstant(Instant.now()),
      CloningBundleFactory.create(), graph, emptySet(), executorService);
    ExecutorServiceParallelExecutor
      .create(
        2, TransformEvaluatorRegistry.javaSdkNativeRegistry(context,
              PipelineOptionsFactory.create().as(DirectOptions.class)), emptyMap(),
              context,
              executorService)
      .stop();
  }
}

@@ -1153,93 +1153,461 @@
 eam.
-sdk.options.PipelineOptionsFactory;%0Aimport org.apache.beam.sdk.test.ThreadLeakTracker
+runners.core.metrics.MetricsPusherTest;%0Aimport org.apache.beam.sdk.io.GenerateSequence;%0Aimport org.apache.beam.sdk.metrics.Counter;%0Aimport org.apache.beam.sdk.metrics.Metrics;%0Aimport org.apache.beam.sdk.options.PipelineOptionsFactory;%0Aimport org.apache.beam.sdk.test.ThreadLeakTracker;%0Aimport org.apache.beam.sdk.testing.TestPipeline;%0Aimport org.apache.beam.sdk.transforms.DoFn;%0Aimport org.apache.beam.sdk.transforms.ParDo;%0Aimport org.joda.time.Duration
 ;%0Aim
@@ -1881,16 +1881,136 @@
 Test %7B%0A%0A
+  private static final long NUM_ELEMENTS = 1000L;%0A  @Rule%0A  public final TestPipeline pipeline = TestPipeline.create();%0A
   @Rule%0A
@@ -2074,17 +2074,16 @@
 cker();%0A
-%0A
   @Rule%0A
@@ -3105,11 +3105,698 @@
 p();%0A  %7D
+%0A%0A  @Test%0A  public void test() throws Exception %7B%0A    pipeline%0A        .apply(%0A            // Use maxReadTime to force unbounded mode.%0A            GenerateSequence.from(0).to(NUM_ELEMENTS).withMaxReadTime(Duration.standardDays(1)))%0A        .apply(ParDo.of(new CountingDoFn()));%0A    pipeline.run();%0A  %7D%0A%0A  private static class CountingDoFn extends DoFn%3CLong, Long%3E %7B%0A    private final Counter counter = Metrics.counter(MetricsPusherTest.class, %22counter%22);%0A%0A    @ProcessElement%0A    public void processElement(ProcessContext context) %7B%0A      try %7B%0A        counter.inc();%0A        context.output(context.element());%0A      %7D catch (Exception e) %7B%0A        e.printStackTrace();%0A      %7D%0A    %7D%0A  %7D
 %0A%7D%0A

Add a test with a real pipeline that has a DoFn with metrics and test that it does not leak.