-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathremote_stress_test.go
139 lines (123 loc) · 2.88 KB
/
remote_stress_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
package main
import (
"bytes"
"fmt"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"hash"
"hash/crc64"
"io"
"io/ioutil"
"math/rand"
"net/http"
"testing/quick"
"time"
)
var _ = Describe("Remote Stress Test", func() {
It("Pass POST", func() {
url := fmt.Sprintf("%s/pass", testURL)
err := quick.Check(func(msg string) bool {
return testPOSTString(url, msg)
}, nil)
Expect(err).Should(Succeed())
})
It("Pass POST small binary", func() {
url := fmt.Sprintf("%s/pass", testURL)
success := testPOSTBinary(url, 100)
Expect(success).Should(BeTrue())
})
It("Pass POST medium binary", func() {
url := fmt.Sprintf("%s/pass", testURL)
success := testPOSTBinary(url, 65539)
Expect(success).Should(BeTrue())
})
It("Pass POST large binary", func() {
url := fmt.Sprintf("%s/pass", testURL)
success := testPOSTBinary(url, 2*1024*1024)
Expect(success).Should(BeTrue())
})
})
func testPOST(url string, bod io.Reader) (bool, io.ReadCloser) {
resp, err := http.Post(url, "text/plain", bod)
if err != nil {
fmt.Fprintf(GinkgoWriter, "POST error: %s\n", err)
return false, nil
}
if resp.StatusCode != http.StatusOK {
fmt.Fprintf(GinkgoWriter, "Bad status %d\n", resp.StatusCode)
return false, nil
}
return true, resp.Body
}
func testPOSTString(url, msg string) bool {
reqBody := bytes.NewBufferString(msg)
success, body := testPOST(url, reqBody)
if !success {
return false
}
defer body.Close()
rbuf, err := ioutil.ReadAll(body)
if err != nil {
fmt.Fprintf(GinkgoWriter, "Body read error: %s\n", err)
return false
}
respMsg := string(rbuf)
if msg != respMsg {
fmt.Fprintf(GinkgoWriter, "Invalid response: Expected \"%s\" got \"%s\"\n",
msg, respMsg)
return false
}
return true
}
func testPOSTBinary(url string, len int) bool {
testRdr := newTestReader(len)
success, body := testPOST(url, testRdr)
if !success {
return false
}
crc := crc64.New(crcTable)
defer body.Close()
buf := make([]byte, 8192)
l, _ := body.Read(buf)
for l > 0 {
crc.Write(buf[:l])
l, _ = body.Read(buf)
}
if crc.Sum64() != testRdr.hash.Sum64() {
fmt.Fprintf(GinkgoWriter, "CRC %d != %d\n", testRdr.hash.Sum64(), crc.Sum64())
return false
}
return true
}
var crcTable = crc64.MakeTable(crc64.ISO)
var randSrc = rand.NewSource(time.Now().UnixNano())
/*
* This is a reader that returns random bytes forever.
*/
type bigTestReader struct {
remaining int
hash hash.Hash64
rnd *rand.Rand
}
func newTestReader(len int) *bigTestReader {
return &bigTestReader{
remaining: len,
hash: crc64.New(crcTable),
rnd: rand.New(randSrc),
}
}
func (r *bigTestReader) Read(buf []byte) (n int, err error) {
toRead := len(buf)
if toRead > r.remaining {
toRead = r.remaining
}
if toRead > 0 {
r.remaining -= toRead
r.rnd.Read(buf[:toRead])
r.hash.Write(buf[:toRead])
}
if r.remaining > 0 {
return toRead, nil
}
return toRead, io.EOF
}