/*
 * 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.geode.internal.cache;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import java.util.concurrent.TimeUnit;

import org.awaitility.Awaitility;
import org.junit.Before;
import org.junit.Test;

import org.apache.geode.CancelCriterion;

public class AfterCompletionTest {
  private AfterCompletion afterCompletion;
  private CancelCriterion cancelCriterion;
  private TXState txState;
  private Thread doOpThread;

  @Before
  public void setup() {
    afterCompletion = new AfterCompletion();
    cancelCriterion = mock(CancelCriterion.class);
    txState = mock(TXState.class);
  }

  @Test
  public void isStartedReturnsFalseIfNotExecuted() {
    assertThat(afterCompletion.isStarted()).isFalse();
  }

  @Test
  public void isStartedReturnsTrueIfExecuted() {
    startDoOp();

    afterCompletion.executeCommit();

    verifyDoOpFinished();
    assertThat(afterCompletion.isStarted()).isTrue();
  }

  @Test
  public void executeCallsDoAfterCompletionCommit() {
    startDoOp();

    afterCompletion.executeCommit();
    verifyDoOpFinished();
    verify(txState, times(1)).doAfterCompletionCommit();
  }

  @Test
  public void executeThrowsDoAfterCompletionCommitThrows() {
    startDoOp();
    doThrow(new RuntimeException()).when(txState).doAfterCompletionCommit();

    assertThatThrownBy(() -> afterCompletion.executeCommit())
        .isInstanceOf(RuntimeException.class);

    verifyDoOpFinished();
  }

  @Test
  public void executeCallsDoAfterCompletionRollback() {
    startDoOp();

    afterCompletion.executeRollback();
    verifyDoOpFinished();
    verify(txState, times(1)).doAfterCompletionRollback();
  }

  @Test
  public void executeThrowsDoAfterCompletionRollbackThrows() {
    startDoOp();
    doThrow(new RuntimeException()).when(txState).doAfterCompletionRollback();

    assertThatThrownBy(() -> afterCompletion.executeRollback())
        .isInstanceOf(RuntimeException.class);

    verifyDoOpFinished();
  }

  @Test
  public void doOpInvokesDoCleanupIfCancelCriteriaThrows() {
    doThrow(new RuntimeException()).when(cancelCriterion).checkCancelInProgress(null);

    afterCompletion.doOp(txState, cancelCriterion);

    verify(txState).doCleanup();
  }

  @Test
  public void cancelCallsDoCleanup() {
    startDoOp();

    afterCompletion.cancel();
    verifyDoOpFinished();
    verify(txState, times(1)).doCleanup();
  }

  private void startDoOp() {
    doOpThread = new Thread(() -> afterCompletion.doOp(txState, cancelCriterion));
    doOpThread.start();
    Awaitility.await().atMost(60, TimeUnit.SECONDS)
        .untilAsserted(() -> verify(cancelCriterion, times(1)).checkCancelInProgress(null));

  }

  private void verifyDoOpFinished() {
    Awaitility.await().atMost(60, TimeUnit.SECONDS).until(() -> !doOpThread.isAlive());
  }

}

@@ -949,16 +949,63 @@
 rownBy;%0A
+import static org.mockito.Mockito.atLeastOnce;%0A
 import s
@@ -1083,49 +1083,8 @@
 ck;%0A
-import static org.mockito.Mockito.times;%0A
 impo
@@ -2110,34 +2110,24 @@
 rify(txState
-, times(1)
 ).doAfterCom
@@ -2614,34 +2614,24 @@
 rify(txState
-, times(1)
 ).doAfterCom
@@ -3352,26 +3352,16 @@
 (txState
-, times(1)
 ).doClea
@@ -3530,34 +3530,33 @@
 .await().atMost(
-60
+5
 , TimeUnit.SECON
@@ -3550,22 +3550,22 @@
 imeUnit.
-SECOND
+MINUTE
 S)%0A     
@@ -3616,15 +3616,20 @@
 on, 
-times(1
+atLeastOnce(
 )).c
@@ -3735,10 +3735,9 @@
 ost(
-60
+5
 , Ti
@@ -3747,14 +3747,14 @@
 nit.
-SECOND
+MINUTE
 S).u

fix timing issues in AfterCompletionTest (#2586)