v3_lock_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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/golang.org/x/net/context"
  20. "github.com/coreos/etcd/clientv3"
  21. "github.com/coreos/etcd/clientv3/concurrency"
  22. "github.com/coreos/etcd/contrib/recipes"
  23. )
  24. func TestMutexSingleNode(t *testing.T) {
  25. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  26. defer clus.Terminate(t)
  27. testMutex(t, 5, func() *clientv3.Client { return clus.clients[0] })
  28. }
  29. func TestMutexMultiNode(t *testing.T) {
  30. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  31. defer clus.Terminate(t)
  32. testMutex(t, 5, func() *clientv3.Client { return clus.RandClient() })
  33. }
  34. func testMutex(t *testing.T, waiters int, chooseClient func() *clientv3.Client) {
  35. // stream lock acquisitions
  36. lockedC := make(chan *concurrency.Mutex, 1)
  37. for i := 0; i < waiters; i++ {
  38. go func() {
  39. m := concurrency.NewMutex(chooseClient(), "test-mutex")
  40. if err := m.Lock(context.TODO()); err != nil {
  41. t.Fatalf("could not wait on lock (%v)", err)
  42. }
  43. lockedC <- m
  44. }()
  45. }
  46. // unlock locked mutexes
  47. timerC := time.After(time.Duration(waiters) * time.Second)
  48. for i := 0; i < waiters; i++ {
  49. select {
  50. case <-timerC:
  51. t.Fatalf("timed out waiting for lock %d", i)
  52. case m := <-lockedC:
  53. // lock acquired with m
  54. select {
  55. case <-lockedC:
  56. t.Fatalf("lock %d followers did not wait", i)
  57. default:
  58. }
  59. if err := m.Unlock(); err != nil {
  60. t.Fatalf("could not release lock (%v)", err)
  61. }
  62. }
  63. }
  64. }
  65. func BenchmarkMutex4Waiters(b *testing.B) {
  66. // XXX switch tests to use TB interface
  67. clus := NewClusterV3(nil, &ClusterConfig{Size: 3})
  68. defer clus.Terminate(nil)
  69. for i := 0; i < b.N; i++ {
  70. testMutex(nil, 4, func() *clientv3.Client { return clus.RandClient() })
  71. }
  72. }
  73. func TestRWMutexSingleNode(t *testing.T) {
  74. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  75. defer clus.Terminate(t)
  76. testRWMutex(t, 5, func() *clientv3.Client { return clus.clients[0] })
  77. }
  78. func TestRWMutexMultiNode(t *testing.T) {
  79. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  80. defer clus.Terminate(t)
  81. testRWMutex(t, 5, func() *clientv3.Client { return clus.RandClient() })
  82. }
  83. func testRWMutex(t *testing.T, waiters int, chooseClient func() *clientv3.Client) {
  84. // stream rwlock acquistions
  85. rlockedC := make(chan *recipe.RWMutex, 1)
  86. wlockedC := make(chan *recipe.RWMutex, 1)
  87. for i := 0; i < waiters; i++ {
  88. go func() {
  89. rwm := recipe.NewRWMutex(chooseClient(), "test-rwmutex")
  90. if rand.Intn(1) == 0 {
  91. if err := rwm.RLock(); err != nil {
  92. t.Fatalf("could not rlock (%v)", err)
  93. }
  94. rlockedC <- rwm
  95. } else {
  96. if err := rwm.Lock(); err != nil {
  97. t.Fatalf("could not lock (%v)", err)
  98. }
  99. wlockedC <- rwm
  100. }
  101. }()
  102. }
  103. // unlock locked rwmutexes
  104. timerC := time.After(time.Duration(waiters) * time.Second)
  105. for i := 0; i < waiters; i++ {
  106. select {
  107. case <-timerC:
  108. t.Fatalf("timed out waiting for lock %d", i)
  109. case wl := <-wlockedC:
  110. select {
  111. case <-rlockedC:
  112. t.Fatalf("rlock %d readers did not wait", i)
  113. default:
  114. }
  115. if err := wl.Unlock(); err != nil {
  116. t.Fatalf("could not release lock (%v)", err)
  117. }
  118. case rl := <-rlockedC:
  119. select {
  120. case <-wlockedC:
  121. t.Fatalf("rlock %d writers did not wait", i)
  122. default:
  123. }
  124. if err := rl.RUnlock(); err != nil {
  125. t.Fatalf("could not release rlock (%v)", err)
  126. }
  127. }
  128. }
  129. }