1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @bug 8204142
  27  * @key headful
  28  * @summary Deadlock when queueing SequencedEvent of different AppContexts
  29  * @author Laurent Bourges
  30  * @modules java.desktop/sun.awt
  31  * @run main/othervm/timeout=30 MultipleContextsFunctionalTest
  32  */
  33 
  34 import java.awt.BorderLayout;
  35 import java.awt.Dimension;
  36 import java.awt.event.ActionEvent;
  37 import java.awt.event.ActionListener;
  38 import java.util.ArrayList;
  39 import java.util.List;
  40 import java.util.concurrent.atomic.AtomicReference;
  41 import javax.swing.JButton;
  42 import javax.swing.JFrame;
  43 import javax.swing.JLabel;
  44 import javax.swing.SwingUtilities;
  45 import javax.swing.Timer;
  46 
  47 public final class MultipleContextsFunctionalTest {
  48 
  49     private static final long serialVersionUID = 1L;
  50 
  51     private static final int NUM_WINDOW = 2;
  52     private static final int INTERVAL = 50;
  53     private static final int MAX_TIME = 10000; // 10s
  54     private static final int TOLERANCE = 10000;// 10s
  55     private static final int CHECK_LAPSE = 100;
  56     private static final int MAX_COUNT = MAX_TIME / INTERVAL;
  57     private static final int EXPECTED = MAX_COUNT * NUM_WINDOW;
  58     private static final List<TestWindow> WINDOWS = new ArrayList<TestWindow>();
  59 
  60     public static void main(String[] args) {
  61         for (int i = 0; i < NUM_WINDOW; i++) {
  62             createWin(i);
  63         }
  64 
  65         int total = 0;
  66         int waitingTime = MAX_TIME + TOLERANCE;
  67         while (waitingTime > 0 && total != EXPECTED) {
  68             try {
  69                 Thread.sleep(CHECK_LAPSE);
  70             } catch (InterruptedException e) {
  71                 e.printStackTrace();
  72             }
  73             waitingTime -= CHECK_LAPSE;
  74 
  75             total = 0;
  76             for (TestWindow window : WINDOWS) {
  77                 total += window.getCounter();
  78             }
  79         }
  80 
  81         // Failure if AWT hanging: assert
  82         System.out.println("Total [" + total + "] - Expected [" + EXPECTED + "]");
  83         if (total == EXPECTED) {
  84             System.out.println("Test PASSED");
  85             return;
  86         }
  87         System.out.println("Test FAILED");
  88         Runtime.getRuntime().halt(-1);
  89     }
  90 
  91     private static void createWin(int tgNum) {
  92         new Thread(new ThreadGroup("TG " + tgNum),
  93                 new Runnable() {
  94             @Override
  95             public void run() {
  96                 sun.awt.SunToolkit.createNewAppContext();
  97 
  98                 final AtomicReference<TestWindow> ref =
  99                         new AtomicReference<TestWindow>();
 100 
 101                 SwingUtilities.invokeLater(new Runnable() {
 102                     @Override
 103                     public void run() {
 104                         final TestWindow window = new TestWindow(tgNum);
 105                         window.setVisible(true);
 106                         ref.set(window);
 107                         WINDOWS.add(window);
 108                     }
 109                 });
 110 
 111                 // Wait for window to show
 112                 TestWindow window = ref.get();
 113                 while (window == null) {
 114                     try {
 115                         Thread.sleep(100);
 116                     } catch (InterruptedException ie) {
 117                         ie.printStackTrace();
 118                     }
 119                     window = ref.get();
 120                 }
 121                 window.enableTimer(true);
 122             }
 123         }).start();
 124     }
 125 
 126     private static final class TestWindow extends JFrame implements ActionListener {
 127 
 128         private final JButton btn;
 129         private int counter = 0;
 130         private final Timer t;
 131 
 132         TestWindow(final int num) {
 133             super("Test Window [" + num + "]");
 134             setMinimumSize(new Dimension(300, 200));
 135             setLocation(100 + 400 * (num - 1), 100);
 136 
 137             setLayout(new BorderLayout());
 138             JLabel textBlock = new JLabel("Lorem ipsum dolor sit amet...");
 139             add(textBlock);
 140 
 141             btn = new JButton("TEST");
 142             add(btn, BorderLayout.SOUTH);
 143 
 144             setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 145             pack();
 146 
 147             t = new Timer(INTERVAL, this);
 148             t.setRepeats(false);
 149         }
 150 
 151         @Override
 152         public void actionPerformed(ActionEvent e) {
 153             this.toFront();
 154             btn.setText("TEST " + (++counter));
 155             this.toBack();
 156             if (counter < MAX_COUNT) {
 157                 enableTimer(true);
 158             } else {
 159                 dispose();
 160             }
 161         }
 162 
 163         void enableTimer(boolean enable) {
 164             if (enable) {
 165                 t.start();
 166             } else {
 167                 t.stop();
 168             }
 169         }
 170 
 171         int getCounter() {
 172             return counter;
 173         }
 174     }
 175 }