标签:ISE correct coding imp -o i++ 设计 byte copy
barrier.go
package barrier
import (
"fmt"
"io/ioutil"
"net/http"
"time"
)
var timeoutMillseconds int = 5000
type barrierResp struct {
Err error
Resp string
}
func barrier(endpoints ...string) {
requestNumber := len(endpoints)
in := make(chan barrierResp, requestNumber)
defer close(in)
responses := make([]barrierResp, requestNumber)
for _, endpoint := range endpoints {
go makeRequest(in, endpoint)
}
var hasError bool
for i := 0; i < requestNumber; i++ {
resp := <-in
if resp.Err != nil {
fmt.Println("ERROR: ", resp.Err)
hasError = true
}
responses[i] = resp
}
if !hasError {
for _, resp := range responses {
fmt.Println(resp.Resp)
}
}
}
func makeRequest(out chan<- barrierResp, url string) {
res := barrierResp{}
client := http.Client{
Timeout: time.Duration(time.Duration(timeoutMillseconds) *
time.Microsecond),
}
resp, err := client.Get(url)
if err != nil {
res.Err = err
out <- res
return
}
byt, err := ioutil.ReadAll(resp.Body)
if err != nil {
res.Err = err
out <- res
return
}
res.Resp = string(byt)
out <- res
}
barrier_test.go
package barrier
import (
"bytes"
"io"
"os"
"strings"
"testing"
)
func TestBarrier(t *testing.T) {
t.Run("Correct endpoints", func(t *testing.T) {
endpoints := []string{"https://www.baidu.com/",
"https://www.sina.com.cn/"}
result := captureBarrierOutput(endpoints...)
if !strings.Contains(result, "Accept-Encoding") {
t.Fail()
}
t.Log(result)
})
t.Run("One endpoints incorrect", func(t *testing.T) {
endpoints := []string{"http://malformed-url",
"http://httpbin.org/User-Agent"}
result := captureBarrierOutput(endpoints...)
if !strings.Contains(result, "ERROR") {
t.Fail()
}
t.Log(result)
})
t.Run("Very short timeout", func(t *testing.T) {
endpoints := []string{"http://httpbin.org/headers",
"http://httpbin.org/User-Agent"}
//timeoutMilliseconds := 1
result := captureBarrierOutput(endpoints...)
if !strings.Contains(result, "Timeout") {
t.Fail()
}
t.Log(result)
})
}
func captureBarrierOutput(endpoints ...string) string {
reader, writer, _ := os.Pipe()
os.Stdout = writer
out := make(chan string)
go func() {
var buf bytes.Buffer
io.Copy(&buf, reader)
out <- buf.String()
}()
barrier(endpoints...)
writer.Close()
temp := <-out
return temp
}
标签:ISE correct coding imp -o i++ 设计 byte copy
原文地址:https://www.cnblogs.com/aguncn/p/11921484.html