2019-06-15 23:45:58 +08:00
|
|
|
package gfpool_test
|
|
|
|
|
|
|
|
import (
|
2019-07-22 15:31:35 +08:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-07-29 21:01:19 +08:00
|
|
|
"github.com/gogf/gf/os/gfile"
|
|
|
|
"github.com/gogf/gf/os/gfpool"
|
|
|
|
"github.com/gogf/gf/os/glog"
|
|
|
|
"github.com/gogf/gf/test/gtest"
|
2019-06-15 23:45:58 +08:00
|
|
|
)
|
|
|
|
|
2019-06-17 17:52:10 +08:00
|
|
|
// TestOpen test open file cache
|
2019-06-15 23:45:58 +08:00
|
|
|
func TestOpen(t *testing.T) {
|
2019-06-16 00:04:46 +08:00
|
|
|
testFile := start("TestOpen.txt")
|
2019-06-15 23:45:58 +08:00
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-06-17 18:09:56 +08:00
|
|
|
f, err := gfpool.Open(testFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertEQ(err, nil)
|
2019-06-15 23:45:58 +08:00
|
|
|
f.Close()
|
|
|
|
|
2019-06-17 18:09:56 +08:00
|
|
|
f2, err1 := gfpool.Open(testFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertEQ(err1, nil)
|
|
|
|
t.AssertEQ(f, f2)
|
2019-06-15 23:45:58 +08:00
|
|
|
f2.Close()
|
|
|
|
})
|
|
|
|
|
2019-06-16 00:04:46 +08:00
|
|
|
stop(testFile)
|
2019-06-15 23:45:58 +08:00
|
|
|
}
|
|
|
|
|
2019-06-17 17:52:10 +08:00
|
|
|
// TestOpenErr test open file error
|
2019-06-15 23:45:58 +08:00
|
|
|
func TestOpenErr(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-06-16 00:07:06 +08:00
|
|
|
testErrFile := "errorPath"
|
2019-06-16 00:04:46 +08:00
|
|
|
_, err := gfpool.Open(testErrFile, os.O_RDWR, 0666)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err, nil)
|
2019-06-17 17:52:10 +08:00
|
|
|
|
|
|
|
// delete file error
|
|
|
|
testFile := start("TestOpenDeleteErr.txt")
|
2019-06-17 18:09:56 +08:00
|
|
|
pool := gfpool.New(testFile, os.O_RDWR, 0666)
|
|
|
|
_, err1 := pool.File()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertEQ(err1, nil)
|
2019-06-17 17:52:10 +08:00
|
|
|
stop(testFile)
|
2019-06-17 18:09:56 +08:00
|
|
|
_, err1 = pool.File()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err1, nil)
|
2019-06-17 17:52:10 +08:00
|
|
|
|
2019-06-17 18:39:27 +08:00
|
|
|
// append mode delete file error and create again
|
2019-06-17 17:52:10 +08:00
|
|
|
testFile = start("TestOpenCreateErr.txt")
|
2019-06-17 18:09:56 +08:00
|
|
|
pool = gfpool.New(testFile, os.O_CREATE, 0666)
|
|
|
|
_, err1 = pool.File()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertEQ(err1, nil)
|
2019-06-17 17:52:10 +08:00
|
|
|
stop(testFile)
|
2019-06-17 18:09:56 +08:00
|
|
|
_, err1 = pool.File()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertEQ(err1, nil)
|
2019-06-17 17:52:10 +08:00
|
|
|
|
|
|
|
// append mode delete file error
|
|
|
|
testFile = start("TestOpenAppendErr.txt")
|
2019-06-17 18:09:56 +08:00
|
|
|
pool = gfpool.New(testFile, os.O_APPEND, 0666)
|
|
|
|
_, err1 = pool.File()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertEQ(err1, nil)
|
2019-06-17 17:52:10 +08:00
|
|
|
stop(testFile)
|
2019-06-17 18:09:56 +08:00
|
|
|
_, err1 = pool.File()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err1, nil)
|
2019-06-17 17:52:10 +08:00
|
|
|
|
|
|
|
// trunc mode delete file error
|
|
|
|
testFile = start("TestOpenTruncErr.txt")
|
2019-06-17 18:09:56 +08:00
|
|
|
pool = gfpool.New(testFile, os.O_TRUNC, 0666)
|
|
|
|
_, err1 = pool.File()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertEQ(err1, nil)
|
2019-06-17 17:52:10 +08:00
|
|
|
stop(testFile)
|
2019-06-17 18:09:56 +08:00
|
|
|
_, err1 = pool.File()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err1, nil)
|
2019-06-15 23:45:58 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-06-17 17:52:10 +08:00
|
|
|
// TestOpenExpire test open file cache expire
|
|
|
|
func TestOpenExpire(t *testing.T) {
|
|
|
|
testFile := start("TestOpenExpire.txt")
|
2019-06-15 23:45:58 +08:00
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-03-14 17:12:44 +08:00
|
|
|
f, err := gfpool.Open(testFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666, 100*time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertEQ(err, nil)
|
2019-06-15 23:45:58 +08:00
|
|
|
f.Close()
|
|
|
|
|
|
|
|
time.Sleep(150 * time.Millisecond)
|
2020-03-14 17:12:44 +08:00
|
|
|
f2, err1 := gfpool.Open(testFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666, 100*time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertEQ(err1, nil)
|
|
|
|
//t.AssertNE(f, f2)
|
2019-06-15 23:45:58 +08:00
|
|
|
f2.Close()
|
|
|
|
})
|
|
|
|
|
2019-06-16 00:04:46 +08:00
|
|
|
stop(testFile)
|
|
|
|
}
|
|
|
|
|
2019-06-17 17:52:10 +08:00
|
|
|
// TestNewPool test gfpool new function
|
2019-06-16 00:04:46 +08:00
|
|
|
func TestNewPool(t *testing.T) {
|
|
|
|
testFile := start("TestNewPool.txt")
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-06-17 18:09:56 +08:00
|
|
|
f, err := gfpool.Open(testFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertEQ(err, nil)
|
2019-06-16 00:04:46 +08:00
|
|
|
f.Close()
|
|
|
|
|
|
|
|
pool := gfpool.New(testFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666)
|
2019-06-17 18:09:56 +08:00
|
|
|
f2, err1 := pool.File()
|
2019-06-16 00:04:46 +08:00
|
|
|
// pool not equal
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertEQ(err1, nil)
|
|
|
|
//t.AssertNE(f, f2)
|
2019-06-16 00:04:46 +08:00
|
|
|
f2.Close()
|
|
|
|
|
|
|
|
pool.Close()
|
|
|
|
})
|
|
|
|
|
|
|
|
stop(testFile)
|
2019-06-15 23:45:58 +08:00
|
|
|
}
|
|
|
|
|
2019-06-17 17:52:10 +08:00
|
|
|
// test before
|
2019-06-16 00:04:46 +08:00
|
|
|
func start(name string) string {
|
|
|
|
testFile := os.TempDir() + string(os.PathSeparator) + name
|
2019-06-15 23:45:58 +08:00
|
|
|
if gfile.Exists(testFile) {
|
|
|
|
gfile.Remove(testFile)
|
|
|
|
}
|
|
|
|
content := "123"
|
|
|
|
gfile.PutContents(testFile, content)
|
|
|
|
return testFile
|
|
|
|
}
|
|
|
|
|
2019-06-17 17:52:10 +08:00
|
|
|
// test after
|
2019-06-16 00:04:46 +08:00
|
|
|
func stop(testFile string) {
|
2019-06-15 23:45:58 +08:00
|
|
|
if gfile.Exists(testFile) {
|
2019-06-17 17:52:10 +08:00
|
|
|
err := gfile.Remove(testFile)
|
|
|
|
if err != nil {
|
|
|
|
glog.Error(err)
|
|
|
|
}
|
2019-06-15 23:45:58 +08:00
|
|
|
}
|
|
|
|
}
|