All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.elasticsearch.test.CloseableTestClusterWrapper Maven / Gradle / Ivy

The newest version!
/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License
 * 2.0 and the Server Side Public License, v 1; you may not use this file except
 * in compliance with, at your election, the Elastic License 2.0 or the Server
 * Side Public License, v 1.
 */

package org.elasticsearch.test;

import org.elasticsearch.common.collect.Iterators;

import java.io.Closeable;
import java.io.IOException;

/**
 * Adapter to make one or more {@link TestCluster} instances compatible with things like try-with-resources blocks and IOUtils.
 */
// NB it is deliberate that TestCluster does not implement AutoCloseable or Closeable, because if we do that then IDEs tell us that we
// should be using a try-with-resources block everywhere and that is almost never correct. The lifecycle of these clusters is managed by the
// test framework itself and should not be touched by most test code. This class provides adapters for the few cases where you do want to
// auto-close these things.
public record CloseableTestClusterWrapper(TestCluster testCluster) implements Closeable {
    @Override
    public void close() throws IOException {
        testCluster().close();
    }

    public static Iterable wrap(Iterable clusters) {
        return () -> Iterators.map(clusters.iterator(), CloseableTestClusterWrapper::new);
    }

    public static Iterable wrap(TestCluster... clusters) {
        return wrap(() -> Iterators.forArray(clusters));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy