stats_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package store
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/coreos/etcd/third_party/github.com/stretchr/testify/assert"
  6. )
  7. // Ensure that a successful Get is recorded in the stats.
  8. func TestStoreStatsGetSuccess(t *testing.T) {
  9. s := newStore()
  10. s.Create("/foo", false, "bar", false, Permanent)
  11. s.Get("/foo", false, false)
  12. assert.Equal(t, uint64(1), s.Stats.GetSuccess, "")
  13. }
  14. // Ensure that a failed Get is recorded in the stats.
  15. func TestStoreStatsGetFail(t *testing.T) {
  16. s := newStore()
  17. s.Create("/foo", false, "bar", false, Permanent)
  18. s.Get("/no_such_key", false, false)
  19. assert.Equal(t, uint64(1), s.Stats.GetFail, "")
  20. }
  21. // Ensure that a successful Create is recorded in the stats.
  22. func TestStoreStatsCreateSuccess(t *testing.T) {
  23. s := newStore()
  24. s.Create("/foo", false, "bar", false, Permanent)
  25. assert.Equal(t, uint64(1), s.Stats.CreateSuccess, "")
  26. }
  27. // Ensure that a failed Create is recorded in the stats.
  28. func TestStoreStatsCreateFail(t *testing.T) {
  29. s := newStore()
  30. s.Create("/foo", true, "", false, Permanent)
  31. s.Create("/foo", false, "bar", false, Permanent)
  32. assert.Equal(t, uint64(1), s.Stats.CreateFail, "")
  33. }
  34. // Ensure that a successful Update is recorded in the stats.
  35. func TestStoreStatsUpdateSuccess(t *testing.T) {
  36. s := newStore()
  37. s.Create("/foo", false, "bar", false, Permanent)
  38. s.Update("/foo", "baz", Permanent)
  39. assert.Equal(t, uint64(1), s.Stats.UpdateSuccess, "")
  40. }
  41. // Ensure that a failed Update is recorded in the stats.
  42. func TestStoreStatsUpdateFail(t *testing.T) {
  43. s := newStore()
  44. s.Update("/foo", "bar", Permanent)
  45. assert.Equal(t, uint64(1), s.Stats.UpdateFail, "")
  46. }
  47. // Ensure that a successful CAS is recorded in the stats.
  48. func TestStoreStatsCompareAndSwapSuccess(t *testing.T) {
  49. s := newStore()
  50. s.Create("/foo", false, "bar", false, Permanent)
  51. s.CompareAndSwap("/foo", "bar", 0, "baz", Permanent)
  52. assert.Equal(t, uint64(1), s.Stats.CompareAndSwapSuccess, "")
  53. }
  54. // Ensure that a failed CAS is recorded in the stats.
  55. func TestStoreStatsCompareAndSwapFail(t *testing.T) {
  56. s := newStore()
  57. s.Create("/foo", false, "bar", false, Permanent)
  58. s.CompareAndSwap("/foo", "wrong_value", 0, "baz", Permanent)
  59. assert.Equal(t, uint64(1), s.Stats.CompareAndSwapFail, "")
  60. }
  61. // Ensure that a successful Delete is recorded in the stats.
  62. func TestStoreStatsDeleteSuccess(t *testing.T) {
  63. s := newStore()
  64. s.Create("/foo", false, "bar", false, Permanent)
  65. s.Delete("/foo", false, false)
  66. assert.Equal(t, uint64(1), s.Stats.DeleteSuccess, "")
  67. }
  68. // Ensure that a failed Delete is recorded in the stats.
  69. func TestStoreStatsDeleteFail(t *testing.T) {
  70. s := newStore()
  71. s.Delete("/foo", false, false)
  72. assert.Equal(t, uint64(1), s.Stats.DeleteFail, "")
  73. }
  74. //Ensure that the number of expirations is recorded in the stats.
  75. func TestStoreStatsExpireCount(t *testing.T) {
  76. s := newStore()
  77. c := make(chan bool)
  78. defer func() {
  79. c <- true
  80. }()
  81. go mockSyncService(s.DeleteExpiredKeys, c)
  82. s.Create("/foo", false, "bar", false, time.Now().Add(500*time.Millisecond))
  83. assert.Equal(t, uint64(0), s.Stats.ExpireCount, "")
  84. time.Sleep(600 * time.Millisecond)
  85. assert.Equal(t, uint64(1), s.Stats.ExpireCount, "")
  86. }