mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
clientv3/concurrency: Expose examples close to the source-code.
This commit is contained in:
parent
c5ccebf792
commit
dd45d04b2d
1
clientv3/concurrency/example_election_test.go
Symbolic link
1
clientv3/concurrency/example_election_test.go
Symbolic link
@ -0,0 +1 @@
|
|||||||
|
../../tests/integration/clientv3/concurrency/example_election_test.go
|
1
clientv3/concurrency/example_mutex_test.go
Symbolic link
1
clientv3/concurrency/example_mutex_test.go
Symbolic link
@ -0,0 +1 @@
|
|||||||
|
../../tests/integration/clientv3/concurrency/example_mutex_test.go
|
1
clientv3/concurrency/example_stm_test.go
Symbolic link
1
clientv3/concurrency/example_stm_test.go
Symbolic link
@ -0,0 +1 @@
|
|||||||
|
../../tests/integration/clientv3/concurrency/example_stm_test.go
|
35
clientv3/concurrency/main_test.go
Normal file
35
clientv3/concurrency/main_test.go
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
// Copyright 2017 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 concurrency_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"go.etcd.io/etcd/v3/pkg/testutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func exampleEndpoints() []string { return nil }
|
||||||
|
|
||||||
|
func forUnitTestsRunInMockedContext(mocking func(), example func()) {
|
||||||
|
mocking()
|
||||||
|
// TODO: Call 'example' when mocking() provides realistic mocking of transport.
|
||||||
|
|
||||||
|
// The real testing logic of examples gets executed
|
||||||
|
// as part of ./tests/integration/clientv3/integration/...
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMain(m *testing.M) {
|
||||||
|
testutil.MustTestMainWithLeakDetection(m)
|
||||||
|
}
|
@ -28,7 +28,7 @@ import (
|
|||||||
func TestResumeElection(t *testing.T) {
|
func TestResumeElection(t *testing.T) {
|
||||||
const prefix = "/resume-election/"
|
const prefix = "/resume-election/"
|
||||||
|
|
||||||
cli, err := clientv3.New(clientv3.Config{Endpoints: endpoints})
|
cli, err := clientv3.New(clientv3.Config{Endpoints: exampleEndpoints()})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -25,8 +25,16 @@ import (
|
|||||||
"go.etcd.io/etcd/v3/clientv3/concurrency"
|
"go.etcd.io/etcd/v3/clientv3/concurrency"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func mockElection_Campaign() {
|
||||||
|
fmt.Println("completed first election with e2")
|
||||||
|
fmt.Println("completed second election with e1")
|
||||||
|
}
|
||||||
|
|
||||||
func ExampleElection_Campaign() {
|
func ExampleElection_Campaign() {
|
||||||
cli, err := clientv3.New(clientv3.Config{Endpoints: endpoints})
|
forUnitTestsRunInMockedContext(
|
||||||
|
mockElection_Campaign,
|
||||||
|
func() {
|
||||||
|
cli, err := clientv3.New(clientv3.Config{Endpoints: exampleEndpoints()})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -83,6 +91,7 @@ func ExampleElection_Campaign() {
|
|||||||
fmt.Println("completed second election with", string((<-e.Observe(cctx)).Kvs[0].Value))
|
fmt.Println("completed second election with", string((<-e.Observe(cctx)).Kvs[0].Value))
|
||||||
|
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
})
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// completed first election with e2
|
// completed first election with e2
|
||||||
|
@ -23,8 +23,18 @@ import (
|
|||||||
"go.etcd.io/etcd/v3/clientv3/concurrency"
|
"go.etcd.io/etcd/v3/clientv3/concurrency"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func mockMutex_TryLock() {
|
||||||
|
fmt.Println("acquired lock for s1")
|
||||||
|
fmt.Println("cannot acquire lock for s2, as already locked in another session")
|
||||||
|
fmt.Println("released lock for s1")
|
||||||
|
fmt.Println("acquired lock for s2")
|
||||||
|
}
|
||||||
|
|
||||||
func ExampleMutex_TryLock() {
|
func ExampleMutex_TryLock() {
|
||||||
cli, err := clientv3.New(clientv3.Config{Endpoints: endpoints})
|
forUnitTestsRunInMockedContext(
|
||||||
|
mockMutex_TryLock,
|
||||||
|
func() {
|
||||||
|
cli, err := clientv3.New(clientv3.Config{Endpoints: exampleEndpoints()})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -66,6 +76,7 @@ func ExampleMutex_TryLock() {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
fmt.Println("acquired lock for s2")
|
fmt.Println("acquired lock for s2")
|
||||||
|
})
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// acquired lock for s1
|
// acquired lock for s1
|
||||||
@ -74,8 +85,17 @@ func ExampleMutex_TryLock() {
|
|||||||
// acquired lock for s2
|
// acquired lock for s2
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func mockMutex_Lock() {
|
||||||
|
fmt.Println("acquired lock for s1")
|
||||||
|
fmt.Println("released lock for s1")
|
||||||
|
fmt.Println("acquired lock for s2")
|
||||||
|
}
|
||||||
|
|
||||||
func ExampleMutex_Lock() {
|
func ExampleMutex_Lock() {
|
||||||
cli, err := clientv3.New(clientv3.Config{Endpoints: endpoints})
|
forUnitTestsRunInMockedContext(
|
||||||
|
mockMutex_Lock,
|
||||||
|
func() {
|
||||||
|
cli, err := clientv3.New(clientv3.Config{Endpoints: exampleEndpoints()})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -118,6 +138,7 @@ func ExampleMutex_Lock() {
|
|||||||
|
|
||||||
<-m2Locked
|
<-m2Locked
|
||||||
fmt.Println("acquired lock for s2")
|
fmt.Println("acquired lock for s2")
|
||||||
|
})
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// acquired lock for s1
|
// acquired lock for s1
|
||||||
|
@ -25,10 +25,17 @@ import (
|
|||||||
"go.etcd.io/etcd/v3/clientv3/concurrency"
|
"go.etcd.io/etcd/v3/clientv3/concurrency"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func mockSTM_apply() {
|
||||||
|
fmt.Println("account sum is 500")
|
||||||
|
}
|
||||||
|
|
||||||
// ExampleSTM_apply shows how to use STM with a transactional
|
// ExampleSTM_apply shows how to use STM with a transactional
|
||||||
// transfer between balances.
|
// transfer between balances.
|
||||||
func ExampleSTM_apply() {
|
func ExampleSTM_apply() {
|
||||||
cli, err := clientv3.New(clientv3.Config{Endpoints: endpoints})
|
forUnitTestsRunInMockedContext(
|
||||||
|
mockSTM_apply,
|
||||||
|
func() {
|
||||||
|
cli, err := clientv3.New(clientv3.Config{Endpoints: exampleEndpoints()})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -92,6 +99,7 @@ func ExampleSTM_apply() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("account sum is", sum)
|
fmt.Println("account sum is", sum)
|
||||||
|
})
|
||||||
// Output:
|
// Output:
|
||||||
// account sum is 500
|
// account sum is 500
|
||||||
}
|
}
|
||||||
|
@ -15,30 +15,28 @@
|
|||||||
package concurrency_test
|
package concurrency_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
|
|
||||||
"go.etcd.io/etcd/tests/v3/integration"
|
"go.etcd.io/etcd/tests/v3/integration"
|
||||||
"go.etcd.io/etcd/v3/pkg/testutil"
|
"go.etcd.io/etcd/v3/pkg/testutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
var endpoints []string
|
var lazyCluster = integration.NewLazyCluster()
|
||||||
|
|
||||||
// TestMain sets up an etcd cluster for running the examples.
|
func exampleEndpoints() []string { return lazyCluster.EndpointsV3() }
|
||||||
func TestMain(m *testing.M) {
|
|
||||||
cfg := integration.ClusterConfig{Size: 1}
|
func forUnitTestsRunInMockedContext(mocking func(), example func()) {
|
||||||
clus := integration.NewClusterV3(nil, &cfg)
|
// For integration tests runs in the provided environment
|
||||||
endpoints = []string{clus.Client(0).Endpoints()[0]}
|
example()
|
||||||
v := m.Run()
|
|
||||||
clus.Terminate(nil)
|
|
||||||
if err := testutil.CheckAfterTest(time.Second); err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "%v", err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
if v == 0 && testutil.CheckLeakedGoroutine() {
|
|
||||||
os.Exit(1)
|
// TestMain sets up an etcd cluster if running the examples.
|
||||||
|
func TestMain(m *testing.M) {
|
||||||
|
v := m.Run()
|
||||||
|
lazyCluster.Terminate()
|
||||||
|
if v == 0 {
|
||||||
|
testutil.MustCheckLeakedGoroutine()
|
||||||
}
|
}
|
||||||
os.Exit(v)
|
os.Exit(v)
|
||||||
}
|
}
|
||||||
|
@ -23,7 +23,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestMutexLockSessionExpired(t *testing.T) {
|
func TestMutexLockSessionExpired(t *testing.T) {
|
||||||
cli, err := clientv3.New(clientv3.Config{Endpoints: endpoints})
|
cli, err := clientv3.New(clientv3.Config{Endpoints: exampleEndpoints()})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user