mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
tests/integration: Simplify the testMain for examples.
We introduce a LazyCluster abstraction (instead of copy-pasted logic) that makes clusters to be created only if there are runnable tests in need for the infrastructure.
This commit is contained in:
@@ -34,8 +34,8 @@ func ExampleKeysAPI_directory() {
|
||||
mockKeysAPI_directory,
|
||||
func() {
|
||||
c, err := client.New(client.Config{
|
||||
Endpoints: exampleEndpoints,
|
||||
Transport: exampleTransport,
|
||||
Endpoints: exampleEndpoints(),
|
||||
Transport: exampleTransport(),
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
@@ -85,8 +85,8 @@ func ExampleKeysAPI_setget() {
|
||||
mockKeysAPI_setget,
|
||||
func() {
|
||||
c, err := client.New(client.Config{
|
||||
Endpoints: exampleEndpoints,
|
||||
Transport: exampleTransport,
|
||||
Endpoints: exampleEndpoints(),
|
||||
Transport: exampleTransport(),
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
|
||||
@@ -15,19 +15,18 @@
|
||||
package client_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"go.etcd.io/etcd/tests/v3/integration"
|
||||
"go.etcd.io/etcd/v3/pkg/testutil"
|
||||
"go.etcd.io/etcd/v3/pkg/transport"
|
||||
)
|
||||
|
||||
var exampleEndpoints []string
|
||||
var exampleTransport *http.Transport
|
||||
var lazyCluster = integration.NewLazyCluster()
|
||||
|
||||
func exampleEndpoints() []string { return lazyCluster.EndpointsV2() }
|
||||
func exampleTransport() *http.Transport { return lazyCluster.Transport() }
|
||||
|
||||
func forUnitTestsRunInMockedContext(mocking func(), example func()) {
|
||||
// For integration tests runs in the provided environment
|
||||
@@ -36,22 +35,8 @@ func forUnitTestsRunInMockedContext(mocking func(), example func()) {
|
||||
|
||||
// TestMain sets up an etcd cluster if running the examples.
|
||||
func TestMain(m *testing.M) {
|
||||
tr, trerr := transport.NewTransport(transport.TLSInfo{}, time.Second)
|
||||
if trerr != nil {
|
||||
fmt.Fprintf(os.Stderr, "%v", trerr)
|
||||
os.Exit(1)
|
||||
}
|
||||
cfg := integration.ClusterConfig{Size: 1}
|
||||
clus := integration.NewClusterV3(nil, &cfg)
|
||||
exampleEndpoints = []string{clus.Members[0].URL()}
|
||||
exampleTransport = tr
|
||||
v := m.Run()
|
||||
clus.Terminate(nil)
|
||||
if err := testutil.CheckAfterTest(time.Second); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "%v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
lazyCluster.Terminate()
|
||||
if v == 0 {
|
||||
testutil.MustCheckLeakedGoroutine()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user