-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6879839
commit 9817c2e
Showing
9 changed files
with
972 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file added
BIN
+46.3 KB
content/articles/how_to_test/assets/image-20181222170909680-5469749.png
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.
Binary file added
BIN
+595 KB
content/articles/how_to_test/assets/image-20181222173117732-5471077.png
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.
Binary file added
BIN
+319 KB
content/articles/how_to_test/assets/image-20181222180733266-5473253.png
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |