-
Notifications
You must be signed in to change notification settings - Fork 15
/
concurrency_test.go
164 lines (135 loc) · 3.96 KB
/
concurrency_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
package govcr_test
import (
"bytes"
"fmt"
"io"
"math/rand"
"net/http"
"net/http/httptest"
"os"
"strconv"
"testing"
"time"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
"github.com/seborama/govcr/v15"
"github.com/seborama/govcr/v15/stats"
)
func TestConcurrencySafety(t *testing.T) {
const cassetteName = "temp-fixtures/TestConcurrencySafety.cassette"
threadMax := int8(50)
// create a test server
ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(time.Millisecond * time.Duration(rand.Intn(50)))
clientNum, err := strconv.ParseInt(r.URL.Query().Get("num"), 0, 8)
require.NoError(t, err)
data := generateBinaryBody(int8(clientNum))
written, err := w.Write(data)
if written != len(data) {
t.Fatalf("** Only %d bytes out of %d were written", written, len(data))
}
if err != nil {
t.Fatalf("err from w.Write(): Expected nil, got %s", err)
}
}))
defer ts.Close()
testServerClient := ts.Client()
testServerClient.Timeout = 5 * time.Second
_ = os.Remove(cassetteName)
defer func() { _ = os.Remove(cassetteName) }()
vcr := createVCR(cassetteName, testServerClient)
client := vcr.HTTPClient()
t.Run("main - phase 1", func(t *testing.T) {
// run requests
for i := int8(1); i <= threadMax; i++ {
func(i1 int8) {
t.Run(fmt.Sprintf("i=%d", i), func(t *testing.T) {
t.Parallel()
func() {
resp, err := client.Get(fmt.Sprintf("%s?num=%d", ts.URL, i1))
require.NoError(t, err)
// check outcome of the request
expectedBody := generateBinaryBody(i1)
err = validateResponseForTestPlaybackOrder(resp, expectedBody)
require.NoError(t, err)
}()
})
}(i)
}
})
expectedStats := stats.Stats{
TotalTracks: int32(threadMax),
TracksLoaded: 0,
TracksRecorded: int32(threadMax),
TracksPlayed: 0,
}
require.EqualValues(t, expectedStats, *vcr.Stats())
// re-run request and expect play back from vcr
vcr = createVCR(cassetteName, testServerClient)
client = vcr.HTTPClient()
// run requests
t.Run("main - phase 2 - playback", func(t *testing.T) {
// run requests
for i := int8(1); i <= threadMax; i++ {
func(i1 int8) {
t.Run(fmt.Sprintf("i=%d", i), func(t *testing.T) {
t.Parallel()
func() {
resp, _ := client.Get(fmt.Sprintf("%s?num=%d", ts.URL, i1))
// check outcome of the request
expectedBody := generateBinaryBody(i1)
if err := validateResponseForTestPlaybackOrder(resp, expectedBody); err != nil {
t.Fatal(err.Error())
}
}()
})
}(i)
}
})
expectedStats = stats.Stats{
TotalTracks: int32(threadMax),
TracksLoaded: int32(threadMax),
TracksRecorded: 0,
TracksPlayed: int32(threadMax),
}
require.EqualValues(t, expectedStats, *vcr.Stats())
}
func createVCR(cassetteName string, client *http.Client) *govcr.ControlPanel {
return govcr.NewVCR(
govcr.NewCassetteLoader(cassetteName),
govcr.WithClient(client))
}
func generateBinaryBody(sequence int8) []byte {
data := make([]byte, 256, 257)
for i := range data {
data[i] = byte(i)
}
data = append(data, byte(sequence))
return data
}
func validateResponseForTestPlaybackOrder(resp *http.Response, expectedBody interface{}) error {
if resp.StatusCode != http.StatusOK {
return errors.Errorf("resp.StatusCode: Expected %d, got %d", http.StatusOK, resp.StatusCode)
}
if resp.Body == nil {
return errors.New("resp.Body: Expected non-nil, got nil")
}
bodyData, err := io.ReadAll(resp.Body)
if err != nil {
return errors.Errorf("err from io.ReadAll(): Expected nil, got %s", err)
}
_ = resp.Body.Close()
var expectedBodyBytes []byte
switch exp := expectedBody.(type) {
case []byte:
expectedBodyBytes = exp
case string:
expectedBodyBytes = []byte(exp)
default:
return errors.New("Unexpected type for 'expectedBody' variable")
}
if !bytes.Equal(bodyData, expectedBodyBytes) {
return errors.Errorf("Body: expected '%v', got '%v'", expectedBody, bodyData)
}
return nil
}