|
| 1 | +/* |
| 2 | + * Copyright 2024 Datastrato Pvt Ltd. |
| 3 | + * This software is licensed under the Apache License version 2. |
| 4 | + */ |
| 5 | +package com.datastrato.gravitino.integration.test.util; |
| 6 | + |
| 7 | +import com.datastrato.gravitino.integration.test.container.ContainerSuite; |
| 8 | +import org.junit.jupiter.api.extension.BeforeAllCallback; |
| 9 | +import org.junit.jupiter.api.extension.ExtensionContext; |
| 10 | +import org.slf4j.Logger; |
| 11 | +import org.slf4j.LoggerFactory; |
| 12 | + |
| 13 | +/** |
| 14 | + * This is an extension for juint 5, which aims to perform certain operations (such as resource |
| 15 | + * recycling, etc.) after all test executions are completed (regardless of success or failure). You |
| 16 | + * can Refer to {@link AbstractIT} for more information. |
| 17 | + */ |
| 18 | +public class CloseContainerExtension implements BeforeAllCallback { |
| 19 | + @Override |
| 20 | + public void beforeAll(ExtensionContext extensionContext) { |
| 21 | + synchronized (CloseContainerExtension.class) { |
| 22 | + extensionContext |
| 23 | + .getRoot() |
| 24 | + .getStore(ExtensionContext.Namespace.GLOBAL) |
| 25 | + .getOrComputeIfAbsent(CloseableContainer.class); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + private static class CloseableContainer implements ExtensionContext.Store.CloseableResource { |
| 30 | + private static final Logger LOGGER = LoggerFactory.getLogger(CloseableContainer.class); |
| 31 | + private static final ContainerSuite CONTAINER_SUITE = ContainerSuite.getInstance(); |
| 32 | + |
| 33 | + @Override |
| 34 | + public void close() { |
| 35 | + try { |
| 36 | + CONTAINER_SUITE.close(); |
| 37 | + LOGGER.info("Containers were closed successfully"); |
| 38 | + } catch (Exception e) { |
| 39 | + LOGGER.warn("Containers were not closed as expected", e); |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | +} |
0 commit comments