/*
 * Copyright 2015 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.samples;

// [START DeferredTaskTest]

import static org.junit.Assert.assertTrue;

import com.google.appengine.api.taskqueue.DeferredTask;
import com.google.appengine.api.taskqueue.QueueFactory;
import com.google.appengine.api.taskqueue.TaskOptions;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import com.google.appengine.tools.development.testing.LocalTaskQueueTestConfig;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class DeferredTaskTest extends BaseTestConfiguration {

  // Unlike CountDownLatch, TaskCountDownlatch lets us reset.
  private final LocalTaskQueueTestConfig.TaskCountDownLatch latch =
      new LocalTaskQueueTestConfig.TaskCountDownLatch(1);

  private final LocalServiceTestHelper helper =
      new LocalServiceTestHelper(new LocalTaskQueueTestConfig()
          .setDisableAutoTaskExecution(false)
          .setCallbackClass(LocalTaskQueueTestConfig.DeferredTaskCallback.class)
          .setTaskExecutionLatch(latch));

  private static class MyTask implements DeferredTask {
    private static boolean taskRan = false;

    @Override
    public void run() {
      taskRan = true;
    }
  }

  @Before
  public void setUp() {
    helper.setUp();
  }

  @After
  public void tearDown() {
    MyTask.taskRan = false;
    helper.tearDown();
    latch.reset();
  }

  @Test
  public void testTaskGetsRun() throws InterruptedException {
    QueueFactory.getDefaultQueue().add(
        TaskOptions.Builder.withPayload(new MyTask()));
    assertTrue(latch.await(5, TimeUnit.SECONDS));
    assertTrue(MyTask.taskRan);
  }
}
// [END DeferredTaskTest]

@@ -1260,32 +1260,39 @@
 eset.%0A  private 
+static 
 final LocalTaskQ
@@ -1333,16 +1333,16 @@
 latch =%0A
-
       ne
@@ -1549,16 +1549,46 @@
 n(false)
+ // Enable auto task execution
 %0A       
@@ -1700,24 +1700,233 @@
 h(latch));%0A%0A
+  private static synchronized boolean requestAwait() throws InterruptedException %7B%0A    return latch.await(5, TimeUnit.SECONDS);%0A  %7D%0A%0A  private static synchronized void requestReset() %7B%0A    latch.reset();%0A  %7D%0A%0A
   private st
@@ -1988,16 +1988,25 @@
  static 
+volatile 
 boolean 
@@ -2217,16 +2217,36 @@
  false;%0A
+    requestReset();%0A
     help
@@ -2260,35 +2260,16 @@
 Down();%0A
-    latch.reset();%0A
   %7D%0A%0A  @
@@ -2450,39 +2450,21 @@
 rue(
-latch.await(5, TimeUnit.SECONDS
+requestAwait(
 ));%0A

debug test by extending timeout (#5649)