stats_test.go 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package store
  2. import (
  3. "testing"
  4. "time"
  5. "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", "bar", false, Permanent, 3, 1)
  11. s.Get("/foo", false, false, 3, 1)
  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", "bar", false, Permanent, 3, 1)
  18. s.Get("/no_such_key", false, false, 3, 1)
  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", "bar", false, Permanent, 3, 1)
  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", "", false, Permanent, 3, 1)
  31. s.Create("/foo", "bar", false, Permanent, 4, 1)
  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", "bar", false, Permanent, 3, 1)
  38. s.Update("/foo", "baz", Permanent, 4, 1)
  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, 4, 1)
  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", "bar", false, Permanent, 3, 1)
  51. s.CompareAndSwap("/foo", "bar", 0, "baz", Permanent, 4, 1)
  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", "bar", false, Permanent, 3, 1)
  58. s.CompareAndSwap("/foo", "wrong_value", 0, "baz", Permanent, 4, 1)
  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", "bar", false, Permanent, 3, 1)
  65. s.Delete("/foo", false, 4, 1)
  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, 4, 1)
  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. s.Create("/foo", "bar", false, time.Now().Add(5 * time.Millisecond), 3, 1)
  78. assert.Equal(t, uint64(0), s.Stats.ExpireCount, "")
  79. time.Sleep(10 * time.Millisecond)
  80. assert.Equal(t, uint64(1), s.Stats.ExpireCount, "")
  81. }