Skip to content

Commit

Permalink
8301767: Convert virtual thread tests to JUnit
Browse files Browse the repository at this point in the history
Reviewed-by: cstein, lancea, jpai
  • Loading branch information
Alan Bateman committed Feb 8, 2023
1 parent 9af2ea2 commit ecf21a9
Show file tree
Hide file tree
Showing 34 changed files with 1,378 additions and 1,253 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -26,7 +26,7 @@
* @bug 8284161 8287008
* @summary Basic test for jcmd Thread.dump_to_file
* @library /test/lib
* @run testng/othervm ThreadDumpToFileTest
* @run junit/othervm ThreadDumpToFileTest
*/

import java.io.IOException;
Expand All @@ -37,16 +37,16 @@
import jdk.test.lib.process.OutputAnalyzer;
import jdk.test.lib.threaddump.ThreadDump;

import org.testng.annotations.Test;
import static org.testng.Assert.*;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class ThreadDumpToFileTest {
class ThreadDumpToFileTest {

/**
* Test thread dump, should be in plain text format.
*/
@Test
public void testThreadDump() throws IOException {
void testThreadDump() throws IOException {
Path file = genThreadDumpPath(".txt");
testPlainThreadDump(file);
}
Expand All @@ -55,7 +55,7 @@ public void testThreadDump() throws IOException {
* Test thread dump in plain text format.
*/
@Test
public void testPlainThreadDump() throws IOException {
void testPlainThreadDump() throws IOException {
Path file = genThreadDumpPath(".txt");
testPlainThreadDump(file, "-format=plain");
}
Expand All @@ -64,7 +64,7 @@ public void testPlainThreadDump() throws IOException {
* Test thread dump in JSON format.
*/
@Test
public void testJsonThreadDump() throws IOException {
void testJsonThreadDump() throws IOException {
Path file = genThreadDumpPath(".json");
threadDump(file, "-format=json").shouldMatch("Created");

Expand All @@ -85,21 +85,21 @@ public void testJsonThreadDump() throws IOException {
* Test that an existing file is not overwritten.
*/
@Test
public void testDoNotOverwriteFile() throws IOException {
void testDoNotOverwriteFile() throws IOException {
Path file = genThreadDumpPath(".txt");
Files.writeString(file, "xxx");

threadDump(file, "").shouldMatch("exists");

// file should not be overridden
assertEquals(Files.readString(file), "xxx");
assertEquals("xxx", Files.readString(file));
}

/**
* Test overwriting an existing file.
*/
@Test
public void testOverwriteFile() throws IOException {
void testOverwriteFile() throws IOException {
Path file = genThreadDumpPath(".txt");
Files.writeString(file, "xxx");
testPlainThreadDump(file, "-overwrite");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -27,10 +27,10 @@
* @summary Basic test for com.sun.management.HotSpotDiagnosticMXBean.dumpThreads
* @enablePreview
* @library /test/lib
* @run testng/othervm DumpThreads
* @run testng/othervm -Djdk.trackAllThreads DumpThreads
* @run testng/othervm -Djdk.trackAllThreads=true DumpThreads
* @run testng/othervm -Djdk.trackAllThreadds=false DumpThreads
* @run junit/othervm DumpThreads
* @run junit/othervm -Djdk.trackAllThreads DumpThreads
* @run junit/othervm -Djdk.trackAllThreads=true DumpThreads
* @run junit/othervm -Djdk.trackAllThreadds=false DumpThreads
*/

import java.lang.management.ManagementFactory;
Expand All @@ -47,10 +47,10 @@
import com.sun.management.HotSpotDiagnosticMXBean.ThreadDumpFormat;
import jdk.test.lib.threaddump.ThreadDump;

import org.testng.annotations.Test;
import static org.testng.Assert.*;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class DumpThreads {
class DumpThreads {
private static final boolean TRACK_ALL_THREADS;
static {
String s = System.getProperty("jdk.trackAllThreads");
Expand All @@ -61,7 +61,7 @@ public class DumpThreads {
* Thread dump in plain text format.
*/
@Test
public void testPlainText() throws Exception {
void testPlainText() throws Exception {
var mbean = ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class);
Path file = genOutputPath("txt");
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
Expand Down Expand Up @@ -98,7 +98,7 @@ public void testPlainText() throws Exception {
* Thread dump in JSON format.
*/
@Test
public void testJson() throws Exception {
void testJson() throws Exception {
var mbean = ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class);
Path file = genOutputPath("json");
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
Expand All @@ -118,7 +118,7 @@ public void testJson() throws Exception {
ZonedDateTime.parse(threadDump.time());

// test threadDump/runtimeVersion
assertEquals(threadDump.runtimeVersion(), Runtime.version().toString());
assertEquals(Runtime.version().toString(), threadDump.runtimeVersion());

// test root container
var rootContainer = threadDump.rootThreadContainer();
Expand Down Expand Up @@ -150,7 +150,7 @@ public void testJson() throws Exception {
* Test that dumpThreads throws if the output file already exists.
*/
@Test
public void testFileAlreadyExsists() throws Exception {
void testFileAlreadyExsists() throws Exception {
var mbean = ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class);
String file = Files.createFile(genOutputPath("txt")).toString();
assertThrows(FileAlreadyExistsException.class,
Expand All @@ -163,7 +163,7 @@ public void testFileAlreadyExsists() throws Exception {
* Test that dumpThreads throws if the file path is relative.
*/
@Test
public void testRelativePath() throws Exception {
void testRelativePath() throws Exception {
var mbean = ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class);
assertThrows(IllegalArgumentException.class,
() -> mbean.dumpThreads("threads.txt", ThreadDumpFormat.TEXT_PLAIN));
Expand All @@ -175,7 +175,7 @@ public void testRelativePath() throws Exception {
* Test that dumpThreads throws with null parameters.
*/
@Test
public void testNull() throws Exception {
void testNull() throws Exception {
var mbean = ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class);
assertThrows(NullPointerException.class,
() -> mbean.dumpThreads(null, ThreadDumpFormat.TEXT_PLAIN));
Expand Down
Loading

1 comment on commit ecf21a9

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.