clientv3/concurrency: Expose examples close to the source-code.
This commit is contained in:
@ -23,49 +23,60 @@ import (
|
||||
"go.etcd.io/etcd/v3/clientv3/concurrency"
|
||||
)
|
||||
|
||||
func ExampleMutex_TryLock() {
|
||||
cli, err := clientv3.New(clientv3.Config{Endpoints: endpoints})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer cli.Close()
|
||||
|
||||
// create two separate sessions for lock competition
|
||||
s1, err := concurrency.NewSession(cli)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer s1.Close()
|
||||
m1 := concurrency.NewMutex(s1, "/my-lock")
|
||||
|
||||
s2, err := concurrency.NewSession(cli)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer s2.Close()
|
||||
m2 := concurrency.NewMutex(s2, "/my-lock")
|
||||
|
||||
// acquire lock for s1
|
||||
if err = m1.Lock(context.TODO()); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
func mockMutex_TryLock() {
|
||||
fmt.Println("acquired lock for s1")
|
||||
|
||||
if err = m2.TryLock(context.TODO()); err == nil {
|
||||
log.Fatal("should not acquire lock")
|
||||
}
|
||||
if err == concurrency.ErrLocked {
|
||||
fmt.Println("cannot acquire lock for s2, as already locked in another session")
|
||||
}
|
||||
|
||||
if err = m1.Unlock(context.TODO()); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Println("cannot acquire lock for s2, as already locked in another session")
|
||||
fmt.Println("released lock for s1")
|
||||
if err = m2.TryLock(context.TODO()); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Println("acquired lock for s2")
|
||||
}
|
||||
|
||||
func ExampleMutex_TryLock() {
|
||||
forUnitTestsRunInMockedContext(
|
||||
mockMutex_TryLock,
|
||||
func() {
|
||||
cli, err := clientv3.New(clientv3.Config{Endpoints: exampleEndpoints()})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer cli.Close()
|
||||
|
||||
// create two separate sessions for lock competition
|
||||
s1, err := concurrency.NewSession(cli)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer s1.Close()
|
||||
m1 := concurrency.NewMutex(s1, "/my-lock")
|
||||
|
||||
s2, err := concurrency.NewSession(cli)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer s2.Close()
|
||||
m2 := concurrency.NewMutex(s2, "/my-lock")
|
||||
|
||||
// acquire lock for s1
|
||||
if err = m1.Lock(context.TODO()); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Println("acquired lock for s1")
|
||||
|
||||
if err = m2.TryLock(context.TODO()); err == nil {
|
||||
log.Fatal("should not acquire lock")
|
||||
}
|
||||
if err == concurrency.ErrLocked {
|
||||
fmt.Println("cannot acquire lock for s2, as already locked in another session")
|
||||
}
|
||||
|
||||
if err = m1.Unlock(context.TODO()); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Println("released lock for s1")
|
||||
if err = m2.TryLock(context.TODO()); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Println("acquired lock for s2")
|
||||
})
|
||||
|
||||
// Output:
|
||||
// acquired lock for s1
|
||||
@ -74,50 +85,60 @@ func ExampleMutex_TryLock() {
|
||||
// acquired lock for s2
|
||||
}
|
||||
|
||||
func ExampleMutex_Lock() {
|
||||
cli, err := clientv3.New(clientv3.Config{Endpoints: endpoints})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer cli.Close()
|
||||
|
||||
// create two separate sessions for lock competition
|
||||
s1, err := concurrency.NewSession(cli)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer s1.Close()
|
||||
m1 := concurrency.NewMutex(s1, "/my-lock/")
|
||||
|
||||
s2, err := concurrency.NewSession(cli)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer s2.Close()
|
||||
m2 := concurrency.NewMutex(s2, "/my-lock/")
|
||||
|
||||
// acquire lock for s1
|
||||
if err := m1.Lock(context.TODO()); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
func mockMutex_Lock() {
|
||||
fmt.Println("acquired lock for s1")
|
||||
|
||||
m2Locked := make(chan struct{})
|
||||
go func() {
|
||||
defer close(m2Locked)
|
||||
// wait until s1 is locks /my-lock/
|
||||
if err := m2.Lock(context.TODO()); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}()
|
||||
|
||||
if err := m1.Unlock(context.TODO()); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Println("released lock for s1")
|
||||
|
||||
<-m2Locked
|
||||
fmt.Println("acquired lock for s2")
|
||||
}
|
||||
|
||||
func ExampleMutex_Lock() {
|
||||
forUnitTestsRunInMockedContext(
|
||||
mockMutex_Lock,
|
||||
func() {
|
||||
cli, err := clientv3.New(clientv3.Config{Endpoints: exampleEndpoints()})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer cli.Close()
|
||||
|
||||
// create two separate sessions for lock competition
|
||||
s1, err := concurrency.NewSession(cli)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer s1.Close()
|
||||
m1 := concurrency.NewMutex(s1, "/my-lock/")
|
||||
|
||||
s2, err := concurrency.NewSession(cli)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer s2.Close()
|
||||
m2 := concurrency.NewMutex(s2, "/my-lock/")
|
||||
|
||||
// acquire lock for s1
|
||||
if err := m1.Lock(context.TODO()); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Println("acquired lock for s1")
|
||||
|
||||
m2Locked := make(chan struct{})
|
||||
go func() {
|
||||
defer close(m2Locked)
|
||||
// wait until s1 is locks /my-lock/
|
||||
if err := m2.Lock(context.TODO()); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}()
|
||||
|
||||
if err := m1.Unlock(context.TODO()); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Println("released lock for s1")
|
||||
|
||||
<-m2Locked
|
||||
fmt.Println("acquired lock for s2")
|
||||
})
|
||||
|
||||
// Output:
|
||||
// acquired lock for s1
|
||||
|
Reference in New Issue
Block a user