forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testserver.go
130 lines (116 loc) · 3.79 KB
/
testserver.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
// Copyright 2014 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License. See the AUTHORS file
// for names of contributors.
//
// Author: Spencer Kimball (spencer.kimball@gmail.com)
package server
import (
"time"
"github.com/cockroachdb/cockroach/gossip"
"github.com/cockroachdb/cockroach/proto"
"github.com/cockroachdb/cockroach/storage"
"github.com/cockroachdb/cockroach/storage/engine"
"github.com/cockroachdb/cockroach/util"
"github.com/cockroachdb/cockroach/util/hlc"
)
const (
defaultHTTPAddr = "127.0.0.1:0"
defaultRPCAddr = "127.0.0.1:0"
)
// A TestServer encapsulates an in-memory instantiation of a cockroach
// node with a single store. Example usage of a TestServer follows:
//
// s := &server.TestServer{}
// if err := s.Start(); err != nil {
// t.Fatal(err)
// }
// defer s.Stop()
//
// TODO(spencer): add support for multiple stores.
type TestServer struct {
// CertDir specifies the directory containing certs for SSL
// connections. Default will load insecure TLS config.
CertDir string
// MaxOffset is the maximum offset for clocks in the cluster.
// This is mostly irrelevant except when testing reads within
// uncertainty intervals.
MaxOffset time.Duration
// HTTPAddr and RPCAddr default to localhost with port set
// at time of call to Start() to an available port.
HTTPAddr, RPCAddr string
// server is the embedded Cockroach server struct.
*Server
}
// Gossip returns the gossip instance used by the TestServer.
func (ts *TestServer) Gossip() *gossip.Gossip {
if ts != nil {
return ts.gossip
}
return nil
}
// Clock returns the clock used by the TestServer.
func (ts *TestServer) Clock() *hlc.Clock {
if ts != nil {
return ts.clock
}
return nil
}
// Start starts the TestServer by bootstrapping an in-memory store
// (defaults to maximum of 100M). The server is started, launching the
// node RPC server and all HTTP endpoints. Use the value of
// TestServer.HTTPAddr after Start() for client connections.
func (ts *TestServer) Start() error {
// We update these with the actual port once the servers
// have been launched for the purpose of this test.
if ts.RPCAddr == "" {
ts.RPCAddr = defaultRPCAddr
}
if ts.HTTPAddr == "" {
ts.HTTPAddr = defaultHTTPAddr
}
ctx := NewContext()
ctx.RPC = ts.RPCAddr
ctx.HTTP = ts.HTTPAddr
ctx.Certs = ts.CertDir
ctx.MaxOffset = ts.MaxOffset
var err error
ts.Server, err = NewServer(ctx)
if err != nil {
return util.Errorf("could not init server: %s", err)
}
ctx.Engines = []engine.Engine{engine.NewInMem(proto.Attributes{}, 100<<20)}
if _, err := BootstrapCluster("cluster-1", ctx.Engines[0]); err != nil {
return util.Errorf("could not bootstrap cluster: %s", err)
}
err = ts.Server.Start(true) // TODO(spencer): should shutdown server.
if err != nil {
return util.Errorf("could not start server: %s", err)
}
// Update the configuration variables to reflect the actual
// ports bound.
ts.HTTPAddr = (*ts.httpListener).Addr().String()
ts.RPCAddr = ts.rpc.Addr().String()
return nil
}
// Stop stops the TestServer.
func (ts *TestServer) Stop() {
ts.Server.Stop()
}
// SetRangeRetryOptions sets the retry options for stores in TestServer.
func (ts *TestServer) SetRangeRetryOptions(ro util.RetryOptions) {
ts.node.lSender.VisitStores(func(s *storage.Store) error {
s.RetryOpts = ro
return nil
})
}