From 5cdc854b13643b86421e96255ab941c4e755e925 Mon Sep 17 00:00:00 2001 From: kukymbr Date: Mon, 22 Jan 2024 17:27:35 +0300 Subject: [PATCH] gc tests --- gc.go | 11 +++++++++++ gc_test.go | 44 +++++++++++++++++++++++++++++++++++++++++++ gcinterval_test.go | 29 ++++++++++++++++++++++++++++ gcnop_test.go | 21 +++++++++++++++++++++ gcprobability_test.go | 35 ++-------------------------------- 5 files changed, 107 insertions(+), 33 deletions(-) create mode 100644 gc_test.go create mode 100644 gcinterval_test.go create mode 100644 gcnop_test.go diff --git a/gc.go b/gc.go index b488f33..ab507f6 100644 --- a/gc.go +++ b/gc.go @@ -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 } @@ -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, diff --git a/gc_test.go b/gc_test.go new file mode 100644 index 0000000..157edc5 --- /dev/null +++ b/gc_test.go @@ -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) +} diff --git a/gcinterval_test.go b/gcinterval_test.go new file mode 100644 index 0000000..70af20a --- /dev/null +++ b/gcinterval_test.go @@ -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") +} diff --git a/gcnop_test.go b/gcnop_test.go new file mode 100644 index 0000000..ef855bd --- /dev/null +++ b/gcnop_test.go @@ -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") +} diff --git a/gcprobability_test.go b/gcprobability_test.go index 70f8cff..dbe5c29 100644 --- a/gcprobability_test.go +++ b/gcprobability_test.go @@ -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{}