github.com/owncloud/ocis/v2@v2.0.0/ocis-pkg/sync/cache_test.go (about)

     1  package sync
     2  
     3  import (
     4  	"github.com/stretchr/testify/assert"
     5  	"strconv"
     6  	"sync"
     7  	"testing"
     8  	"time"
     9  )
    10  
    11  func cacheRunner(size int) (*Cache, func(f func(v string))) {
    12  	c := NewCache(size)
    13  	run := func(f func(v string)) {
    14  		wg := sync.WaitGroup{}
    15  		for i := 0; i < size; i++ {
    16  			wg.Add(1)
    17  			go func(v string) {
    18  				f(v)
    19  				wg.Done()
    20  			}(strconv.Itoa(i))
    21  		}
    22  		wg.Wait()
    23  	}
    24  
    25  	return &c, run
    26  }
    27  
    28  func BenchmarkCache(b *testing.B) {
    29  	b.ReportAllocs()
    30  	size := 1024
    31  	c, cr := cacheRunner(size)
    32  
    33  	cr(func(v string) { c.Store(v, v, time.Now().Add(100*time.Millisecond)) })
    34  	cr(func(v string) { c.Delete(v) })
    35  }
    36  
    37  func TestCache(t *testing.T) {
    38  	size := 1024
    39  	c, cr := cacheRunner(size)
    40  
    41  	cr(func(v string) { c.Store(v, v, time.Now().Add(100*time.Millisecond)) })
    42  	assert.Equal(t, size, int(c.length), "length is atomic")
    43  
    44  	cr(func(v string) { c.Delete(v) })
    45  	assert.Equal(t, 0, int(c.length), "delete is atomic")
    46  
    47  	cr(func(v string) {
    48  		time.Sleep(101 * time.Millisecond)
    49  		c.evict()
    50  	})
    51  	assert.Equal(t, 0, int(c.length), "evict is atomic")
    52  }
    53  
    54  func TestCache_Load(t *testing.T) {
    55  	size := 1024
    56  	c, cr := cacheRunner(size)
    57  
    58  	cr(func(v string) {
    59  		c.Store(v, v, time.Now().Add(10*time.Second))
    60  	})
    61  
    62  	cr(func(v string) {
    63  		assert.Equal(t, v, c.Load(v).V, "entry value is the same")
    64  	})
    65  
    66  	cr(func(v string) {
    67  		assert.Nil(t, c.Load(v+strconv.Itoa(size)), "entry is nil if unknown")
    68  	})
    69  
    70  	cr(func(v string) {
    71  		wait := 100 * time.Millisecond
    72  		c.Store(v, v, time.Now().Add(wait))
    73  		time.Sleep(wait + 1)
    74  		assert.Nil(t, c.Load(v), "entry is nil if it's expired")
    75  	})
    76  }
    77  
    78  func TestCache_Store(t *testing.T) {
    79  	c, cr := cacheRunner(1024)
    80  
    81  	cr(func(v string) {
    82  		c.Store(v, v, time.Now().Add(100*time.Millisecond))
    83  		assert.Equal(t, v, c.Load(v).V, "new entries can be added")
    84  	})
    85  
    86  	cr(func(v string) {
    87  		replacedExpiration := time.Now().Add(10 * time.Minute)
    88  		c.Store(v, "old", time.Now().Add(10*time.Minute))
    89  		c.Store(v, "updated", replacedExpiration)
    90  		assert.Equal(t, "updated", c.Load(v).V, "entry values can be updated")
    91  		assert.Equal(t, replacedExpiration, c.Load(v).expiration, "entry expiration can be updated")
    92  	})
    93  }
    94  
    95  func TestCache_Delete(t *testing.T) {
    96  	c, cr := cacheRunner(1024)
    97  
    98  	cr(func(v string) {
    99  		c.Store(v, v, time.Now().Add(100*time.Millisecond))
   100  		c.Delete(v)
   101  		assert.Nil(t, c.Load(v), "entries can be deleted")
   102  	})
   103  
   104  	assert.Equal(t, 0, int(c.length), "removing a entry decreases the cache size")
   105  }