Skip to content

Commit 8afdcae

Browse files
author
Brent Christian
committed
8255031: Update java/util/prefs/AddNodeChangeListener.java to report more failure info
Reviewed-by: bpb
1 parent 0aa3c92 commit 8afdcae

File tree

1 file changed

+53
-19
lines changed

1 file changed

+53
-19
lines changed

test/jdk/java/util/prefs/AddNodeChangeListener.java

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,22 +23,23 @@
2323

2424
/* @test
2525
* @bug 7160252 7197662
26+
* @key intermittent
2627
* @summary Checks if events are delivered to a listener
2728
* when a child node is added or removed
2829
* @run main/othervm -Djava.util.prefs.userRoot=. AddNodeChangeListener
2930
*/
3031

3132
import java.util.prefs.*;
3233

33-
public class AddNodeChangeListener {
34+
public class AddNodeChangeListener {
3435

35-
private static boolean failed = false;
36-
private static Preferences userRoot, N2;
37-
private static NodeChangeListenerAdd ncla;
36+
private static final int SLEEP_ITRS = 10;
37+
private static boolean failed = false;
38+
private static Preferences userRoot, N2;
39+
private static NodeChangeListenerAdd ncla;
3840

39-
public static void main(String[] args)
40-
throws BackingStoreException, InterruptedException
41-
{
41+
public static void main(String[] args)
42+
throws BackingStoreException, InterruptedException {
4243
userRoot = Preferences.userRoot();
4344
ncla = new NodeChangeListenerAdd();
4445
userRoot.addNodeChangeListener(ncla);
@@ -49,28 +50,61 @@ public static void main(String[] args)
4950
//Should initate a child removed event
5051
removeNode();
5152

52-
if (failed)
53+
if (failed) {
5354
throw new RuntimeException("Failed");
55+
}
5456
}
5557

5658
private static void addNode()
57-
throws BackingStoreException, InterruptedException
58-
{
59+
throws BackingStoreException, InterruptedException {
5960
N2 = userRoot.node("N2");
6061
userRoot.flush();
61-
Thread.sleep(3000);
62-
if (ncla.getAddNumber() != 1)
63-
failed = true;
62+
int passItr = -1;
63+
64+
for (int i = 0; i < SLEEP_ITRS; i++) {
65+
System.out.print("addNode sleep iteration " + i + "...");
66+
Thread.sleep(3000);
67+
System.out.println("done.");
68+
if (ncla.getAddNumber() == 1) {
69+
passItr = i;
70+
break;
71+
}
72+
}
73+
checkPassItr(passItr, "addNode()");
6474
}
6575

6676
private static void removeNode()
67-
throws BackingStoreException, InterruptedException
68-
{
77+
throws BackingStoreException, InterruptedException {
6978
N2.removeNode();
7079
userRoot.flush();
71-
Thread.sleep(3000);
72-
if (ncla.getAddNumber() != 0)
80+
int passItr = -1;
81+
82+
for (int i = 0; i < SLEEP_ITRS; i++) {
83+
System.out.print("removeNode sleep iteration " + i + "...");
84+
Thread.sleep(3000);
85+
System.out.println("done.");
86+
if (ncla.getAddNumber() == 0) {
87+
passItr = i;
88+
break;
89+
}
90+
}
91+
checkPassItr(passItr, "removeNode()");
92+
}
93+
94+
/* If the listener wasn't notified on iteration 0, throw a RuntimeException
95+
* with some contextual information
96+
*/
97+
private static void checkPassItr(int itr, String methodName) {
98+
if (itr == 0) {
99+
System.out.println(methodName + " test passed");
100+
} else {
73101
failed = true;
102+
if (itr == -1) {
103+
throw new RuntimeException("Failed in " + methodName + " - change listener never notified");
104+
} else {
105+
throw new RuntimeException("Failed in " + methodName + " - listener notified on iteration " + itr);
106+
}
107+
}
74108
}
75109

76110
private static class NodeChangeListenerAdd implements NodeChangeListener {
@@ -90,4 +124,4 @@ public int getAddNumber(){
90124
return totalNode;
91125
}
92126
}
93-
}
127+
}

0 commit comments

Comments
 (0)