Skip to content

Commit

Permalink
gc tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kukymbr committed Jan 22, 2024
1 parent 75b166c commit 5cdc854
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 33 deletions.
11 changes: 11 additions & 0 deletions gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ package filecache

import "time"

// GarbageCollector is a tool to remove expired cache items.
type GarbageCollector interface {
// OnInstanceInit is executed on the initialization of the FileCache instance.
OnInstanceInit()

// OnOperation is executed on the every item's operation in the FileCache instance.
OnOperation()

// Close closes the GarbageCollector.
Close() error
}

Expand All @@ -15,6 +21,11 @@ func NewNopGarbageCollector() GarbageCollector {

// NewProbabilityGarbageCollector returns the GarbageCollector running with the defined probability.
// Divisor is a run probability divisor (e.g., divisor equals 100 is a 1/100 probability).
//
// Function arguments:
// * dir - the directory with the FileCache's instance files;
// * onInitDivisor - divisor for the probability on the OnInstanceInit() function call;
// * onOpDivisor - divisor for the probability on the OnOperation() function call.
func NewProbabilityGarbageCollector(dir string, onInitDivisor uint, onOpDivisor uint) GarbageCollector {
return &gcProbability{
dir: dir,
Expand Down
44 changes: 44 additions & 0 deletions gc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package filecache

import (
"context"
"os"
"testing"
"time"

"github.com/stretchr/testify/require"
)

func prepareGCTestFiles(t *testing.T) {
t.Cleanup(func() {
_ = os.RemoveAll("./testdata/gc")
})

_ = os.RemoveAll("./testdata/gc")

err := os.MkdirAll("./testdata/gc", dirsMode)
require.NoError(t, err)

err = os.WriteFile("./testdata/gc/test1.cache", []byte("value1"), filesMode)
require.NoError(t, err)

err = os.WriteFile("./testdata/gc/test2.cache", []byte("value2"), filesMode)
require.NoError(t, err)

m1 := newMeta("test1", &ItemOptions{TTL: time.Millisecond}, time.Hour)
f1, err := os.Create("./testdata/gc/test1.cache--meta")
require.NoError(t, err)

err = saveMeta(context.Background(), m1, f1)
require.NoError(t, err)

m2 := newMeta("test2", &ItemOptions{TTL: time.Hour}, time.Hour)
f2, err := os.Create("./testdata/gc/test2.cache--meta")
require.NoError(t, err)

err = saveMeta(context.Background(), m2, f2)
require.NoError(t, err)

// To invalidate test1 item.
time.Sleep(5 * time.Millisecond)
}
29 changes: 29 additions & 0 deletions gcinterval_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package filecache

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestIntervalGarbageCollector(t *testing.T) {
prepareGCTestFiles(t)

gc := NewIntervalGarbageCollector("./testdata/gc", 50*time.Millisecond)

gc.OnInstanceInit()
gc.OnOperation()

assert.FileExists(t, "./testdata/gc/test1.cache")
assert.FileExists(t, "./testdata/gc/test1.cache--meta")
assert.FileExists(t, "./testdata/gc/test2.cache")
assert.FileExists(t, "./testdata/gc/test2.cache--meta")

time.Sleep(55 * time.Millisecond)

assert.NoFileExists(t, "./testdata/gc/test1.cache")
assert.NoFileExists(t, "./testdata/gc/test1.cache--meta")
assert.FileExists(t, "./testdata/gc/test2.cache")
assert.FileExists(t, "./testdata/gc/test2.cache--meta")
}
21 changes: 21 additions & 0 deletions gcnop_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package filecache

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestNopGarbageCollector(t *testing.T) {
prepareGCTestFiles(t)

gc := NewNopGarbageCollector()

gc.OnInstanceInit()
gc.OnOperation()

assert.FileExists(t, "./testdata/gc/test1.cache")
assert.FileExists(t, "./testdata/gc/test1.cache--meta")
assert.FileExists(t, "./testdata/gc/test2.cache")
assert.FileExists(t, "./testdata/gc/test2.cache--meta")
}
35 changes: 2 additions & 33 deletions gcprobability_test.go
Original file line number Diff line number Diff line change
@@ -1,44 +1,13 @@
package filecache

import (
"context"
"os"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestGarbageCollector(t *testing.T) {
t.Cleanup(func() {
_ = os.RemoveAll("./testdata/gc")
})

{
err := os.MkdirAll("./testdata/gc", dirsMode)
require.NoError(t, err)

err = os.WriteFile("./testdata/gc/test1.cache", []byte("value1"), filesMode)
require.NoError(t, err)

err = os.WriteFile("./testdata/gc/test2.cache", []byte("value2"), filesMode)
require.NoError(t, err)

m1 := newMeta("test1", &ItemOptions{TTL: time.Millisecond}, time.Hour)
f1, err := os.Create("./testdata/gc/test1.cache--meta")
require.NoError(t, err)

err = saveMeta(context.Background(), m1, f1)
require.NoError(t, err)

m2 := newMeta("test2", &ItemOptions{TTL: time.Hour}, time.Hour)
f2, err := os.Create("./testdata/gc/test2.cache--meta")
require.NoError(t, err)

err = saveMeta(context.Background(), m2, f2)
require.NoError(t, err)
}
func TestProbabilityGarbageCollector(t *testing.T) {
prepareGCTestFiles(t)

{
gc := &gcProbability{}
Expand Down

0 comments on commit 5cdc854

Please sign in to comment.