This commit is contained in:
lyyyuna 2020-09-11 22:28:53 +08:00
parent c4e12e6d86
commit 81485d7702
4 changed files with 78 additions and 9 deletions

View File

@ -97,10 +97,9 @@ func (c *client) Profile(param ProfileParam) ([]byte, error) {
return nil, fmt.Errorf("use 'service' flag and 'address' flag at the same time may cause ambiguity, please use them separately") return nil, fmt.Errorf("use 'service' flag and 'address' flag at the same time may cause ambiguity, please use them separately")
} }
body, err := json.Marshal(param) // the json.Marshal function can return two types of errors: UnsupportedTypeError or UnsupportedValueError
if err != nil { // so no need to check here
return nil, fmt.Errorf("json.Marshal failed, param: %v, err:%v", param, err) body, _ := json.Marshal(param)
}
res, profile, err := c.do("POST", u, "application/json", bytes.NewReader(body)) res, profile, err := c.do("POST", u, "application/json", bytes.NewReader(body))
if err != nil && isNetworkError(err) { if err != nil && isNetworkError(err) {
@ -119,10 +118,9 @@ func (c *client) Clear(param ProfileParam) ([]byte, error) {
return nil, fmt.Errorf("use 'service' flag and 'address' flag at the same time may cause ambiguity, please use them separately") return nil, fmt.Errorf("use 'service' flag and 'address' flag at the same time may cause ambiguity, please use them separately")
} }
body, err := json.Marshal(param) // the json.Marshal function can return two types of errors: UnsupportedTypeError or UnsupportedValueError
if err != nil { // so no need to check here
return nil, fmt.Errorf("json.Marshal failed, param: %v, err:%v", param, err) body, _ := json.Marshal(param)
}
_, resp, err := c.do("POST", u, "application/json", bytes.NewReader(body)) _, resp, err := c.do("POST", u, "application/json", bytes.NewReader(body))
if err != nil && isNetworkError(err) { if err != nil && isNetworkError(err) {
_, resp, err = c.do("POST", u, "application/json", bytes.NewReader(body)) _, resp, err = c.do("POST", u, "application/json", bytes.NewReader(body))

View File

@ -189,3 +189,16 @@ func TestClientDo(t *testing.T) {
_, _, err := c.do(" ", "http://127.0.0.1:7777", "", nil) // a invalid method _, _, err := c.do(" ", "http://127.0.0.1:7777", "", nil) // a invalid method
assert.Contains(t, err.Error(), "invalid method") assert.Contains(t, err.Error(), "invalid method")
} }
func TestClientClearWithInvalidParam(t *testing.T) {
p := ProfileParam{
Service: []string{"goc"},
Address: []string{"http://127.0.0.1:777"},
}
c := &client{
client: http.DefaultClient,
}
_, err := c.Clear(p)
assert.Error(t, err)
assert.Contains(t, err.Error(), "use 'service' flag and 'address' flag at the same time may cause ambiguity, please use them separately")
}

View File

@ -2,6 +2,7 @@ package cover
import ( import (
"bytes" "bytes"
"encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
@ -187,13 +188,36 @@ func TestClearService(t *testing.T) {
router := GocServer(os.Stdout) router := GocServer(os.Stdout)
// get profile with invalid force parameter // clear profile with non-exist port
w := httptest.NewRecorder() w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/v1/cover/clear", bytes.NewBuffer([]byte(`{}`))) req, _ := http.NewRequest("POST", "/v1/cover/clear", bytes.NewBuffer([]byte(`{}`)))
req.Header.Set("Content-Type", "application/json")
router.ServeHTTP(w, req) router.ServeHTTP(w, req)
assert.Equal(t, http.StatusExpectationFailed, w.Code) assert.Equal(t, http.StatusExpectationFailed, w.Code)
assert.Contains(t, w.Body.String(), "invalid port") assert.Contains(t, w.Body.String(), "invalid port")
// clear profile with invalid service
w = httptest.NewRecorder()
req, _ = http.NewRequest("POST", "/v1/cover/clear", nil)
req.Header.Set("Content-Type", "application/json")
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusExpectationFailed, w.Code)
assert.Contains(t, w.Body.String(), "invalid request")
// clear profile with service and address set at at the same time
p := ProfileParam{
Service: []string{"goc"},
Address: []string{"http://127.0.0.1:3333"},
}
encoded, err := json.Marshal(p)
assert.NoError(t, err)
w = httptest.NewRecorder()
req, _ = http.NewRequest("POST", "/v1/cover/clear", bytes.NewBuffer(encoded))
req.Header.Set("Content-Type", "application/json")
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusExpectationFailed, w.Code)
assert.Contains(t, w.Body.String(), "use 'service' flag and 'address' flag at the same time may cause ambiguity, please use them separately")
} }
func TestInitService(t *testing.T) { func TestInitService(t *testing.T) {

View File

@ -38,6 +38,7 @@ setup_file() {
} }
teardown_file() { teardown_file() {
rm *_profile_listen_addr
kill -9 $GOC_PID kill -9 $GOC_PID
kill -9 $GOCC_PID kill -9 $GOCC_PID
kill -9 $SAMPLE_PID kill -9 $SAMPLE_PID
@ -65,4 +66,37 @@ teardown_file() {
[[ "$output" == *"coverage counter clear call successfully"* ]] [[ "$output" == *"coverage counter clear call successfully"* ]]
wait $profile_pid wait $profile_pid
}
@test "test clear by service name" {
goc build --output=./test-service
./test-service 3>&- &
TEST_SERVICE=$!
sleep 1
# clear by wrong service name
run goc clear --service="test-servicej"
[ "$status" -eq 0 ]
[ "$output" = "" ]
# check by goc profile, as the last step is wrong
# the coverage count should be 1
run goc profile --coverfile="simple-project/a/a.go" --force
info clear3 output: $output
[ "$status" -eq 0 ]
[[ "$output" =~ "example.com/simple-project/a/a.go:4.12,6.2 1 1" ]]
# clear by right service name
run goc clear --service="test-service"
[ "$status" -eq 0 ]
[[ "$output" =~ "coverage counter clear call successfully" ]]
# check by goc profile, the coverage count should be reset to 0
run goc profile --coverfile="simple-project/a/a.go" --force
info clear4 output: $output
[ "$status" -eq 0 ]
[[ "$output" =~ "example.com/simple-project/a/a.go:4.12,6.2 1 0" ]]
kill -9 $TEST_SERVICE
} }