Skip to content

Commit

Permalink
Release testMyServerExampleWithCleaner publicly.
Browse files Browse the repository at this point in the history
(followup cl/705490132)

The method requires JDK 9+ to build, but our Maven build [always builds with JDK 23](#6549 (comment)). It also requires JDK 9+ to _run_, so I've added code to skip running the test under older versions.

Much of my goal here is to shake out any ways that we might cause problems if we begin conditionally using Java 9+ APIs more widely, such as [for `VarHandle`](#6806 (comment)).

RELNOTES=n/a
PiperOrigin-RevId: 705498400
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Dec 12, 2024
1 parent 62c6187 commit e2e65ed
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.io.Closeable;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.lang.ref.Cleaner;
import java.lang.ref.Cleaner.Cleanable;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
Expand Down Expand Up @@ -220,4 +222,55 @@ public void testMyServerExampleWithFrq() throws Exception {
GcFinalization.awaitDone(finalizeReferentRan::get);
assertThat(serverSocket.isClosed()).isTrue();
}

@SuppressWarnings("Java8ApiChecker")
static class MyServerExampleWithCleaner implements AutoCloseable {
private static final Cleaner cleaner = Cleaner.create();

private static Runnable closeServerSocketRunnable(
ServerSocket serverSocket, AtomicBoolean cleanerRan) {
return () -> {
cleanerRan.set(true);
try {
serverSocket.close();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
};
}

private final ServerSocket serverSocket;
private final Cleanable cleanable;

MyServerExampleWithCleaner(AtomicBoolean cleanerRan) throws IOException {
this.serverSocket = new ServerSocket(0);
this.cleanable = cleaner.register(this, closeServerSocketRunnable(serverSocket, cleanerRan));
}

@Override
public void close() {
cleanable.clean();
}
}

@SuppressWarnings("Java8ApiChecker")
private ServerSocket makeMyServerExampleWithCleaner(AtomicBoolean cleanerRan) throws IOException {
MyServerExampleWithCleaner myServer = new MyServerExampleWithCleaner(cleanerRan);
assertThat(myServer.serverSocket.isClosed()).isFalse();
return myServer.serverSocket;
}

@SuppressWarnings("Java8ApiChecker")
@Test
public void testMyServerExampleWithCleaner() throws Exception {
try {
Class.forName("java.lang.ref.Cleaner");
} catch (ClassNotFoundException beforeJava9) {
return;
}
AtomicBoolean cleanerRan = new AtomicBoolean(false);
ServerSocket serverSocket = makeMyServerExampleWithCleaner(cleanerRan);
GcFinalization.awaitDone(cleanerRan::get);
assertThat(serverSocket.isClosed()).isTrue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.io.Closeable;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.lang.ref.Cleaner;
import java.lang.ref.Cleaner.Cleanable;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
Expand Down Expand Up @@ -220,4 +222,55 @@ public void testMyServerExampleWithFrq() throws Exception {
GcFinalization.awaitDone(finalizeReferentRan::get);
assertThat(serverSocket.isClosed()).isTrue();
}

@SuppressWarnings("Java8ApiChecker")
static class MyServerExampleWithCleaner implements AutoCloseable {
private static final Cleaner cleaner = Cleaner.create();

private static Runnable closeServerSocketRunnable(
ServerSocket serverSocket, AtomicBoolean cleanerRan) {
return () -> {
cleanerRan.set(true);
try {
serverSocket.close();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
};
}

private final ServerSocket serverSocket;
private final Cleanable cleanable;

MyServerExampleWithCleaner(AtomicBoolean cleanerRan) throws IOException {
this.serverSocket = new ServerSocket(0);
this.cleanable = cleaner.register(this, closeServerSocketRunnable(serverSocket, cleanerRan));
}

@Override
public void close() {
cleanable.clean();
}
}

@SuppressWarnings("Java8ApiChecker")
private ServerSocket makeMyServerExampleWithCleaner(AtomicBoolean cleanerRan) throws IOException {
MyServerExampleWithCleaner myServer = new MyServerExampleWithCleaner(cleanerRan);
assertThat(myServer.serverSocket.isClosed()).isFalse();
return myServer.serverSocket;
}

@SuppressWarnings("Java8ApiChecker")
@Test
public void testMyServerExampleWithCleaner() throws Exception {
try {
Class.forName("java.lang.ref.Cleaner");
} catch (ClassNotFoundException beforeJava9) {
return;
}
AtomicBoolean cleanerRan = new AtomicBoolean(false);
ServerSocket serverSocket = makeMyServerExampleWithCleaner(cleanerRan);
GcFinalization.awaitDone(cleanerRan::get);
assertThat(serverSocket.isClosed()).isTrue();
}
}

0 comments on commit e2e65ed

Please sign in to comment.