<commit_before>/*
 * Copyright 2016 Google Inc. All Rights Reserved.
 *
 * Licensed 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 com.google.cloud.pubsub;

import static org.junit.Assert.assertEquals;

import com.google.cloud.pubsub.FakeSubscriberServiceImpl.ModifyAckDeadline;
import com.google.cloud.pubsub.Subscriber.Builder;
import com.google.cloud.pubsub.Subscriber.MessageReceiver;
import com.google.cloud.pubsub.Subscriber.MessageReceiver.AckReply;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.Service.State;
import com.google.common.util.concurrent.SettableFuture;
import com.google.pubsub.v1.PubsubMessage;
import com.google.pubsub.v1.PullResponse;
import com.google.pubsub.v1.ReceivedMessage;
import com.google.pubsub.v1.StreamingPullResponse;
import io.grpc.Status;
import io.grpc.StatusException;
import io.grpc.StatusRuntimeException;
import io.grpc.inprocess.InProcessChannelBuilder;
import io.grpc.inprocess.InProcessServerBuilder;
import io.grpc.internal.ServerImpl;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Deque;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import org.joda.time.Duration;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

/** Tests for {@link SubscriberImpl}. */
@RunWith(Parameterized.class)
public class SubscriberImplTest {

  private static final String TEST_SUBSCRIPTION =
      "projects/test-project/subscriptions/test-subscription";

  private static final PubsubMessage TEST_MESSAGE =
      PubsubMessage.newBuilder().setMessageId("1").build();

  @Parameters
  public static Collection<Object[]> data() {
    return Arrays.asList(new Object[][] {{true}, {false}});
  }

  private final boolean isStreamingTest;

  private InProcessChannelBuilder testChannelBuilder;
  private FakeScheduledExecutorService fakeExecutor;
  private FakeSubscriberServiceImpl fakeSubscriberServiceImpl;
  private ServerImpl testServer;

  private FakeCredentials testCredentials;
  private TestReceiver testReceiver;

  static class TestReceiver implements MessageReceiver {
    private final Deque<SettableFuture<AckReply>> outstandingMessageReplies =
        new ConcurrentLinkedDeque<>();
    private AckReply ackReply = AckReply.ACK;
    private Optional<CountDownLatch> messageCountLatch = Optional.absent();
    private Optional<Throwable> error = Optional.absent();
    private boolean explicitAckReplies;

    void setReply(AckReply ackReply) {
      this.ackReply = ackReply;
    }

    void setErrorReply(Throwable error) {
      this.error = Optional.of(error);
    }

    void setExplicitAck(boolean explicitAckReplies) {
      this.explicitAckReplies = explicitAckReplies;
    }

    void setExpectedMessages(int expected) {
      this.messageCountLatch = Optional.of(new CountDownLatch(expected));
    }

    void waitForExpectedMessages() throws InterruptedException {
      if (messageCountLatch.isPresent()) {
        messageCountLatch.get().await();
      }
    }

    @Override
    public ListenableFuture<AckReply> receiveMessage(PubsubMessage message) {
      if (messageCountLatch.isPresent()) {
        messageCountLatch.get().countDown();
      }
      SettableFuture<AckReply> reply = SettableFuture.create();

      if (explicitAckReplies) {
        outstandingMessageReplies.add(reply);
      } else {
        if (error.isPresent()) {
          reply.setException(error.get());
        } else {
          reply.set(ackReply);
        }
      }

      return reply;
    }

    public void replyNextOutstandingMessage() {
      Preconditions.checkState(explicitAckReplies);

      SettableFuture<AckReply> reply = outstandingMessageReplies.poll();
      if (error.isPresent()) {
        reply.setException(error.get());
      } else {
        reply.set(ackReply);
      }
    }

    public void replyAllOutstandingMessage() {
      Preconditions.checkState(explicitAckReplies);

      while (!outstandingMessageReplies.isEmpty()) {
        replyNextOutstandingMessage();
      }
    }
  }

  public SubscriberImplTest(boolean streamingTest) {
    this.isStreamingTest = streamingTest;
  }

  @Rule public TestName testName = new TestName();

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

    InProcessServerBuilder serverBuilder = InProcessServerBuilder.forName(testName.getMethodName());
    fakeSubscriberServiceImpl = Mockito.spy(new FakeSubscriberServiceImpl());
    fakeExecutor = new FakeScheduledExecutorService();
    testChannelBuilder = InProcessChannelBuilder.forName(testName.getMethodName());
    serverBuilder.addService(fakeSubscriberServiceImpl);
    testServer = serverBuilder.build();
    testServer.start();

