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