github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/framework/cache/file_test.go (about)

     1  // The package is migrated from beego, you can get from following link:
     2  // import(
     3  //   "github.com/beego/beego/v2/client/cache"
     4  // )
     5  // Copyright 2023. All Rights Reserved.
     6  //
     7  // Licensed under the Apache License, Version 2.0 (the "License");
     8  // you may not use this file except in compliance with the License.
     9  // You may obtain a copy of the License at
    10  //
    11  //      http://www.apache.org/licenses/LICENSE-2.0
    12  //
    13  // Unless required by applicable law or agreed to in writing, software
    14  // distributed under the License is distributed on an "AS IS" BASIS,
    15  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16  // See the License for the specific language governing permissions and
    17  // limitations under the License.
    18  
    19  package cache
    20  
    21  import (
    22  	"context"
    23  	"fmt"
    24  	"os"
    25  	"path/filepath"
    26  	"strings"
    27  	"testing"
    28  
    29  	"github.com/stretchr/testify/assert"
    30  )
    31  
    32  func TestFileCacheStartAndGC(t *testing.T) {
    33  	fc := NewFileCache().(*FileCache)
    34  	err := fc.StartAndGC(`{`)
    35  	assert.NotNil(t, err)
    36  	err = fc.StartAndGC(`{}`)
    37  	assert.Nil(t, err)
    38  
    39  	assert.Equal(t, fc.CachePath, FileCachePath)
    40  	assert.Equal(t, fc.DirectoryLevel, FileCacheDirectoryLevel)
    41  	assert.Equal(t, fc.EmbedExpiry, int(FileCacheEmbedExpiry))
    42  	assert.Equal(t, fc.FileSuffix, FileCacheFileSuffix)
    43  
    44  	err = fc.StartAndGC(`{"CachePath":"//cache","FileSuffix":".bin","DirectoryLevel":"2","EmbedExpiry":"0"}`)
    45  	// could not create dir
    46  	assert.NotNil(t, err)
    47  
    48  	str := getTestCacheFilePath()
    49  	err = os.MkdirAll(str, os.ModePerm)
    50  	assert.Nil(t, err)
    51  	fmt.Println(str)
    52  	str = strings.Replace(str, "\\", "\\\\", -1)
    53  	fmt.Println(str)
    54  	err = fc.StartAndGC(fmt.Sprintf(`{"CachePath":"%s","FileSuffix":".bin","DirectoryLevel":"2","EmbedExpiry":"0"}`, str))
    55  	assert.Nil(t, err)
    56  	assert.Equal(t, fc.CachePath, str)
    57  	assert.Equal(t, fc.DirectoryLevel, 2)
    58  	assert.Equal(t, fc.EmbedExpiry, 0)
    59  	assert.Equal(t, fc.FileSuffix, ".bin")
    60  
    61  	err = fc.StartAndGC(fmt.Sprintf(`{"CachePath":"%s","FileSuffix":".bin","DirectoryLevel":"aaa","EmbedExpiry":"0"}`, str))
    62  	assert.NotNil(t, err)
    63  
    64  	err = fc.StartAndGC(fmt.Sprintf(`{"CachePath":"%s","FileSuffix":".bin","DirectoryLevel":"2","EmbedExpiry":"aaa"}`, str))
    65  	assert.NotNil(t, err)
    66  }
    67  
    68  func TestFileCacheInit(t *testing.T) {
    69  	fc := NewFileCache().(*FileCache)
    70  	fc.CachePath = "////aaa"
    71  	err := fc.Init()
    72  	assert.NotNil(t, err)
    73  	fc.CachePath = getTestCacheFilePath()
    74  	err = fc.Init()
    75  	assert.Nil(t, err)
    76  }
    77  
    78  func TestFileGetContents(t *testing.T) {
    79  	_, err := FileGetContents("/bin/aaa")
    80  	assert.NotNil(t, err)
    81  	fn := filepath.Join(os.TempDir(), "fileCache.txt")
    82  	f, err := os.Create(fn)
    83  	assert.Nil(t, err)
    84  	_, err = f.WriteString("text")
    85  	assert.Nil(t, err)
    86  	data, err := FileGetContents(fn)
    87  	assert.Nil(t, err)
    88  	assert.Equal(t, "text", string(data))
    89  }
    90  
    91  func TestGobEncodeDecode(t *testing.T) {
    92  	_, err := GobEncode(func() {
    93  		fmt.Print("test func")
    94  	})
    95  	assert.NotNil(t, err)
    96  	data, err := GobEncode(&FileCacheItem{
    97  		Data: "hello",
    98  	})
    99  	assert.Nil(t, err)
   100  	err = GobDecode([]byte("wrong data"), &FileCacheItem{})
   101  	assert.NotNil(t, err)
   102  	dci := &FileCacheItem{}
   103  	err = GobDecode(data, dci)
   104  	assert.Nil(t, err)
   105  	assert.Equal(t, "hello", dci.Data)
   106  }
   107  
   108  func TestFileCacheDelete(t *testing.T) {
   109  	fc := NewFileCache()
   110  	err := fc.StartAndGC(`{}`)
   111  	assert.Nil(t, err)
   112  	err = fc.Delete(context.Background(), "my-key")
   113  	assert.Nil(t, err)
   114  }
   115  
   116  func getTestCacheFilePath() string {
   117  	return filepath.Join(os.TempDir(), "test", "file.txt")
   118  }