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,34 @@ 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 (
277
+ TestCase .builder ()
278
+ .setExpectedResponseCode (408 )
279
+ .setExpectedResponseText (Optional .empty ())
280
+ .build ()),
281
+ ImmutableMap .of ("CLOUD_RUN_TIMEOUT_SECONDS" , "1" ));
282
+ }
283
+
255
284
@ Test
256
285
public void exceptionHttp () throws Exception {
257
286
String exceptionExpectedOutput =
@@ -290,7 +319,8 @@ public void exceptionBackground() throws Exception {
290
319
.setRequestText (gcfRequestText )
291
320
.setExpectedResponseCode (500 )
292
321
.setExpectedOutput (exceptionExpectedOutput )
293
- .build ()));
322
+ .build ()),
323
+ Collections .emptyMap ());
294
324
}
295
325
296
326
@ Test
@@ -400,7 +430,8 @@ public void typedFunction() throws Exception {
400
430
TestCase .builder ()
401
431
.setRequestText (originalJson )
402
432
.setExpectedResponseText ("{\" fullName\" :\" JohnDoe\" }" )
403
- .build ()));
433
+ .build ()),
434
+ Collections .emptyMap ());
404
435
}
405
436
406
437
@ Test
@@ -410,7 +441,8 @@ public void typedVoidFunction() throws Exception {
410
441
fullTarget ("TypedVoid" ),
411
442
ImmutableList .of (),
412
443
ImmutableList .of (
413
- TestCase .builder ().setRequestText ("{}" ).setExpectedResponseCode (204 ).build ()));
444
+ TestCase .builder ().setRequestText ("{}" ).setExpectedResponseCode (204 ).build ()),
445
+ Collections .emptyMap ());
414
446
}
415
447
416
448
@ Test
@@ -424,7 +456,8 @@ public void typedCustomFormat() throws Exception {
424
456
.setRequestText ("abc\n 123\n $#@\n " )
425
457
.setExpectedResponseText ("abc123$#@" )
426
458
.setExpectedResponseCode (200 )
427
- .build ()));
459
+ .build ()),
460
+ Collections .emptyMap ());
428
461
}
429
462
430
463
private void backgroundTest (String target ) throws Exception {
@@ -595,7 +628,8 @@ public void classpathOptionHttp() throws Exception {
595
628
SignatureType .HTTP ,
596
629
"com.example.functionjar.Foreground" ,
597
630
ImmutableList .of ("--classpath" , functionJarString ()),
598
- ImmutableList .of (testCase ));
631
+ ImmutableList .of (testCase ),
632
+ Collections .emptyMap ());
599
633
}
600
634
601
635
/** Like {@link #classpathOptionHttp} but for background functions. */
@@ -612,7 +646,8 @@ public void classpathOptionBackground() throws Exception {
612
646
SignatureType .BACKGROUND ,
613
647
"com.example.functionjar.Background" ,
614
648
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 ());
616
651
}
617
652
618
653
/** Like {@link #classpathOptionHttp} but for typed functions. */
@@ -629,7 +664,8 @@ public void classpathOptionTyped() throws Exception {
629
664
TestCase .builder ()
630
665
.setRequestText (originalJson )
631
666
.setExpectedResponseText ("{\" fullName\" :\" JohnDoe\" }" )
632
- .build ()));
667
+ .build ()),
668
+ Collections .emptyMap ());
633
669
}
634
670
635
671
// In these tests, we test a number of different functions that express the same functionality
@@ -643,7 +679,12 @@ private void backgroundTest(
643
679
for (TestCase testCase : testCases ) {
644
680
File snoopFile = testCase .snoopFile ().get ();
645
681
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 ());
647
688
String snooped = new String (Files .readAllBytes (snoopFile .toPath ()), StandardCharsets .UTF_8 );
648
689
Gson gson = new Gson ();
649
690
JsonObject snoopedJson = gson .fromJson (snooped , JsonObject .class );
@@ -667,16 +708,18 @@ private void checkSnoopFile(TestCase testCase) throws IOException {
667
708
}
668
709
669
710
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 () );
671
712
}
672
713
673
714
private void testFunction (
674
715
SignatureType signatureType ,
675
716
String target ,
676
717
ImmutableList <String > extraArgs ,
677
- List <TestCase > testCases )
718
+ List <TestCase > testCases ,
719
+ Map <String , String > environmentVariables )
678
720
throws Exception {
679
- ServerProcess serverProcess = startServer (signatureType , target , extraArgs );
721
+ ServerProcess serverProcess =
722
+ startServer (signatureType , target , extraArgs , environmentVariables );
680
723
try {
681
724
HttpClient httpClient = new HttpClient ();
682
725
httpClient .start ();
@@ -772,7 +815,10 @@ public void close() {
772
815
}
773
816
774
817
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 )
776
822
throws IOException , InterruptedException {
777
823
File javaHome = new File (System .getProperty ("java.home" ));
778
824
assertThat (javaHome .exists ()).isTrue ();
@@ -798,6 +844,7 @@ private ServerProcess startServer(
798
844
"FUNCTION_TARGET" ,
799
845
target );
800
846
processBuilder .environment ().putAll (environment );
847
+ processBuilder .environment ().putAll (environmentVariables );
801
848
Process serverProcess = processBuilder .start ();
802
849
CountDownLatch ready = new CountDownLatch (1 );
803
850
StringBuilder output = new StringBuilder ();
0 commit comments