github.com/mailgun/holster/v4@v4.20.0/collections/expire_cache_test.go (about)

     1  package collections_test
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/mailgun/holster/v4/collections"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestNewExpireCache(t *testing.T) {
    12  	ec := collections.NewExpireCache(time.Millisecond * 100)
    13  
    14  	ec.Add("one", "one")
    15  	time.Sleep(time.Millisecond * 100)
    16  
    17  	var runs int
    18  	ec.Each(1, func(key interface{}, value interface{}) error {
    19  		assert.Equal(t, key, "one")
    20  		assert.Equal(t, value, "one")
    21  		runs++
    22  		return nil
    23  	})
    24  	assert.Equal(t, runs, 1)
    25  
    26  	// Should NOT be in the cache
    27  	time.Sleep(time.Millisecond * 100)
    28  	ec.Each(1, func(key interface{}, value interface{}) error {
    29  		runs++
    30  		return nil
    31  	})
    32  	assert.Equal(t, runs, 1)
    33  }