v3_stm_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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.package recipe
  14. package integration
  15. import (
  16. "fmt"
  17. "math/rand"
  18. "strconv"
  19. "testing"
  20. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  21. v3 "github.com/coreos/etcd/clientv3"
  22. "github.com/coreos/etcd/clientv3/concurrency"
  23. )
  24. // TestSTMConflict tests that conflicts are retried.
  25. func TestSTMConflict(t *testing.T) {
  26. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  27. defer clus.Terminate(t)
  28. etcdc := clus.RandClient()
  29. keys := make([]string, 5)
  30. for i := 0; i < len(keys); i++ {
  31. keys[i] = fmt.Sprintf("foo-%d", i)
  32. if _, err := etcdc.Put(context.TODO(), keys[i], "100"); err != nil {
  33. t.Fatalf("could not make key (%v)", err)
  34. }
  35. }
  36. errc := make(chan error)
  37. for i := range keys {
  38. curEtcdc := clus.RandClient()
  39. srcKey := keys[i]
  40. applyf := func(stm concurrency.STM) error {
  41. src := stm.Get(srcKey)
  42. // must be different key to avoid double-adding
  43. dstKey := srcKey
  44. for dstKey == srcKey {
  45. dstKey = keys[rand.Intn(len(keys))]
  46. }
  47. dst := stm.Get(dstKey)
  48. srcV, _ := strconv.ParseInt(src, 10, 64)
  49. dstV, _ := strconv.ParseInt(dst, 10, 64)
  50. xfer := int64(rand.Intn(int(srcV)) / 2)
  51. stm.Put(srcKey, fmt.Sprintf("%d", srcV-xfer))
  52. stm.Put(dstKey, fmt.Sprintf("%d", dstV+xfer))
  53. return nil
  54. }
  55. go func() {
  56. _, err := concurrency.NewSTMRepeatable(context.TODO(), curEtcdc, applyf)
  57. errc <- err
  58. }()
  59. }
  60. // wait for txns
  61. for range keys {
  62. if err := <-errc; err != nil {
  63. t.Fatalf("apply failed (%v)", err)
  64. }
  65. }
  66. // ensure sum matches initial sum
  67. sum := 0
  68. for _, oldkey := range keys {
  69. rk, err := etcdc.Get(context.TODO(), oldkey)
  70. if err != nil {
  71. t.Fatalf("couldn't fetch key %s (%v)", oldkey, err)
  72. }
  73. v, _ := strconv.ParseInt(string(rk.Kvs[0].Value), 10, 64)
  74. sum += int(v)
  75. }
  76. if sum != len(keys)*100 {
  77. t.Fatalf("bad sum. got %d, expected %d", sum, len(keys)*100)
  78. }
  79. }
  80. // TestSTMPutNewKey confirms a STM put on a new key is visible after commit.
  81. func TestSTMPutNewKey(t *testing.T) {
  82. clus := NewClusterV3(t, &ClusterConfig{Size: 1})
  83. defer clus.Terminate(t)
  84. etcdc := clus.RandClient()
  85. applyf := func(stm concurrency.STM) error {
  86. stm.Put("foo", "bar")
  87. return nil
  88. }
  89. if _, err := concurrency.NewSTMRepeatable(context.TODO(), etcdc, applyf); err != nil {
  90. t.Fatalf("error on stm txn (%v)", err)
  91. }
  92. resp, err := etcdc.Get(context.TODO(), "foo")
  93. if err != nil {
  94. t.Fatalf("error fetching key (%v)", err)
  95. }
  96. if string(resp.Kvs[0].Value) != "bar" {
  97. t.Fatalf("bad value. got %+v, expected 'bar' value", resp)
  98. }
  99. }
  100. // TestSTMAbort tests that an aborted txn does not modify any keys.
  101. func TestSTMAbort(t *testing.T) {
  102. clus := NewClusterV3(t, &ClusterConfig{Size: 1})
  103. defer clus.Terminate(t)
  104. etcdc := clus.RandClient()
  105. ctx, cancel := context.WithCancel(context.TODO())
  106. applyf := func(stm concurrency.STM) error {
  107. stm.Put("foo", "baz")
  108. cancel()
  109. stm.Put("foo", "bap")
  110. return nil
  111. }
  112. if _, err := concurrency.NewSTMRepeatable(ctx, etcdc, applyf); err == nil {
  113. t.Fatalf("no error on stm txn")
  114. }
  115. resp, err := etcdc.Get(context.TODO(), "foo")
  116. if err != nil {
  117. t.Fatalf("error fetching key (%v)", err)
  118. }
  119. if len(resp.Kvs) != 0 {
  120. t.Fatalf("bad value. got %+v, expected nothing", resp)
  121. }
  122. }
  123. // TestSTMSerialize tests that serialization is honored when serializable.
  124. func TestSTMSerialize(t *testing.T) {
  125. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  126. defer clus.Terminate(t)
  127. etcdc := clus.RandClient()
  128. // set up initial keys
  129. keys := make([]string, 5)
  130. for i := 0; i < len(keys); i++ {
  131. keys[i] = fmt.Sprintf("foo-%d", i)
  132. }
  133. // update keys in full batches
  134. updatec := make(chan struct{})
  135. go func() {
  136. defer close(updatec)
  137. for i := 0; i < 5; i++ {
  138. s := fmt.Sprintf("%d", i)
  139. ops := []v3.Op{}
  140. for _, k := range keys {
  141. ops = append(ops, v3.OpPut(k, s))
  142. }
  143. if _, err := etcdc.Txn(context.TODO()).Then(ops...).Commit(); err != nil {
  144. t.Fatalf("couldn't put keys (%v)", err)
  145. }
  146. updatec <- struct{}{}
  147. }
  148. }()
  149. // read all keys in txn, make sure all values match
  150. errc := make(chan error)
  151. for range updatec {
  152. curEtcdc := clus.RandClient()
  153. applyf := func(stm concurrency.STM) error {
  154. vs := []string{}
  155. for i := range keys {
  156. vs = append(vs, stm.Get(keys[i]))
  157. }
  158. for i := range vs {
  159. if vs[0] != vs[i] {
  160. return fmt.Errorf("got vs[%d] = %v, want %v", i, vs[i], vs[0])
  161. }
  162. }
  163. return nil
  164. }
  165. go func() {
  166. _, err := concurrency.NewSTMSerializable(context.TODO(), curEtcdc, applyf)
  167. errc <- err
  168. }()
  169. }
  170. for i := 0; i < 5; i++ {
  171. if err := <-errc; err != nil {
  172. t.Error(err)
  173. }
  174. }
  175. }