gf/os/gfpool/gfpool_z_unit_test.go

325 lines
8.1 KiB
Go
Raw Normal View History

2021-09-28 19:04:36 +08:00
// Copyright GoFrame Author(https://goframe.org). 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.
2019-06-15 23:45:58 +08:00
package gfpool_test
import (
2021-09-28 19:04:36 +08:00
"context"
2019-07-22 15:31:35 +08:00
"os"
"testing"
"time"
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/os/gfpool"
"github.com/gogf/gf/v2/os/glog"
2021-11-17 23:20:58 +08:00
"github.com/gogf/gf/v2/os/gtime"
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/test/gtest"
2021-11-17 23:20:58 +08:00
"github.com/gogf/gf/v2/text/gstr"
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) {
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)
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 {
2021-09-28 19:04:36 +08:00
glog.Error(context.TODO(), err)
2019-06-17 17:52:10 +08:00
}
2019-06-15 23:45:58 +08:00
}
}
2021-11-17 23:20:58 +08:00
func Test_ConcurrentOS(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
path := gfile.TempDir(gtime.TimestampNanoStr())
defer gfile.Remove(path)
f1, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666)
t.Assert(err, nil)
defer f1.Close()
f2, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666)
t.Assert(err, nil)
defer f2.Close()
for i := 0; i < 100; i++ {
_, err = f1.Write([]byte("@1234567890#"))
t.Assert(err, nil)
}
for i := 0; i < 100; i++ {
_, err = f2.Write([]byte("@1234567890#"))
t.Assert(err, nil)
}
for i := 0; i < 1000; i++ {
_, err = f1.Write([]byte("@1234567890#"))
t.Assert(err, nil)
}
for i := 0; i < 1000; i++ {
_, err = f2.Write([]byte("@1234567890#"))
t.Assert(err, nil)
}
t.Assert(gstr.Count(gfile.GetContents(path), "@1234567890#"), 2200)
})
gtest.C(t, func(t *gtest.T) {
path := gfile.TempDir(gtime.TimestampNanoStr())
defer gfile.Remove(path)
f1, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666)
t.Assert(err, nil)
defer f1.Close()
f2, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666)
t.Assert(err, nil)
defer f2.Close()
for i := 0; i < 1000; i++ {
_, err = f1.Write([]byte("@1234567890#"))
t.Assert(err, nil)
}
for i := 0; i < 1000; i++ {
_, err = f2.Write([]byte("@1234567890#"))
t.Assert(err, nil)
}
t.Assert(gstr.Count(gfile.GetContents(path), "@1234567890#"), 2000)
})
gtest.C(t, func(t *gtest.T) {
path := gfile.TempDir(gtime.TimestampNanoStr())
defer gfile.Remove(path)
f1, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666)
t.Assert(err, nil)
defer f1.Close()
f2, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666)
t.Assert(err, nil)
defer f2.Close()
s1 := ""
for i := 0; i < 1000; i++ {
s1 += "@1234567890#"
}
_, err = f2.Write([]byte(s1))
t.Assert(err, nil)
s2 := ""
for i := 0; i < 1000; i++ {
s2 += "@1234567890#"
}
_, err = f2.Write([]byte(s2))
t.Assert(err, nil)
t.Assert(gstr.Count(gfile.GetContents(path), "@1234567890#"), 2000)
})
// DATA RACE
// gtest.C(t, func(t *gtest.T) {
// path := gfile.TempDir(gtime.TimestampNanoStr())
// defer gfile.Remove(path)
// f1, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666)
// t.Assert(err, nil)
// defer f1.Close()
//
// f2, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666)
// t.Assert(err, nil)
// defer f2.Close()
//
// wg := sync.WaitGroup{}
// ch := make(chan struct{})
// for i := 0; i < 1000; i++ {
// wg.Add(1)
// go func() {
// defer wg.Done()
// <-ch
// _, err = f1.Write([]byte("@1234567890#"))
// t.Assert(err, nil)
// }()
// }
// for i := 0; i < 1000; i++ {
// wg.Add(1)
// go func() {
// defer wg.Done()
// <-ch
// _, err = f2.Write([]byte("@1234567890#"))
// t.Assert(err, nil)
// }()
// }
// close(ch)
// wg.Wait()
// t.Assert(gstr.Count(gfile.GetContents(path), "@1234567890#"), 2000)
// })
}
func Test_ConcurrentGFPool(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
path := gfile.TempDir(gtime.TimestampNanoStr())
defer gfile.Remove(path)
f1, err := gfpool.Open(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666)
t.Assert(err, nil)
defer f1.Close()
f2, err := gfpool.Open(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666)
t.Assert(err, nil)
defer f2.Close()
for i := 0; i < 1000; i++ {
_, err = f1.Write([]byte("@1234567890#"))
t.Assert(err, nil)
}
for i := 0; i < 1000; i++ {
_, err = f2.Write([]byte("@1234567890#"))
t.Assert(err, nil)
}
t.Assert(gstr.Count(gfile.GetContents(path), "@1234567890#"), 2000)
})
// DATA RACE
// gtest.C(t, func(t *gtest.T) {
// path := gfile.TempDir(gtime.TimestampNanoStr())
// defer gfile.Remove(path)
// f1, err := gfpool.Open(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666)
// t.Assert(err, nil)
// defer f1.Close()
//
// f2, err := gfpool.Open(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666)
// t.Assert(err, nil)
// defer f2.Close()
//
// wg := sync.WaitGroup{}
// ch := make(chan struct{})
// for i := 0; i < 1000; i++ {
// wg.Add(1)
// go func() {
// defer wg.Done()
// <-ch
// _, err = f1.Write([]byte("@1234567890#"))
// t.Assert(err, nil)
// }()
// }
// for i := 0; i < 1000; i++ {
// wg.Add(1)
// go func() {
// defer wg.Done()
// <-ch
// _, err = f2.Write([]byte("@1234567890#"))
// t.Assert(err, nil)
// }()
// }
// close(ch)
// wg.Wait()
// t.Assert(gstr.Count(gfile.GetContents(path), "@1234567890#"), 2000)
// })
}