github.com/ghodss/etcd@v0.3.1-0.20140417172404-cc329bfa55cb/store/stats_test.go (about)

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