<commit_before>// Copyright 2011 Google Inc.
//
// 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.appengine.tools.pipeline;

import com.google.appengine.tools.pipeline.JobInfo.State;
import com.google.appengine.tools.pipeline.JobSetting.StatusConsoleUrl;
import com.google.appengine.tools.pipeline.impl.PipelineManager;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * Misc tests including:
 *  Passing large values.
 *  Delay used in a slow job
 *  JobSetting inheritance
 *  PromisedValue (and filling it more than once)
 *  Cancel pipeline
 *
 * @author <EMAIL> (<NAME>)
 */
public class MiscPipelineTest extends PipelineTest {

  private static long[] largeValue;

  @Override
  public void setUp() throws Exception {
    super.setUp();
    largeValue = new long[2000000];
    Random random = new Random();
    for (int i = 0; i < largeValue.length; i++) {
      largeValue[i] = random.nextLong();
    }
  }

  @SuppressWarnings("serial")
  private static class FailedJob extends Job0<String> {<commit_msg>Add test for WaitFor being used by new Pipeline.<commit_after>// Copyright 2011 Google Inc.
//
// 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.appengine.tools.pipeline;

import com.google.appengine.tools.pipeline.JobInfo.State;
import com.google.appengine.tools.pipeline.JobSetting.StatusConsoleUrl;
import com.google.appengine.tools.pipeline.impl.PipelineManager;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * Misc tests including:
 *  Passing large values.
 *  Delay used in a slow job
 *  JobSetting inheritance
 *  PromisedValue (and filling it more than once)
 *  Cancel pipeline
 *  WaitFor passed to a new pipeline
 *  FutureValue passed to a new pipeline
 *
 * @author <EMAIL> (<NAME>)
 */
public class MiscPipelineTest extends PipelineTest {

  private static long[] largeValue;

  @Override
  public void setUp() throws Exception {
    super.setUp();
    largeValue = new long[2000000];
    Random random = new Random();
    for (int i = 0; i < largeValue.length; i++) {
      largeValue[i] = random.nextLong();
    }
  }


  @SuppressWarnings("serial")
  private static class CallerJob extends Job0<String> {

    static AtomicBoolean flag = new AtomicBoolean();

    @Override
    public Value<String> run() throws Exception {
      FutureValue<Void> child1 = futureCall(new ChildJob());
      FutureValue<String> child2 = futureCall(new StrJob<>(), immediate(Long.valueOf(123)));
      PipelineService service = PipelineServiceFactory.newPipelineService();
      String str1 = service.startNewPipeline(new EchoJob<>(), child2);
      String str2 = service.startNewPipeline(new CalledJob(), waitFor(child1));
      return immediate(str1 + "," + str2);
    }
  }

  @SuppressWarnings("serial")
  private static class EchoJob<T> extends Job1<T, T> {

    @Override
    public Value<T> run(T t) throws Exception {
      return immediate(t);
    }
  }

  @SuppressWarnings("serial")
  private static class ChildJob extends Job0<Void> {

    @Override
    public Value<Void> run() throws Exception {
      Thread.sleep(5000);
      CallerJob.flag.set(true);
      return null;
    }
  }

  @SuppressWarnings("serial")
  private static class CalledJob extends Job0<Boolean> {

    @Override
    public Value<Boolean> run() throws Exception {
      return immediate(CallerJob.flag.get());
    }
  }

  public void testWaitForUsedByNewPipeline() throws Exception {
    PipelineService service = PipelineServiceFactory.newPipelineService();
    String pipelineId = service.startNewPipeline(new CallerJob());
    JobInfo jobInfo = waitUntilJobComplete(pipelineId);
    assertEquals(JobInfo.State.COMPLETED_SUCCESSFULLY, jobInfo.getJobState());
    String[] calledPipelines = ((String) jobInfo.getOutput()).split(",");
    jobInfo = waitUntilJobComplete(calledPipelines[0]);
    assertEquals(JobInfo.State.COMPLETED_SUCCESSFULLY, jobInfo.getJobState());
    assertEquals("123", jobInfo.getOutput());
    jobInfo = waitUntilJobComplete(calledPipelines[1]);
    assertEquals(JobInfo.State.COMPLETED_SUCCESSFULLY, jobInfo.getJobState());
    assertTrue((Boolean) jobInfo.getOutput());
  }

  @SuppressWarnings("serial")
  private static class FailedJob extends Job0<String> {
{'fraction_non_alphanumeric': 0.1056140350877193, 'fraction_numerical': 0.010175438596491228, 'mean_word_length': 4.131413141314131, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 22, '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': '681792', 'n_tokens_mistral': 1635, 'n_tokens_neox': 1601, 'n_words': 489}