forked from slon/shad-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
178 lines (139 loc) · 3.76 KB
/
main_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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//go:build !change
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"os"
"os/exec"
"sort"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/require"
"gitlab.com/slon/shad-go/tools/testtool"
)
const fetchallImportPath = "gitlab.com/slon/shad-go/fetchall"
var binCache testtool.BinCache
func TestMain(m *testing.M) {
os.Exit(func() int {
var teardown testtool.CloseFunc
binCache, teardown = testtool.NewBinCache()
defer teardown()
return m.Run()
}())
}
func TestFetchall_valid(t *testing.T) {
binary, err := binCache.GetBinary(fetchallImportPath)
require.NoError(t, err)
type endpoint string
for _, tc := range []struct {
name string
h http.HandlerFunc
queries []endpoint
}{
{
name: "404",
h: func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "The requested URL was not found.", http.StatusNotFound)
},
queries: []endpoint{"/" + endpoint(testtool.RandomName())},
},
{
name: "200",
h: func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("The requested URL was found.\n"))
},
queries: []endpoint{"/"},
},
} {
t.Run(tc.name, func(t *testing.T) {
s := httptest.NewServer(tc.h)
defer s.Close()
urls := make([]string, len(tc.queries))
for i, q := range tc.queries {
urls[i] = s.URL + string(q)
}
cmd := exec.Command(binary, urls...)
cmd.Stdout = nil
cmd.Stderr = os.Stderr
require.NoError(t, cmd.Run())
})
}
}
func TestFetchall_multipleURLs(t *testing.T) {
binary, err := binCache.GetBinary(fetchallImportPath)
require.NoError(t, err)
var fooHit, barHit int32
mux := http.NewServeMux()
mux.HandleFunc("/foo", func(w http.ResponseWriter, h *http.Request) {
atomic.StoreInt32(&fooHit, 1)
_, _ = w.Write([]byte("foo"))
})
mux.HandleFunc("/bar", func(w http.ResponseWriter, h *http.Request) {
atomic.StoreInt32(&barHit, 1)
_, _ = w.Write([]byte("bar"))
})
s := httptest.NewServer(mux)
defer s.Close()
cmd := exec.Command(binary, s.URL+"/foo", s.URL+"/bar")
cmd.Stdout = nil
cmd.Stderr = os.Stderr
require.NoError(t, cmd.Run())
require.Equal(t, int32(1), atomic.LoadInt32(&fooHit))
require.Equal(t, int32(1), atomic.LoadInt32(&barHit))
}
func TestFetchall_malformed(t *testing.T) {
binary, err := binCache.GetBinary(fetchallImportPath)
require.NoError(t, err)
hit := int32(0)
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
atomic.AddInt32(&hit, 1)
_, _ = w.Write([]byte("success"))
}))
defer s.Close()
cmd := exec.Command(binary, "golang.org", s.URL, s.URL)
cmd.Stdout = nil
cmd.Stderr = os.Stderr
err = cmd.Run()
require.NoError(t, err)
require.True(t, atomic.LoadInt32(&hit) >= 2)
}
func TestFetchall_concurrency(t *testing.T) {
binary, err := binCache.GetBinary(fetchallImportPath)
require.NoError(t, err)
var mu sync.Mutex
var callOrder []time.Duration
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
s := r.URL.Query().Get("duration")
require.NotEmpty(t, s)
d, err := time.ParseDuration(s)
require.NoError(t, err)
time.Sleep(d)
mu.Lock()
callOrder = append(callOrder, d)
mu.Unlock()
_, _ = fmt.Fprintln(w, "hello")
}))
defer s.Close()
makeURL := func(d time.Duration) string {
v := url.Values{}
v.Add("duration", d.String())
return fmt.Sprintf("%s?%s", s.URL, v.Encode())
}
fastURL := makeURL(time.Millisecond * 10)
slowURL := makeURL(time.Second)
cmd := exec.Command(binary, slowURL, fastURL)
cmd.Stdout = nil
cmd.Stderr = os.Stderr
require.NoError(t, cmd.Run())
mu.Lock()
defer mu.Unlock()
require.Len(t, callOrder, 2)
require.True(t, sort.SliceIsSorted(callOrder, func(i, j int) bool {
return callOrder[i] < callOrder[j]
}))
}