v3_lock_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // Copyright 2016 The etcd Authors
  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. "sync"
  18. "testing"
  19. "time"
  20. "github.com/coreos/etcd/clientv3"
  21. "github.com/coreos/etcd/clientv3/concurrency"
  22. "github.com/coreos/etcd/contrib/recipes"
  23. "golang.org/x/net/context"
  24. )
  25. func TestMutexSingleNode(t *testing.T) {
  26. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  27. defer clus.Terminate(t)
  28. var clients []*clientv3.Client
  29. testMutex(t, 5, makeSingleNodeClients(t, clus.cluster, &clients))
  30. closeClients(t, clients)
  31. }
  32. func TestMutexMultiNode(t *testing.T) {
  33. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  34. defer clus.Terminate(t)
  35. var clients []*clientv3.Client
  36. testMutex(t, 5, makeMultiNodeClients(t, clus.cluster, &clients))
  37. closeClients(t, clients)
  38. }
  39. func testMutex(t *testing.T, waiters int, chooseClient func() *clientv3.Client) {
  40. // stream lock acquisitions
  41. lockedC := make(chan *concurrency.Mutex)
  42. for i := 0; i < waiters; i++ {
  43. go func() {
  44. m := concurrency.NewMutex(chooseClient(), "test-mutex")
  45. if err := m.Lock(context.TODO()); err != nil {
  46. t.Fatalf("could not wait on lock (%v)", err)
  47. }
  48. lockedC <- m
  49. }()
  50. }
  51. // unlock locked mutexes
  52. timerC := time.After(time.Duration(waiters) * time.Second)
  53. for i := 0; i < waiters; i++ {
  54. select {
  55. case <-timerC:
  56. t.Fatalf("timed out waiting for lock %d", i)
  57. case m := <-lockedC:
  58. // lock acquired with m
  59. select {
  60. case <-lockedC:
  61. t.Fatalf("lock %d followers did not wait", i)
  62. default:
  63. }
  64. if err := m.Unlock(context.TODO()); err != nil {
  65. t.Fatalf("could not release lock (%v)", err)
  66. }
  67. }
  68. }
  69. }
  70. // TestMutexSessionRelock ensures that acquiring the same lock with the same
  71. // session will not result in deadlock.
  72. func TestMutexSessionRelock(t *testing.T) {
  73. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  74. defer clus.Terminate(t)
  75. cli := clus.RandClient()
  76. m := concurrency.NewMutex(cli, "test-mutex")
  77. if err := m.Lock(context.TODO()); err != nil {
  78. t.Fatal(err)
  79. }
  80. m2 := concurrency.NewMutex(cli, "test-mutex")
  81. if err := m2.Lock(context.TODO()); err != nil {
  82. t.Fatal(err)
  83. }
  84. }
  85. func BenchmarkMutex4Waiters(b *testing.B) {
  86. // XXX switch tests to use TB interface
  87. clus := NewClusterV3(nil, &ClusterConfig{Size: 3})
  88. defer clus.Terminate(nil)
  89. for i := 0; i < b.N; i++ {
  90. testMutex(nil, 4, func() *clientv3.Client { return clus.RandClient() })
  91. }
  92. }
  93. func TestRWMutexSingleNode(t *testing.T) {
  94. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  95. defer clus.Terminate(t)
  96. testRWMutex(t, 5, func() *clientv3.Client { return clus.clients[0] })
  97. }
  98. func TestRWMutexMultiNode(t *testing.T) {
  99. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  100. defer clus.Terminate(t)
  101. testRWMutex(t, 5, func() *clientv3.Client { return clus.RandClient() })
  102. }
  103. func testRWMutex(t *testing.T, waiters int, chooseClient func() *clientv3.Client) {
  104. // stream rwlock acquistions
  105. rlockedC := make(chan *recipe.RWMutex, 1)
  106. wlockedC := make(chan *recipe.RWMutex, 1)
  107. for i := 0; i < waiters; i++ {
  108. go func() {
  109. rwm := recipe.NewRWMutex(chooseClient(), "test-rwmutex")
  110. if rand.Intn(1) == 0 {
  111. if err := rwm.RLock(); err != nil {
  112. t.Fatalf("could not rlock (%v)", err)
  113. }
  114. rlockedC <- rwm
  115. } else {
  116. if err := rwm.Lock(); err != nil {
  117. t.Fatalf("could not lock (%v)", err)
  118. }
  119. wlockedC <- rwm
  120. }
  121. }()
  122. }
  123. // unlock locked rwmutexes
  124. timerC := time.After(time.Duration(waiters) * time.Second)
  125. for i := 0; i < waiters; i++ {
  126. select {
  127. case <-timerC:
  128. t.Fatalf("timed out waiting for lock %d", i)
  129. case wl := <-wlockedC:
  130. select {
  131. case <-rlockedC:
  132. t.Fatalf("rlock %d readers did not wait", i)
  133. default:
  134. }
  135. if err := wl.Unlock(); err != nil {
  136. t.Fatalf("could not release lock (%v)", err)
  137. }
  138. case rl := <-rlockedC:
  139. select {
  140. case <-wlockedC:
  141. t.Fatalf("rlock %d writers did not wait", i)
  142. default:
  143. }
  144. if err := rl.RUnlock(); err != nil {
  145. t.Fatalf("could not release rlock (%v)", err)
  146. }
  147. }
  148. }
  149. }
  150. func makeClients(t *testing.T, clients *[]*clientv3.Client, choose func() *member) func() *clientv3.Client {
  151. var mu sync.Mutex
  152. *clients = nil
  153. return func() *clientv3.Client {
  154. cli, err := NewClientV3(choose())
  155. if err != nil {
  156. t.Fatalf("cannot create client: %v", err)
  157. }
  158. mu.Lock()
  159. *clients = append(*clients, cli)
  160. mu.Unlock()
  161. return cli
  162. }
  163. }
  164. func makeSingleNodeClients(t *testing.T, clus *cluster, clients *[]*clientv3.Client) func() *clientv3.Client {
  165. return makeClients(t, clients, func() *member {
  166. return clus.Members[0]
  167. })
  168. }
  169. func makeMultiNodeClients(t *testing.T, clus *cluster, clients *[]*clientv3.Client) func() *clientv3.Client {
  170. return makeClients(t, clients, func() *member {
  171. return clus.Members[rand.Intn(len(clus.Members))]
  172. })
  173. }
  174. func closeClients(t *testing.T, clients []*clientv3.Client) {
  175. for _, cli := range clients {
  176. if err := cli.Close(); err != nil {
  177. t.Fatal(err)
  178. }
  179. }
  180. }