Path: blob/master/test/jdk/java/foreign/TestResourceScope.java
41144 views
/*1* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @modules java.base/jdk.internal.ref26* jdk.incubator.foreign/jdk.incubator.foreign27* @run testng/othervm TestResourceScope28*/2930import java.lang.ref.Cleaner;3132import jdk.incubator.foreign.ResourceScope;33import jdk.internal.ref.CleanerFactory;3435import org.testng.annotations.DataProvider;36import org.testng.annotations.Test;37import static org.testng.Assert.*;3839import java.util.ArrayList;40import java.util.List;41import java.util.concurrent.atomic.AtomicInteger;42import java.util.concurrent.atomic.AtomicReference;43import java.util.function.Supplier;44import java.util.stream.IntStream;4546public class TestResourceScope {4748final static int N_THREADS = 10000;4950@Test(dataProvider = "cleaners")51public void testConfined(Supplier<Cleaner> cleanerSupplier) {52AtomicInteger acc = new AtomicInteger();53Cleaner cleaner = cleanerSupplier.get();54ResourceScope scope = cleaner != null ?55ResourceScope.newConfinedScope(cleaner) :56ResourceScope.newConfinedScope();57for (int i = 0 ; i < N_THREADS ; i++) {58int delta = i;59scope.addCloseAction(() -> acc.addAndGet(delta));60}61assertEquals(acc.get(), 0);6263if (cleaner == null) {64scope.close();65assertEquals(acc.get(), IntStream.range(0, N_THREADS).sum());66} else {67scope = null;68int expected = IntStream.range(0, N_THREADS).sum();69while (acc.get() != expected) {70kickGC();71}72}73}7475@Test(dataProvider = "cleaners")76public void testSharedSingleThread(Supplier<Cleaner> cleanerSupplier) {77AtomicInteger acc = new AtomicInteger();78Cleaner cleaner = cleanerSupplier.get();79ResourceScope scope = cleaner != null ?80ResourceScope.newSharedScope(cleaner) :81ResourceScope.newSharedScope();82for (int i = 0 ; i < N_THREADS ; i++) {83int delta = i;84scope.addCloseAction(() -> acc.addAndGet(delta));85}86assertEquals(acc.get(), 0);8788if (cleaner == null) {89scope.close();90assertEquals(acc.get(), IntStream.range(0, N_THREADS).sum());91} else {92scope = null;93int expected = IntStream.range(0, N_THREADS).sum();94while (acc.get() != expected) {95kickGC();96}97}98}99100@Test(dataProvider = "cleaners")101public void testSharedMultiThread(Supplier<Cleaner> cleanerSupplier) {102AtomicInteger acc = new AtomicInteger();103Cleaner cleaner = cleanerSupplier.get();104List<Thread> threads = new ArrayList<>();105ResourceScope scope = cleaner != null ?106ResourceScope.newSharedScope(cleaner) :107ResourceScope.newSharedScope();108AtomicReference<ResourceScope> scopeRef = new AtomicReference<>(scope);109for (int i = 0 ; i < N_THREADS ; i++) {110int delta = i;111Thread thread = new Thread(() -> {112try {113scopeRef.get().addCloseAction(() -> {114acc.addAndGet(delta);115});116} catch (IllegalStateException ex) {117// already closed - we need to call cleanup manually118acc.addAndGet(delta);119}120});121threads.add(thread);122}123assertEquals(acc.get(), 0);124threads.forEach(Thread::start);125126// if no cleaner, close - not all segments might have been added to the scope!127// if cleaner, don't unset the scope - after all, the scope is kept alive by threads128if (cleaner == null) {129while (true) {130try {131scope.close();132break;133} catch (IllegalStateException ise) {134// scope is acquired (by add) - wait some more135}136}137}138139threads.forEach(t -> {140try {141t.join();142} catch (InterruptedException ex) {143fail();144}145});146147if (cleaner == null) {148assertEquals(acc.get(), IntStream.range(0, N_THREADS).sum());149} else {150scope = null;151scopeRef.set(null);152int expected = IntStream.range(0, N_THREADS).sum();153while (acc.get() != expected) {154kickGC();155}156}157}158159@Test(dataProvider = "cleaners")160public void testLockSingleThread(Supplier<Cleaner> cleanerSupplier) {161Cleaner cleaner = cleanerSupplier.get();162ResourceScope scope = cleaner != null ?163ResourceScope.newConfinedScope(cleaner) :164ResourceScope.newConfinedScope();165List<ResourceScope.Handle> handles = new ArrayList<>();166for (int i = 0 ; i < N_THREADS ; i++) {167handles.add(scope.acquire());168}169170while (true) {171try {172scope.close();173assertEquals(handles.size(), 0);174break;175} catch (IllegalStateException ex) {176assertTrue(handles.size() > 0);177ResourceScope.Handle handle = handles.remove(0);178scope.release(handle);179scope.release(handle); // make sure it's idempotent180scope.release(handle); // make sure it's idempotent181}182}183}184185@Test(dataProvider = "cleaners")186public void testLockSharedMultiThread(Supplier<Cleaner> cleanerSupplier) {187Cleaner cleaner = cleanerSupplier.get();188ResourceScope scope = cleaner != null ?189ResourceScope.newSharedScope(cleaner) :190ResourceScope.newSharedScope();191AtomicInteger lockCount = new AtomicInteger();192for (int i = 0 ; i < N_THREADS ; i++) {193new Thread(() -> {194lockCount.incrementAndGet();195try {196ResourceScope.Handle handle = scope.acquire();197waitSomeTime();198scope.release(handle);199scope.release(handle); // make sure it's idempotent200scope.release(handle); // make sure it's idempotent201} catch (IllegalStateException ex) {202// might be already closed - do nothing203} finally {204lockCount.decrementAndGet();205}206}).start();207}208209while (lockCount.get() > 0) {210try {211scope.close();212assertEquals(lockCount.get(), 0);213break;214} catch (IllegalStateException ex) {215waitSomeTime();216}217}218}219220@Test221public void testCloseEmptyConfinedScope() {222ResourceScope.newConfinedScope().close();223}224225@Test226public void testCloseEmptySharedScope() {227ResourceScope.newSharedScope().close();228}229230@Test231public void testCloseConfinedLock() {232ResourceScope scope = ResourceScope.newConfinedScope();233ResourceScope.Handle handle = scope.acquire();234AtomicReference<Throwable> failure = new AtomicReference<>();235Thread t = new Thread(() -> {236try {237scope.release(handle);238scope.release(handle); // make sure it's idempotent239scope.release(handle); // make sure it's idempotent240} catch (Throwable ex) {241failure.set(ex);242}243});244t.start();245try {246t.join();247assertNotNull(failure.get());248assertEquals(failure.get().getClass(), IllegalStateException.class);249} catch (Throwable ex) {250throw new AssertionError(ex);251}252}253254@Test(dataProvider = "scopes")255public void testScopeHandles(Supplier<ResourceScope> scopeFactory) {256ResourceScope scope = scopeFactory.get();257acquireRecursive(scope, 5);258if (!scope.isImplicit()) {259scope.close();260}261}262263private void acquireRecursive(ResourceScope scope, int acquireCount) {264ResourceScope.Handle handle = scope.acquire();265assertEquals(handle.scope(), scope);266if (acquireCount > 0) {267// recursive acquire268acquireRecursive(scope, acquireCount - 1);269}270if (!scope.isImplicit()) {271assertThrows(IllegalStateException.class, scope::close);272}273scope.release(handle);274scope.release(handle); // make sure it's idempotent275scope.release(handle); // make sure it's idempotent276}277278private void waitSomeTime() {279try {280Thread.sleep(10);281} catch (InterruptedException ex) {282// ignore283}284}285286private void kickGC() {287for (int i = 0 ; i < 100 ; i++) {288byte[] b = new byte[100];289System.gc();290Thread.onSpinWait();291}292}293294@DataProvider295static Object[][] cleaners() {296return new Object[][] {297{ (Supplier<Cleaner>)() -> null },298{ (Supplier<Cleaner>)Cleaner::create },299{ (Supplier<Cleaner>)CleanerFactory::cleaner }300};301}302303@DataProvider304static Object[][] scopes() {305return new Object[][] {306{ (Supplier<ResourceScope>)ResourceScope::newConfinedScope },307{ (Supplier<ResourceScope>)ResourceScope::newSharedScope },308{ (Supplier<ResourceScope>)ResourceScope::newImplicitScope },309{ (Supplier<ResourceScope>)ResourceScope::globalScope }310};311}312}313314315