Files
gf/os/gfile/gfile_z_unit_cache_test.go

88 lines
1.7 KiB
Go
Raw Normal View History

2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
2019-07-04 15:03:56 +08:00
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gfile_test
2019-07-04 15:03:56 +08:00
import (
2019-07-09 15:10:02 +08:00
"io/ioutil"
2019-07-04 15:03:56 +08:00
"os"
"testing"
"time"
2021-11-15 20:26:31 +08:00
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/test/gtest"
2019-07-04 15:03:56 +08:00
)
func Test_GetContentsWithCache(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-07-04 15:03:56 +08:00
var f *os.File
var err error
2019-07-09 15:10:02 +08:00
fileName := "test"
2019-07-04 15:03:56 +08:00
strTest := "123"
if !gfile.Exists(fileName) {
2019-07-09 15:10:02 +08:00
f, err = ioutil.TempFile("", fileName)
2019-07-04 15:03:56 +08:00
if err != nil {
t.Error("create file fail")
}
}
2019-07-09 15:10:02 +08:00
defer f.Close()
defer os.Remove(f.Name())
if gfile.Exists(f.Name()) {
f, err = gfile.OpenFile(f.Name(), os.O_APPEND|os.O_WRONLY, os.ModeAppend)
2019-07-04 15:03:56 +08:00
if err != nil {
t.Error("file open fail", err)
}
2019-07-09 15:10:02 +08:00
err = gfile.PutContents(f.Name(), strTest)
if err != nil {
t.Error("write error", err)
}
cache := gfile.GetContentsWithCache(f.Name(), 1)
2020-03-19 22:56:12 +08:00
t.Assert(cache, strTest)
2019-07-04 15:03:56 +08:00
}
2019-07-09 15:10:02 +08:00
})
2019-07-04 15:03:56 +08:00
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-07-09 15:10:02 +08:00
var f *os.File
var err error
fileName := "test2"
strTest := "123"
2019-07-04 15:03:56 +08:00
2019-07-09 15:10:02 +08:00
if !gfile.Exists(fileName) {
f, err = ioutil.TempFile("", fileName)
if err != nil {
t.Error("create file fail")
}
2019-07-04 15:03:56 +08:00
}
2019-07-09 15:10:02 +08:00
defer f.Close()
defer os.Remove(f.Name())
if gfile.Exists(f.Name()) {
cache := gfile.GetContentsWithCache(f.Name())
2019-07-04 15:03:56 +08:00
2019-07-09 15:10:02 +08:00
f, err = gfile.OpenFile(f.Name(), os.O_APPEND|os.O_WRONLY, os.ModeAppend)
if err != nil {
t.Error("file open fail", err)
}
2019-07-04 15:03:56 +08:00
2019-07-09 15:10:02 +08:00
err = gfile.PutContents(f.Name(), strTest)
2019-07-04 15:03:56 +08:00
if err != nil {
2019-07-09 15:10:02 +08:00
t.Error("write error", err)
2019-07-04 15:03:56 +08:00
}
2019-07-09 15:10:02 +08:00
2020-03-19 22:56:12 +08:00
t.Assert(cache, "")
2019-07-09 15:10:02 +08:00
time.Sleep(100 * time.Millisecond)
2019-07-04 15:03:56 +08:00
}
})
}