Fix cover service listen port issue for #74

This commit is contained in:
memoryliu 2020-08-03 23:33:26 +08:00
parent f1c2d28087
commit 81d69dea54
5 changed files with 153 additions and 17 deletions

View File

@ -153,7 +153,7 @@ func registerHandlers() {
if resp, err := registerSelf(profileAddr); err != nil {
log.Fatalf("register address %v failed, err: %v, response: %v", profileAddr, err, string(resp))
}
go genProfileAddr(host)
mux := http.NewServeMux()
// Coverage reports the current code coverage as a fraction in the range [0, 1].
// 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) {
clearValues()
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w,"clear call successfully")
fmt.Fprintln(w, "clear call successfully")
})
log.Fatal(http.Serve(ln, mux))
@ -252,26 +252,37 @@ func isNetworkError(err error) bool {
}
func listen() (ln net.Listener, host string, err error) {
// 获取上次使用的监听地址
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
}
}
agentPort := "{{.AgentPort }}"
if agentPort != "" {
ln, err = net.Listen("tcp4", agentPort)
if ln, err = net.Listen("tcp4", agentPort); err != nil {
return
}
if host, err = getRealHost(ln); err != nil {
return
}
} else {
ln, err = net.Listen("tcp4", ":0")
}
if err != nil {
return
// 获取上次使用的监听地址
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
}
}
go genProfileAddr(host)
return
}
func getRealHost(ln net.Listener) (host string, err error) {
adds, err := net.InterfaceAddrs()
if err != nil {
return
@ -293,6 +304,7 @@ func listen() (ln net.Listener, host string, err error) {
} else {
host = fmt.Sprintf("%s:%d", localIPV4, ln.Addr().(*net.TCPAddr).Port)
}
return
}

108
tests/agent.bats Executable file
View 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
}

View File

@ -41,4 +41,6 @@ bats -t diff.bats
bats -t cover.bats
bats -t agent.bats
bash <(curl -s https://codecov.io/bash) -f 'filtered*' -F e2e-$GOVERSION

View File

@ -0,0 +1,3 @@
module example.com/simple-agent
go 1.14

View File

@ -0,0 +1,11 @@
package main
import (
"fmt"
"time"
)
func main() {
fmt.Println("hello")
time.Sleep(time.Second * 15)
}