package spark;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.junit.Assert;
import org.junit.Test;

import static spark.Service.ignite;

public class InitExceptionHandlerTest {

    @Test
    public void testInitExceptionHandler() throws Exception {
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        CompletableFuture<String> future = new CompletableFuture<>();
        executorService.submit(() -> {
            Service service1 = ignite().port(1122);
            service1.init();
            nap();
            Service service2 = ignite().port(1122);
            service2.initExceptionHandler((e) -> future.complete("Custom init error"));
            service2.init();
            service1.stop();
            service2.stop();
        });
        Assert.assertEquals("Custom init error", future.get());
    }

    private void nap() {
        try {
            Thread.sleep(100);
        } catch (InterruptedException ignored) {
        }
    }

}

@@ -20,133 +20,55 @@
 ort 
-java.util.concurrent.CompletableFuture;%0Aimport java.util.concurrent.ExecutorService;%0Aimport java.util.concurrent.Executors
+org.junit.AfterClass;%0Aimport org.junit.Assert
 ;%0A
-%0A
 impo
@@ -72,38 +72,43 @@
 mport org.junit.
-Assert
+BeforeClass
 ;%0Aimport org.jun
@@ -203,263 +203,226 @@
     
-@Test%0A    public void 
+priva
 te
+ 
 st
-InitExceptionHandler() throws Exception %7B%0A        ExecutorService executorService = Executors.newSingleThreadExecutor();%0A        CompletableFuture%3CString%3E future = new CompletableFuture%3C%3E();%0A        executorService.submit(() -%3E %7B
+atic Service service1;%0A    private static Service service2;%0A    private static String errorMessage = %22%22;%0A%0A%0A    @BeforeClass%0A    public static void setUpClass() throws Exception %7B%0A%0A        service1 = ignite();
 %0A   
@@ -430,39 +430,16 @@
     
-    Service service1 = ignite()
+service1
 .por
@@ -439,36 +439,32 @@
 ce1.port(1122);%0A
-    
         service1
@@ -484,38 +484,48 @@
     
-    nap();%0A            Service
+service1.awaitInitialization();%0A%0A       
  ser
@@ -540,16 +540,34 @@
 ignite()
+;%0A        service2
 .port(11
@@ -567,28 +567,24 @@
 port(1122);%0A
-    
         serv
@@ -620,24 +620,23 @@
  -%3E 
-future.complete(
+errorMessage = 
 %22Cus
@@ -651,17 +651,16 @@
  error%22)
-)
 ;%0A      
@@ -653,36 +653,32 @@
 rror%22);%0A        
-    
 service2.init();
@@ -690,69 +690,121 @@
     
-    
 service
-1.stop
+2.awaitInitialization
 ();%0A
+%0A
     
+%7D%0A%0A
     
-    service2.stop();%0A        %7D);
+@Test%0A    public void testGetPort_withRandomPort() throws Exception %7B
 %0A   
@@ -853,20 +853,20 @@
 r%22, 
-future.get()
+errorMessage
 );%0A 
@@ -879,132 +879,122 @@
     
-private void nap() %7B%0A        try %7B%0A            Thread.sleep(100);%0A        %7D catch (InterruptedException ignored) %7B%0A        %7D
+@AfterClass%0A    public static void tearDown() throws Exception %7B%0A        service1.stop();%0A        service2.stop();
 %0A   

Rewrite initExceptionTest to be less stupid