v3_double_barrier_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. "testing"
  17. "time"
  18. "github.com/coreos/etcd/clientv3/concurrency"
  19. "github.com/coreos/etcd/contrib/recipes"
  20. )
  21. func TestDoubleBarrier(t *testing.T) {
  22. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  23. defer clus.Terminate(t)
  24. defer dropSessionLease(clus)
  25. waiters := 10
  26. b := recipe.NewDoubleBarrier(clus.RandClient(), "test-barrier", waiters)
  27. donec := make(chan struct{})
  28. for i := 0; i < waiters-1; i++ {
  29. go func() {
  30. bb := recipe.NewDoubleBarrier(clus.RandClient(), "test-barrier", waiters)
  31. if err := bb.Enter(); err != nil {
  32. t.Fatalf("could not enter on barrier (%v)", err)
  33. }
  34. donec <- struct{}{}
  35. if err := bb.Leave(); err != nil {
  36. t.Fatalf("could not leave on barrier (%v)", err)
  37. }
  38. donec <- struct{}{}
  39. }()
  40. }
  41. time.Sleep(10 * time.Millisecond)
  42. select {
  43. case <-donec:
  44. t.Fatalf("barrier did not enter-wait")
  45. default:
  46. }
  47. if err := b.Enter(); err != nil {
  48. t.Fatalf("could not enter last barrier (%v)", err)
  49. }
  50. timerC := time.After(time.Duration(waiters*100) * time.Millisecond)
  51. for i := 0; i < waiters-1; i++ {
  52. select {
  53. case <-timerC:
  54. t.Fatalf("barrier enter timed out")
  55. case <-donec:
  56. }
  57. }
  58. time.Sleep(10 * time.Millisecond)
  59. select {
  60. case <-donec:
  61. t.Fatalf("barrier did not leave-wait")
  62. default:
  63. }
  64. b.Leave()
  65. timerC = time.After(time.Duration(waiters*100) * time.Millisecond)
  66. for i := 0; i < waiters-1; i++ {
  67. select {
  68. case <-timerC:
  69. t.Fatalf("barrier leave timed out")
  70. case <-donec:
  71. }
  72. }
  73. }
  74. func TestDoubleBarrierFailover(t *testing.T) {
  75. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  76. defer clus.Terminate(t)
  77. defer dropSessionLease(clus)
  78. waiters := 10
  79. donec := make(chan struct{})
  80. // sacrificial barrier holder; lease will be revoked
  81. go func() {
  82. b := recipe.NewDoubleBarrier(clus.clients[0], "test-barrier", waiters)
  83. if err := b.Enter(); err != nil {
  84. t.Fatalf("could not enter on barrier (%v)", err)
  85. }
  86. donec <- struct{}{}
  87. }()
  88. for i := 0; i < waiters-1; i++ {
  89. go func() {
  90. b := recipe.NewDoubleBarrier(clus.clients[1], "test-barrier", waiters)
  91. if err := b.Enter(); err != nil {
  92. t.Fatalf("could not enter on barrier (%v)", err)
  93. }
  94. donec <- struct{}{}
  95. b.Leave()
  96. donec <- struct{}{}
  97. }()
  98. }
  99. // wait for barrier enter to unblock
  100. for i := 0; i < waiters; i++ {
  101. select {
  102. case <-donec:
  103. case <-time.After(10 * time.Second):
  104. t.Fatalf("timed out waiting for enter, %d", i)
  105. }
  106. }
  107. // kill lease, expect Leave unblock
  108. s, err := concurrency.NewSession(clus.clients[0])
  109. if err != nil {
  110. t.Fatal(err)
  111. }
  112. if err = s.Close(); err != nil {
  113. t.Fatal(err)
  114. }
  115. // join on rest of waiters
  116. for i := 0; i < waiters-1; i++ {
  117. select {
  118. case <-donec:
  119. case <-time.After(10 * time.Second):
  120. t.Fatalf("timed out waiting for leave, %d", i)
  121. }
  122. }
  123. }
  124. func dropSessionLease(clus *ClusterV3) {
  125. for _, client := range clus.clients {
  126. s, _ := concurrency.NewSession(client)
  127. s.Orphan()
  128. }
  129. }