/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 */
package groovy.bugs;

import groovy.lang.GroovyClassLoader;
import groovy.lang.Script;
import org.codehaus.groovy.runtime.ResourceGroovyMethods;

import java.io.File;

public class Groovy2365Bug extends Groovy2365Base {

    public void testDeadlock () {
        String path = createData();

        try {
            for (int i = 0; i != 100; ++i ) {
                    final GroovyClassLoader groovyLoader = new GroovyClassLoader ();
                    groovyLoader.addClasspath(path);

                    Class _script1Class = null;
                    try {
                        _script1Class = groovyLoader.loadClass("Script1", true, true);
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                    final Class script1Class = _script1Class;

                    // setup two threads to try a deadlock

                    // thread one: newInstance script foo
                    final boolean completed [] = new boolean[2] ;
                    Thread thread1 = new Thread(() -> {
                        try {
                            Script script = (Script) script1Class.getDeclaredConstructor().newInstance();
                            script.run();
                            completed [0] = true;
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    });

                    Thread thread2 = new Thread(() -> {
                        try {
                            Class cls = groovyLoader.loadClass("Script2", true, true);
                            Script script = (Script) cls.getDeclaredConstructor().newInstance();
                            script.run();
                            completed [1] = true;
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    });

                    // let's see if we get a deadlock
                    thread2.start();
                    thread1.start();

                try {
                    thread1.join(5000);
                    thread2.join(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                assertTrue("Potentially deadlock", completed[0] && completed[1]);
            }
        } finally {
            ResourceGroovyMethods.deleteDir(new File(path));
        }
    }
}

@@ -1064,17 +1064,16 @@
 Deadlock
- 
 () %7B%0A   
@@ -1165,17 +1165,12 @@
  ++i
- 
 ) %7B%0A
-    
     
@@ -1245,17 +1245,12 @@
 ader
- 
 ();%0A
-    
     
@@ -1307,28 +1307,24 @@
             
-    
 Class _scrip
@@ -1347,36 +1347,32 @@
                 
-    
 try %7B%0A          
@@ -1381,20 +1381,16 @@
         
- 
-   
 _script1
@@ -1452,36 +1452,32 @@
                 
-    
 %7D catch (ClassNo
@@ -1509,36 +1509,32 @@
                 
-  
-  
 e.printStackTrac
@@ -1550,33 +1550,26 @@
             
-    %7D%0A   
+%7D%0A
             
@@ -1568,25 +1568,24 @@
             
- 
 final Class 
@@ -1615,20 +1615,16 @@
 Class;%0A%0A
-    
         
@@ -1687,20 +1687,16 @@
         
-    
 // threa
@@ -1737,28 +1737,24 @@
             
- 
-   
 final boolea
@@ -1764,17 +1764,16 @@
 ompleted
- 
 %5B%5D = new
@@ -1787,15 +1787,10 @@
 n%5B2%5D
- 
 ;%0A
-    
     
@@ -1853,38 +1853,30 @@
             
-    
 try %7B%0A
-    
             
@@ -1981,36 +1981,32 @@
                 
-    
 script.run();%0A  
@@ -2019,36 +2019,32 @@
                 
-    
 completed %5B0%5D = 
@@ -2036,17 +2036,16 @@
 ompleted
- 
 %5B0%5D = tr
@@ -2060,36 +2060,32 @@
                 
- 
-   
 %7D catch (Excepti
@@ -2108,36 +2108,32 @@
                 
-    
 e.printStackTrac
@@ -2153,34 +2153,26 @@
             
-   
- 
 %7D%0A
-    
             
@@ -2172,36 +2172,32 @@
            %7D);%0A%0A
-    
                 
@@ -2248,38 +2248,30 @@
             
-    
 try %7B%0A
-    
             
@@ -2357,36 +2357,32 @@
                 
- 
-   
 Script script = 
@@ -2450,36 +2450,32 @@
                 
-    
 script.run();%0A  
@@ -2488,36 +2488,32 @@
                 
-  
-  
 completed %5B1%5D = 
@@ -2505,17 +2505,16 @@
 ompleted
- 
 %5B1%5D = tr
@@ -2509,36 +2509,32 @@
 eted%5B1%5D = true;%0A
-    
                 
@@ -2577,36 +2577,32 @@
                 
-   
- 
 e.printStackTrac
@@ -2622,34 +2622,26 @@
             
-    %7D%0A    
+%7D%0A
             
@@ -2665,20 +2665,16 @@
         
-    
 // let's
@@ -2707,36 +2707,32 @@
                 
-    
 thread2.start();
@@ -2740,36 +2740,32 @@
                 
- 
-   
 thread1.start();
@@ -2817,25 +2817,26 @@
 hread1.join(
-5
+10
 000);%0A      
@@ -2862,17 +2862,18 @@
 d2.join(
-5
+10
 000);%0A  

make test more resilient to failure under high load (plus formatting)