2015-01-25 06:19:16 +03:00
|
|
|
// Copyright 2015 CoreOS, Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2014-10-18 02:41:22 +04:00
|
|
|
|
2013-09-29 03:26:19 +04:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2013-11-06 09:56:21 +04:00
|
|
|
"time"
|
2013-10-20 23:39:29 +04:00
|
|
|
|
2014-10-14 11:35:28 +04:00
|
|
|
"github.com/coreos/etcd/Godeps/_workspace/src/github.com/stretchr/testify/assert"
|
2013-09-29 03:26:19 +04:00
|
|
|
)
|
|
|
|
|
2013-10-20 23:39:29 +04:00
|
|
|
// Ensure that a successful Get is recorded in the stats.
|
|
|
|
func TestStoreStatsGetSuccess(t *testing.T) {
|
2013-10-14 21:12:30 +04:00
|
|
|
s := newStore()
|
2013-12-06 02:48:32 +04:00
|
|
|
s.Create("/foo", false, "bar", false, Permanent)
|
2013-11-10 05:55:54 +04:00
|
|
|
s.Get("/foo", false, false)
|
2013-10-20 23:39:29 +04:00
|
|
|
assert.Equal(t, uint64(1), s.Stats.GetSuccess, "")
|
|
|
|
}
|
2013-09-29 03:26:19 +04:00
|
|
|
|
2013-10-20 23:39:29 +04:00
|
|
|
// Ensure that a failed Get is recorded in the stats.
|
|
|
|
func TestStoreStatsGetFail(t *testing.T) {
|
|
|
|
s := newStore()
|
2013-12-06 02:48:32 +04:00
|
|
|
s.Create("/foo", false, "bar", false, Permanent)
|
2013-11-10 05:55:54 +04:00
|
|
|
s.Get("/no_such_key", false, false)
|
2013-10-20 23:39:29 +04:00
|
|
|
assert.Equal(t, uint64(1), s.Stats.GetFail, "")
|
|
|
|
}
|
2013-09-29 03:26:19 +04:00
|
|
|
|
2013-10-20 23:39:29 +04:00
|
|
|
// Ensure that a successful Create is recorded in the stats.
|
|
|
|
func TestStoreStatsCreateSuccess(t *testing.T) {
|
|
|
|
s := newStore()
|
2013-12-06 02:48:32 +04:00
|
|
|
s.Create("/foo", false, "bar", false, Permanent)
|
2013-10-20 23:39:29 +04:00
|
|
|
assert.Equal(t, uint64(1), s.Stats.CreateSuccess, "")
|
|
|
|
}
|
2013-09-29 03:26:19 +04:00
|
|
|
|
2013-10-20 23:39:29 +04:00
|
|
|
// Ensure that a failed Create is recorded in the stats.
|
|
|
|
func TestStoreStatsCreateFail(t *testing.T) {
|
|
|
|
s := newStore()
|
2013-12-06 02:48:32 +04:00
|
|
|
s.Create("/foo", true, "", false, Permanent)
|
|
|
|
s.Create("/foo", false, "bar", false, Permanent)
|
2013-10-20 23:39:29 +04:00
|
|
|
assert.Equal(t, uint64(1), s.Stats.CreateFail, "")
|
|
|
|
}
|
2013-09-29 03:26:19 +04:00
|
|
|
|
2013-10-20 23:39:29 +04:00
|
|
|
// Ensure that a successful Update is recorded in the stats.
|
|
|
|
func TestStoreStatsUpdateSuccess(t *testing.T) {
|
|
|
|
s := newStore()
|
2013-12-06 02:48:32 +04:00
|
|
|
s.Create("/foo", false, "bar", false, Permanent)
|
2013-11-10 05:55:54 +04:00
|
|
|
s.Update("/foo", "baz", Permanent)
|
2013-10-20 23:39:29 +04:00
|
|
|
assert.Equal(t, uint64(1), s.Stats.UpdateSuccess, "")
|
|
|
|
}
|
2013-09-29 03:26:19 +04:00
|
|
|
|
2013-10-20 23:39:29 +04:00
|
|
|
// Ensure that a failed Update is recorded in the stats.
|
|
|
|
func TestStoreStatsUpdateFail(t *testing.T) {
|
|
|
|
s := newStore()
|
2013-11-10 05:55:54 +04:00
|
|
|
s.Update("/foo", "bar", Permanent)
|
2013-10-20 23:39:29 +04:00
|
|
|
assert.Equal(t, uint64(1), s.Stats.UpdateFail, "")
|
|
|
|
}
|
2013-09-30 10:39:40 +04:00
|
|
|
|
2013-10-20 23:39:29 +04:00
|
|
|
// Ensure that a successful CAS is recorded in the stats.
|
|
|
|
func TestStoreStatsCompareAndSwapSuccess(t *testing.T) {
|
|
|
|
s := newStore()
|
2013-12-06 02:48:32 +04:00
|
|
|
s.Create("/foo", false, "bar", false, Permanent)
|
2013-11-10 05:55:54 +04:00
|
|
|
s.CompareAndSwap("/foo", "bar", 0, "baz", Permanent)
|
2013-10-20 23:39:29 +04:00
|
|
|
assert.Equal(t, uint64(1), s.Stats.CompareAndSwapSuccess, "")
|
|
|
|
}
|
2013-09-30 10:39:40 +04:00
|
|
|
|
2013-10-20 23:39:29 +04:00
|
|
|
// Ensure that a failed CAS is recorded in the stats.
|
|
|
|
func TestStoreStatsCompareAndSwapFail(t *testing.T) {
|
|
|
|
s := newStore()
|
2013-12-06 02:48:32 +04:00
|
|
|
s.Create("/foo", false, "bar", false, Permanent)
|
2013-11-10 05:55:54 +04:00
|
|
|
s.CompareAndSwap("/foo", "wrong_value", 0, "baz", Permanent)
|
2013-10-20 23:39:29 +04:00
|
|
|
assert.Equal(t, uint64(1), s.Stats.CompareAndSwapFail, "")
|
|
|
|
}
|
2013-09-30 10:39:40 +04:00
|
|
|
|
2013-10-20 23:39:29 +04:00
|
|
|
// Ensure that a successful Delete is recorded in the stats.
|
|
|
|
func TestStoreStatsDeleteSuccess(t *testing.T) {
|
|
|
|
s := newStore()
|
2013-12-06 02:48:32 +04:00
|
|
|
s.Create("/foo", false, "bar", false, Permanent)
|
|
|
|
s.Delete("/foo", false, false)
|
2013-10-20 23:39:29 +04:00
|
|
|
assert.Equal(t, uint64(1), s.Stats.DeleteSuccess, "")
|
|
|
|
}
|
2013-09-30 10:39:40 +04:00
|
|
|
|
2013-10-20 23:39:29 +04:00
|
|
|
// Ensure that a failed Delete is recorded in the stats.
|
|
|
|
func TestStoreStatsDeleteFail(t *testing.T) {
|
|
|
|
s := newStore()
|
2013-12-06 02:48:32 +04:00
|
|
|
s.Delete("/foo", false, false)
|
2013-10-20 23:39:29 +04:00
|
|
|
assert.Equal(t, uint64(1), s.Stats.DeleteFail, "")
|
|
|
|
}
|
2013-09-30 10:39:40 +04:00
|
|
|
|
2013-11-06 09:56:21 +04:00
|
|
|
//Ensure that the number of expirations is recorded in the stats.
|
|
|
|
func TestStoreStatsExpireCount(t *testing.T) {
|
|
|
|
s := newStore()
|
2014-10-17 08:32:48 +04:00
|
|
|
fc := newFakeClock()
|
2014-10-17 02:19:00 +04:00
|
|
|
s.clock = fc
|
2013-11-12 09:31:12 +04:00
|
|
|
|
2014-10-17 02:19:00 +04:00
|
|
|
s.Create("/foo", false, "bar", false, fc.Now().Add(500*time.Millisecond))
|
2013-11-06 09:56:21 +04:00
|
|
|
assert.Equal(t, uint64(0), s.Stats.ExpireCount, "")
|
2014-10-17 07:23:07 +04:00
|
|
|
fc.Advance(600 * time.Millisecond)
|
2014-10-17 02:19:00 +04:00
|
|
|
s.DeleteExpiredKeys(fc.Now())
|
2013-11-06 09:56:21 +04:00
|
|
|
assert.Equal(t, uint64(1), s.Stats.ExpireCount, "")
|
|
|
|
}
|