1
1
/*
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.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
23
23
24
24
/* @test
25
25
* @bug 7160252 7197662
26
+ * @key intermittent
26
27
* @summary Checks if events are delivered to a listener
27
28
* when a child node is added or removed
28
29
* @run main/othervm -Djava.util.prefs.userRoot=. AddNodeChangeListener
29
30
*/
30
31
31
32
import java .util .prefs .*;
32
33
33
- public class AddNodeChangeListener {
34
+ public class AddNodeChangeListener {
34
35
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 ;
38
40
39
- public static void main (String [] args )
40
- throws BackingStoreException , InterruptedException
41
- {
41
+ public static void main (String [] args )
42
+ throws BackingStoreException , InterruptedException {
42
43
userRoot = Preferences .userRoot ();
43
44
ncla = new NodeChangeListenerAdd ();
44
45
userRoot .addNodeChangeListener (ncla );
@@ -49,28 +50,61 @@ public static void main(String[] args)
49
50
//Should initate a child removed event
50
51
removeNode ();
51
52
52
- if (failed )
53
+ if (failed ) {
53
54
throw new RuntimeException ("Failed" );
55
+ }
54
56
}
55
57
56
58
private static void addNode ()
57
- throws BackingStoreException , InterruptedException
58
- {
59
+ throws BackingStoreException , InterruptedException {
59
60
N2 = userRoot .node ("N2" );
60
61
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()" );
64
74
}
65
75
66
76
private static void removeNode ()
67
- throws BackingStoreException , InterruptedException
68
- {
77
+ throws BackingStoreException , InterruptedException {
69
78
N2 .removeNode ();
70
79
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 {
73
101
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
+ }
74
108
}
75
109
76
110
private static class NodeChangeListenerAdd implements NodeChangeListener {
@@ -90,4 +124,4 @@ public int getAddNumber(){
90
124
return totalNode ;
91
125
}
92
126
}
93
- }
127
+ }
0 commit comments