github.com/wasilibs/wazerox@v0.0.0-20240124024944-4923be63ab5f/internal/filecache/file_cache_test.go (about) 1 package filecache 2 3 import ( 4 "bytes" 5 "io" 6 "os" 7 "testing" 8 9 "github.com/wasilibs/wazerox/internal/testing/require" 10 ) 11 12 func TestFileReadCloser_Close(t *testing.T) { 13 fc := newFileCache(t.TempDir()) 14 key := Key{1, 2, 3} 15 16 err := fc.Add(key, bytes.NewReader([]byte{1, 2, 3, 4})) 17 require.NoError(t, err) 18 19 c, ok, err := fc.Get(key) 20 require.NoError(t, err) 21 require.True(t, ok) 22 23 // At this point, file is not closed, therefore TryLock should fail. 24 require.False(t, fc.mux.TryLock()) 25 26 // Close, and then TryLock should succeed this time. 27 require.NoError(t, c.Close()) 28 require.True(t, fc.mux.TryLock()) 29 } 30 31 func TestFileCache_Add(t *testing.T) { 32 fc := newFileCache(t.TempDir()) 33 34 t.Run("not exist", func(t *testing.T) { 35 content := []byte{1, 2, 3, 4, 5} 36 id := Key{1, 2, 3, 4, 5, 6, 7} 37 err := fc.Add(id, bytes.NewReader(content)) 38 require.NoError(t, err) 39 40 // Ensures that file exists. 41 cached, err := os.ReadFile(fc.path(id)) 42 require.NoError(t, err) 43 44 // Check if the saved content is the same as the given one. 45 require.Equal(t, content, cached) 46 }) 47 48 t.Run("already exists", func(t *testing.T) { 49 content := []byte{1, 2, 3, 4, 5} 50 51 id := Key{1, 2, 3} 52 53 // Writes the pre-existing file for the same ID. 54 p := fc.path(id) 55 f, err := os.Create(p) 56 require.NoError(t, err) 57 _, err = f.Write(content) 58 require.NoError(t, err) 59 require.NoError(t, f.Close()) 60 61 err = fc.Add(id, bytes.NewReader(content)) 62 require.NoError(t, err) 63 64 // Ensures that file exists. 65 cached, err := os.ReadFile(fc.path(id)) 66 require.NoError(t, err) 67 68 // Check if the saved content is the same as the given one. 69 require.Equal(t, content, cached) 70 }) 71 } 72 73 func TestFileCache_Delete(t *testing.T) { 74 fc := newFileCache(t.TempDir()) 75 t.Run("non-exist", func(t *testing.T) { 76 id := Key{0} 77 err := fc.Delete(id) 78 require.NoError(t, err) 79 }) 80 t.Run("exist", func(t *testing.T) { 81 id := Key{1, 2, 3} 82 p := fc.path(id) 83 f, err := os.Create(p) 84 require.NoError(t, err) 85 require.NoError(t, f.Close()) 86 87 // Ensures that file exists now. 88 f, err = os.Open(p) 89 require.NoError(t, err) 90 require.NoError(t, f.Close()) 91 92 // Delete the cache. 93 err = fc.Delete(id) 94 require.NoError(t, err) 95 96 // Ensures that file no longer exists. 97 _, err = os.Open(p) 98 require.ErrorIs(t, err, os.ErrNotExist) 99 }) 100 } 101 102 func TestFileCache_Get(t *testing.T) { 103 fc := newFileCache(t.TempDir()) 104 105 t.Run("exist", func(t *testing.T) { 106 content := []byte{1, 2, 3, 4, 5} 107 id := Key{1, 2, 3} 108 109 // Writes the pre-existing file for the ID. 110 p := fc.path(id) 111 f, err := os.Create(p) 112 require.NoError(t, err) 113 _, err = f.Write(content) 114 require.NoError(t, err) 115 require.NoError(t, f.Close()) 116 117 result, ok, err := fc.Get(id) 118 require.NoError(t, err) 119 require.True(t, ok) 120 defer func() { 121 require.NoError(t, result.Close()) 122 }() 123 124 actual, err := io.ReadAll(result) 125 require.NoError(t, err) 126 127 require.Equal(t, content, actual) 128 }) 129 t.Run("not exist", func(t *testing.T) { 130 _, ok, err := fc.Get(Key{0xf}) 131 // Non-exist should not be error. 132 require.NoError(t, err) 133 require.False(t, ok) 134 }) 135 } 136 137 func TestFileCache_path(t *testing.T) { 138 fc := &fileCache{dirPath: "/tmp/.wazero"} 139 actual := fc.path(Key{1, 2, 3, 4, 5}) 140 require.Equal(t, "/tmp/.wazero/0102030405000000000000000000000000000000000000000000000000000000", actual) 141 }