File: TestLoggerNames.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (272 lines) | stat: -rw-r--r-- 11,497 bytes parent folder | download | duplicates (16)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
 * Copyright (c) 2015, 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
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Phaser;
import java.util.concurrent.Semaphore;
import java.util.logging.Handler;
import java.util.logging.LogManager;
import java.util.logging.Logger;


/**
 * @test
 * @bug 7113878
 * @summary This is not a test that will check that 7113878 is fixed, but
 *          rather a test that will invoke the modified code & try to verify
 *          that fixing 7113878 has not introduced some big regression.
 *          This test should pass, whether 7113878 is there or not.
 * @run main/othervm TestLoggerNames
 * @author danielfuchs
 */
public class TestLoggerNames {

    static final class TestLogger extends java.util.logging.Logger {

        final Semaphore sem = new Semaphore(0);
        final Semaphore wait = new Semaphore(0);

        public TestLogger(String name, String resourceBundleName) {
            super(name, resourceBundleName);
        }

        @Override
        public Handler[] getHandlers() {
           boolean found = false;
           try {
                System.out.println("Entering "+getName()+" getHandlers()");
                for (StackTraceElement ste : Thread.currentThread().getStackTrace()) {
                    if (LogManager.class.getName().equals(ste.getClassName())
                            && "reset".equals(ste.getMethodName())) {
                        found = true;
                        System.out.println(getName()+" getHandlers() called by " + ste);
                    }
                }
                sem.release();
                try {
                    System.out.println("TestLogger: Acquiring wait for "+getName());
                    wait.acquire();
                    try {
                        System.out.println("TestLogger: Acquired wait for "+getName());
                        return super.getHandlers();
                    } finally {
                        System.out.println("TestLogger: Releasing wait for "+getName());
                        wait.release();
                    }
                } finally {
                    System.out.println("Unblocking "+getName());
                    sem.acquire();
                    System.out.println("Unblocked "+getName());
                    if (found) {
                        System.out.println("Reset will proceed...");
                    }
                }
            } catch (InterruptedException x) {
                throw new IllegalStateException(x);
            }
        }
    }

    static volatile boolean stop;
    static volatile Throwable resetFailed;
    static volatile Throwable checkLoggerNamesFailed;
    static volatile Phaser phaser = new Phaser(2);


    static void checkLoggerNames(List<Logger> loggers) {
        Enumeration<String> names = LogManager.getLogManager().getLoggerNames();
        if (names instanceof Iterator) {
            for (Iterator<?> it = Iterator.class.cast(names); it.hasNext(); ) {
                try {
                    it.remove();
                    throw new RuntimeException("Iterator supports remove!");
                } catch (UnsupportedOperationException x) {
                    System.out.println("OK: Iterator doesn't support remove.");
                }
            }
            // We're not supposed to come here, but if we do then we
            // need to rewind names...
            names = LogManager.getLogManager().getLoggerNames();
        }

        List<String> loggerNames = Collections.list(names);
        if (!loggerNames.contains("")) {
            throw new RuntimeException("\"\"" +
                    " not found in " + loggerNames);
        }
        if (!loggerNames.contains("global")) {
            throw new RuntimeException("global" +
                    " not found in " + loggerNames);
        }
        for (Logger l : loggers) {
            if (!loggerNames.contains(l.getName())) {
                throw new RuntimeException(l.getName() +
                        " not found in " + loggerNames);
            }
        }
        System.out.println("Got all expected logger names");
    }


