From 8c582e59426fe05fa275ed44bf7d73ab4070b146 Mon Sep 17 00:00:00 2001 From: Chih-Hung Hsieh Date: Wed, 15 Sep 2021 10:58:06 -0700 Subject: compiler_wrapper: Handle TIDY_TIMEOUT for Android clang-tidy * To check in upstream chromeos tools. * In Android mode, for clang-tidy, call runAndroidClangTidy to handle TIDY_TIMEOUT * Add runWithTimeout to env.go and runCmdWithTimeouit to command.go. BUG=b:199451930 TEST=Use a fake Android clang-tidy.real binary, TEST=to dump passed environment variables and run for required time. Change-Id: I629ae372251c034595011ef70559f8b12ed8568c Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/3162668 Reviewed-by: Chih-Hung Hsieh Reviewed-by: Manoj Gupta Tested-by: George Burgess Commit-Queue: George Burgess --- compiler_wrapper/env_test.go | 46 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'compiler_wrapper/env_test.go') 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" -- cgit v1.2.3