mirror of
https://gitee.com/johng/gf
synced 2026-07-05 13:22:16 +08:00
gfcache test 20190704 15:00
This commit is contained in:
65
g/os/gfcache/gfcache_z_unit_test.go
Executable file
65
g/os/gfcache/gfcache_z_unit_test.go
Executable file
@ -0,0 +1,65 @@
|
||||
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// 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.
|
||||
|
||||
// go test *.go -bench=".*" -benchmem
|
||||
|
||||
package gfcache_test
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/g/os/gfcache"
|
||||
"github.com/gogf/gf/g/os/gfile"
|
||||
"github.com/gogf/gf/g/test/gtest"
|
||||
)
|
||||
|
||||
func TestGetContents(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
|
||||
var f *os.File
|
||||
var err error
|
||||
fileName := "test.txt"
|
||||
strTest := "123"
|
||||
|
||||
if !gfile.Exists(fileName) {
|
||||
f, err = gfile.Create(fileName)
|
||||
if err != nil {
|
||||
t.Error("create file fail")
|
||||
}
|
||||
}
|
||||
|
||||
cache := gfcache.GetContents(fileName, 2)
|
||||
|
||||
if gfile.Exists(fileName) {
|
||||
f, err = gfile.OpenFile(fileName, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
|
||||
if err != nil {
|
||||
t.Error("file open fail", err)
|
||||
}
|
||||
}
|
||||
|
||||
defer f.Close()
|
||||
|
||||
_, err = f.Write([]byte(strTest))
|
||||
if err != nil {
|
||||
t.Error("write error", err)
|
||||
}
|
||||
|
||||
cache = gfcache.GetContents(fileName)
|
||||
gtest.Assert(cache, "")
|
||||
|
||||
time.Sleep(time.Duration(4) * time.Second)
|
||||
|
||||
if gfile.Exists(fileName) {
|
||||
err = gfile.Remove(fileName)
|
||||
if err != nil {
|
||||
t.Error("file remove fail", err)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user