github.com/tetratelabs/wazero@v1.7.3-0.20240513003603-48f702e154b5/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/tetratelabs/wazero/internal/testing/require"
    10  )
    11  
    12  func TestFileCache_Add(t *testing.T) {
    13  	fc := newFileCache(t.TempDir())
    14  
    15  	t.Run("not exist", func(t *testing.T) {
    16  		content := []byte{1, 2, 3, 4, 5}
    17  		id := Key{1, 2, 3, 4, 5, 6, 7}
    18  		err := fc.Add(id, bytes.NewReader(content))
    19  		require.NoError(t, err)
    20  
    21  		// Ensures that file exists.
    22  		cached, err := os.ReadFile(fc.path(id))
    23  		require.NoError(t, err)
    24  
    25  		// Check if the saved content is the same as the given one.
    26  		require.Equal(t, content, cached)
    27  	})
    28  
    29  	t.Run("already exists", func(t *testing.T) {
    30  		content := []byte{1, 2, 3, 4, 5}
    31  
    32  		id := Key{1, 2, 3}
    33  
    34  		// Writes the pre-existing file for the same ID.
    35  		p := fc.path(id)
    36  		f, err := os.Create(p)
    37  		require.NoError(t, err)
    38  		_, err = f.Write(content)
    39  		require.NoError(t, err)
    40  		require.NoError(t, f.Close())
    41  
    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  
    54  func TestFileCache_Delete(t *testing.T) {
    55  	fc := newFileCache(t.TempDir())
    56  	t.Run("non-exist", func(t *testing.T) {
    57  		id := Key{0}
    58  		err := fc.Delete(id)
    59  		require.NoError(t, err)
    60  	})
    61  	t.Run("exist", func(t *testing.T) {
    62  		id := Key{1, 2, 3}
    63  		p := fc.path(id)
    64  		f, err := os.Create(p)
    65  		require.NoError(t, err)
    66  		require.NoError(t, f.Close())
    67  
    68  		// Ensures that file exists now.
    69  		f, err = os.Open(p)
    70  		require.NoError(t, err)
    71  		require.NoError(t, f.Close())
    72  
    73  		// Delete the cache.
    74  		err = fc.Delete(id)
    75  		require.NoError(t, err)
    76  
    77  		// Ensures that file no longer exists.
    78  		_, err = os.Open(p)
    79  		require.ErrorIs(t, err, os.ErrNotExist)
    80  	})
    81  }
    82  
    83  func TestFileCache_Get(t *testing.T) {
    84  	fc := newFileCache(t.TempDir())
    85  
    86  	t.Run("exist", func(t *testing.T) {
    87  		content := []byte{1, 2, 3, 4, 5}
    88  		id := Key{1, 2, 3}
    89  
    90  		// Writes the pre-existing file for the ID.
    91  		p := fc.path(id)
    92  		f, err := os.Create(p)
    93  		require.NoError(t, err)
    94  		_, err = f.Write(content)
    95  		require.NoError(t, err)
    96  		require.NoError(t, f.Close())
    97  
    98  		result, ok, err := fc.Get(id)
    99  		require.NoError(t, err)
   100  		require.True(t, ok)
   101  		defer func() {
   102  			require.NoError(t, result.Close())
   103  		}()
   104  
   105  		actual, err := io.ReadAll(result)
   106  		require.NoError(t, err)
   107  
   108  		require.Equal(t, content, actual)
   109  	})
   110  	t.Run("not exist", func(t *testing.T) {
   111  		_, ok, err := fc.Get(Key{0xf})
   112  		// Non-exist should not be error.
   113  		require.NoError(t, err)
   114  		require.False(t, ok)
   115  	})
   116  }
   117  
   118  func TestFileCache_path(t *testing.T) {
   119  	fc := &fileCache{dirPath: "/tmp/.wazero"}
   120  	actual := fc.path(Key{1, 2, 3, 4, 5})
   121  	require.Equal(t, "/tmp/.wazero/0102030405000000000000000000000000000000000000000000000000000000", actual)
   122  }