Skip to content

Commit

Permalink
8283349: Robustness improvements to java/util/prefs/AddNodeChangeList…
Browse files Browse the repository at this point in the history
…ener.jar

Backport-of: 656cba7af376d6460202591230ac95d2366de9f3
  • Loading branch information
Amos Shi committed May 8, 2024
1 parent 6b225d5 commit 4c81a1c
Showing 1 changed file with 53 additions and 40 deletions.
93 changes: 53 additions & 40 deletions test/jdk/java/util/prefs/AddNodeChangeListener.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2022, 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 @@ -34,75 +34,88 @@
public class AddNodeChangeListener {

private static final int SLEEP_ITRS = 10;
private static final String N2_STR = "N2";
private static boolean failed = false;
private static Preferences userRoot, N2;
private static NodeChangeListenerAdd ncla;

public static void main(String[] args)
throws BackingStoreException, InterruptedException {
userRoot = Preferences.userRoot();
ncla = new NodeChangeListenerAdd();
userRoot.addNodeChangeListener(ncla);
//Should initiate a node added event
addNode();
// Should not initiate a node added event
addNode();
//Should initate a child removed event
removeNode();

if (failed) {
throw new RuntimeException("Failed");
try {
userRoot = Preferences.userRoot();
// Make sure test node is not present before test
clearPrefs();

ncla = new NodeChangeListenerAdd();
userRoot.addNodeChangeListener(ncla);
//Should initiate a node added event
addNode();
// Should not initiate a node added event
addNode();
//Should initate a child removed event
removeNode();

if (failed) {
throw new RuntimeException("Failed");
}
} finally {
// Make sure test node is not present after the test
clearPrefs();
}
}

private static void addNode()
throws BackingStoreException, InterruptedException {
N2 = userRoot.node("N2");
N2 = userRoot.node(N2_STR);
userRoot.flush();
int passItr = -1;

for (int i = 0; i < SLEEP_ITRS; i++) {
System.out.print("addNode sleep iteration " + i + "...");
Thread.sleep(3000);
System.out.println("done.");
if (ncla.getAddNumber() == 1) {
passItr = i;
break;
}
}
checkPassItr(passItr, "addNode()");
checkAndSleep(1, "addNode");
}

private static void removeNode()
throws BackingStoreException, InterruptedException {
N2.removeNode();
userRoot.flush();
int passItr = -1;
checkAndSleep(0, "removeNode");
}

/* Check for the expected value in the listener (1 after addNode(), 0 after removeNode()).
* Sleep a few extra times in a loop, if needed, for debugging purposes, to
* see if the event gets delivered late.
*/
private static void checkAndSleep(int expectedListenerVal, String methodName) throws InterruptedException {
int expectedItr = -1; // iteration in which the expected value was retrieved from the listener, or -1 if never
for (int i = 0; i < SLEEP_ITRS; i++) {
System.out.print("removeNode sleep iteration " + i + "...");
System.out.print(methodName + " sleep iteration " + i + "...");
Thread.sleep(3000);
System.out.println("done.");
if (ncla.getAddNumber() == 0) {
passItr = i;
if (ncla.getAddNumber() == expectedListenerVal) {
expectedItr = i;
break;
}
}
checkPassItr(passItr, "removeNode()");
}

/* If the listener wasn't notified on iteration 0, throw a RuntimeException
* with some contextual information
*/
private static void checkPassItr(int itr, String methodName) {
if (itr == 0) {
if (expectedItr == 0) {
System.out.println(methodName + " test passed");
} else {
failed = true;
if (itr == -1) {
throw new RuntimeException("Failed in " + methodName + " - change listener never notified");
if (expectedItr == -1) {
System.out.println("Failed in " + methodName + " - change listener never notified");
} else {
throw new RuntimeException("Failed in " + methodName + " - listener notified on iteration " + itr);
System.out.println("Failed in " + methodName + " - listener notified on iteration " + expectedItr);
}
}
}

/* Check if the node already exists in userRoot, and remove it if so. */
private static void clearPrefs() throws BackingStoreException {
if (userRoot.nodeExists(N2_STR)) {
System.out.println("Node " + N2_STR + " already/still exists; clearing");
Preferences clearNode = userRoot.node(N2_STR);
userRoot.flush();
clearNode.removeNode();
userRoot.flush();
if (userRoot.nodeExists(N2_STR)) {
throw new RuntimeException("Unable to clear pre-existing node." + (failed ? " Also, the test failed" : ""));
}
}
}
Expand Down

1 comment on commit 4c81a1c

@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.