Skip to content
This repository was archived by the owner on Mar 3, 2026. It is now read-only.

Commit dbaac5a

Browse files
committed
use(Jooby) missing onStart and onStop fix jooby-project#421
1 parent 2c541e4 commit dbaac5a

File tree

4 files changed

+126
-14
lines changed

4 files changed

+126
-14
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package org.jooby.issues;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import java.util.concurrent.CountDownLatch;
8+
import java.util.concurrent.Executors;
9+
import java.util.concurrent.ScheduledExecutorService;
10+
import java.util.concurrent.TimeUnit;
11+
12+
import org.jooby.Jooby;
13+
import org.jooby.netty.Netty;
14+
import org.jooby.test.OnServer;
15+
import org.jooby.test.ServerFeature;
16+
import org.junit.AfterClass;
17+
import org.junit.Test;
18+
19+
@OnServer(Netty.class)
20+
public class Issue421 extends ServerFeature {
21+
22+
private static CountDownLatch latch = new CountDownLatch(4);
23+
24+
private static List<String> values = new ArrayList<>();
25+
26+
public static class Foo extends Jooby {
27+
28+
{
29+
onStart(() -> {
30+
values.add("Start:" + getClass().getSimpleName());
31+
latch.countDown();
32+
});
33+
onStop(() -> {
34+
values.add("Stop:" + getClass().getSimpleName());
35+
latch.countDown();
36+
});
37+
38+
map(v -> "foo");
39+
}
40+
}
41+
42+
{
43+
onStart(() -> {
44+
values.add("Before:" + getClass().getSimpleName());
45+
latch.countDown();
46+
});
47+
onStop(() -> {
48+
values.add("StopBefore:" + getClass().getSimpleName());
49+
latch.countDown();
50+
});
51+
52+
use(new Foo());
53+
54+
onStart(() -> {
55+
values.add("After:" + getClass().getSimpleName());
56+
latch.countDown();
57+
});
58+
onStop(() -> {
59+
values.add("StopAfter:" + getClass().getSimpleName());
60+
latch.countDown();
61+
});
62+
63+
ScheduledExecutorService exe = Executors.newSingleThreadScheduledExecutor();
64+
get("/421", () -> {
65+
exe.schedule(this::stop, 500L, TimeUnit.MILLISECONDS);
66+
return "bar";
67+
});
68+
}
69+
70+
@Test
71+
public void shouldImportStartStopCallback() throws Exception {
72+
request()
73+
.get("/421")
74+
.expect("foo");
75+
}
76+
77+
@AfterClass
78+
public static void onStop() throws InterruptedException {
79+
latch.await();
80+
assertEquals(
81+
"[Before:Issue421, Start:Foo, After:Issue421, StopBefore:Issue421, Stop:Foo, StopAfter:Issue421]",
82+
values.toString());
83+
}
84+
85+
}

jooby/src/main/java/org/jooby/Jooby.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -659,11 +659,8 @@ public Jooby(final String prefix) {
659659
}
660660

661661
/**
662-
* Import ALL the direct routes from the given app.
663-
*
664-
* <p>
665-
* PLEASE NOTE: that ONLY routes are imported.
666-
* </p>
662+
* Import content from provide application (routes, parsers/renderers, start/stop callbacks, ...
663+
* etc.).
667664
*
668665
* @param app Routes provider.
669666
* @return This jooby instance.
@@ -673,11 +670,8 @@ public Jooby use(final Jooby app) {
673670
}
674671

675672
/**
676-
* Import ALL the direct routes from the given app, under the given path.
677-
*
678-
* <p>
679-
* PLEASE NOTE: that ONLY routes are imported.
680-
* </p>
673+
* Import content from provide application (routes, parsers/renderers, start/stop callbacks, ...
674+
* etc.). Routes will be mounted at the provided path.
681675
*
682676
* @param path Path to mount the given app.
683677
* @param app Routes provider.
@@ -719,10 +713,18 @@ private Jooby use(final Optional<String> path, final Jooby app) {
719713
Object routes = path.<Object> map(p -> new MvcClass(((MvcClass) it).routeClass, p))
720714
.orElse(it);
721715
this.bag.add(routes);
722-
} else if (it instanceof EnvDep) {
716+
} else {
717+
// everything else
723718
this.bag.add(it);
724719
}
725720
});
721+
// start/stop callback
722+
app.onStart.forEach(this.onStart::add);
723+
app.onStop.forEach(this.onStop::add);
724+
// mapper
725+
if (app.mapper != null) {
726+
this.map(app.mapper);
727+
}
726728
return this;
727729
}
728730

jooby/src/test/java/org/jooby/test/JoobySuite.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package org.jooby.test;
22

33
import java.util.ArrayList;
4+
import java.util.Arrays;
45
import java.util.Collections;
56
import java.util.List;
67

78
import org.junit.runner.Runner;
89
import org.junit.runners.Suite;
910
import org.junit.runners.model.InitializationError;
1011

12+
import com.google.common.base.Predicate;
13+
import com.google.common.base.Predicates;
14+
1115
public class JoobySuite extends Suite {
1216

1317
private List<Runner> runners;
@@ -19,17 +23,26 @@ public class JoobySuite extends Suite {
1923
public JoobySuite(final Class<?> klass) throws InitializationError {
2024
super(klass, Collections.emptyList());
2125

22-
runners = runners();
26+
runners = runners(klass);
2327
}
2428

25-
private List<Runner> runners() throws InitializationError {
29+
@SuppressWarnings("rawtypes")
30+
private List<Runner> runners(final Class<?> klass) throws InitializationError {
2631
List<Runner> runners = new ArrayList<>();
32+
Predicate<Class> filter = Predicates.alwaysTrue();
33+
OnServer onserver = klass.getAnnotation(OnServer.class);
34+
if (onserver != null) {
35+
List<Class<?>> server = Arrays.asList(onserver.value());
36+
filter = server::contains;
37+
}
2738
String[] servers = {"org.jooby.undertow.Undertow", "org.jooby.jetty.Jetty",
2839
"org.jooby.netty.Netty" };
2940
for (String server : servers) {
3041
try {
3142
Class<?> serverClass = getClass().getClassLoader().loadClass(server);
32-
runners.add(new JoobyRunner(getTestClass().getJavaClass(), serverClass));
43+
if (filter.apply(serverClass)) {
44+
runners.add(new JoobyRunner(getTestClass().getJavaClass(), serverClass));
45+
}
3346
} catch (ClassNotFoundException ex) {
3447
// do nothing
3548
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.jooby.test;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Retention(RetentionPolicy.RUNTIME)
9+
@Target(ElementType.TYPE)
10+
public @interface OnServer {
11+
Class<?>[] value();
12+
}

0 commit comments

Comments
 (0)