github.com/bhojpur/cache@v0.0.4/pkg/file/cache_test.go (about)

     1  package file_test
     2  
     3  // Copyright (c) 2018 Bhojpur Consulting Private Limited, India. All rights reserved.
     4  
     5  // Permission is hereby granted, free of charge, to any person obtaining a copy
     6  // of this software and associated documentation files (the "Software"), to deal
     7  // in the Software without restriction, including without limitation the rights
     8  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     9  // copies of the Software, and to permit persons to whom the Software is
    10  // furnished to do so, subject to the following conditions:
    11  
    12  // The above copyright notice and this permission notice shall be included in
    13  // all copies or substantial portions of the Software.
    14  
    15  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    16  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    17  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    18  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    19  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    20  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    21  // THE SOFTWARE.
    22  
    23  import (
    24  	"fmt"
    25  	"math"
    26  	"os"
    27  	"strconv"
    28  	"testing"
    29  	"time"
    30  
    31  	"github.com/bhojpur/cache/pkg/file"
    32  	"github.com/stretchr/testify/assert"
    33  )
    34  
    35  func Example_Cache() {
    36  	cache := file.New("./cache.data")
    37  	defer os.Remove("./cache.data")
    38  
    39  	v, err := cache.Get("not-exist")
    40  	fmt.Println(v.Valid(), err)
    41  
    42  	fmt.Println(cache.Set("k", "v", time.Minute))
    43  
    44  	v, err = cache.Get("k")
    45  	fmt.Println(v.Valid(), v.String(), err)
    46  
    47  	ttl, err := cache.TTL("k")
    48  	fmt.Println(int(math.Ceil(ttl.Seconds())), err)
    49  
    50  	time.Sleep(time.Second)
    51  
    52  	ttl, err = cache.TTL("k")
    53  	fmt.Println(int(math.Ceil(ttl.Seconds())), err)
    54  
    55  	fmt.Println(cache.Del("k"))
    56  
    57  	v, err = cache.Get("k")
    58  	fmt.Println(v.Valid(), err)
    59  
    60  	// output:
    61  	// false <nil>
    62  	// <nil>
    63  	// true v <nil>
    64  	// 60 <nil>
    65  	// 59 <nil>
    66  	// <nil>
    67  	// false <nil>
    68  }
    69  
    70  func TestNew(t *testing.T) {
    71  	as := assert.New(t)
    72  	defer os.Remove("./test")
    73  
    74  	os.Remove("./test")
    75  	c := file.New("./test")
    76  
    77  	t.Run("not found", func(t *testing.T) {
    78  		v, err := c.Get("k1")
    79  		as.Equal(nil, err)
    80  		as.False(v.Valid())
    81  		as.Empty(v.String())
    82  	})
    83  
    84  	t.Run("exist get set", func(t *testing.T) {
    85  		as.Nil(c.Set("k", "v", time.Second))
    86  
    87  		v, err := c.Get("k")
    88  		as.Nil(err)
    89  		as.Equal("v", v.String())
    90  	})
    91  
    92  	t.Run("expired", func(t *testing.T) {
    93  		as.Nil(c.Set("k", "v", time.Second))
    94  
    95  		time.Sleep(time.Second)
    96  
    97  		v, err := c.Get("k")
    98  		as.Equal(nil, err)
    99  		as.False(v.Valid())
   100  		as.Empty(v.String())
   101  	})
   102  
   103  	t.Run("ttl", func(t *testing.T) {
   104  		as.Nil(c.Set("k", "v", time.Second))
   105  
   106  		ttl, err := c.TTL("k")
   107  		as.Nil(err)
   108  		as.True(ttl <= time.Second && ttl >= time.Second-100*time.Millisecond, ttl)
   109  	})
   110  
   111  	t.Run("expire", func(t *testing.T) {
   112  		as.Nil(c.Set("k", "v", time.Second))
   113  
   114  		ttl, err := c.TTL("k")
   115  		as.Nil(err)
   116  		as.True(ttl <= time.Second && ttl >= time.Second-100*time.Millisecond)
   117  
   118  		as.Nil(c.Expire("k", time.Minute))
   119  
   120  		ttl, err = c.TTL("k")
   121  		as.Nil(err)
   122  		as.True(ttl <= time.Minute && ttl >= time.Minute-100*time.Millisecond)
   123  	})
   124  
   125  	t.Run("range", func(t *testing.T) {
   126  		os.Remove("./test")
   127  		c = file.New("./test")
   128  
   129  		for i := 0; i < 1000; i++ {
   130  			j := strconv.Itoa(i)
   131  			as.Nil(c.Set(j, j, time.Minute), i)
   132  		}
   133  
   134  		kvs, err := c.Range()
   135  		as.Nil(err)
   136  		for _, v := range kvs {
   137  			as.Equal(v.Key, v.Val)
   138  		}
   139  		as.Len(kvs, 1000)
   140  	})
   141  }