forked from intercom/dvara
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon_test.go
121 lines (104 loc) · 2.77 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
package dvara
import (
"testing"
"time"
"gopkg.in/mgo.v2"
"github.com/facebookgo/ensure"
"github.com/facebookgo/inject"
"github.com/facebookgo/mgotest"
"github.com/facebookgo/startstop"
"github.com/facebookgo/stats"
)
type NoopLogger struct {
}
func (l *NoopLogger) Debugf(f string, args ...interface{}) {
}
func (l *NoopLogger) Errorf(f 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
Manager *StateManager
Graph *inject.Graph
Log NoopLogger
}
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,
}
manager := NewStateManager(&replicaSet)
var graph inject.Graph
err := graph.Provide(
&inject.Object{Value: &replicaSet},
&inject.Object{Value: manager},
&inject.Object{Value: &stats.HookClient{}},
)
ensure.Nil(t, err)
ensure.Nil(t, graph.Populate())
objects := graph.Objects()
log := NoopLogger{}
ensure.Nil(t, startstop.Start(objects, &log))
return &Harness{
T: t,
Stopper: s,
ReplicaSet: &replicaSet,
Manager: manager,
Graph: &graph,
Log: log,
}
}
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 {
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.Manager.ProxyMembers()[0])
}
func (h *Harness) RealSession() *mgo.Session {
return h.Dial(h.Manager.currentReplicaSetState.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
}