-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
171 additions
and
10 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
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,9 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/amyangfei/fpcov/pkg/hello" | ||
) | ||
|
||
func main() { | ||
hello.Goodbye() | ||
} |
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,87 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"os" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/amyangfei/fpcov/pkg/hello" | ||
) | ||
|
||
func test1(t *testing.T) { | ||
var ( | ||
args []string | ||
exit = make(chan int) | ||
waitCh = make(chan interface{}, 1) | ||
) | ||
for _, arg := range os.Args { | ||
switch { | ||
case arg == "DEVEL": | ||
case strings.HasPrefix(arg, "-test."): | ||
default: | ||
args = append(args, arg) | ||
} | ||
} | ||
|
||
oldOsExit := hello.OsExit | ||
defer func() { hello.OsExit = oldOsExit }() | ||
hello.OsExit = func(code int) { | ||
log.Printf("[test] os.Exit with code %d", code) | ||
exit <- code | ||
// sleep here to prevent following code execution in the caller routine | ||
time.Sleep(time.Second * 60) | ||
} | ||
|
||
os.Args = args | ||
go func() { | ||
main() | ||
close(waitCh) | ||
}() | ||
|
||
select { | ||
case <-waitCh: | ||
case <-exit: | ||
} | ||
} | ||
|
||
func test2(t *testing.T) { | ||
var ( | ||
args []string | ||
exit = make(chan int) | ||
) | ||
for _, arg := range os.Args { | ||
switch { | ||
case arg == "DEVEL": | ||
case strings.HasPrefix(arg, "-test."): | ||
default: | ||
args = append(args, arg) | ||
} | ||
} | ||
|
||
oldOsExit := hello.OsExit | ||
defer func() { hello.OsExit = oldOsExit }() | ||
hello.OsExit = func(code int) { | ||
log.Printf("[test] os.Exit with code %d", code) | ||
exit <- code | ||
// sleep here to prevent following code execution in the caller routine | ||
time.Sleep(time.Second * 60) | ||
} | ||
|
||
go func() { | ||
select { | ||
case <-exit: | ||
time.Sleep(time.Millisecond * 100) | ||
os.Exit(0) | ||
} | ||
}() | ||
|
||
os.Args = args | ||
main() | ||
} | ||
|
||
func TestRunMain(t *testing.T) { | ||
test1(t) | ||
// test2(t) | ||
} |
File renamed without changes.
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,15 @@ | ||
package hello | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func Goodbye() { | ||
defer func() { | ||
fmt.Println("do some cleanup") | ||
}() | ||
|
||
fmt.Println("before exit halfway") | ||
OsExit(1) | ||
fmt.Println("after exit halfway") | ||
} |
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 @@ | ||
package hello |
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,14 @@ | ||
package hello | ||
|
||
import ( | ||
"os" | ||
) | ||
|
||
var ( | ||
// OsExit is function placeholder for os.Exit | ||
OsExit func(int) | ||
) | ||
|
||
func init() { | ||
OsExit = os.Exit | ||
} |
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 @@ | ||
package hello |
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,8 @@ | ||
#!/bin/bash | ||
|
||
TEST_DIR="/tmp/failpoint_test" | ||
CUR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) | ||
|
||
mkdir -p $TEST_DIR | ||
|
||
$CUR/../bin/goodbye.test -test.coverprofile="$TEST_DIR/cov.goodbye.$(date +"%s").out" DEVEL >> $TEST_DIR/goodbye.log 2>&1 |