Skip to content

Commit

Permalink
Merge a80f0bf into da20216
Browse files Browse the repository at this point in the history
  • Loading branch information
amyangfei committed May 8, 2019
2 parents da20216 + a80f0bf commit ec8f268
Show file tree
Hide file tree
Showing 11 changed files with 171 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -8,5 +8,5 @@ env:
go_import_path: github.com/amyangfei/fpcov

script:
- make unit_test
- make test
- env GL_TRAVIS_CI=on make coverage
24 changes: 20 additions & 4 deletions Makefile
Expand Up @@ -8,19 +8,32 @@ FAILPOINT_ENABLE := $$(echo $(FAILPOINT_DIR) | xargs $(FAILPOINT) enable >/dev/
FAILPOINT_DISABLE := $$(find $(FAILPOINT_DIR) | xargs $(FAILPOINT) disable >/dev/null)
TEST_DIR := /tmp/failpoint_test

.PHONY: unit_test coverage build
.PHONY: test unit_test coverage build integration_test_build

build:
$(GOBUILD) -o bin/main ./cmd
$(GOBUILD) -o bin/hello ./cmd/hello
$(GOBUILD) -o bin/goodbye ./cmd/goodbye

unit_test:
which $(FAILPOINT) >/dev/null 2>&1 || $(GOBUILD) -o $(FAILPOINT) github.com/pingcap/failpoint/failpoint-ctl
integration_test_build: install-failpoint
$(FAILPOINT_ENABLE)
$(GOTEST) -c -race -cover -covermode=atomic \
-coverpkg=github.com/amyangfei/fpcov/... \
-o bin/goodbye.test github.com/amyangfei/fpcov/cmd/goodbye \
|| { $(FAILPOINT_DISABLE); exit 1; }
$(FAILPOINT_DISABLE)

integration_test: integration_test_build
tests/run.sh

unit_test: install-failpoint
mkdir -p $(TEST_DIR)
$(FAILPOINT_ENABLE)
$(GOTEST) -covermode=atomic -coverprofile="$(TEST_DIR)/cov.unit_test.out" $(PACKAGES) \
|| { $(FAILPOINT_DISABLE); exit 1; }
$(FAILPOINT_DISABLE)

test: unit_test integration_test

coverage:
GO111MODULE=off go get github.com/zhouqiang-cl/gocovmerge
gocovmerge "$(TEST_DIR)"/cov.* | grep -vE ".*.pb.go|.*.__failpoint_binding__.go" > "$(TEST_DIR)/all_cov.out"
Expand All @@ -31,6 +44,9 @@ else
go tool cover -html "$(TEST_DIR)/all_cov.out" -o "$(TEST_DIR)/all_cov.html"
endif

install-failpoint:
which $(FAILPOINT) >/dev/null 2>&1 || $(GOBUILD) -o $(FAILPOINT) github.com/pingcap/failpoint/failpoint-ctl

failpoint-enable:
$(FAILPOINT_ENABLE)

Expand Down
9 changes: 9 additions & 0 deletions cmd/goodbye/main.go
@@ -0,0 +1,9 @@
package main

import (
"github.com/amyangfei/fpcov/pkg/hello"
)

func main() {
hello.Goodbye()
}
87 changes: 87 additions & 0 deletions cmd/goodbye/main_test.go
@@ -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.
15 changes: 15 additions & 0 deletions pkg/hello/goodbye.go
@@ -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")
}
1 change: 1 addition & 0 deletions pkg/hello/goodbye_test.go
@@ -0,0 +1 @@
package hello
20 changes: 15 additions & 5 deletions pkg/hello/hello.go
Expand Up @@ -72,11 +72,13 @@ func Boundary() {
ctx, cancel := context.WithCancel(context.Background())

go func() {
failpoint.Inject("BoundaryEnable", func() {
fmt.Println("before sleep 3")
time.Sleep(time.Second * 300)
fmt.Println("after long sleep 3")
})
fmt.Println("before never return function")
func() {
for {
time.Sleep(time.Second)
}
}()
fmt.Println("after never return function")
}()

go func() {
Expand All @@ -85,6 +87,14 @@ func Boundary() {
fmt.Println("after long sleep 1")
}()

go func() {
failpoint.Inject("BoundaryEnable", func() {
fmt.Println("before sleep 3")
time.Sleep(time.Second * 300)
fmt.Println("after long sleep 3")
})
}()

wg.Add(1)
go func(cctx context.Context) {
defer wg.Done()
Expand Down
14 changes: 14 additions & 0 deletions pkg/hello/utils.go
@@ -0,0 +1,14 @@
package hello

import (
"os"
)

var (
// OsExit is function placeholder for os.Exit
OsExit func(int)
)

func init() {
OsExit = os.Exit
}
1 change: 1 addition & 0 deletions pkg/hello/utils_test.go
@@ -0,0 +1 @@
package hello
8 changes: 8 additions & 0 deletions tests/run.sh
@@ -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

0 comments on commit ec8f268

Please sign in to comment.