Fix cover service listen port issue for #74
This commit is contained in:
parent
f1c2d28087
commit
81d69dea54
@ -153,7 +153,7 @@ func registerHandlers() {
|
|||||||
if resp, err := registerSelf(profileAddr); err != nil {
|
if resp, err := registerSelf(profileAddr); err != nil {
|
||||||
log.Fatalf("register address %v failed, err: %v, response: %v", profileAddr, err, string(resp))
|
log.Fatalf("register address %v failed, err: %v, response: %v", profileAddr, err, string(resp))
|
||||||
}
|
}
|
||||||
go genProfileAddr(host)
|
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
// Coverage reports the current code coverage as a fraction in the range [0, 1].
|
// Coverage reports the current code coverage as a fraction in the range [0, 1].
|
||||||
// If coverage is not enabled, Coverage returns 0.
|
// If coverage is not enabled, Coverage returns 0.
|
||||||
@ -206,7 +206,7 @@ func registerHandlers() {
|
|||||||
mux.HandleFunc("/v1/cover/clear", func(w http.ResponseWriter, r *http.Request) {
|
mux.HandleFunc("/v1/cover/clear", func(w http.ResponseWriter, r *http.Request) {
|
||||||
clearValues()
|
clearValues()
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
fmt.Fprintln(w,"clear call successfully")
|
fmt.Fprintln(w, "clear call successfully")
|
||||||
})
|
})
|
||||||
|
|
||||||
log.Fatal(http.Serve(ln, mux))
|
log.Fatal(http.Serve(ln, mux))
|
||||||
@ -252,26 +252,37 @@ func isNetworkError(err error) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func listen() (ln net.Listener, host string, err error) {
|
func listen() (ln net.Listener, host string, err error) {
|
||||||
// 获取上次使用的监听地址
|
agentPort := "{{.AgentPort }}"
|
||||||
if previousAddr := getPreviousAddr(); previousAddr != "" {
|
if agentPort != "" {
|
||||||
ss := strings.Split(previousAddr, ":")
|
if ln, err = net.Listen("tcp4", agentPort); err != nil {
|
||||||
// listen on all network interface
|
return
|
||||||
ln, err = net.Listen("tcp4", ":"+ss[len(ss)-1])
|
}
|
||||||
if err == nil {
|
if host, err = getRealHost(ln); err != nil {
|
||||||
host = previousAddr
|
return
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 获取上次使用的监听地址
|
||||||
|
if previousAddr := getPreviousAddr(); previousAddr != "" {
|
||||||
|
ss := strings.Split(previousAddr, ":")
|
||||||
|
// listen on all network interface
|
||||||
|
ln, err = net.Listen("tcp4", ":"+ss[len(ss)-1])
|
||||||
|
if err == nil {
|
||||||
|
host = previousAddr
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ln, err = net.Listen("tcp4", ":0"); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if host, err = getRealHost(ln); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
agentPort := "{{.AgentPort }}"
|
go genProfileAddr(host)
|
||||||
if agentPort != "" {
|
return
|
||||||
ln, err = net.Listen("tcp4", agentPort)
|
}
|
||||||
} else {
|
|
||||||
ln, err = net.Listen("tcp4", ":0")
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
|
func getRealHost(ln net.Listener) (host string, err error) {
|
||||||
adds, err := net.InterfaceAddrs()
|
adds, err := net.InterfaceAddrs()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@ -293,6 +304,7 @@ func listen() (ln net.Listener, host string, err error) {
|
|||||||
} else {
|
} else {
|
||||||
host = fmt.Sprintf("%s:%d", localIPV4, ln.Addr().(*net.TCPAddr).Port)
|
host = fmt.Sprintf("%s:%d", localIPV4, ln.Addr().(*net.TCPAddr).Port)
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
108
tests/agent.bats
Executable file
108
tests/agent.bats
Executable file
@ -0,0 +1,108 @@
|
|||||||
|
#!/usr/bin/env bats
|
||||||
|
# Copyright 2020 Qiniu Cloud (七牛云)
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
|
||||||
|
load util.sh
|
||||||
|
|
||||||
|
setup_file() {
|
||||||
|
if [ -e samples/simple_agent/register.port ]; then
|
||||||
|
rm samples/simple_agent/register.port
|
||||||
|
fi
|
||||||
|
if [ -e samples/simple_agent/simple-agent_profile_listen_addr ]; then
|
||||||
|
rm samples/simple_agent/simple-agent_profile_listen_addr
|
||||||
|
fi
|
||||||
|
# run centered server
|
||||||
|
goc server > samples/simple_agent/register.port &
|
||||||
|
GOC_PID=$!
|
||||||
|
sleep 2
|
||||||
|
goc init
|
||||||
|
|
||||||
|
WORKDIR=$PWD
|
||||||
|
info "goc server started"
|
||||||
|
}
|
||||||
|
|
||||||
|
teardown_file() {
|
||||||
|
kill -9 $GOC_PID
|
||||||
|
}
|
||||||
|
|
||||||
|
setup() {
|
||||||
|
goc init
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "test cover service listen port" {
|
||||||
|
|
||||||
|
cd samples/simple_agent
|
||||||
|
|
||||||
|
# test1: check cover with agent port
|
||||||
|
goc build --agentport=:7888
|
||||||
|
sleep 2
|
||||||
|
|
||||||
|
./simple-agent 3>&- &
|
||||||
|
SAMPLE_PID=$!
|
||||||
|
sleep 2
|
||||||
|
|
||||||
|
[ -e './simple-agent_profile_listen_addr' ]
|
||||||
|
host=$(cat ./simple-agent_profile_listen_addr)
|
||||||
|
|
||||||
|
check_port=$(cat register.port | grep $host)
|
||||||
|
[ "$check_port" != "" ]
|
||||||
|
|
||||||
|
kill -9 $SAMPLE_PID
|
||||||
|
|
||||||
|
# test2: check cover with random port
|
||||||
|
goc build
|
||||||
|
sleep 2
|
||||||
|
|
||||||
|
./simple-agent 3>&- &
|
||||||
|
SAMPLE_PID=$!
|
||||||
|
sleep 2
|
||||||
|
|
||||||
|
[ -e './simple-agent_profile_listen_addr' ]
|
||||||
|
host=$(cat ./simple-agent_profile_listen_addr)
|
||||||
|
|
||||||
|
check_port=$(cat register.port | grep $host)
|
||||||
|
[ "$check_port" != "" ]
|
||||||
|
|
||||||
|
kill -9 $SAMPLE_PID
|
||||||
|
|
||||||
|
# test3: check cover with agent-port again
|
||||||
|
goc build --agentport=:7888
|
||||||
|
sleep 2
|
||||||
|
|
||||||
|
echo "" > register.port
|
||||||
|
./simple-agent 3>&- &
|
||||||
|
SAMPLE_PID=$!
|
||||||
|
sleep 2
|
||||||
|
|
||||||
|
check_port=$(cat register.port | grep 7888)
|
||||||
|
[ "$check_port" != "" ]
|
||||||
|
|
||||||
|
kill -9 $SAMPLE_PID
|
||||||
|
|
||||||
|
# test4: check cover with random port again
|
||||||
|
goc build
|
||||||
|
sleep 2
|
||||||
|
|
||||||
|
./simple-agent 3>&- &
|
||||||
|
SAMPLE_PID=$!
|
||||||
|
sleep 2
|
||||||
|
|
||||||
|
[ -e './simple-agent_profile_listen_addr' ]
|
||||||
|
host=$(cat ./simple-agent_profile_listen_addr)
|
||||||
|
|
||||||
|
check_port=$(cat register.port | grep $host)
|
||||||
|
[ "$check_port" != "" ]
|
||||||
|
|
||||||
|
kill -9 $SAMPLE_PID
|
||||||
|
}
|
@ -41,4 +41,6 @@ bats -t diff.bats
|
|||||||
|
|
||||||
bats -t cover.bats
|
bats -t cover.bats
|
||||||
|
|
||||||
|
bats -t agent.bats
|
||||||
|
|
||||||
bash <(curl -s https://codecov.io/bash) -f 'filtered*' -F e2e-$GOVERSION
|
bash <(curl -s https://codecov.io/bash) -f 'filtered*' -F e2e-$GOVERSION
|
3
tests/samples/simple_agent/go.mod
Normal file
3
tests/samples/simple_agent/go.mod
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
module example.com/simple-agent
|
||||||
|
|
||||||
|
go 1.14
|
11
tests/samples/simple_agent/main.go
Normal file
11
tests/samples/simple_agent/main.go
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println("hello")
|
||||||
|
time.Sleep(time.Second * 15)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user