github.com/outcaste-io/ristretto@v0.2.3/store_test.go (about)

     1  package ristretto
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/outcaste-io/ristretto/z"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func TestStoreSetGet(t *testing.T) {
    12  	s := newShardedMap(nil)
    13  	key, conflict := z.KeyToHash(1)
    14  	i := Item{
    15  		Key:      key,
    16  		Conflict: conflict,
    17  		Value:    2,
    18  	}
    19  	s.Set(&i)
    20  	val, ok := s.Get(key, conflict)
    21  	require.True(t, ok)
    22  	require.Equal(t, 2, val.(int))
    23  
    24  	i.Value = 3
    25  	s.Set(&i)
    26  	val, ok = s.Get(key, conflict)
    27  	require.True(t, ok)
    28  	require.Equal(t, 3, val.(int))
    29  
    30  	key, conflict = z.KeyToHash(2)
    31  	i = Item{
    32  		Key:      key,
    33  		Conflict: conflict,
    34  		Value:    2,
    35  	}
    36  	s.Set(&i)
    37  	val, ok = s.Get(key, conflict)
    38  	require.True(t, ok)
    39  	require.Equal(t, 2, val.(int))
    40  }
    41  
    42  func TestStoreDel(t *testing.T) {
    43  	s := newShardedMap(nil)
    44  	key, conflict := z.KeyToHash(1)
    45  	i := Item{
    46  		Key:      key,
    47  		Conflict: conflict,
    48  		Value:    1,
    49  	}
    50  	s.Set(&i)
    51  	s.Del(key, conflict)
    52  	val, ok := s.Get(key, conflict)
    53  	require.False(t, ok)
    54  	require.Nil(t, val)
    55  
    56  	s.Del(2, 0)
    57  }
    58  
    59  func TestStoreClear(t *testing.T) {
    60  	s := newShardedMap(nil)
    61  	for i := uint64(0); i < 1000; i++ {
    62  		key, conflict := z.KeyToHash(i)
    63  		it := Item{
    64  			Key:      key,
    65  			Conflict: conflict,
    66  			Value:    i,
    67  		}
    68  		s.Set(&it)
    69  	}
    70  	s.Clear(nil)
    71  	for i := uint64(0); i < 1000; i++ {
    72  		key, conflict := z.KeyToHash(i)
    73  		val, ok := s.Get(key, conflict)
    74  		require.False(t, ok)
    75  		require.Nil(t, val)
    76  	}
    77  }
    78  
    79  func TestStoreUpdate(t *testing.T) {
    80  	s := newShardedMap(nil)
    81  	key, conflict := z.KeyToHash(1)
    82  	i := Item{
    83  		Key:      key,
    84  		Conflict: conflict,
    85  		Value:    1,
    86  	}
    87  	s.Set(&i)
    88  	i.Value = 2
    89  	_, ok := s.Update(&i)
    90  	require.True(t, ok)
    91  
    92  	val, ok := s.Get(key, conflict)
    93  	require.True(t, ok)
    94  	require.NotNil(t, val)
    95  
    96  	val, ok = s.Get(key, conflict)
    97  	require.True(t, ok)
    98  	require.Equal(t, 2, val.(int))
    99  
   100  	i.Value = 3
   101  	_, ok = s.Update(&i)
   102  	require.True(t, ok)
   103  
   104  	val, ok = s.Get(key, conflict)
   105  	require.True(t, ok)
   106  	require.Equal(t, 3, val.(int))
   107  
   108  	key, conflict = z.KeyToHash(2)
   109  	i = Item{
   110  		Key:      key,
   111  		Conflict: conflict,
   112  		Value:    2,
   113  	}
   114  	_, ok = s.Update(&i)
   115  	require.False(t, ok)
   116  	val, ok = s.Get(key, conflict)
   117  	require.False(t, ok)
   118  	require.Nil(t, val)
   119  }
   120  
   121  func TestStoreCollision(t *testing.T) {
   122  	s := newShardedMap(nil)
   123  	s.shards[1].Lock()
   124  	s.shards[1].data[1] = storeItem{
   125  		key:      1,
   126  		conflict: 0,
   127  		value:    1,
   128  	}
   129  	s.shards[1].Unlock()
   130  	val, ok := s.Get(1, 1)
   131  	require.False(t, ok)
   132  	require.Nil(t, val)
   133  
   134  	i := Item{
   135  		Key:      1,
   136  		Conflict: 1,
   137  		Value:    2,
   138  	}
   139  	s.Set(&i)
   140  	val, ok = s.Get(1, 0)
   141  	require.True(t, ok)
   142  	require.NotEqual(t, 2, val.(int))
   143  
   144  	_, ok = s.Update(&i)
   145  	require.False(t, ok)
   146  	val, ok = s.Get(1, 0)
   147  	require.True(t, ok)
   148  	require.NotEqual(t, 2, val.(int))
   149  
   150  	s.Del(1, 1)
   151  	val, ok = s.Get(1, 0)
   152  	require.True(t, ok)
   153  	require.NotNil(t, val)
   154  }
   155  
   156  func TestStoreExpiration(t *testing.T) {
   157  	s := newShardedMap(nil)
   158  	key, conflict := z.KeyToHash(1)
   159  	expiration := time.Now().Add(time.Second)
   160  	i := Item{
   161  		Key:        key,
   162  		Conflict:   conflict,
   163  		Value:      1,
   164  		Expiration: expiration,
   165  	}
   166  	s.Set(&i)
   167  	val, ok := s.Get(key, conflict)
   168  	require.True(t, ok)
   169  	require.Equal(t, 1, val.(int))
   170  
   171  	ttl := s.Expiration(key)
   172  	require.Equal(t, expiration, ttl)
   173  
   174  	s.Del(key, conflict)
   175  
   176  	_, ok = s.Get(key, conflict)
   177  	require.False(t, ok)
   178  	require.True(t, s.Expiration(key).IsZero())
   179  
   180  	// missing item
   181  	key, _ = z.KeyToHash(4340958203495)
   182  	ttl = s.Expiration(key)
   183  	require.True(t, ttl.IsZero())
   184  }
   185  
   186  func BenchmarkStoreGet(b *testing.B) {
   187  	s := newShardedMap(nil)
   188  	key, conflict := z.KeyToHash(1)
   189  	i := Item{
   190  		Key:      key,
   191  		Conflict: conflict,
   192  		Value:    1,
   193  	}
   194  	s.Set(&i)
   195  	b.SetBytes(1)
   196  	b.RunParallel(func(pb *testing.PB) {
   197  		for pb.Next() {
   198  			s.Get(key, conflict)
   199  		}
   200  	})
   201  }
   202  
   203  func BenchmarkStoreSet(b *testing.B) {
   204  	s := newShardedMap(nil)
   205  	key, conflict := z.KeyToHash(1)
   206  	b.SetBytes(1)
   207  	b.RunParallel(func(pb *testing.PB) {
   208  		for pb.Next() {
   209  			i := Item{
   210  				Key:      key,
   211  				Conflict: conflict,
   212  				Value:    1,
   213  			}
   214  			s.Set(&i)
   215  		}
   216  	})
   217  }
   218  
   219  func BenchmarkStoreUpdate(b *testing.B) {
   220  	s := newShardedMap(nil)
   221  	key, conflict := z.KeyToHash(1)
   222  	i := Item{
   223  		Key:      key,
   224  		Conflict: conflict,
   225  		Value:    1,
   226  	}
   227  	s.Set(&i)
   228  	b.SetBytes(1)
   229  	b.RunParallel(func(pb *testing.PB) {
   230  		for pb.Next() {
   231  			s.Update(&Item{
   232  				Key:      key,
   233  				Conflict: conflict,
   234  				Value:    2,
   235  			})
   236  		}
   237  	})
   238  }