    testReceiver = new TestReceiver();
    testCredentials = new FakeCredentials();
  }

  @After
  public void tearDown() throws Exception {
    testServer.shutdownNow().awaitTermination();
    fakeSubscriberServiceImpl.reset();
  }<commit_msg>Added a timeout while waiting for the fake server to terminate<commit_after>/*
 * Copyright 2016 Google Inc. All Rights Reserved.
 *
 * Licensed 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 com.google.cloud.pubsub;

import static org.junit.Assert.assertEquals;

import com.google.cloud.pubsub.FakeSubscriberServiceImpl.ModifyAckDeadline;
import com.google.cloud.pubsub.Subscriber.Builder;
import com.google.cloud.pubsub.Subscriber.MessageReceiver;
import com.google.cloud.pubsub.Subscriber.MessageReceiver.AckReply;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.Service.State;
import com.google.common.util.concurrent.SettableFuture;
import com.google.pubsub.v1.PubsubMessage;
import com.google.pubsub.v1.PullResponse;
import com.google.pubsub.v1.ReceivedMessage;
import com.google.pubsub.v1.StreamingPullResponse;
import io.grpc.Status;
import io.grpc.StatusException;
import io.grpc.StatusRuntimeException;
import io.grpc.inprocess.InProcessChannelBuilder;
import io.grpc.inprocess.InProcessServerBuilder;
import io.grpc.internal.ServerImpl;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Deque;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.joda.time.Duration;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

/** Tests for {@link SubscriberImpl}. */
@RunWith(Parameterized.class)
public class SubscriberImplTest {

  private static final String TEST_SUBSCRIPTION =
      "projects/test-project/subscriptions/test-subscription";

  private static final PubsubMessage TEST_MESSAGE =
      PubsubMessage.newBuilder().setMessageId("1").build();

  @Parameters
  public static Collection<Object[]> data() {
    return Arrays.asList(new Object[][] {{true}, {false}});
  }

  private final boolean isStreamingTest;

  private InProcessChannelBuilder testChannelBuilder;
  private FakeScheduledExecutorService fakeExecutor;
  private FakeSubscriberServiceImpl fakeSubscriberServiceImpl;
  private ServerImpl testServer;

  private FakeCredentials testCredentials;
  private TestReceiver testReceiver;

  static class TestReceiver implements MessageReceiver {
    private final Deque<SettableFuture<AckReply>> outstandingMessageReplies =
        new ConcurrentLinkedDeque<>();
    private AckReply ackReply = AckReply.ACK;
    private Optional<CountDownLatch> messageCountLatch = Optional.absent();
    private Optional<Throwable> error = Optional.absent();
    private boolean explicitAckReplies;

    void setReply(AckReply ackReply) {
      this.ackReply = ackReply;
    }

    void setErrorReply(Throwable error) {
      this.error = Optional.of(error);
    }

    void setExplicitAck(boolean explicitAckReplies) {
      this.explicitAckReplies = explicitAckReplies;
    }

    void setExpectedMessages(int expected) {
      this.messageCountLatch = Optional.of(new CountDownLatch(expected));
    }

    void waitForExpectedMessages() throws InterruptedException {
      if (messageCountLatch.isPresent()) {
        messageCountLatch.get().await();
      }
    }

    @Override
    public ListenableFuture<AckReply> receiveMessage(PubsubMessage message) {
      if (messageCountLatch.isPresent()) {
        messageCountLatch.get().countDown();
      }
      SettableFuture<AckReply> reply = SettableFuture.create();

      if (explicitAckReplies) {
        outstandingMessageReplies.add(reply);
      } else {
        if (error.isPresent()) {
          reply.setException(error.get());
        } else {
          reply.set(ackReply);
        }
      }

      return reply;
    }

    public void replyNextOutstandingMessage() {
      Preconditions.checkState(explicitAckReplies);

      SettableFuture<AckReply> reply = outstandingMessageReplies.poll();
      if (error.isPresent()) {
        reply.setException(error.get());
      } else {
        reply.set(ackReply);
      }
    }

    public void replyAllOutstandingMessage() {
      Preconditions.checkState(explicitAckReplies);

      while (!outstandingMessageReplies.isEmpty()) {
        replyNextOutstandingMessage();
      }
    }
  }

  public SubscriberImplTest(boolean streamingTest) {
    this.isStreamingTest = streamingTest;
  }

  @Rule public TestName testName = new TestName();

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

    InProcessServerBuilder serverBuilder = InProcessServerBuilder.forName(testName.getMethodName());
    fakeSubscriberServiceImpl = Mockito.spy(new FakeSubscriberServiceImpl());
    fakeExecutor = new FakeScheduledExecutorService();
    testChannelBuilder = InProcessChannelBuilder.forName(testName.getMethodName());
    serverBuilder.addService(fakeSubscriberServiceImpl);
    testServer = serverBuilder.build();
    testServer.start();

    testReceiver = new TestReceiver();
    testCredentials = new FakeCredentials();
  }

  @After
  public void tearDown() throws Exception {
    testServer.shutdownNow().awaitTermination(10, TimeUnit.SECONDS);
    fakeSubscriberServiceImpl.reset();
  }
{'fraction_non_alphanumeric': 0.09456690977628451, 'fraction_numerical': 0.002294517741538966, 'mean_word_length': 4.352631578947369, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5832286', 'n_tokens_mistral': 3459, 'n_tokens_neox': 3433, 'n_words': 743}