2019-06-15 23:45:58 +08:00
|
|
|
package gfpool_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gogf/gf/g/os/gfile"
|
|
|
|
"github.com/gogf/gf/g/os/gfpool"
|
2019-06-17 17:52:10 +08:00
|
|
|
"github.com/gogf/gf/g/os/glog"
|
2019-06-15 23:45:58 +08:00
|
|
|
"github.com/gogf/gf/g/test/gtest"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
f, _ := gfpool.Open(testFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666)
|
|
|
|
f.Close()
|
|
|
|
|
|
|
|
f2, _ := gfpool.Open(testFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666)
|
|
|
|
gtest.AssertEQ(f, f2)
|
|
|
|
f2.Close()
|
|
|
|
|
|
|
|
// Deprecated test
|
|
|
|
f3, _ := gfpool.OpenFile(testFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666)
|
|
|
|
gtest.AssertEQ(f, f3)
|
|
|
|
f3.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) {
|
|
|
|
gtest.Case(t, func() {
|
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)
|
2019-06-15 23:45:58 +08:00
|
|
|
gtest.AssertNE(err, nil)
|
2019-06-17 17:52:10 +08:00
|
|
|
|
|
|
|
// delete file error
|
|
|
|
testFile := start("TestOpenDeleteErr.txt")
|
|
|
|
f, _ := gfpool.Open(testFile, os.O_RDWR, 0666)
|
|
|
|
f.Close()
|
|
|
|
stop(testFile)
|
|
|
|
_, err = gfpool.Open(testFile, os.O_RDWR, 0666)
|
|
|
|
gtest.AssertNE(err, nil)
|
|
|
|
|
|
|
|
// append mode delete file error
|
|
|
|
testFile = start("TestOpenCreateErr.txt")
|
|
|
|
f, _ = gfpool.Open(testFile, os.O_CREATE, 0666)
|
|
|
|
f.Close()
|
|
|
|
stop(testFile)
|
|
|
|
_, err = gfpool.Open(testFile, os.O_CREATE, 0666)
|
|
|
|
gtest.AssertNE(err, nil)
|
|
|
|
|
|
|
|
// append mode delete file error
|
|
|
|
testFile = start("TestOpenAppendErr.txt")
|
|
|
|
f, _ = gfpool.Open(testFile, os.O_APPEND, 0666)
|
|
|
|
f.Close()
|
|
|
|
stop(testFile)
|
|
|
|
_, err = gfpool.Open(testFile, os.O_APPEND, 0666)
|
|
|
|
gtest.AssertNE(err, nil)
|
|
|
|
|
|
|
|
// trunc mode delete file error
|
|
|
|
testFile = start("TestOpenTruncErr.txt")
|
|
|
|
f, _ = gfpool.Open(testFile, os.O_TRUNC, 0666)
|
|
|
|
f.Close()
|
|
|
|
stop(testFile)
|
|
|
|
_, err = gfpool.Open(testFile, os.O_TRUNC, 0666)
|
|
|
|
gtest.AssertNE(err, 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
|
|
|
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
f, _ := gfpool.Open(testFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666, 100)
|
|
|
|
f.Close()
|
|
|
|
|
|
|
|
time.Sleep(150 * time.Millisecond)
|
|
|
|
f2, _ := gfpool.Open(testFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666, 100)
|
|
|
|
gtest.AssertNE(f, f2)
|
|
|
|
f2.Close()
|
|
|
|
|
|
|
|
// Deprecated test
|
|
|
|
f3, _ := gfpool.OpenFile(testFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666, 100)
|
|
|
|
gtest.AssertEQ(f2, f3)
|
|
|
|
f3.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")
|
|
|
|
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
f, _ := gfpool.Open(testFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666)
|
|
|
|
f.Close()
|
|
|
|
|
|
|
|
pool := gfpool.New(testFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666)
|
|
|
|
f2, _ := pool.File()
|
|
|
|
// pool not equal
|
|
|
|
gtest.AssertNE(f, f2)
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|