-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ExecuteWithContext allows a user to pass in a context with a timeout, or a cancellation that can be used to terminate the process. Fixes: #10 #9 Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
- Loading branch information
Showing
4 changed files
with
207 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
module github.com/alexellis/go-execute | ||
|
||
go 1.16 | ||
|
||
require golang.org/x/sync v0.0.0-20210220032951-036812b2e83c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ= | ||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package execute | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestExecuteWithContext_SleepInterruptedByTimeout(t *testing.T) { | ||
task := ExecTask{Command: "/bin/sleep 1", Shell: true} | ||
timeout := time.Millisecond * 200 | ||
ctx, cancel := context.WithTimeout(context.Background(), timeout) | ||
defer cancel() | ||
|
||
start := time.Now() | ||
res, err := task.ExecuteWithContext(ctx) | ||
if err != nil { | ||
t.Errorf(err.Error()) | ||
t.Fail() | ||
} | ||
|
||
duration := time.Since(start) | ||
if duration > timeout*2 { | ||
t.Fatalf("Cancellation failed, took %s, max timeout was: %s", duration, timeout*2) | ||
} | ||
|
||
if len(res.Stdout) != 0 { | ||
t.Errorf("want stdout to be empty, but got: %s", res.Stdout) | ||
t.Fail() | ||
} | ||
|
||
if len(res.Stderr) != 0 { | ||
t.Errorf("want empty on stderr, but got: %s", res.Stderr) | ||
t.Fail() | ||
} | ||
} | ||
|
||
func TestExecuteWithContext_SleepWithinTimeout(t *testing.T) { | ||
task := ExecTask{Command: "/bin/sleep 0.1", Shell: true} | ||
timeout := time.Millisecond * 500 | ||
ctx, cancel := context.WithTimeout(context.Background(), timeout) | ||
defer cancel() | ||
|
||
start := time.Now() | ||
res, err := task.ExecuteWithContext(ctx) | ||
if err != nil { | ||
t.Errorf(err.Error()) | ||
t.Fail() | ||
} | ||
|
||
duration := time.Since(start) | ||
if duration > timeout*2 { | ||
t.Fatalf("Cancellation failed, took %s, max timeout was: %s", duration, timeout*2) | ||
} | ||
|
||
if len(res.Stdout) != 0 { | ||
t.Errorf("want stdout to be empty, but got: %s", res.Stdout) | ||
t.Fail() | ||
} | ||
|
||
if len(res.Stderr) != 0 { | ||
t.Errorf("want empty on stderr, but got: %s", res.Stderr) | ||
t.Fail() | ||
} | ||
} |