v3_stm_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. v3 "github.com/coreos/etcd/clientv3"
  21. "github.com/coreos/etcd/contrib/recipes"
  22. )
  23. // TestSTMConflict tests that conflicts are retried.
  24. func TestSTMConflict(t *testing.T) {
  25. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  26. defer clus.Terminate(t)
  27. etcdc := clus.RandClient()
  28. keys := make([]*recipe.RemoteKV, 5)
  29. for i := 0; i < len(keys); i++ {
  30. rk, err := recipe.NewKV(v3.NewKV(etcdc), fmt.Sprintf("foo-%d", i), "100", 0)
  31. if err != nil {
  32. t.Fatalf("could not make key (%v)", err)
  33. }
  34. keys[i] = rk
  35. }
  36. errc := make([]<-chan error, len(keys))
  37. for i, rk := range keys {
  38. curEtcdc := clus.RandClient()
  39. srcKey := rk.Key()
  40. applyf := func(stm *recipe.STM) error {
  41. src, err := stm.Get(srcKey)
  42. if err != nil {
  43. return err
  44. }
  45. // must be different key to avoid double-adding
  46. dstKey := srcKey
  47. for dstKey == srcKey {
  48. dstKey = keys[rand.Intn(len(keys))].Key()
  49. }
  50. dst, err := stm.Get(dstKey)
  51. if err != nil {
  52. return err
  53. }
  54. srcV, _ := strconv.ParseInt(src, 10, 64)
  55. dstV, _ := strconv.ParseInt(dst, 10, 64)
  56. xfer := int64(rand.Intn(int(srcV)) / 2)
  57. stm.Put(srcKey, fmt.Sprintf("%d", srcV-xfer))
  58. stm.Put(dstKey, fmt.Sprintf("%d", dstV+xfer))
  59. return nil
  60. }
  61. errc[i] = recipe.NewSTM(curEtcdc, applyf)
  62. }
  63. // wait for txns
  64. for _, ch := range errc {
  65. if err := <-ch; err != nil {
  66. t.Fatalf("apply failed (%v)", err)
  67. }
  68. }
  69. // ensure sum matches initial sum
  70. sum := 0
  71. for _, oldRK := range keys {
  72. rk, err := recipe.GetRemoteKV(v3.NewKV(etcdc), oldRK.Key())
  73. if err != nil {
  74. t.Fatalf("couldn't fetch key %s (%v)", oldRK.Key(), err)
  75. }
  76. v, _ := strconv.ParseInt(rk.Value(), 10, 64)
  77. sum += int(v)
  78. }
  79. if sum != len(keys)*100 {
  80. t.Fatalf("bad sum. got %d, expected %d", sum, len(keys)*100)
  81. }
  82. }
  83. // TestSTMPutNewKey confirms a STM put on a new key is visible after commit.
  84. func TestSTMPutNewKey(t *testing.T) {
  85. clus := NewClusterV3(t, &ClusterConfig{Size: 1})
  86. defer clus.Terminate(t)
  87. etcdc := clus.RandClient()
  88. applyf := func(stm *recipe.STM) error {
  89. stm.Put("foo", "bar")
  90. return nil
  91. }
  92. errc := recipe.NewSTM(etcdc, applyf)
  93. if err := <-errc; err != nil {
  94. t.Fatalf("error on stm txn (%v)", err)
  95. }
  96. rk, err := recipe.GetRemoteKV(v3.NewKV(etcdc), "foo")
  97. if err != nil {
  98. t.Fatalf("error fetching key (%v)", err)
  99. }
  100. if rk.Value() != "bar" {
  101. t.Fatalf("bad value. got %v, expected bar", rk.Value())
  102. }
  103. }
  104. // TestSTMAbort tests that an aborted txn does not modify any keys.
  105. func TestSTMAbort(t *testing.T) {
  106. clus := NewClusterV3(t, &ClusterConfig{Size: 1})
  107. defer clus.Terminate(t)
  108. etcdc := clus.RandClient()
  109. applyf := func(stm *recipe.STM) error {
  110. stm.Put("foo", "baz")
  111. stm.Abort()
  112. stm.Put("foo", "baz")
  113. return nil
  114. }
  115. errc := recipe.NewSTM(etcdc, applyf)
  116. if err := <-errc; err != nil {
  117. t.Fatalf("error on stm txn (%v)", err)
  118. }
  119. rk, err := recipe.GetRemoteKV(v3.NewKV(etcdc), "foo")
  120. if err != nil {
  121. t.Fatalf("error fetching key (%v)", err)
  122. }
  123. if rk.Value() != "" {
  124. t.Fatalf("bad value. got %v, expected empty string", rk.Value())
  125. }
  126. }