v3_lock_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright 2016 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package integration
  15. import (
  16. "math/rand"
  17. "testing"
  18. "time"
  19. "github.com/coreos/etcd/Godeps/_workspace/src/google.golang.org/grpc"
  20. "github.com/coreos/etcd/contrib/recipes"
  21. )
  22. func TestMutexSingleNode(t *testing.T) {
  23. clus := newClusterGRPC(t, &clusterConfig{size: 3})
  24. defer clus.Terminate(t)
  25. testMutex(t, 5, func() *grpc.ClientConn { return clus.conns[0] })
  26. }
  27. func TestMutexMultiNode(t *testing.T) {
  28. clus := newClusterGRPC(t, &clusterConfig{size: 3})
  29. defer clus.Terminate(t)
  30. testMutex(t, 5, func() *grpc.ClientConn { return clus.RandConn() })
  31. }
  32. func testMutex(t *testing.T, waiters int, chooseConn func() *grpc.ClientConn) {
  33. // stream lock acquistions
  34. lockedC := make(chan *recipe.Mutex, 1)
  35. for i := 0; i < waiters; i++ {
  36. go func() {
  37. m := recipe.NewMutex(recipe.NewEtcdClient(chooseConn()), "test-mutex")
  38. if err := m.Lock(); err != nil {
  39. t.Fatalf("could not wait on lock (%v)", err)
  40. }
  41. lockedC <- m
  42. }()
  43. }
  44. // unlock locked mutexes
  45. timerC := time.After(time.Duration(waiters) * time.Second)
  46. for i := 0; i < waiters; i++ {
  47. select {
  48. case <-timerC:
  49. t.Fatalf("timed out waiting for lock %d", i)
  50. case m := <-lockedC:
  51. // lock acquired with m
  52. select {
  53. case <-lockedC:
  54. t.Fatalf("lock %d followers did not wait", i)
  55. default:
  56. }
  57. if err := m.Unlock(); err != nil {
  58. t.Fatalf("could not release lock (%v)", err)
  59. }
  60. }
  61. }
  62. }
  63. func BenchmarkMutex4Waiters(b *testing.B) {
  64. // XXX switch tests to use TB interface
  65. clus := newClusterGRPC(nil, &clusterConfig{size: 3})
  66. defer clus.Terminate(nil)
  67. for i := 0; i < b.N; i++ {
  68. testMutex(nil, 4, func() *grpc.ClientConn { return clus.RandConn() })
  69. }
  70. }
  71. func TestRWMutexSingleNode(t *testing.T) {
  72. clus := newClusterGRPC(t, &clusterConfig{size: 3})
  73. defer clus.Terminate(t)
  74. testRWMutex(t, 5, func() *grpc.ClientConn { return clus.conns[0] })
  75. }
  76. func TestRWMutexMultiNode(t *testing.T) {
  77. clus := newClusterGRPC(t, &clusterConfig{size: 3})
  78. defer clus.Terminate(t)
  79. testRWMutex(t, 5, func() *grpc.ClientConn { return clus.RandConn() })
  80. }
  81. func testRWMutex(t *testing.T, waiters int, chooseConn func() *grpc.ClientConn) {
  82. // stream rwlock acquistions
  83. rlockedC := make(chan *recipe.RWMutex, 1)
  84. wlockedC := make(chan *recipe.RWMutex, 1)
  85. for i := 0; i < waiters; i++ {
  86. go func() {
  87. rwm := recipe.NewRWMutex(recipe.NewEtcdClient(chooseConn()), "test-rwmutex")
  88. if rand.Intn(1) == 0 {
  89. if err := rwm.RLock(); err != nil {
  90. t.Fatalf("could not rlock (%v)", err)
  91. }
  92. rlockedC <- rwm
  93. } else {
  94. if err := rwm.Lock(); err != nil {
  95. t.Fatalf("could not lock (%v)", err)
  96. }
  97. wlockedC <- rwm
  98. }
  99. }()
  100. }
  101. // unlock locked rwmutexes
  102. timerC := time.After(time.Duration(waiters) * time.Second)
  103. for i := 0; i < waiters; i++ {
  104. select {
  105. case <-timerC:
  106. t.Fatalf("timed out waiting for lock %d", i)
  107. case wl := <-wlockedC:
  108. select {
  109. case <-rlockedC:
  110. t.Fatalf("rlock %d readers did not wait", i)
  111. default:
  112. }
  113. if err := wl.Unlock(); err != nil {
  114. t.Fatalf("could not release lock (%v)", err)
  115. }
  116. case rl := <-rlockedC:
  117. select {
  118. case <-wlockedC:
  119. t.Fatalf("rlock %d writers did not wait", i)
  120. default:
  121. }
  122. if err := rl.RUnlock(); err != nil {
  123. t.Fatalf("could not release rlock (%v)", err)
  124. }
  125. }
  126. }
  127. }