From c7ff2fcefefea6b4db1038ac5b8cc340446cdf8f Mon Sep 17 00:00:00 2001 From: smarks Date: Fri, 21 Nov 2014 13:32:17 -0800 Subject: [PATCH] 8056313: TEST_BUG: java/util/Timer/NameConstructors.java fails intermittently Reviewed-by: lancea, rriggs --- test/java/util/Timer/NameConstructors.java | 41 ++++++++++++---------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/test/java/util/Timer/NameConstructors.java b/test/java/util/Timer/NameConstructors.java index bc74522e4..fbf1649ef 100644 --- a/test/java/util/Timer/NameConstructors.java +++ b/test/java/util/Timer/NameConstructors.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2014, 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 @@ -23,37 +23,42 @@ /* * @test - * @bug 4279061 + * @bug 4279061 8056313 * @summary Basic test for constructors with thread name */ -import java.util.*; +import java.util.Timer; +import java.util.TimerTask; +import java.util.concurrent.LinkedTransferQueue; public class NameConstructors { private static final String NAME1 = "Norm D. Plume"; private static final String NAME2 = "Ann Onymous"; - public static void main (String[] args) throws Exception { - Random rnd = new Random(); + public static void main (String[] args) throws InterruptedException { test(new Timer(NAME1), NAME1); test(new Timer(NAME2, true), NAME2); } - private static boolean done, passed; + public static void test(Timer timer, String expected) throws InterruptedException { + try { + LinkedTransferQueue queue = new LinkedTransferQueue<>(); - public static void test(Timer timer, final String name) throws Exception { - done = passed = false; + TimerTask task = new TimerTask() { + public void run() { + queue.put(Thread.currentThread().getName()); + } + }; - TimerTask task = new TimerTask() { - public void run() { - passed = Thread.currentThread().getName().equals(name); - done = true; + timer.schedule(task, 0L); // immediately + String actual = queue.take(); + + if (!expected.equals(actual)) { + throw new AssertionError( + String.format("expected='%s', actual='%s'", expected, actual)); } - }; - timer.schedule(task, 0); // Immediate - Thread.sleep(500); - if (!(done && passed)) - throw new RuntimeException(done + " : " + passed); - timer.cancel(); + } finally { + timer.cancel(); + } } } -- GitLab