5151import java .time .OffsetDateTime ;
5252import java .time .ZoneOffset ;
5353import java .util .Arrays ;
54+ import java .util .Collections ;
5455import java .util .List ;
5556import java .util .Map ;
5657import java .util .Optional ;
@@ -252,6 +253,34 @@ public void helloWorld() throws Exception {
252253 ROBOTS_TXT_TEST_CASE ));
253254 }
254255
256+ @ Test
257+ public void timeoutHttpSuccess () throws Exception {
258+ testFunction (
259+ SignatureType .HTTP ,
260+ fullTarget ("TimeoutHttp" ),
261+ ImmutableList .of (),
262+ ImmutableList .of (
263+ TestCase .builder ()
264+ .setExpectedResponseText ("finished\n " )
265+ .setExpectedResponseText (Optional .empty ())
266+ .build ()),
267+ ImmutableMap .of ("CLOUD_RUN_TIMEOUT_SECONDS" , "3" ));
268+ }
269+
270+ @ Test
271+ public void timeoutHttpTimesOut () throws Exception {
272+ testFunction (
273+ SignatureType .HTTP ,
274+ fullTarget ("TimeoutHttp" ),
275+ ImmutableList .of (),
276+ ImmutableList .of (
277+ TestCase .builder ()
278+ .setExpectedResponseCode (408 )
279+ .setExpectedResponseText (Optional .empty ())
280+ .build ()),
281+ ImmutableMap .of ("CLOUD_RUN_TIMEOUT_SECONDS" , "1" ));
282+ }
283+
255284 @ Test
256285 public void exceptionHttp () throws Exception {
257286 String exceptionExpectedOutput =
@@ -290,7 +319,8 @@ public void exceptionBackground() throws Exception {
290319 .setRequestText (gcfRequestText )
291320 .setExpectedResponseCode (500 )
292321 .setExpectedOutput (exceptionExpectedOutput )
293- .build ()));
322+ .build ()),
323+ Collections .emptyMap ());
294324 }
295325
296326 @ Test
@@ -400,7 +430,8 @@ public void typedFunction() throws Exception {
400430 TestCase .builder ()
401431 .setRequestText (originalJson )
402432 .setExpectedResponseText ("{\" fullName\" :\" JohnDoe\" }" )
403- .build ()));
433+ .build ()),
434+ Collections .emptyMap ());
404435 }
405436
406437 @ Test
@@ -410,7 +441,8 @@ public void typedVoidFunction() throws Exception {
410441 fullTarget ("TypedVoid" ),
411442 ImmutableList .of (),
412443 ImmutableList .of (
413- TestCase .builder ().setRequestText ("{}" ).setExpectedResponseCode (204 ).build ()));
444+ TestCase .builder ().setRequestText ("{}" ).setExpectedResponseCode (204 ).build ()),
445+ Collections .emptyMap ());
414446 }
415447
416448 @ Test
@@ -424,7 +456,8 @@ public void typedCustomFormat() throws Exception {
424456 .setRequestText ("abc\n 123\n $#@\n " )
425457 .setExpectedResponseText ("abc123$#@" )
426458 .setExpectedResponseCode (200 )
427- .build ()));
459+ .build ()),
460+ Collections .emptyMap ());
428461 }
429462
430463 private void backgroundTest (String target ) throws Exception {
@@ -595,7 +628,8 @@ public void classpathOptionHttp() throws Exception {
595628 SignatureType .HTTP ,
596629 "com.example.functionjar.Foreground" ,
597630 ImmutableList .of ("--classpath" , functionJarString ()),
598- ImmutableList .of (testCase ));
631+ ImmutableList .of (testCase ),
632+ Collections .emptyMap ());
599633 }
600634
601635 /** Like {@link #classpathOptionHttp} but for background functions. */
@@ -612,7 +646,8 @@ public void classpathOptionBackground() throws Exception {
612646 SignatureType .BACKGROUND ,
613647 "com.example.functionjar.Background" ,
614648 ImmutableList .of ("--classpath" , functionJarString ()),
615- ImmutableList .of (TestCase .builder ().setRequestText (json .toString ()).build ()));
649+ ImmutableList .of (TestCase .builder ().setRequestText (json .toString ()).build ()),
650+ Collections .emptyMap ());
616651 }
617652
618653 /** Like {@link #classpathOptionHttp} but for typed functions. */
@@ -629,7 +664,8 @@ public void classpathOptionTyped() throws Exception {
629664 TestCase .builder ()
630665 .setRequestText (originalJson )
631666 .setExpectedResponseText ("{\" fullName\" :\" JohnDoe\" }" )
632- .build ()));
667+ .build ()),
668+ Collections .emptyMap ());
633669 }
634670
635671 // In these tests, we test a number of different functions that express the same functionality
@@ -643,7 +679,12 @@ private void backgroundTest(
643679 for (TestCase testCase : testCases ) {
644680 File snoopFile = testCase .snoopFile ().get ();
645681 snoopFile .delete ();
646- testFunction (signatureType , functionTarget , ImmutableList .of (), ImmutableList .of (testCase ));
682+ testFunction (
683+ signatureType ,
684+ functionTarget ,
685+ ImmutableList .of (),
686+ ImmutableList .of (testCase ),
687+ Collections .emptyMap ());
647688 String snooped = new String (Files .readAllBytes (snoopFile .toPath ()), StandardCharsets .UTF_8 );
648689 Gson gson = new Gson ();
649690 JsonObject snoopedJson = gson .fromJson (snooped , JsonObject .class );
@@ -667,16 +708,18 @@ private void checkSnoopFile(TestCase testCase) throws IOException {
667708 }
668709
669710 private void testHttpFunction (String target , List <TestCase > testCases ) throws Exception {
670- testFunction (SignatureType .HTTP , target , ImmutableList .of (), testCases );
711+ testFunction (SignatureType .HTTP , target , ImmutableList .of (), testCases , Collections . emptyMap () );
671712 }
672713
673714 private void testFunction (
674715 SignatureType signatureType ,
675716 String target ,
676717 ImmutableList <String > extraArgs ,
677- List <TestCase > testCases )
718+ List <TestCase > testCases ,
719+ Map <String , String > environmentVariables )
678720 throws Exception {
679- ServerProcess serverProcess = startServer (signatureType , target , extraArgs );
721+ ServerProcess serverProcess =
722+ startServer (signatureType , target , extraArgs , environmentVariables );
680723 try {
681724 HttpClient httpClient = new HttpClient ();
682725 httpClient .start ();
@@ -772,7 +815,10 @@ public void close() {
772815 }
773816
774817 private ServerProcess startServer (
775- SignatureType signatureType , String target , ImmutableList <String > extraArgs )
818+ SignatureType signatureType ,
819+ String target ,
820+ ImmutableList <String > extraArgs ,
821+ Map <String , String > environmentVariables )
776822 throws IOException , InterruptedException {
777823 File javaHome = new File (System .getProperty ("java.home" ));
778824 assertThat (javaHome .exists ()).isTrue ();
@@ -798,6 +844,7 @@ private ServerProcess startServer(
798844 "FUNCTION_TARGET" ,
799845 target );
800846 processBuilder .environment ().putAll (environment );
847+ processBuilder .environment ().putAll (environmentVariables );
801848 Process serverProcess = processBuilder .start ();
802849 CountDownLatch ready = new CountDownLatch (1 );
803850 StringBuilder output = new StringBuilder ();
0 commit comments