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