51
51
import java .time .OffsetDateTime ;
52
52
import java .time .ZoneOffset ;
53
53
import java .util .Arrays ;
54
+ import java .util .Collections ;
54
55
import java .util .List ;
55
56
import java .util .Map ;
56
57
import java .util .Optional ;
@@ -252,6 +253,30 @@ public void helloWorld() throws Exception {
252
253
ROBOTS_TXT_TEST_CASE ));
253
254
}
254
255
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 (TestCase .builder ().setExpectedResponseCode (408 ).build ()),
277
+ ImmutableMap .of ("CLOUD_RUN_TIMEOUT_SECONDS" , "1" ));
278
+ }
279
+
255
280
@ Test
256
281
public void exceptionHttp () throws Exception {
257
282
String exceptionExpectedOutput =
@@ -290,7 +315,8 @@ public void exceptionBackground() throws Exception {
290
315
.setRequestText (gcfRequestText )
291
316
.setExpectedResponseCode (500 )
292
317
.setExpectedOutput (exceptionExpectedOutput )
293
- .build ()));
318
+ .build ()),
319
+ Collections .emptyMap ());
294
320
}
295
321
296
322
@ Test
@@ -400,7 +426,8 @@ public void typedFunction() throws Exception {
400
426
TestCase .builder ()
401
427
.setRequestText (originalJson )
402
428
.setExpectedResponseText ("{\" fullName\" :\" JohnDoe\" }" )
403
- .build ()));
429
+ .build ()),
430
+ Collections .emptyMap ());
404
431
}
405
432
406
433
@ Test
@@ -410,7 +437,8 @@ public void typedVoidFunction() throws Exception {
410
437
fullTarget ("TypedVoid" ),
411
438
ImmutableList .of (),
412
439
ImmutableList .of (
413
- TestCase .builder ().setRequestText ("{}" ).setExpectedResponseCode (204 ).build ()));
440
+ TestCase .builder ().setRequestText ("{}" ).setExpectedResponseCode (204 ).build ()),
441
+ Collections .emptyMap ());
414
442
}
415
443
416
444
@ Test
@@ -424,7 +452,8 @@ public void typedCustomFormat() throws Exception {
424
452
.setRequestText ("abc\n 123\n $#@\n " )
425
453
.setExpectedResponseText ("abc123$#@" )
426
454
.setExpectedResponseCode (200 )
427
- .build ()));
455
+ .build ()),
456
+ Collections .emptyMap ());
428
457
}
429
458
430
459
private void backgroundTest (String target ) throws Exception {
@@ -595,7 +624,8 @@ public void classpathOptionHttp() throws Exception {
595
624
SignatureType .HTTP ,
596
625
"com.example.functionjar.Foreground" ,
597
626
ImmutableList .of ("--classpath" , functionJarString ()),
598
- ImmutableList .of (testCase ));
627
+ ImmutableList .of (testCase ),
628
+ Collections .emptyMap ());
599
629
}
600
630
601
631
/** Like {@link #classpathOptionHttp} but for background functions. */
@@ -612,7 +642,8 @@ public void classpathOptionBackground() throws Exception {
612
642
SignatureType .BACKGROUND ,
613
643
"com.example.functionjar.Background" ,
614
644
ImmutableList .of ("--classpath" , functionJarString ()),
615
- ImmutableList .of (TestCase .builder ().setRequestText (json .toString ()).build ()));
645
+ ImmutableList .of (TestCase .builder ().setRequestText (json .toString ()).build ()),
646
+ Collections .emptyMap ());
616
647
}
617
648
618
649
/** Like {@link #classpathOptionHttp} but for typed functions. */
@@ -629,7 +660,8 @@ public void classpathOptionTyped() throws Exception {
629
660
TestCase .builder ()
630
661
.setRequestText (originalJson )
631
662
.setExpectedResponseText ("{\" fullName\" :\" JohnDoe\" }" )
632
- .build ()));
663
+ .build ()),
664
+ Collections .emptyMap ());
633
665
}
634
666
635
667
// In these tests, we test a number of different functions that express the same functionality
@@ -643,7 +675,12 @@ private void backgroundTest(
643
675
for (TestCase testCase : testCases ) {
644
676
File snoopFile = testCase .snoopFile ().get ();
645
677
snoopFile .delete ();
646
- testFunction (signatureType , functionTarget , ImmutableList .of (), ImmutableList .of (testCase ));
678
+ testFunction (
679
+ signatureType ,
680
+ functionTarget ,
681
+ ImmutableList .of (),
682
+ ImmutableList .of (testCase ),
683
+ Collections .emptyMap ());
647
684
String snooped = new String (Files .readAllBytes (snoopFile .toPath ()), StandardCharsets .UTF_8 );
648
685
Gson gson = new Gson ();
649
686
JsonObject snoopedJson = gson .fromJson (snooped , JsonObject .class );
@@ -667,16 +704,18 @@ private void checkSnoopFile(TestCase testCase) throws IOException {
667
704
}
668
705
669
706
private void testHttpFunction (String target , List <TestCase > testCases ) throws Exception {
670
- testFunction (SignatureType .HTTP , target , ImmutableList .of (), testCases );
707
+ testFunction (SignatureType .HTTP , target , ImmutableList .of (), testCases , Collections . emptyMap () );
671
708
}
672
709
673
710
private void testFunction (
674
711
SignatureType signatureType ,
675
712
String target ,
676
713
ImmutableList <String > extraArgs ,
677
- List <TestCase > testCases )
714
+ List <TestCase > testCases ,
715
+ Map <String , String > environmentVariables )
678
716
throws Exception {
679
- ServerProcess serverProcess = startServer (signatureType , target , extraArgs );
717
+ ServerProcess serverProcess =
718
+ startServer (signatureType , target , extraArgs , environmentVariables );
680
719
try {
681
720
HttpClient httpClient = new HttpClient ();
682
721
httpClient .start ();
@@ -772,7 +811,10 @@ public void close() {
772
811
}
773
812
774
813
private ServerProcess startServer (
775
- SignatureType signatureType , String target , ImmutableList <String > extraArgs )
814
+ SignatureType signatureType ,
815
+ String target ,
816
+ ImmutableList <String > extraArgs ,
817
+ Map <String , String > environmentVariables )
776
818
throws IOException , InterruptedException {
777
819
File javaHome = new File (System .getProperty ("java.home" ));
778
820
assertThat (javaHome .exists ()).isTrue ();
@@ -798,6 +840,7 @@ private ServerProcess startServer(
798
840
"FUNCTION_TARGET" ,
799
841
target );
800
842
processBuilder .environment ().putAll (environment );
843
+ processBuilder .environment ().putAll (environmentVariables );
801
844
Process serverProcess = processBuilder .start ();
802
845
CountDownLatch ready = new CountDownLatch (1 );
803
846
StringBuilder output = new StringBuilder ();
0 commit comments