stats_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright 2015 The etcd Authors
  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 v2store
  15. import (
  16. "testing"
  17. "time"
  18. "go.etcd.io/etcd/pkg/testutil"
  19. )
  20. // Ensure that a successful Get is recorded in the stats.
  21. func TestStoreStatsGetSuccess(t *testing.T) {
  22. s := newStore()
  23. s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent})
  24. s.Get("/foo", false, false)
  25. testutil.AssertEqual(t, uint64(1), s.Stats.GetSuccess, "")
  26. }
  27. // Ensure that a failed Get is recorded in the stats.
  28. func TestStoreStatsGetFail(t *testing.T) {
  29. s := newStore()
  30. s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent})
  31. s.Get("/no_such_key", false, false)
  32. testutil.AssertEqual(t, uint64(1), s.Stats.GetFail, "")
  33. }
  34. // Ensure that a successful Create is recorded in the stats.
  35. func TestStoreStatsCreateSuccess(t *testing.T) {
  36. s := newStore()
  37. s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent})
  38. testutil.AssertEqual(t, uint64(1), s.Stats.CreateSuccess, "")
  39. }
  40. // Ensure that a failed Create is recorded in the stats.
  41. func TestStoreStatsCreateFail(t *testing.T) {
  42. s := newStore()
  43. s.Create("/foo", true, "", false, TTLOptionSet{ExpireTime: Permanent})
  44. s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent})
  45. testutil.AssertEqual(t, uint64(1), s.Stats.CreateFail, "")
  46. }
  47. // Ensure that a successful Update is recorded in the stats.
  48. func TestStoreStatsUpdateSuccess(t *testing.T) {
  49. s := newStore()
  50. s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent})
  51. s.Update("/foo", "baz", TTLOptionSet{ExpireTime: Permanent})
  52. testutil.AssertEqual(t, uint64(1), s.Stats.UpdateSuccess, "")
  53. }
  54. // Ensure that a failed Update is recorded in the stats.
  55. func TestStoreStatsUpdateFail(t *testing.T) {
  56. s := newStore()
  57. s.Update("/foo", "bar", TTLOptionSet{ExpireTime: Permanent})
  58. testutil.AssertEqual(t, uint64(1), s.Stats.UpdateFail, "")
  59. }
  60. // Ensure that a successful CAS is recorded in the stats.
  61. func TestStoreStatsCompareAndSwapSuccess(t *testing.T) {
  62. s := newStore()
  63. s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent})
  64. s.CompareAndSwap("/foo", "bar", 0, "baz", TTLOptionSet{ExpireTime: Permanent})
  65. testutil.AssertEqual(t, uint64(1), s.Stats.CompareAndSwapSuccess, "")
  66. }
  67. // Ensure that a failed CAS is recorded in the stats.
  68. func TestStoreStatsCompareAndSwapFail(t *testing.T) {
  69. s := newStore()
  70. s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent})
  71. s.CompareAndSwap("/foo", "wrong_value", 0, "baz", TTLOptionSet{ExpireTime: Permanent})
  72. testutil.AssertEqual(t, uint64(1), s.Stats.CompareAndSwapFail, "")
  73. }
  74. // Ensure that a successful Delete is recorded in the stats.
  75. func TestStoreStatsDeleteSuccess(t *testing.T) {
  76. s := newStore()
  77. s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent})
  78. s.Delete("/foo", false, false)
  79. testutil.AssertEqual(t, uint64(1), s.Stats.DeleteSuccess, "")
  80. }
  81. // Ensure that a failed Delete is recorded in the stats.
  82. func TestStoreStatsDeleteFail(t *testing.T) {
  83. s := newStore()
  84. s.Delete("/foo", false, false)
  85. testutil.AssertEqual(t, uint64(1), s.Stats.DeleteFail, "")
  86. }
  87. //Ensure that the number of expirations is recorded in the stats.
  88. func TestStoreStatsExpireCount(t *testing.T) {
  89. s := newStore()
  90. fc := newFakeClock()
  91. s.clock = fc
  92. s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond)})
  93. testutil.AssertEqual(t, uint64(0), s.Stats.ExpireCount, "")
  94. fc.Advance(600 * time.Millisecond)
  95. s.DeleteExpiredKeys(fc.Now())
  96. testutil.AssertEqual(t, uint64(1), s.Stats.ExpireCount, "")
  97. }