    public static void main(String[] args) throws InterruptedException {
        TestLogger test = new TestLogger("com.foo.bar.zzz", null);
        LogManager.getLogManager().addLogger(test);
        try {
            Logger.getLogger(null);
            throw new RuntimeException("Logger.getLogger(null) didn't throw expected NPE");
        } catch (NullPointerException x) {
            System.out.println("Got expected NullPointerException for Logger.getLogger(null)");
        }
        List<Logger> loggers = new CopyOnWriteArrayList<>();
        loggers.add(Logger.getLogger("one.two.addMeAChild"));
        loggers.add(Logger.getLogger("aaa.bbb.replaceMe"));
        loggers.add(Logger.getLogger("bbb.aaa.addMeAChild"));
        if (test != Logger.getLogger("com.foo.bar.zzz")) {
            throw new AssertionError("wrong logger returned");
        }
        loggers.add(Logger.getLogger("ddd.aaa.addMeAChild"));

        checkLoggerNames(loggers);

        Thread loggerNamesThread = new Thread(() -> {
            try {
                while (!stop) {
                    // Make a defensive copy of the list of loggers that we pass
                    // to checkLoggerNames - in order to make sure that
                    // we won't see new loggers that might appear after
                    // checkLoggerNames has called LogManager.getLoggerNames().
                    // ('loggers' is a live list and the main thread adds and
                    //  and removes loggers from it concurrently to this thread).
                    checkLoggerNames(new ArrayList<>(loggers));
                    Thread.sleep(10);
                    if (!stop) {
                        phaser.arriveAndAwaitAdvance();
                    }
                }
            } catch (Throwable t) {
                t.printStackTrace(System.err);
                checkLoggerNamesFailed = t;
                // makes sure the main thread isn't going to wait
                // forever waiting for this dead thread to arrive at the
                // phaser.
                phaser.arrive();
            }
        }, "loggerNames");

        Thread resetThread = new Thread(() -> {
            try {
                System.out.println("Calling reset...");
                LogManager.getLogManager().reset();
                System.out.println("Reset done...");
                System.out.println("Reset again...");
                LogManager.getLogManager().reset();
                System.out.println("Reset done...");
            } catch(Throwable t) {
                resetFailed = t;
                System.err.println("Unexpected exception or error in reset Thread");
                t.printStackTrace(System.err);
            }
        }, "reset");

        resetThread.setDaemon(true);
        resetThread.start();

        System.out.println("Waiting for reset to get handlers");
        test.sem.acquire();
        try {
            loggerNamesThread.start();
            System.out.println("Reset has called getHandlers on " + test.getName());
            int i = 0;
            for (Enumeration<String> e = LogManager.getLogManager().getLoggerNames();
                e.hasMoreElements();) {
                String name = e.nextElement();
                if (name.isEmpty()) continue;
                if (name.endsWith(".addMeAChild")) {
                    Logger l =  Logger.getLogger(name+".child");
                    loggers.add(l);
                    System.out.println("*** Added " + l.getName());
                    i++;
                } else if (name.endsWith("replaceMe")) {
                    Logger l = Logger.getLogger(name);
                    loggers.remove(l);
                    l = Logger.getLogger(name.replace("replaceMe", "meReplaced"));
                    loggers.add(l);
                    // Give some chance to the loggerNames thread to arrive at the
                    // phaser first. We don't really care if the logger is actually
                    // replaced this turn - or the next.
                    Thread.sleep(100);
                    System.gc();
                    if (LogManager.getLogManager().getLogger(name) == null) {
                        // The original logger may not have been gc'ed yet, since
                        // it could still be referenced by the list being processed
                        // by the loggerNamesThread, if that thread hasn't arrived
                        // to the phaser yet...
                        System.out.println("*** "+ name
                                + " successfully replaced with " + l.getName());
                    }
                    i++;
                } else {
                    System.out.println("Nothing to do for logger: " + name);
                }
                if (checkLoggerNamesFailed != null) {
                    // The checkLoggerNames thread failed. No need to
                    // continue.
                    break;
                }
                // Waits for the checkLoggerNamesThread to arrive
                phaser.arriveAndAwaitAdvance();
                if (i >= 3 && i++ == 3) {
                    System.out.println("Loggers are now: " +
                            Collections.list(LogManager.getLogManager().getLoggerNames()));
                    test.wait.release();
                    test.sem.release();
                    System.out.println("Joining " + resetThread);
                    resetThread.join();
                }
            }
        } catch (RuntimeException | InterruptedException | Error x) {
            test.wait.release();
            test.sem.release();
            throw x;
        } finally {
            stop = true;
            phaser.arriveAndDeregister();
            loggerNamesThread.join();
            loggers.clear();
        }


        if (resetFailed != null || checkLoggerNamesFailed != null) {
            RuntimeException r = new RuntimeException("Some of the concurrent threads failed");
            if (resetFailed != null) r.addSuppressed(resetFailed);
            if (checkLoggerNamesFailed != null) r.addSuppressed(checkLoggerNamesFailed);
            throw r;
        }

    }

}