Skip to content

Commit

Permalink
ARTICLES: 增加how_to_test
Browse files Browse the repository at this point in the history
  • Loading branch information
xpzouying authored and yangwenmai committed Dec 25, 2018
1 parent 6879839 commit 9817c2e
Show file tree
Hide file tree
Showing 9 changed files with 972 additions and 0 deletions.
784 changes: 784 additions & 0 deletions content/articles/how_to_test/README.md

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 37 additions & 0 deletions content/articles/how_to_test/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package main

import (
"fmt"
"net/http"
"sync"

"github.com/sirupsen/logrus"
)

var counter = map[string]int{}
var mu sync.Mutex // mutex for counter

func handleHello(w http.ResponseWriter, r *http.Request) {
name := r.FormValue("name")
mu.Lock()
counter[name]++
cnt := counter[name]
mu.Unlock()

w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write([]byte("<h1 style='color: " + r.FormValue("color") +
"'>Welcome!</h1> <p>Name: " + name + "</p> <p>Count: " + fmt.Sprint(cnt) + "</p>"))

logrus.WithFields(logrus.Fields{
"module": "main",
"name": name,
"count": cnt,
}).Infof("visited")
}

func main() {
logrus.SetFormatter(&logrus.JSONFormatter{})

http.HandleFunc("/hello", handleHello)
logrus.Fatal(http.ListenAndServe(":8080", nil))
}
151 changes: 151 additions & 0 deletions content/articles/how_to_test/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package main

import (
"net/http"
"net/http/httptest"
"sync"
"testing"

"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)

func TestHelloHandleFunc(t *testing.T) {
rw := httptest.NewRecorder()
name := "zouying"
req := httptest.NewRequest(http.MethodPost, "/hello?name="+name, nil)
handleHello(rw, req)

if rw.Code != http.StatusOK {
t.Errorf("status code not ok, status code is %v", rw.Code)
}

if len(counter) != 1 {
t.Errorf("counter len not correct")
}

if counter[name] != 1 {
t.Errorf("counter value is error: visitor=%s count=%v", name, counter[name])
}
}

func TestHTTPServer(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(handleHello))
defer ts.Close()

logrus.Infof("server url: %s", ts.URL)

testURL := ts.URL + "/hello?name=zouying"
resp, err := http.Get(testURL)
if err != nil {
t.Error(err)
return
}
if g, w := resp.StatusCode, http.StatusOK; g != w {
t.Errorf("status code = %q; want %q", g, w)
return
}
}

func TestHelloHandlerMultiple(t *testing.T) {

tests := []struct {
name string
wCnt int
}{
{name: "zouying", wCnt: 1},
{name: "zouying", wCnt: 2},
{name: "user2", wCnt: 1},
{name: "user3", wCnt: 1},
}

for _, tc := range tests {
rw := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/hello?name="+tc.name, nil)
handleHello(rw, req)

if rw.Code != http.StatusOK {
t.Errorf("status code not ok, status code is %v", rw.Code)
}

if counter[tc.name] != tc.wCnt {
t.Errorf("counter value is error: visitor=%s count=%v", tc.name, counter[tc.name])
}
}
}

func TestHelloHandlerMultipleWithAssert(t *testing.T) {

tests := []struct {
name string
wCnt int
}{
{name: "zouying", wCnt: 1},
{name: "zouying", wCnt: 2},
{name: "user2", wCnt: 1},
{name: "user3", wCnt: 1},
}

for _, tc := range tests {
rw := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/hello?name="+tc.name, nil)
handleHello(rw, req)

assert.Equal(t, http.StatusOK, rw.Code)
assert.Equal(t, tc.wCnt, counter[tc.name])
}
}

func TestHelloHandlerInSubtest(t *testing.T) {

tests := []struct {
name string
wCnt int
}{
{name: "zouying", wCnt: 1},
{name: "user2", wCnt: 1},
{name: "user3", wCnt: 1},
}

for _, tc := range tests {
t.Run("test-"+tc.name, func(t *testing.T) {
rw := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/hello?name="+tc.name, nil)
handleHello(rw, req)

assert.Equal(t, http.StatusOK, rw.Code)
assert.Equal(t, tc.wCnt, counter[tc.name])
})
}
}

func TestHelloHandlerDetectDataRace(t *testing.T) {

tests := []struct {
name string
wCnt int
}{
{name: "zouying", wCnt: 1},
{name: "user2", wCnt: 1},
{name: "user3", wCnt: 1},
}

var wg sync.WaitGroup
wg.Add(len(tests))
for _, tc := range tests {
name := tc.name
want := tc.wCnt

go func() {
defer wg.Done()

rw := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/hello?name="+name, nil)
handleHello(rw, req)

assert.Equal(t, http.StatusOK, rw.Code)
assert.Equal(t, want, counter[name])
}()
}
wg.Wait()
}

0 comments on commit 9817c2e

Please sign in to comment.