forked from intercom/dvara
/
common_test.go
136 lines (118 loc) · 3.42 KB
/
common_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
package dvara
import (
"os"
"testing"
"time"
"gopkg.in/mgo.v2"
"github.com/facebookgo/ensure"
"github.com/facebookgo/gangliamr"
"github.com/facebookgo/inject"
"github.com/facebookgo/mgotest"
"github.com/facebookgo/startstop"
"github.com/facebookgo/stats"
)
var disableSlowTests = os.Getenv("GO_RUN_LONG_TEST") == ""
type nopLogger struct{}
func (n nopLogger) Error(args ...interface{}) {}
func (n nopLogger) Errorf(format string, args ...interface{}) {}
func (n nopLogger) Warn(args ...interface{}) {}
func (n nopLogger) Warnf(format string, args ...interface{}) {}
func (n nopLogger) Info(args ...interface{}) {}
func (n nopLogger) Infof(format string, args ...interface{}) {}
func (n nopLogger) Debug(args ...interface{}) {}
func (n nopLogger) Debugf(format string, args ...interface{}) {}
type stopper interface {
Stop()
}
type Harness struct {
T testing.TB
Stopper stopper // This is either mgotest.Server or mgotest.ReplicaSet
ReplicaSet *ReplicaSet
Graph *inject.Graph
Log nopLogger
}
func newHarnessInternal(url string, s stopper, t testing.TB) *Harness {
replicaSet := ReplicaSet{
Addrs: url,
ListenAddr: "",
PortStart: 2000,
PortEnd: 3000,
MaxConnections: 5,
MinIdleConnections: 5,
ServerIdleTimeout: 5 * time.Minute,
ServerClosePoolSize: 5,
ClientIdleTimeout: 5 * time.Minute,
MaxPerClientConnections: 250,
GetLastErrorTimeout: 5 * time.Minute,
MessageTimeout: 5 * time.Second,
}
var log nopLogger
var graph inject.Graph
err := graph.Provide(
&inject.Object{Value: &log},
&inject.Object{Value: &replicaSet},
&inject.Object{Value: &stats.HookClient{}},
)
ensure.Nil(t, err)
ensure.Nil(t, graph.Populate())
objects := graph.Objects()
gregistry := gangliamr.NewTestRegistry()
for _, o := range objects {
if rmO, ok := o.Value.(registerMetrics); ok {
rmO.RegisterMetrics(gregistry)
}
}
ensure.Nil(t, startstop.Start(objects, &log))
return &Harness{
T: t,
Stopper: s,
ReplicaSet: &replicaSet,
Graph: &graph,
}
}
type SingleHarness struct {
*Harness
MgoServer *mgotest.Server
}
func NewSingleHarness(t testing.TB) *SingleHarness {
mgoserver := mgotest.NewStartedServer(t)
return &SingleHarness{
Harness: newHarnessInternal(mgoserver.URL(), mgoserver, t),
MgoServer: mgoserver,
}
}
type ReplicaSetHarness struct {
*Harness
MgoReplicaSet *mgotest.ReplicaSet
}
func NewReplicaSetHarness(n uint, t testing.TB) *ReplicaSetHarness {
if disableSlowTests {
t.Skip("disabled because it's slow")
}
mgoRS := mgotest.NewReplicaSet(n, t)
return &ReplicaSetHarness{
Harness: newHarnessInternal(mgoRS.Addrs()[n-1], mgoRS, t),
MgoReplicaSet: mgoRS,
}
}
func (h *Harness) Stop() {
defer h.Stopper.Stop()
ensure.Nil(h.T, startstop.Stop(h.Graph.Objects(), &h.Log))
}
func (h *Harness) ProxySession() *mgo.Session {
return h.Dial(h.ReplicaSet.ProxyMembers()[0])
}
func (h *Harness) RealSession() *mgo.Session {
return h.Dial(h.ReplicaSet.lastState.Addrs()[0])
}
func (h *Harness) Dial(u string) *mgo.Session {
session, err := mgo.Dial(u)
ensure.Nil(h.T, err, u)
session.SetSafe(&mgo.Safe{FSync: true, W: 1})
session.SetSyncTimeout(time.Minute)
session.SetSocketTimeout(time.Minute)
return session
}
type registerMetrics interface {
RegisterMetrics(r *gangliamr.Registry)
}