-
Notifications
You must be signed in to change notification settings - Fork 361
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
interp: add eval cancelation by semaphore
- Loading branch information
Showing
9 changed files
with
479 additions
and
65 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
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,91 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestYaegiCmdCancel(t *testing.T) { | ||
tmp, err := ioutil.TempDir("", "yaegi-") | ||
if err != nil { | ||
t.Fatalf("failed to create tmp directory: %v", err) | ||
} | ||
defer func() { | ||
err = os.RemoveAll(tmp) | ||
if err != nil { | ||
t.Errorf("failed to clean up %v: %v", tmp, err) | ||
} | ||
}() | ||
|
||
yaegi := filepath.Join(tmp, "yaegi") | ||
build := exec.Command("go", "build", "-race", "-o", yaegi, ".") | ||
err = build.Run() | ||
if err != nil { | ||
t.Fatalf("failed to build yaegi command: %v", err) | ||
} | ||
|
||
// Test src must be terminated by a single newline. | ||
tests := []string{ | ||
"for {}\n", | ||
"select {}\n", | ||
} | ||
for _, src := range tests { | ||
cmd := exec.Command(yaegi) | ||
in, err := cmd.StdinPipe() | ||
if err != nil { | ||
t.Errorf("failed to get stdin pipe to yaegi command: %v", err) | ||
} | ||
var outBuf, errBuf bytes.Buffer | ||
cmd.Stdout = &outBuf | ||
cmd.Stderr = &errBuf | ||
|
||
// https://golang.org/doc/articles/race_detector.html#Options | ||
cmd.Env = []string{`GORACE="halt_on_error=1"`} | ||
|
||
err = cmd.Start() | ||
if err != nil { | ||
t.Fatalf("failed to start yaegi command: %v", err) | ||
} | ||
|
||
_, err = in.Write([]byte(src)) | ||
if err != nil { | ||
t.Errorf("failed pipe test source to yaegi command: %v", err) | ||
} | ||
time.Sleep(100 * time.Millisecond) | ||
err = cmd.Process.Signal(os.Interrupt) | ||
if err != nil { | ||
t.Errorf("failed to send os.Interrupt to yaegi command: %v", err) | ||
} | ||
|
||
_, err = in.Write([]byte("1+1\n")) | ||
if err != nil { | ||
t.Errorf("failed to probe race: %v", err) | ||
} | ||
err = in.Close() | ||
if err != nil { | ||
t.Errorf("failed to close stdin pipe: %v", err) | ||
} | ||
|
||
err = cmd.Wait() | ||
if err != nil { | ||
if cmd.ProcessState.ExitCode() == 66 { // See race_detector.html article. | ||
t.Errorf("race detected running yaegi command canceling %q: %v", src, err) | ||
if testing.Verbose() { | ||
t.Log(&errBuf) | ||
} | ||
} else { | ||
t.Errorf("error running yaegi command for %q: %v", src, err) | ||
} | ||
continue | ||
} | ||
|
||
if outBuf.String() != "context canceled\n2\n" { | ||
t.Errorf("unexpected output: %q", &outBuf) | ||
} | ||
} | ||
} |
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
Oops, something went wrong.