Skip to content

Commit bdd6c07

Browse files
juherrkilmajster
authored andcommitted
Add event for Ngrok initialization
1 parent 4556e06 commit bdd6c07

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package ngrok;
2+
3+
import ngrok.api.model.NgrokTunnel;
4+
import org.springframework.context.ApplicationEvent;
5+
6+
import java.util.List;
7+
8+
public class NgrokInitializedEvent extends ApplicationEvent {
9+
10+
private final List<NgrokTunnel> tunnels;
11+
12+
public NgrokInitializedEvent(Object source, List<NgrokTunnel> tunnels) {
13+
super(source);
14+
this.tunnels = tunnels;
15+
}
16+
public List<NgrokTunnel> getTunnels() {
17+
return tunnels;
18+
}
19+
}

src/main/java/ngrok/NgrokRunner.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
import org.apache.commons.lang3.StringUtils;
1313
import org.slf4j.Logger;
1414
import org.slf4j.LoggerFactory;
15+
import org.springframework.beans.factory.annotation.Autowired;
1516
import org.springframework.beans.factory.annotation.Value;
1617
import org.springframework.boot.web.context.WebServerInitializedEvent;
18+
import org.springframework.context.ApplicationEventPublisher;
1719
import org.springframework.context.event.EventListener;
1820
import org.springframework.core.task.TaskExecutor;
1921

@@ -29,6 +31,9 @@ public class NgrokRunner {
2931
@Value("${" + NgrokProperties.NGROK_COMMAND + ":}")
3032
private String ngrokCustomCommand;
3133

34+
@Autowired
35+
private ApplicationEventPublisher applicationEventPublisher;
36+
3237
private final NgrokApiClient ngrokApiClient;
3338
private final NgrokBinaryProvider ngrokBinaryProvider;
3439
private final NgrokConfigurationProvider ngrokConfigurationProvider;
@@ -64,6 +69,7 @@ public void run(WebServerInitializedEvent event) throws NgrokDownloadException,
6469
log.info("Ngrok was already running! Dashboard url -> [ {} ]", ngrokApiClient.getNgrokApiUrl());
6570
}
6671
logTunnelsDetails();
72+
applicationEventPublisher.publishEvent(new NgrokInitializedEvent(this, ngrokApiClient.fetchTunnels()));
6773
});
6874
}
6975

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package ngrok.runner;
2+
3+
import ngrok.NgrokInitializedEvent;
4+
import ngrok.runner.config.NgrokRunnerUnixTestConfiguration;
5+
import org.junit.jupiter.api.Test;
6+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
7+
import org.springframework.boot.test.context.SpringBootTest;
8+
import org.springframework.boot.test.context.TestConfiguration;
9+
import org.springframework.context.ApplicationListener;
10+
import org.springframework.stereotype.Component;
11+
import org.springframework.test.context.ActiveProfiles;
12+
13+
import static ngrok.TestConstants.*;
14+
import static org.assertj.core.api.Assertions.assertThat;
15+
16+
@ActiveProfiles({
17+
TEST_NGROK_PROFILE,
18+
TEST_NGROK_PROFILE_UNIX
19+
})
20+
@EnableAutoConfiguration
21+
@SpringBootTest(
22+
webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT,
23+
classes = NgrokRunnerUnixTestConfiguration.class,
24+
properties = {
25+
TEST_NGROK_PROP_ENABLED,
26+
TEST_NGROK_PROP_CUSTOM_COMMAND,
27+
TEST_SPRING_PROP_SERVER_PORT
28+
})
29+
public class NgrokRunnerEventTest extends BaseNgrokRunnerIntegrationTest {
30+
31+
@TestConfiguration
32+
public static class Config {
33+
34+
private static NgrokInitializedEvent event;
35+
36+
@Component
37+
public static class CustomSpringEventListener implements ApplicationListener<NgrokInitializedEvent> {
38+
@Override
39+
public void onApplicationEvent(NgrokInitializedEvent event) {
40+
Config.event = event;
41+
}
42+
}
43+
}
44+
45+
@Test
46+
public void shouldReceiveEvent() {
47+
assertThat(Config.event).isNotNull();
48+
assertThat(Config.event.getTunnels()).hasSize(2);
49+
}
50+
}

0 commit comments

Comments
 (0)