diff options
author | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2022-05-10 07:10:09 +0000 |
---|---|---|
committer | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2022-05-10 07:10:09 +0000 |
commit | 836cde1fd7aa7f58f421ea96f2c1e2e318c09356 (patch) | |
tree | b0522edde1d3c5356c95eb1ee2eae3e87befa1f3 /compiler_wrapper/env_test.go | |
parent | f3bf46bb50d5113aa736e03885bb2d80e0e9e290 (diff) | |
parent | 882a18888febb9cb0b9d6c6069498cbc4aa30f88 (diff) | |
download | toolchain-utils-836cde1fd7aa7f58f421ea96f2c1e2e318c09356.tar.gz |
Snap for 8564071 from 882a18888febb9cb0b9d6c6069498cbc4aa30f88 to mainline-conscrypt-releaseaml_con_331413000aml_con_331411000aml_con_331312000aml_con_331115000aml_con_331011010android13-mainline-conscrypt-release
Change-Id: I117939d2cb57e282403503297a15d6317ddfba34
Diffstat (limited to 'compiler_wrapper/env_test.go')
-rw-r--r-- | compiler_wrapper/env_test.go | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/compiler_wrapper/env_test.go b/compiler_wrapper/env_test.go index e03d60a8..b5bf65a3 100644 --- a/compiler_wrapper/env_test.go +++ b/compiler_wrapper/env_test.go @@ -6,13 +6,17 @@ package main import ( "bytes" + "context" + "errors" "flag" "io/ioutil" "os" "os/exec" + "path" "path/filepath" "strings" "testing" + "time" ) // Attention: The tests in this file execute the test binary again with the `-run` flag. @@ -164,6 +168,48 @@ func TestProcessEnvRunCmdDeleteEnv(t *testing.T) { }) } +func TestRunWithTimeoutRunsTheGivenProcess(t *testing.T) { + withTestContext(t, func(ctx *testContext) { + env, err := newProcessEnv() + if err != nil { + t.Fatalf("Unexpected error making new process env: %v", err) + } + + tempFile := path.Join(ctx.tempDir, "some_file") + cmd := &command{ + Path: "touch", + Args: []string{tempFile}, + } + if err := env.runWithTimeout(cmd, time.Second*120); err != nil { + t.Fatalf("Unexpected error touch'ing %q: %v", tempFile, err) + } + + // This should be fine, since `touch` should've created the file. + if _, err := os.Stat(tempFile); err != nil { + t.Errorf("Stat'ing temp file at %q failed: %v", tempFile, err) + } + }) +} + +func TestRunWithTimeoutReturnsErrorOnTimeout(t *testing.T) { + withTestContext(t, func(ctx *testContext) { + env, err := newProcessEnv() + if err != nil { + t.Fatalf("Unexpected error making new process env: %v", err) + } + + cmd := &command{ + Path: "sleep", + Args: []string{"30"}, + } + + err = env.runWithTimeout(cmd, 100*time.Millisecond) + if !errors.Is(err, context.DeadlineExceeded) { + t.Errorf("Expected context.DeadlineExceeded after `sleep` timed out; got error: %v", err) + } + }) +} + func TestNewProcessEnvResolvesPwdAwayProperly(t *testing.T) { // This test cannot be t.Parallel(), since it modifies our environment. const envPwd = "PWD" |