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