From f53452e06d75134a366c54ea52ffc5c795857b97 Mon Sep 17 00:00:00 2001 From: Bruce Date: Sat, 29 Sep 2018 13:00:48 +0800 Subject: [PATCH] play http --- playground/http/get_test.go | 48 +++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 playground/http/get_test.go diff --git a/playground/http/get_test.go b/playground/http/get_test.go new file mode 100644 index 0000000..df65422 --- /dev/null +++ b/playground/http/get_test.go @@ -0,0 +1,48 @@ +package http + +import ( + "fmt" + "io/ioutil" + "log" + "math/rand" + "net/http" + "sync" + "testing" + "time" +) + +func TestGet(t *testing.T) { + counts := 2 + + var wg sync.WaitGroup + wg.Add(counts) + + for i := 0; i < counts; i++ { + go func() { + defer wg.Done() + getValue() + }() + } + wg.Wait() +} + +func getValue() { + rand.Seed(time.Now().UnixNano()) + x := rand.Intn(10) + var url string + if x > 5 { + url = "http://localhost:48082/api/v1/device/5bae2ef4f37ba14693a5e4fc/command/5bae2ef4f37ba14693a5e4eb" + } else { + url = "http://localhost:48082/api/v1/device/5bae2d1bf37ba14693a5e4e9/command/5bae2d05f37ba14693a5e4e2" + } + resp, err := http.Get(url) + if err != nil { + fmt.Println(err) + } + + resBody, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Fatal(err) + } + fmt.Println(string(resBody)) +}