-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial fs cache test with tests (#22)
- Loading branch information
Showing
11 changed files
with
524 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,3 +41,4 @@ config.yaml | |
.idea | ||
|
||
dist/ | ||
tempcache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package filesystem_cache | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path" | ||
) | ||
|
||
const PERMISSIONS = 0644 | ||
|
||
type FilesystemCache struct { | ||
path string | ||
} | ||
|
||
func NewFilesystemCache(path string) (*FilesystemCache, error) { | ||
f, err := os.Stat(path) | ||
if os.IsNotExist(err) { | ||
// if the path does not exist, we can create it | ||
err := os.MkdirAll(path, 0755) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create directory %s: %v", path, err) | ||
} | ||
} else if !f.Mode().IsDir() { | ||
return nil, fmt.Errorf("path %s is not a directory", path) | ||
} | ||
return &FilesystemCache{path}, nil | ||
} | ||
|
||
func (c *FilesystemCache) Get(key string) ([]byte, bool) { | ||
// Read the content of the file with the given key | ||
// If the file does not exist, return false | ||
// If the file exists, return the content as a byte slice | ||
content, err := os.ReadFile(fmt.Sprintf("%v/%v", c.path, key)) | ||
if err != nil { | ||
return nil, false | ||
} | ||
return content, true | ||
} | ||
|
||
func (c *FilesystemCache) Set(key string, content string, duration int) error { | ||
// Write the content to a file with the given key | ||
// duration is not used in this implementation as pruning is not implemented | ||
cachePath := fmt.Sprintf("%v/%v", c.path, key) | ||
if _, err := os.Stat(cachePath); os.IsNotExist(err) { | ||
dir := path.Dir(cachePath) | ||
err := os.MkdirAll(dir, 0755) | ||
if err != nil { | ||
return fmt.Errorf("failed to create directory %s: %v", dir, err) | ||
} | ||
} | ||
return os.WriteFile(cachePath, []byte(content), PERMISSIONS) | ||
} | ||
|
||
func (c *FilesystemCache) DeleteWithPrefix(prefix string) error { | ||
// Delete all files with the given prefix from the cache. | ||
// We can use the filepath.Glob function to get all files with the given prefix | ||
files, err := os.ReadDir(c.path) | ||
if err != nil { | ||
return fmt.Errorf("failed to read directory %s: %v", c.path, err) | ||
} | ||
for _, file := range files { | ||
if file.IsDir() { | ||
continue | ||
} | ||
if !file.Type().IsRegular() { | ||
continue | ||
} | ||
|
||
if file.Name()[:len(prefix)] == prefix { | ||
err := os.Remove(fmt.Sprintf("%v/%v", c.path, file.Name())) | ||
if err != nil { | ||
return fmt.Errorf("failed to delete file %s: %v", file.Name(), err) | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *FilesystemCache) Name() string { | ||
return "Filesystem" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
package filesystem_cache | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
func TestNewFilesystemCache(t *testing.T) { | ||
cachePath, _ := os.MkdirTemp("", "filesystem_cache_test") | ||
defer os.RemoveAll(cachePath) | ||
cache, err := NewFilesystemCache(cachePath) | ||
if err != nil { | ||
t.Errorf("Failed to create filesystem cache: %v", err) | ||
} | ||
|
||
// Verify that the cache path is set correctly | ||
if cache.path != cachePath { | ||
t.Errorf("Expected cache path %s, got %s", cachePath, cache.path) | ||
} | ||
|
||
// Verify that the cache directory is created | ||
_, err = os.Stat(cachePath) | ||
if os.IsNotExist(err) { | ||
t.Errorf("Cache directory %s does not exist", cachePath) | ||
} | ||
} | ||
|
||
func TestFilesystemCache_Get(t *testing.T) { | ||
cachePath, _ := os.MkdirTemp("", "filesystem_cache_test") | ||
defer os.RemoveAll(cachePath) | ||
cache, _ := NewFilesystemCache(cachePath) | ||
|
||
// Create a test file | ||
testKey := "test_key" | ||
testContent := "test_content" | ||
testFilePath := filepath.Join(cachePath, testKey) | ||
err := os.WriteFile(testFilePath, []byte(testContent), PERMISSIONS) | ||
if err != nil { | ||
t.Errorf("Failed to create test file: %v", err) | ||
} | ||
|
||
// Retrieve the content from the cache | ||
content, ok := cache.Get(testKey) | ||
if !ok { | ||
t.Errorf("Expected cache hit for key %s, got cache miss", testKey) | ||
} | ||
|
||
// Verify that the retrieved content matches the test content | ||
if string(content) != testContent { | ||
t.Errorf("Expected content %s, got %s", testContent, string(content)) | ||
} | ||
|
||
// Clean up the test file | ||
err = os.Remove(testFilePath) | ||
if err != nil { | ||
t.Errorf("Failed to remove test file: %v", err) | ||
} | ||
|
||
// Test that it'll create subdirectories if needed | ||
nestedDir := filepath.Join(cachePath, "nested") | ||
_, err = NewFilesystemCache(nestedDir) | ||
if err != nil { | ||
t.Errorf("Failed to create nested cache: %v", err) | ||
} | ||
if f, err := os.Stat(nestedDir); os.IsNotExist(err) || !f.IsDir() { | ||
t.Errorf("Expected nested cache directory to be created") | ||
} | ||
} | ||
|
||
func TestFilesystemCache_Set(t *testing.T) { | ||
cachePath, _ := os.MkdirTemp("", "filesystem_cache_test") | ||
defer os.RemoveAll(cachePath) | ||
cache, _ := NewFilesystemCache(cachePath) | ||
|
||
// Set a test key-value pair in the cache | ||
testKey := "test_key" | ||
testContent := "test_content" | ||
err := cache.Set(testKey, testContent, 0) | ||
if err != nil { | ||
t.Errorf("Failed to set key-value pair in cache: %v", err) | ||
} | ||
|
||
// Verify that the test file is created in the cache directory | ||
testFilePath := filepath.Join(cachePath, testKey) | ||
_, err = os.Stat(testFilePath) | ||
if os.IsNotExist(err) { | ||
t.Errorf("Test file %s does not exist in cache directory", testFilePath) | ||
} | ||
|
||
// Clean up the test file | ||
err = os.Remove(testFilePath) | ||
if err != nil { | ||
t.Errorf("Failed to remove test file: %v", err) | ||
} | ||
} | ||
|
||
func TestFilesystemCache_DeleteWithPrefix(t *testing.T) { | ||
cachePath, _ := os.MkdirTemp("", "filesystem_cache_test") | ||
defer os.RemoveAll(cachePath) | ||
cache, _ := NewFilesystemCache(cachePath) | ||
|
||
// Create test files with different prefixes | ||
testPrefix1 := "prefix1" | ||
testPrefix2 := "prefix2" | ||
testKey1 := testPrefix1 + "_key" | ||
testKey2 := testPrefix2 + "_key" | ||
testContent := "test_content" | ||
testFilePath1 := filepath.Join(cachePath, testKey1) | ||
testFilePath2 := filepath.Join(cachePath, testKey2) | ||
err := os.WriteFile(testFilePath1, []byte(testContent), PERMISSIONS) | ||
if err != nil { | ||
t.Errorf("Failed to create test file: %v", err) | ||
} | ||
err = os.WriteFile(testFilePath2, []byte(testContent), PERMISSIONS) | ||
if err != nil { | ||
t.Errorf("Failed to create test file: %v", err) | ||
} | ||
|
||
// Delete files with prefix "prefix1" | ||
err = cache.DeleteWithPrefix(testPrefix1) | ||
if err != nil { | ||
t.Errorf("Failed to delete files with prefix %s: %v", testPrefix1, err) | ||
} | ||
|
||
// Verify that the file with prefix "prefix1" is deleted | ||
_, err = os.Stat(testFilePath1) | ||
if !os.IsNotExist(err) { | ||
t.Errorf("Expected file %s to be deleted, but it still exists", testFilePath1) | ||
} | ||
|
||
// Verify that the file with prefix "prefix2" still exists | ||
_, err = os.Stat(testFilePath2) | ||
if os.IsNotExist(err) { | ||
t.Errorf("Expected file %s to exist, but it does not", testFilePath2) | ||
} | ||
|
||
// Clean up the remaining test file | ||
err = os.Remove(testFilePath2) | ||
if err != nil { | ||
t.Errorf("Failed to remove test file: %v", err) | ||
} | ||
} | ||
|
||
func TestFilesystemCache_Name(t *testing.T) { | ||
cachePath, _ := os.MkdirTemp("", "filesystem_cache_test") | ||
defer os.RemoveAll(cachePath) | ||
cache, _ := NewFilesystemCache(cachePath) | ||
|
||
// Verify that the cache name is returned correctly | ||
expectedName := "Filesystem" | ||
name := cache.Name() | ||
if name != expectedName { | ||
t.Errorf("Expected cache name %s, got %s", expectedName, name) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.