forked from tsuru/planb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
173 lines (161 loc) · 4.21 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
// Copyright 2016 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"net/http/httptest"
"os"
"runtime"
"runtime/pprof"
"sync"
"testing"
"time"
"github.com/go-redis/redis"
"github.com/opencoff/planb/backend"
"github.com/opencoff/planb/reverseproxy"
"github.com/opencoff/planb/router"
"gopkg.in/check.v1"
)
type S struct {
redis *redis.Client
}
var _ = check.Suite(&S{})
var redisDB int = 2
func Test(t *testing.T) {
check.TestingT(t)
}
func clearKeys(r *redis.Client) error {
val := r.Keys("frontend:*").Val()
val = append(val, r.Keys("dead:*").Val()...)
if len(val) > 0 {
return r.Del(val...).Err()
}
return nil
}
func redisConn() (*redis.Client, error) {
return redis.NewClient(&redis.Options{Addr: "127.0.0.1:6379", DB: redisDB}), nil
}
func (s *S) SetUpTest(c *check.C) {
var err error
s.redis, err = redisConn()
c.Assert(err, check.IsNil)
err = clearKeys(s.redis)
c.Assert(err, check.IsNil)
}
func (s *S) TearDownTest(c *check.C) {
s.redis.Close()
}
func (s *S) TestServeHTTPStressAllLeakDetector(c *check.C) {
if testing.Short() {
c.Skip("this test takes a long time, specially with -race")
}
checkLeaksEnabled := os.Getenv("PLANB_CHECK_LEAKS") != ""
log.SetOutput(ioutil.Discard)
defer log.SetOutput(os.Stderr)
nFrontends := 50
nServers := nFrontends * 4
servers := make([]*httptest.Server, nServers)
allNamesMap := map[string]struct{}{}
for i := range servers {
msg := fmt.Sprintf("server-%d", i)
allNamesMap[msg] = struct{}{}
srv := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.Write([]byte(msg))
}))
defer srv.Close()
servers[i] = srv
}
frontends := make([]string, nFrontends)
for i := range frontends {
frontend := fmt.Sprintf("stressfront%0d.com", i)
frontends[i] = frontend
err := s.redis.RPush("frontend:"+frontend, frontend).Err()
c.Assert(err, check.IsNil)
ratio := nServers / nFrontends
for j := 0; j < ratio; j++ {
err := s.redis.RPush("frontend:"+frontend, servers[(i*ratio)+j].URL).Err()
c.Assert(err, check.IsNil)
}
if i > nFrontends/2 {
// Add invalid backends forcing errors on half of the frontends
err := s.redis.RPush("frontend:"+frontend, "http://127.0.0.1:32412", "http://127.0.0.1:32413").Err()
c.Assert(err, check.IsNil)
}
}
nProffs := 4
files := make([]*os.File, nProffs)
if checkLeaksEnabled {
for i := range files {
files[i], _ = os.OpenFile(fmt.Sprintf("./planb_stress_%d_mem.pprof", i), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0660)
}
}
opts := backend.RedisOptions{
Host: "localhost",
Port: 6379,
DB: int(redisDB),
}
routesBE, err := backend.NewRedisBackend(opts, opts)
c.Assert(err, check.IsNil)
r := router.Router{Backend: routesBE}
err = r.Init()
c.Assert(err, check.IsNil)
var nativeRP reverseproxy.ReverseProxy = &reverseproxy.NativeReverseProxy{}
err = nativeRP.Initialize(reverseproxy.ReverseProxyConfig{
Router: &r,
DialTimeout: time.Second,
})
c.Assert(err, check.IsNil)
listener, err := net.Listen("tcp", ":0")
c.Assert(err, check.IsNil)
addr := listener.Addr().String()
go nativeRP.Listen(listener, nil)
defer nativeRP.Stop()
defer listener.Close()
nClients := 4
rec := make(chan string, 1000)
wg := sync.WaitGroup{}
accessedBackends := map[string]struct{}{}
mtx := sync.Mutex{}
for i := 0; i < nClients; i++ {
go func() {
for host := range rec {
req, inErr := http.NewRequest("GET", fmt.Sprintf("http://%s/", addr), nil)
c.Assert(inErr, check.IsNil)
req.Host = host
rsp, inErr := http.DefaultClient.Do(req)
c.Assert(inErr, check.IsNil)
srvName, _ := ioutil.ReadAll(rsp.Body)
rsp.Body.Close()
if len(srvName) != 0 {
mtx.Lock()
accessedBackends[string(srvName)] = struct{}{}
mtx.Unlock()
}
wg.Done()
}
}()
}
N := 20000
for _, f := range files {
for i := 0; i < N; i++ {
wg.Add(1)
rec <- frontends[i%len(frontends)]
}
wg.Wait()
c.Assert(accessedBackends, check.DeepEquals, allNamesMap)
if checkLeaksEnabled {
runtime.GC()
pprof.WriteHeapProfile(f)
}
}
if checkLeaksEnabled {
for _, f := range files {
f.Close()
}
}
}