2021-06-02 10:53:06 +00:00
|
|
|
/*
|
2022-05-12 16:17:45 +00:00
|
|
|
* Copyright (c) 2021, 2022, Oracle and/or its affiliates. All rights reserved.
|
2021-06-02 10:53:06 +00:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
|
|
*
|
|
|
|
* This code is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 only, as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* version 2 for more details (a copy is included in the LICENSE file that
|
|
|
|
* accompanied this code).
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License version
|
|
|
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
|
|
* or visit www.oracle.com if you need additional information or have any
|
|
|
|
* questions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2022-05-12 16:17:45 +00:00
|
|
|
import java.lang.foreign.MemorySegment;
|
|
|
|
import java.lang.foreign.MemorySession;
|
2021-06-02 10:53:06 +00:00
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Random;
|
|
|
|
import java.util.concurrent.ExecutionException;
|
|
|
|
import java.util.function.Supplier;
|
|
|
|
import java.util.stream.Stream;
|
2021-11-24 11:51:16 +00:00
|
|
|
|
2021-06-02 10:53:06 +00:00
|
|
|
import jdk.test.lib.RandomFactory;
|
|
|
|
import org.testng.annotations.*;
|
2021-11-24 11:51:16 +00:00
|
|
|
|
2022-05-12 16:17:45 +00:00
|
|
|
import static java.lang.foreign.ValueLayout.JAVA_BYTE;
|
2021-06-02 10:53:06 +00:00
|
|
|
import static org.testng.Assert.*;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Not a test, but infra for channel tests.
|
|
|
|
*/
|
|
|
|
public class AbstractChannelsTest {
|
|
|
|
|
|
|
|
static final Class<IOException> IOE = IOException.class;
|
|
|
|
static final Class<ExecutionException> EE = ExecutionException.class;
|
|
|
|
static final Class<IllegalStateException> ISE = IllegalStateException.class;
|
|
|
|
|
|
|
|
@FunctionalInterface
|
|
|
|
interface ThrowingConsumer<T, X extends Throwable> {
|
|
|
|
void accept(T action) throws X;
|
|
|
|
}
|
|
|
|
|
2022-05-12 16:17:45 +00:00
|
|
|
static MemorySession closeableSessionOrNull(MemorySession session) {
|
|
|
|
return (session.isCloseable()) ?
|
|
|
|
session : null;
|
2021-06-02 10:53:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static long remaining(ByteBuffer[] buffers) {
|
|
|
|
return Arrays.stream(buffers).mapToLong(ByteBuffer::remaining).sum();
|
|
|
|
}
|
|
|
|
|
|
|
|
static ByteBuffer[] flip(ByteBuffer[] buffers) {
|
|
|
|
Arrays.stream(buffers).forEach(ByteBuffer::flip);
|
|
|
|
return buffers;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ByteBuffer[] clear(ByteBuffer[] buffers) {
|
|
|
|
Arrays.stream(buffers).forEach(ByteBuffer::clear);
|
|
|
|
return buffers;
|
|
|
|
}
|
|
|
|
|
|
|
|
static final Random RANDOM = RandomFactory.getRandom();
|
|
|
|
|
2022-05-12 16:17:45 +00:00
|
|
|
static ByteBuffer segmentBufferOfSize(MemorySession session, int size) {
|
|
|
|
var segment = MemorySegment.allocateNative(size, 1, session);
|
2021-06-02 10:53:06 +00:00
|
|
|
for (int i = 0; i < size; i++) {
|
2021-11-24 11:51:16 +00:00
|
|
|
segment.set(JAVA_BYTE, i, ((byte)RANDOM.nextInt()));
|
2021-06-02 10:53:06 +00:00
|
|
|
}
|
|
|
|
return segment.asByteBuffer();
|
|
|
|
}
|
|
|
|
|
2022-05-12 16:17:45 +00:00
|
|
|
static ByteBuffer[] segmentBuffersOfSize(int len, MemorySession session, int size) {
|
2021-06-02 10:53:06 +00:00
|
|
|
ByteBuffer[] bufs = new ByteBuffer[len];
|
|
|
|
for (int i = 0; i < len; i++)
|
2022-05-12 16:17:45 +00:00
|
|
|
bufs[i] = segmentBufferOfSize(session, size);
|
2021-06-02 10:53:06 +00:00
|
|
|
return bufs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an array of mixed source byte buffers; both heap and direct,
|
2022-05-12 16:17:45 +00:00
|
|
|
* where heap can be from the global session or session-less, and direct are
|
|
|
|
* associated with the given session.
|
2021-06-02 10:53:06 +00:00
|
|
|
*/
|
2022-05-12 16:17:45 +00:00
|
|
|
static ByteBuffer[] mixedBuffersOfSize(int len, MemorySession session, int size) {
|
2021-06-02 10:53:06 +00:00
|
|
|
ByteBuffer[] bufs;
|
2022-05-12 16:17:45 +00:00
|
|
|
boolean atLeastOneSessionBuffer = false;
|
2021-06-02 10:53:06 +00:00
|
|
|
do {
|
|
|
|
bufs = new ByteBuffer[len];
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
bufs[i] = switch (RANDOM.nextInt(3)) {
|
|
|
|
case 0 -> { byte[] b = new byte[size];
|
|
|
|
RANDOM.nextBytes(b);
|
|
|
|
yield ByteBuffer.wrap(b); }
|
|
|
|
case 1 -> { byte[] b = new byte[size];
|
|
|
|
RANDOM.nextBytes(b);
|
|
|
|
yield MemorySegment.ofArray(b).asByteBuffer(); }
|
2022-05-12 16:17:45 +00:00
|
|
|
case 2 -> { atLeastOneSessionBuffer = true;
|
|
|
|
yield segmentBufferOfSize(session, size); }
|
2021-06-02 10:53:06 +00:00
|
|
|
default -> throw new AssertionError("cannot happen");
|
|
|
|
};
|
|
|
|
}
|
2022-05-12 16:17:45 +00:00
|
|
|
} while (!atLeastOneSessionBuffer);
|
2021-06-02 10:53:06 +00:00
|
|
|
return bufs;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void assertMessage(Exception ex, String msg) {
|
|
|
|
assertTrue(ex.getMessage().contains(msg), "Expected [%s], in: [%s]".formatted(msg, ex.getMessage()));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void assertCauses(Throwable ex, Class<? extends Exception>... exceptions) {
|
|
|
|
for (var expectedClass : exceptions) {
|
|
|
|
ex = ex.getCause();
|
|
|
|
assertTrue(expectedClass.isInstance(ex), "Expected %s, got: %s".formatted(expectedClass, ex));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-12 16:17:45 +00:00
|
|
|
@DataProvider(name = "confinedSessions")
|
|
|
|
public static Object[][] confinedSessions() {
|
2021-06-02 10:53:06 +00:00
|
|
|
return new Object[][] {
|
2022-05-12 16:17:45 +00:00
|
|
|
{ SessionSupplier.NEW_CONFINED },
|
2021-06-02 10:53:06 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-05-12 16:17:45 +00:00
|
|
|
@DataProvider(name = "sharedSessions")
|
|
|
|
public static Object[][] sharedSessions() {
|
2021-06-02 10:53:06 +00:00
|
|
|
return new Object[][] {
|
2022-05-12 16:17:45 +00:00
|
|
|
{ SessionSupplier.NEW_SHARED },
|
2021-06-02 10:53:06 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-05-12 16:17:45 +00:00
|
|
|
@DataProvider(name = "closeableSessions")
|
|
|
|
public static Object[][] closeableSessions() {
|
|
|
|
return Stream.of(sharedSessions(), confinedSessions())
|
2021-06-02 10:53:06 +00:00
|
|
|
.flatMap(Arrays::stream)
|
|
|
|
.toArray(Object[][]::new);
|
|
|
|
}
|
|
|
|
|
2022-05-12 16:17:45 +00:00
|
|
|
@DataProvider(name = "implicitSessions")
|
|
|
|
public static Object[][] implicitSessions() {
|
2021-06-02 10:53:06 +00:00
|
|
|
return new Object[][] {
|
2022-05-12 16:17:45 +00:00
|
|
|
{ SessionSupplier.GLOBAL },
|
2021-06-02 10:53:06 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-05-12 16:17:45 +00:00
|
|
|
@DataProvider(name = "sharedAndImplicitSessions")
|
|
|
|
public static Object[][] sharedAndImplicitSessions() {
|
|
|
|
return Stream.of(sharedSessions(), implicitSessions())
|
2021-06-02 10:53:06 +00:00
|
|
|
.flatMap(Arrays::stream)
|
|
|
|
.toArray(Object[][]::new);
|
|
|
|
}
|
|
|
|
|
2022-05-12 16:17:45 +00:00
|
|
|
@DataProvider(name = "allSessions")
|
|
|
|
public static Object[][] allSessions() {
|
|
|
|
return Stream.of(implicitSessions(), closeableSessions())
|
2021-06-02 10:53:06 +00:00
|
|
|
.flatMap(Arrays::stream)
|
|
|
|
.toArray(Object[][]::new);
|
|
|
|
}
|
|
|
|
|
2022-05-12 16:17:45 +00:00
|
|
|
@DataProvider(name = "sharedSessionsAndTimeouts")
|
|
|
|
public static Object[][] sharedSessionsAndTimeouts() {
|
2021-06-02 10:53:06 +00:00
|
|
|
return new Object[][] {
|
2022-05-12 16:17:45 +00:00
|
|
|
{ SessionSupplier.NEW_SHARED , 0 },
|
|
|
|
{ SessionSupplier.NEW_SHARED , 30 },
|
2021-06-02 10:53:06 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-05-12 16:17:45 +00:00
|
|
|
static class SessionSupplier implements Supplier<MemorySession> {
|
2021-06-02 10:53:06 +00:00
|
|
|
|
2022-05-12 16:17:45 +00:00
|
|
|
static final Supplier<MemorySession> NEW_CONFINED =
|
|
|
|
new SessionSupplier(MemorySession::openConfined, "newConfinedSession()");
|
|
|
|
static final Supplier<MemorySession> NEW_SHARED =
|
|
|
|
new SessionSupplier(MemorySession::openShared, "newSharedSession()");
|
|
|
|
static final Supplier<MemorySession> NEW_IMPLICIT =
|
|
|
|
new SessionSupplier(MemorySession::openImplicit, "newImplicitSession()");
|
|
|
|
static final Supplier<MemorySession> GLOBAL =
|
|
|
|
new SessionSupplier(MemorySession::global, "globalSession()");
|
2021-06-02 10:53:06 +00:00
|
|
|
|
2022-05-12 16:17:45 +00:00
|
|
|
private final Supplier<MemorySession> supplier;
|
2021-06-02 10:53:06 +00:00
|
|
|
private final String str;
|
2022-05-12 16:17:45 +00:00
|
|
|
private SessionSupplier(Supplier<MemorySession> supplier, String str) {
|
2021-06-02 10:53:06 +00:00
|
|
|
this.supplier = supplier;
|
|
|
|
this.str = str;
|
|
|
|
}
|
|
|
|
@Override public String toString() { return str; }
|
2022-05-12 16:17:45 +00:00
|
|
|
@Override public MemorySession get() { return supplier.get(); }
|
2021-06-02 10:53:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|