-
Notifications
You must be signed in to change notification settings - Fork 0
/
codec_race_test.go
54 lines (46 loc) · 1.08 KB
/
codec_race_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// +build race
package ep
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"sync"
"testing"
"github.com/advanderveer/ep/epcoding"
)
func TestConcurrentCodecHandling(t *testing.T) {
for i, c := range []struct {
fn interface{}
body string
expCode int
expBody string
}{
{func() {}, ``, 200, ``},
{func(s string) string { return s }, `"foo"`, 200, `"foo"` + "\n"},
} {
t.Run(strconv.Itoa(i), func(t *testing.T) {
srv := httptest.NewServer(New(
RequestDecoding(epcoding.JSON{}),
ResponseEncoding(epcoding.JSON{}),
).Handle(c.fn))
var wg sync.WaitGroup
defer wg.Wait()
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
resp, _ := http.Post(srv.URL, "application/json", strings.NewReader(c.body))
if resp.StatusCode != c.expCode {
t.Fatalf("expected code: %d, got: %d", c.expCode, resp.StatusCode)
}
body, _ := ioutil.ReadAll(resp.Body)
if string(body) != c.expBody {
t.Fatalf("expected body: '%s', got: '%s'", c.expBody, string(body))
}
}()
}
})
}
}