mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
test, clientv3: run examples as integration tests
This commit is contained in:
parent
c1455a4f10
commit
9bfa0172f5
@ -37,7 +37,7 @@ func ExampleCluster_memberList() {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
fmt.Println("members:", len(resp.Members))
|
fmt.Println("members:", len(resp.Members))
|
||||||
// members: 3
|
// Output: members: 3
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleCluster_memberAdd() {
|
func ExampleCluster_memberAdd() {
|
||||||
|
@ -33,13 +33,11 @@ func ExampleKV_put() {
|
|||||||
defer cli.Close()
|
defer cli.Close()
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
|
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
|
||||||
resp, err := cli.Put(ctx, "sample_key", "sample_value")
|
_, err = cli.Put(ctx, "sample_key", "sample_value")
|
||||||
cancel()
|
cancel()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
fmt.Println("current revision:", resp.Header.Revision) // revision start at 1
|
|
||||||
// current revision: 2
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleKV_get() {
|
func ExampleKV_get() {
|
||||||
@ -66,7 +64,7 @@ func ExampleKV_get() {
|
|||||||
for _, ev := range resp.Kvs {
|
for _, ev := range resp.Kvs {
|
||||||
fmt.Printf("%s : %s\n", ev.Key, ev.Value)
|
fmt.Printf("%s : %s\n", ev.Key, ev.Value)
|
||||||
}
|
}
|
||||||
// foo : bar
|
// Output: foo : bar
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleKV_getWithRev() {
|
func ExampleKV_getWithRev() {
|
||||||
@ -79,7 +77,7 @@ func ExampleKV_getWithRev() {
|
|||||||
}
|
}
|
||||||
defer cli.Close()
|
defer cli.Close()
|
||||||
|
|
||||||
_, err = cli.Put(context.TODO(), "foo", "bar1")
|
presp, err := cli.Put(context.TODO(), "foo", "bar1")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -89,7 +87,7 @@ func ExampleKV_getWithRev() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
|
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
|
||||||
resp, err := cli.Get(ctx, "foo", clientv3.WithRev(2))
|
resp, err := cli.Get(ctx, "foo", clientv3.WithRev(presp.Header.Revision))
|
||||||
cancel()
|
cancel()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
@ -97,7 +95,7 @@ func ExampleKV_getWithRev() {
|
|||||||
for _, ev := range resp.Kvs {
|
for _, ev := range resp.Kvs {
|
||||||
fmt.Printf("%s : %s\n", ev.Key, ev.Value)
|
fmt.Printf("%s : %s\n", ev.Key, ev.Value)
|
||||||
}
|
}
|
||||||
// foo : bar1
|
// Output: foo : bar1
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleKV_getSortedPrefix() {
|
func ExampleKV_getSortedPrefix() {
|
||||||
@ -128,6 +126,7 @@ func ExampleKV_getSortedPrefix() {
|
|||||||
for _, ev := range resp.Kvs {
|
for _, ev := range resp.Kvs {
|
||||||
fmt.Printf("%s : %s\n", ev.Key, ev.Value)
|
fmt.Printf("%s : %s\n", ev.Key, ev.Value)
|
||||||
}
|
}
|
||||||
|
// Output:
|
||||||
// key_2 : value
|
// key_2 : value
|
||||||
// key_1 : value
|
// key_1 : value
|
||||||
// key_0 : value
|
// key_0 : value
|
||||||
@ -144,13 +143,23 @@ func ExampleKV_delete() {
|
|||||||
defer cli.Close()
|
defer cli.Close()
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
|
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
|
||||||
resp, err := cli.Delete(ctx, "key", clientv3.WithPrefix())
|
defer cancel()
|
||||||
cancel()
|
|
||||||
|
// count keys about to be deleted
|
||||||
|
gresp, err := cli.Get(ctx, "key", clientv3.WithPrefix())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
fmt.Println("Deleted", resp.Deleted, "keys")
|
|
||||||
// Deleted n keys
|
// delete the keys
|
||||||
|
dresp, err := cli.Delete(ctx, "key", clientv3.WithPrefix())
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("Deleted all keys:", int64(len(gresp.Kvs)) == dresp.Deleted)
|
||||||
|
// Output:
|
||||||
|
// Deleted all keys: true
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleKV_compact() {
|
func ExampleKV_compact() {
|
||||||
@ -215,7 +224,7 @@ func ExampleKV_txn() {
|
|||||||
for _, ev := range gresp.Kvs {
|
for _, ev := range gresp.Kvs {
|
||||||
fmt.Printf("%s : %s\n", ev.Key, ev.Value)
|
fmt.Printf("%s : %s\n", ev.Key, ev.Value)
|
||||||
}
|
}
|
||||||
// key : XYZ
|
// Output: key : XYZ
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleKV_do() {
|
func ExampleKV_do() {
|
||||||
|
@ -76,7 +76,7 @@ func ExampleLease_revoke() {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
fmt.Println("number of keys:", len(gresp.Kvs))
|
fmt.Println("number of keys:", len(gresp.Kvs))
|
||||||
// number of keys: 0
|
// Output: number of keys: 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleLease_keepAlive() {
|
func ExampleLease_keepAlive() {
|
||||||
@ -100,10 +100,14 @@ func ExampleLease_keepAlive() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// the key 'foo' will be kept forever
|
// the key 'foo' will be kept forever
|
||||||
_, err = cli.KeepAlive(context.TODO(), resp.ID)
|
ch, kaerr := cli.KeepAlive(context.TODO(), resp.ID)
|
||||||
if err != nil {
|
if kaerr != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(kaerr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ka := <-ch
|
||||||
|
fmt.Println("ttl:", ka.TTL)
|
||||||
|
// Output: ttl: 5
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleLease_keepAliveOnce() {
|
func ExampleLease_keepAliveOnce() {
|
||||||
@ -127,8 +131,11 @@ func ExampleLease_keepAliveOnce() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// to renew the lease only once
|
// to renew the lease only once
|
||||||
_, err = cli.KeepAliveOnce(context.TODO(), resp.ID)
|
ka, kaerr := cli.KeepAliveOnce(context.TODO(), resp.ID)
|
||||||
if err != nil {
|
if kaerr != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(kaerr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fmt.Println("ttl:", ka.TTL)
|
||||||
|
// Output: ttl: 5
|
||||||
}
|
}
|
||||||
|
57
clientv3/main_test.go
Normal file
57
clientv3/main_test.go
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
// Copyright 2016 CoreOS, Inc.
|
||||||
|
//
|
||||||
|
// 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 clientv3_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/coreos/etcd/integration"
|
||||||
|
"github.com/coreos/etcd/pkg/testutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestMain sets up an etcd cluster if running the examples.
|
||||||
|
func TestMain(m *testing.M) {
|
||||||
|
useCluster := true // default to running all tests
|
||||||
|
for _, arg := range os.Args {
|
||||||
|
if strings.HasPrefix(arg, "-test.run=") {
|
||||||
|
exp := strings.Split(arg, "=")[1]
|
||||||
|
match, err := regexp.MatchString(exp, "Example")
|
||||||
|
useCluster = (err == nil && match) || strings.Contains(exp, "Example")
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
v := 0
|
||||||
|
if useCluster {
|
||||||
|
cfg := integration.ClusterConfig{Size: 3}
|
||||||
|
clus := integration.NewClusterV3(nil, &cfg)
|
||||||
|
endpoints = make([]string, 3)
|
||||||
|
for i := range endpoints {
|
||||||
|
endpoints[i] = clus.Client(i).Endpoints()[0]
|
||||||
|
}
|
||||||
|
v = m.Run()
|
||||||
|
clus.Terminate(nil)
|
||||||
|
} else {
|
||||||
|
v = m.Run()
|
||||||
|
}
|
||||||
|
|
||||||
|
if v == 0 && testutil.CheckLeakedGoroutine() {
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
os.Exit(v)
|
||||||
|
}
|
@ -17,13 +17,9 @@ package clientv3
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/coreos/etcd/pkg/testutil"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestTxnPanics(t *testing.T) {
|
func TestTxnPanics(t *testing.T) {
|
||||||
defer testutil.AfterTest(t)
|
|
||||||
|
|
||||||
kv := NewKV(&Client{})
|
kv := NewKV(&Client{})
|
||||||
|
|
||||||
errc := make(chan string)
|
errc := make(chan string)
|
||||||
|
@ -20,10 +20,13 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// so tests pass if given a -run that doesn't include TestSample
|
||||||
|
var ranSample = false
|
||||||
|
|
||||||
func TestMain(m *testing.M) {
|
func TestMain(m *testing.M) {
|
||||||
m.Run()
|
m.Run()
|
||||||
isLeaked := CheckLeakedGoroutine()
|
isLeaked := CheckLeakedGoroutine()
|
||||||
if !isLeaked {
|
if ranSample && !isLeaked {
|
||||||
fmt.Fprintln(os.Stderr, "expected leaky goroutines but none is detected")
|
fmt.Fprintln(os.Stderr, "expected leaky goroutines but none is detected")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
@ -32,6 +35,7 @@ func TestMain(m *testing.M) {
|
|||||||
|
|
||||||
func TestSample(t *testing.T) {
|
func TestSample(t *testing.T) {
|
||||||
defer AfterTest(t)
|
defer AfterTest(t)
|
||||||
|
ranSample = true
|
||||||
for range make([]struct{}, 100) {
|
for range make([]struct{}, 100) {
|
||||||
go func() {
|
go func() {
|
||||||
select {}
|
select {}
|
||||||
|
4
test
4
test
@ -57,7 +57,8 @@ fi
|
|||||||
|
|
||||||
function unit_tests {
|
function unit_tests {
|
||||||
echo "Running tests..."
|
echo "Running tests..."
|
||||||
go test -timeout 3m ${COVER} ${RACE} -cpu 1,2,4 $@ ${TEST}
|
# only -run=Test so examples can run in integration tests
|
||||||
|
go test -timeout 3m ${COVER} ${RACE} -cpu 1,2,4 -run=Test $@ ${TEST}
|
||||||
}
|
}
|
||||||
|
|
||||||
function integration_tests {
|
function integration_tests {
|
||||||
@ -66,6 +67,7 @@ function integration_tests {
|
|||||||
go test -timeout 15m -v -cpu 1,2,4 $@ ${REPO_PATH}/integration
|
go test -timeout 15m -v -cpu 1,2,4 $@ ${REPO_PATH}/integration
|
||||||
go test -timeout 10m -v ${RACE} -cpu 1,2,4 $@ ${REPO_PATH}/clientv3/integration
|
go test -timeout 10m -v ${RACE} -cpu 1,2,4 $@ ${REPO_PATH}/clientv3/integration
|
||||||
go test -timeout 1m -v -cpu 1,2,4 $@ ${REPO_PATH}/contrib/raftexample
|
go test -timeout 1m -v -cpu 1,2,4 $@ ${REPO_PATH}/contrib/raftexample
|
||||||
|
go test -timeout 1m -v ${RACE} -cpu 1,2,4 -run=Example $@ ${TEST}
|
||||||
}
|
}
|
||||||
|
|
||||||
function fmt_tests {
|
function fmt_tests {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user