package io.jooby.i2572;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.LinkedHashMap;
import java.util.Map;

import org.json.JSONObject;

import io.jooby.ExecutionMode;
import io.jooby.json.JacksonModule;
import io.jooby.junit.ServerTest;
import io.jooby.junit.ServerTestRunner;

public class Issue2572 {
  @ServerTest(executionMode = {ExecutionMode.EVENT_LOOP, ExecutionMode.WORKER})
  public void onCompleteShouldRunOnCallerThread(ServerTestRunner runner) {
    runner.define(app -> {
      Map<String, Object> state = new LinkedHashMap<>();

      app.install(new JacksonModule());

      app.get("/2572/state", ctx -> state);

      app.decorator(next -> ctx -> {
        state.put("caller", Thread.currentThread().getName());
        ctx.onComplete(context -> {
          state.put("onComplete", Thread.currentThread().getName());
        });
        return next.apply(ctx);
      });

      app.get("/2572/init", ctx -> "Initialized");

    }).ready(http -> {
      http.get("/2572/init", rsp -> {
        assertEquals("Initialized", rsp.body().string());
      });

      http.get("/2572/state", rsp -> {
        JSONObject json = new JSONObject(rsp.body().string());
        assertEquals(json.get("caller"), json.get("onComplete"));
      });
    });
  }
}

@@ -195,16 +195,39 @@
 onMode;%0A
+import io.jooby.Jooby;%0A
 import i
@@ -358,192 +358,56 @@
 2 %7B%0A
+%0A
   
-@ServerTest(executionMode = %7BExecutionMode.EVENT_LOOP, ExecutionMode.WORKER%7D)%0A  public void onCompleteShouldRunOnCallerThread(ServerTestRunner runner) %7B%0A    runner.define(app -%3E %7B%0A  
+private static class App2572 extends Jooby %7B%0A
     
@@ -462,22 +462,24 @@
 );%0A%0A    
-  app.
+%7B%0A      
 install(
@@ -499,36 +499,32 @@
 dule());%0A%0A      
-app.
 get(%22/2572/state
@@ -548,20 +548,16 @@
 %0A%0A      
-app.
 decorato
@@ -808,12 +808,8 @@
     
-app.
 get(
@@ -845,22 +845,209 @@
 ized%22);%0A
-%0A
     %7D
+%0A  %7D%0A%0A  @ServerTest(executionMode = %7BExecutionMode.EVENT_LOOP, ExecutionMode.WORKER%7D)%0A  public void onCompleteShouldRunOnCallerThread(ServerTestRunner runner) %7B%0A    runner.use(App2572::new
 ).ready(

fix random failure caused by GC?