forked from etcd-io/etcd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lazy_cluster.go
105 lines (87 loc) · 2.84 KB
/
lazy_cluster.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
// Copyright 2020 The etcd 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.
package integration
import (
"log"
"net/http"
"sync"
"time"
"go.etcd.io/etcd/pkg/v3/transport"
)
// Infrastructure to provision a single shared cluster for tests - only
// when its needed.
//
// See ./tests/integration/clientv3/examples/main_test.go for canonical usage.
// Please notice that the shared (LazyCluster's) state is preserved between
// testcases, so left-over state might has cross-testcase effects.
// Prefer dedicated clusters for substancial test-cases.
type LazyCluster interface {
// EndpointsV2 - exposes connection points for client v2.
// Calls to this method might initialize the cluster.
EndpointsV2() []string
// EndpointsV3 - exposes connection points for client v3.
// Calls to this method might initialize the cluster.
EndpointsV3() []string
// Cluster - calls to this method might initialize the cluster.
Cluster() *ClusterV3
// Transport - call to this method might initialize the cluster.
Transport() *http.Transport
Terminate()
}
type lazyCluster struct {
cfg ClusterConfig
cluster *ClusterV3
transport *http.Transport
once sync.Once
}
// NewLazyCluster returns a new test cluster handler that gets created on the
// first call to GetEndpoints() or GetTransport()
func NewLazyCluster() LazyCluster {
return NewLazyClusterWithConfig(ClusterConfig{Size: 1})
}
// NewLazyClusterWithConfig returns a new test cluster handler that gets created
// on the first call to GetEndpoints() or GetTransport()
func NewLazyClusterWithConfig(cfg ClusterConfig) LazyCluster {
return &lazyCluster{cfg: cfg}
}
func (lc *lazyCluster) mustLazyInit() {
lc.once.Do(func() {
var err error
lc.transport, err = transport.NewTransport(transport.TLSInfo{}, time.Second)
if err != nil {
log.Fatal(err)
}
lc.cluster = NewClusterV3(nil, &lc.cfg)
})
}
func (lc *lazyCluster) Terminate() {
if lc != nil && lc.cluster != nil {
lc.cluster.Terminate(nil)
lc.cluster = nil
}
}
func (lc *lazyCluster) EndpointsV2() []string {
return []string{lc.Cluster().Members[0].URL()}
}
func (lc *lazyCluster) EndpointsV3() []string {
return lc.Cluster().Client(0).Endpoints()
}
func (lc *lazyCluster) Cluster() *ClusterV3 {
lc.mustLazyInit()
return lc.cluster
}
func (lc *lazyCluster) Transport() *http.Transport {
lc.mustLazyInit()
return lc.transport
}