Skip to content

Commit

Permalink
8255031: Update java/util/prefs/AddNodeChangeListener.java to report …
Browse files Browse the repository at this point in the history
…more failure info

Reviewed-by: bpb
  • Loading branch information
Brent Christian committed Oct 22, 2020
1 parent 0aa3c92 commit 8afdcae
Showing 1 changed file with 53 additions and 19 deletions.
72 changes: 53 additions & 19 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, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2020, 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 @@ -23,22 +23,23 @@

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

import java.util.prefs.*;

public class AddNodeChangeListener {
public class AddNodeChangeListener {

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

public static void main(String[] args)
throws BackingStoreException, InterruptedException
{
public static void main(String[] args)
throws BackingStoreException, InterruptedException {
userRoot = Preferences.userRoot();
ncla = new NodeChangeListenerAdd();
userRoot.addNodeChangeListener(ncla);
Expand All @@ -49,28 +50,61 @@ public static void main(String[] args)
//Should initate a child removed event
removeNode();

if (failed)
if (failed) {
throw new RuntimeException("Failed");
}
}

private static void addNode()
throws BackingStoreException, InterruptedException
{
throws BackingStoreException, InterruptedException {
N2 = userRoot.node("N2");
userRoot.flush();
Thread.sleep(3000);
if (ncla.getAddNumber() != 1)
failed = true;
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()");
}

private static void removeNode()
throws BackingStoreException, InterruptedException
{
throws BackingStoreException, InterruptedException {
N2.removeNode();
userRoot.flush();
Thread.sleep(3000);
if (ncla.getAddNumber() != 0)
int passItr = -1;

for (int i = 0; i < SLEEP_ITRS; i++) {
System.out.print("removeNode sleep iteration " + i + "...");
Thread.sleep(3000);
System.out.println("done.");
if (ncla.getAddNumber() == 0) {
passItr = 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) {
System.out.println(methodName + " test passed");
} else {
failed = true;
if (itr == -1) {
throw new RuntimeException("Failed in " + methodName + " - change listener never notified");
} else {
throw new RuntimeException("Failed in " + methodName + " - listener notified on iteration " + itr);
}
}
}

private static class NodeChangeListenerAdd implements NodeChangeListener {
Expand All @@ -90,4 +124,4 @@ public int getAddNumber(){
return totalNode;
}
}
}
}

1 comment on commit 8afdcae

@bridgekeeper
Copy link

@bridgekeeper bridgekeeper bot commented on 8afdcae Oct 22, 2020

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.