001/*
002 * Copyright (C) 2008 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.testing;
018
019import static com.google.common.base.Preconditions.checkNotNull;
020
021import com.google.common.annotations.GwtCompatible;
022import com.google.common.annotations.VisibleForTesting;
023import com.google.common.collect.Lists;
024import com.google.errorprone.annotations.concurrent.GuardedBy;
025import java.util.ArrayList;
026import java.util.LinkedList;
027import java.util.List;
028import java.util.logging.Level;
029import java.util.logging.Logger;
030import org.jspecify.annotations.NullMarked;
031
032/**
033 * A {@code TearDownStack} contains a stack of {@link TearDown} instances.
034 *
035 * <p>This class is thread-safe.
036 *
037 * @author Kevin Bourrillion
038 * @since 10.0
039 */
040@GwtCompatible
041@NullMarked
042public class TearDownStack implements TearDownAccepter {
043  private static final Logger logger = Logger.getLogger(TearDownStack.class.getName());
044
045  @VisibleForTesting final Object lock = new Object();
046
047  @GuardedBy("lock")
048  final LinkedList<TearDown> stack = new LinkedList<>();
049
050  private final boolean suppressThrows;
051
052  public TearDownStack() {
053    this.suppressThrows = false;
054  }
055
056  public TearDownStack(boolean suppressThrows) {
057    this.suppressThrows = suppressThrows;
058  }
059
060  @Override
061  public final void addTearDown(TearDown tearDown) {
062    synchronized (lock) {
063      stack.addFirst(checkNotNull(tearDown));
064    }
065  }
066
067  /** Causes teardown to execute. */
068  public final void runTearDown() {
069    List<Throwable> exceptions = new ArrayList<>();
070    List<TearDown> stackCopy;
071    synchronized (lock) {
072      stackCopy = Lists.newArrayList(stack);
073      stack.clear();
074    }
075    for (TearDown tearDown : stackCopy) {
076      try {
077        tearDown.tearDown();
078      } catch (Throwable t) {
079        if (suppressThrows) {
080          logger.log(Level.INFO, "exception thrown during tearDown", t);
081        } else {
082          exceptions.add(t);
083        }
084      }
085    }
086    if (!suppressThrows && (exceptions.size() > 0)) {
087      throw ClusterException.create(exceptions);
088    }
089  }
090}