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.ArrayDeque; 026import java.util.ArrayList; 027import java.util.Deque; 028import java.util.List; 029import java.util.logging.Level; 030import java.util.logging.Logger; 031import org.jspecify.annotations.NullMarked; 032 033/** 034 * A {@code TearDownStack} contains a stack of {@link TearDown} instances. 035 * 036 * <p>This class is thread-safe. 037 * 038 * @author Kevin Bourrillion 039 * @since 10.0 040 */ 041@GwtCompatible 042@NullMarked 043public class TearDownStack implements TearDownAccepter { 044 private static final Logger logger = Logger.getLogger(TearDownStack.class.getName()); 045 046 @VisibleForTesting final Object lock = new Object(); 047 048 @GuardedBy("lock") 049 final Deque<TearDown> stack = new ArrayDeque<>(); 050 051 private final boolean suppressThrows; 052 053 public TearDownStack() { 054 this.suppressThrows = false; 055 } 056 057 public TearDownStack(boolean suppressThrows) { 058 this.suppressThrows = suppressThrows; 059 } 060 061 @Override 062 public final void addTearDown(TearDown tearDown) { 063 synchronized (lock) { 064 stack.addFirst(checkNotNull(tearDown)); 065 } 066 } 067 068 /** Causes teardown to execute. */ 069 public final void runTearDown() { 070 List<Throwable> exceptions = new ArrayList<>(); 071 List<TearDown> stackCopy; 072 synchronized (lock) { 073 stackCopy = Lists.newArrayList(stack); 074 stack.clear(); 075 } 076 for (TearDown tearDown : stackCopy) { 077 try { 078 tearDown.tearDown(); 079 } catch (Throwable t) { 080 if (suppressThrows) { 081 logger.log(Level.INFO, "exception thrown during tearDown", t); 082 } else { 083 exceptions.add(t); 084 } 085 } 086 } 087 if (!suppressThrows && (exceptions.size() > 0)) { 088 throw ClusterException.create(exceptions); 089 } 090 } 091}