Skip to content

Commit ae6fd4f

Browse files
committed
Added slow init tests for Spring and SpringBoot 1.x applications that use the asyn initialization wrapper (#210)
1 parent 4fec449 commit ae6fd4f

File tree

7 files changed

+210
-0
lines changed

7 files changed

+210
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.amazonaws.serverless.proxy.spring;
2+
3+
import com.amazonaws.serverless.exceptions.ContainerInitializationException;
4+
import com.amazonaws.serverless.proxy.internal.testutils.AwsProxyRequestBuilder;
5+
import com.amazonaws.serverless.proxy.internal.testutils.MockLambdaContext;
6+
import com.amazonaws.serverless.proxy.model.AwsProxyRequest;
7+
import com.amazonaws.serverless.proxy.model.AwsProxyResponse;
8+
import com.amazonaws.serverless.proxy.spring.springbootslowapp.SBLambdaHandler;
9+
import com.amazonaws.serverless.proxy.spring.springslowapp.LambdaHandler;
10+
import com.amazonaws.serverless.proxy.spring.springslowapp.MessageController;
11+
import com.amazonaws.serverless.proxy.spring.springslowapp.SlowAppConfig;
12+
import org.junit.Assert;
13+
import org.junit.Test;
14+
15+
import java.time.Instant;
16+
17+
import static org.junit.Assert.*;
18+
19+
public class SlowAppTest {
20+
21+
@Test
22+
public void springSlowApp_continuesInBackgroundThread_returnsCorrect() {
23+
LambdaHandler slowApp = null;
24+
try {
25+
slowApp = new LambdaHandler();
26+
} catch (ContainerInitializationException e) {
27+
e.printStackTrace();
28+
fail("Exception during initialization");
29+
}
30+
System.out.println("Start time: " + slowApp.getConstructorTime());
31+
assertTrue(slowApp.getConstructorTime() < 10_000);
32+
AwsProxyRequest req = new AwsProxyRequestBuilder("/hello", "GET").build();
33+
long startRequestTime = Instant.now().toEpochMilli();
34+
AwsProxyResponse resp = slowApp.handleRequest(req, new MockLambdaContext());
35+
long endRequestTime = Instant.now().toEpochMilli();
36+
assertTrue(endRequestTime - startRequestTime > SlowAppConfig.SlowDownInit.INIT_SLEEP_TIME_MS - 10_000);
37+
assertEquals(200, resp.getStatusCode());
38+
Assert.assertEquals(MessageController.HELLO_MESSAGE, resp.getBody());
39+
}
40+
41+
@Test
42+
public void springBootSlowApp_continuesInBackgroundThread_returnsCorrect() {
43+
SBLambdaHandler slowApp = null;
44+
try {
45+
slowApp = new SBLambdaHandler();
46+
} catch (ContainerInitializationException e) {
47+
e.printStackTrace();
48+
fail("Exception during initialization");
49+
}
50+
System.out.println("Start time: " + slowApp.getConstructorTime());
51+
assertTrue(slowApp.getConstructorTime() < 10_000);
52+
AwsProxyRequest req = new AwsProxyRequestBuilder("/hello", "GET").build();
53+
long startRequestTime = Instant.now().toEpochMilli();
54+
AwsProxyResponse resp = slowApp.handleRequest(req, new MockLambdaContext());
55+
long endRequestTime = Instant.now().toEpochMilli();
56+
assertTrue(endRequestTime - startRequestTime > SlowAppConfig.SlowDownInit.INIT_SLEEP_TIME_MS - 10_000);
57+
assertEquals(200, resp.getStatusCode());
58+
Assert.assertEquals(MessageController.HELLO_MESSAGE, resp.getBody());
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.amazonaws.serverless.proxy.spring.springbootslowapp;
2+
3+
import org.springframework.web.bind.annotation.RequestMapping;
4+
import org.springframework.web.bind.annotation.RequestMethod;
5+
import org.springframework.web.bind.annotation.RestController;
6+
7+
@RestController
8+
public class MessageController {
9+
public static final String HELLO_MESSAGE = "Hello";
10+
11+
@RequestMapping(path="/hello", method= RequestMethod.GET)
12+
public String hello() {
13+
return HELLO_MESSAGE;
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.amazonaws.serverless.proxy.spring.springbootslowapp;
2+
3+
4+
import com.amazonaws.serverless.exceptions.ContainerInitializationException;
5+
import com.amazonaws.serverless.proxy.model.AwsProxyRequest;
6+
import com.amazonaws.serverless.proxy.model.AwsProxyResponse;
7+
import com.amazonaws.serverless.proxy.spring.SpringBootLambdaContainerHandler;
8+
import com.amazonaws.serverless.proxy.spring.SpringBootProxyHandlerBuilder;
9+
import com.amazonaws.services.lambda.runtime.Context;
10+
import com.amazonaws.services.lambda.runtime.RequestHandler;
11+
12+
import java.time.Instant;
13+
14+
15+
public class SBLambdaHandler
16+
implements RequestHandler<AwsProxyRequest, AwsProxyResponse>
17+
{
18+
SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;
19+
private long constructorTime;
20+
21+
public SBLambdaHandler() throws ContainerInitializationException {
22+
long startTime = Instant.now().toEpochMilli();
23+
handler = new SpringBootProxyHandlerBuilder()
24+
.defaultProxy()
25+
.asyncInit(startTime)
26+
.springBootApplication(TestApplication.class)
27+
.buildAndInitialize();
28+
constructorTime = Instant.now().toEpochMilli() - startTime;
29+
}
30+
31+
public long getConstructorTime() {
32+
return constructorTime;
33+
}
34+
35+
public AwsProxyResponse handleRequest(AwsProxyRequest awsProxyRequest, Context context)
36+
{
37+
return handler.proxy(awsProxyRequest, context);
38+
}
39+
}
40+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.amazonaws.serverless.proxy.spring.springbootslowapp;
2+
3+
4+
import org.springframework.beans.factory.InitializingBean;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.boot.web.support.SpringBootServletInitializer;
7+
import org.springframework.context.annotation.ComponentScan;
8+
import org.springframework.context.annotation.PropertySource;
9+
import org.springframework.stereotype.Component;
10+
11+
12+
@SpringBootApplication
13+
@ComponentScan(basePackages = "com.amazonaws.serverless.proxy.spring.springbootslowapp")
14+
@PropertySource("classpath:boot-application.properties")
15+
public class TestApplication extends SpringBootServletInitializer {
16+
@Component
17+
public static class SlowDownInit implements InitializingBean {
18+
public static final int INIT_SLEEP_TIME_MS = 13_000;
19+
20+
@Override
21+
public void afterPropertiesSet() throws Exception {
22+
Thread.sleep(INIT_SLEEP_TIME_MS);
23+
}
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.amazonaws.serverless.proxy.spring.springslowapp;
2+
3+
import com.amazonaws.serverless.exceptions.ContainerInitializationException;
4+
import com.amazonaws.serverless.proxy.model.AwsProxyRequest;
5+
import com.amazonaws.serverless.proxy.model.AwsProxyResponse;
6+
import com.amazonaws.serverless.proxy.spring.SpringLambdaContainerHandler;
7+
import com.amazonaws.serverless.proxy.spring.SpringProxyHandlerBuilder;
8+
import com.amazonaws.services.lambda.runtime.Context;
9+
import com.amazonaws.services.lambda.runtime.RequestHandler;
10+
11+
import java.time.Instant;
12+
13+
public class LambdaHandler implements RequestHandler<AwsProxyRequest, AwsProxyResponse> {
14+
private SpringLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;
15+
private long constructorTime;
16+
17+
public LambdaHandler() throws ContainerInitializationException {
18+
long startTime = Instant.now().toEpochMilli();
19+
handler = new SpringProxyHandlerBuilder()
20+
.defaultProxy()
21+
.asyncInit(startTime)
22+
.configurationClasses(SlowAppConfig.class)
23+
.buildAndInitialize();
24+
constructorTime = Instant.now().toEpochMilli() - startTime;
25+
}
26+
27+
public long getConstructorTime() {
28+
return constructorTime;
29+
}
30+
31+
@Override
32+
public AwsProxyResponse handleRequest(AwsProxyRequest awsProxyRequest, Context context) {
33+
return handler.proxy(awsProxyRequest, context);
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.amazonaws.serverless.proxy.spring.springslowapp;
2+
3+
import org.springframework.web.bind.annotation.RequestMapping;
4+
import org.springframework.web.bind.annotation.RequestMethod;
5+
import org.springframework.web.bind.annotation.RestController;
6+
7+
@RestController
8+
public class MessageController {
9+
public static final String HELLO_MESSAGE = "Hello";
10+
11+
@RequestMapping(path="/hello", method= RequestMethod.GET)
12+
public String hello() {
13+
return HELLO_MESSAGE;
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.amazonaws.serverless.proxy.spring.springslowapp;
2+
3+
import org.springframework.beans.factory.InitializingBean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.context.annotation.Import;
6+
import org.springframework.stereotype.Component;
7+
8+
@Configuration
9+
@Import({MessageController.class})
10+
public class SlowAppConfig {
11+
@Component
12+
public static class SlowDownInit implements InitializingBean {
13+
public static final int INIT_SLEEP_TIME_MS = 13_000;
14+
15+
@Override
16+
public void afterPropertiesSet() throws Exception {
17+
Thread.sleep(INIT_SLEEP_TIME_MS);
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)