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

com.oracle.bedrock.junit.CoherenceClusterExtension Maven / Gradle / Ivy

The newest version!
package com.oracle.bedrock.junit;

import com.oracle.bedrock.Option;
import com.oracle.bedrock.OptionsByType;
import com.oracle.bedrock.runtime.Application;
import com.oracle.bedrock.runtime.AssemblyBuilder;
import com.oracle.bedrock.runtime.LocalPlatform;
import com.oracle.bedrock.runtime.Platform;
import com.oracle.bedrock.runtime.coherence.CoherenceCluster;
import com.oracle.bedrock.runtime.coherence.CoherenceClusterBuilder;
import com.oracle.bedrock.runtime.coherence.CoherenceClusterMember;
import com.oracle.bedrock.runtime.coherence.options.LocalStorage;
import com.oracle.bedrock.runtime.coherence.options.RoleName;
import com.oracle.bedrock.runtime.java.options.Headless;
import com.oracle.bedrock.runtime.java.options.HeapSize;
import com.oracle.bedrock.runtime.java.options.HotSpot;
import com.oracle.bedrock.runtime.options.Console;
import com.oracle.bedrock.runtime.options.PlatformPredicate;
import com.oracle.bedrock.testsupport.junit.AbstractBaseAssembly;
import com.tangosol.net.CacheFactory;
import com.tangosol.net.ConfigurableCacheFactory;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;

import java.util.HashMap;
import java.util.Properties;

/**
 * A Coherence-based {@link AbstractBaseAssembly} that creates {@link CoherenceCluster}s for use with JUnit 5 tests.
 * 

* Copyright (c) 2021. All Rights Reserved. Oracle Corporation.
* Oracle is a registered trademark of Oracle Corporation and/or its affiliates. * * @author Jonathan Knight */ public class CoherenceClusterExtension extends AbstractBaseAssembly implements BeforeAllCallback, AfterAllCallback { /** * The system properties prior to the creation of the {@link CoherenceClusterResource }. */ private Properties systemProperties; /** * The {@link ConfigurableCacheFactory} sessions that have been locally created against the * {@link CoherenceCluster} using this {@link CoherenceClusterResource}. */ private final HashMap sessions; /** * Constructs a {@link CoherenceClusterExtension}. */ public CoherenceClusterExtension() { super(); // by default we have no sessions this.sessions = new HashMap<>(); // establish default java process options this.commonOptionsByType.add(Headless.enabled()); this.commonOptionsByType.add(HotSpot.Mode.SERVER); this.commonOptionsByType.add(HeapSize.of(256, HeapSize.Units.MB, 1024, HeapSize.Units.MB)); // establish default bedrock options this.commonOptionsByType.add(Console.system()); } @Override protected CoherenceClusterBuilder createBuilder() { return new CoherenceClusterBuilder(); } @Override public void beforeAll(ExtensionContext extensionContext) throws Exception { if (launchDefinitions.isEmpty()) { throw new IllegalStateException("CoherenceClusterResource fails to define members to include when launching"); } // take a snapshot of the current system properties so we can restore them when cleaning up the resource this.systemProperties = com.oracle.bedrock.util.SystemProperties.createSnapshot(); // let's ensure that we don't have a local cluster member CacheFactory.setCacheFactoryBuilder(null); CacheFactory.shutdown(); start(); } @Override public void afterAll(ExtensionContext extensionContext) throws Exception { // clean up the sessions synchronized (sessions) { for (ConfigurableCacheFactory session : sessions.values()) { CacheFactory.getCacheFactoryBuilder().release(session); } } // let's ensure that we don't have a local cluster member CacheFactory.setCacheFactoryBuilder(null); CacheFactory.shutdown(); close(); // restore the system properties com.oracle.bedrock.util.SystemProperties.replaceWith(systemProperties); } /** * Obtains the {@link CoherenceCluster} created by the {@link CoherenceClusterResource}. * * @return the {@link CoherenceCluster} */ public CoherenceCluster getCluster() { return assembly; } /** * Defines the necessary information for launching one or more {@link CoherenceClusterMember}s * as part of the {@link CoherenceCluster} when the {@link CoherenceClusterResource} is established. *

* The {@link Platform} on which the {@link CoherenceClusterMember}s are launched is based on the * {@link PlatformPredicate} specified as an {@link Option}. By default this is {@link PlatformPredicate#any()}. *

* Multiple calls to this method are permitted, allowing a {@link CoherenceCluster} to be created containing * multiple different types of {@link CoherenceCluster}s. *

* This is equivalent to calling {@link #include(int, Class, Option...)} using a {@link CoherenceClusterMember} * class as the {@link Application} class. * * @param count the number of instances of the {@link CoherenceCluster} that should be launched for * the {@link CoherenceCluster} * @param options the {@link Option}s to use for launching the {@link CoherenceCluster}s * * @return the {@link CoherenceClusterResource} to permit fluent-style method calls */ public CoherenceClusterExtension include(int count, Option... options) { return include(count, CoherenceClusterMember.class, options); } /** * Obtains a session, represented as a {@link ConfigurableCacheFactory}, against the {@link CoherenceCluster}. *

* Only a single session may be created by a {@link CoherenceClusterResource} against the {@link CoherenceCluster}. *

* Attempts to request a session multiple times with the same {@link SessionBuilder} will return the same session. * * @param builder the builder for the specific type of session * * @return a {@link ConfigurableCacheFactory} representing the Coherence Session. * * @throws IllegalStateException when an attempt to request sessions for * different {@link SessionBuilder}s is made */ public synchronized ConfigurableCacheFactory createSession(SessionBuilder builder) { // restore the system properties (as the session needs to start cleanly) com.oracle.bedrock.util.SystemProperties.replaceWith(systemProperties); ConfigurableCacheFactory session = sessions.get(builder); if (session == null) { OptionsByType optionsByType = OptionsByType.of(commonOptionsByType); optionsByType.add(RoleName.of("client")); optionsByType.add(LocalStorage.disabled()); session = builder.build(LocalPlatform.get(), getCluster(), optionsByType); sessions.put(builder, session); } return session; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy