From 54392941f333f6444c5aeb9840b46229294aedc3 Mon Sep 17 00:00:00 2001 From: "zcool321@sina.com" <1234qwer> Date: Sat, 15 Jun 2019 23:45:58 +0800 Subject: [PATCH 1/6] add gfpool normal test --- g/os/gfpool/gfpool_z_unit_test.go | 80 +++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 g/os/gfpool/gfpool_z_unit_test.go diff --git a/g/os/gfpool/gfpool_z_unit_test.go b/g/os/gfpool/gfpool_z_unit_test.go new file mode 100644 index 000000000..ee667b699 --- /dev/null +++ b/g/os/gfpool/gfpool_z_unit_test.go @@ -0,0 +1,80 @@ +package gfpool_test + +import ( + "fmt" + "github.com/gogf/gf/g/os/gfile" + "github.com/gogf/gf/g/os/gfpool" + "github.com/gogf/gf/g/test/gtest" + "os" + "testing" + "time" +) + +func TestOpen(t *testing.T) { + testFile := start() + + 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() + + }) + + stop() +} + +func TestOpenErr(t *testing.T) { + testFile := "errorPath" + + gtest.Case(t, func() { + _, err := gfpool.Open(testFile, os.O_RDWR, 0666) + gtest.AssertNE(err, nil) + }) +} + +func TestOpenExipre(t *testing.T) { + testFile := start() + + 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() + }) + + stop() +} + +func start() string { + testFile := os.TempDir() + string(os.PathSeparator) + "testGfpool.txt" + fmt.Println(testFile) + if gfile.Exists(testFile) { + gfile.Remove(testFile) + } + content := "123" + gfile.PutContents(testFile, content) + return testFile +} + +func stop() { + testFile := os.TempDir() + string(os.PathSeparator) + "testGfpool.txt" + if gfile.Exists(testFile) { + gfile.Remove(testFile) + } +} From 4f007fdd442007b8d4d9e70113f8569fd64542eb Mon Sep 17 00:00:00 2001 From: "zcool321@sina.com" <1234qwer> Date: Sun, 16 Jun 2019 00:04:46 +0800 Subject: [PATCH 2/6] update gfpool test --- g/os/gfpool/gfpool_z_unit_test.go | 43 ++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/g/os/gfpool/gfpool_z_unit_test.go b/g/os/gfpool/gfpool_z_unit_test.go index ee667b699..ab0506110 100644 --- a/g/os/gfpool/gfpool_z_unit_test.go +++ b/g/os/gfpool/gfpool_z_unit_test.go @@ -1,7 +1,6 @@ package gfpool_test import ( - "fmt" "github.com/gogf/gf/g/os/gfile" "github.com/gogf/gf/g/os/gfpool" "github.com/gogf/gf/g/test/gtest" @@ -10,8 +9,12 @@ import ( "time" ) +var ( + testErrFile = "errorPath" +) + func TestOpen(t *testing.T) { - testFile := start() + testFile := start("TestOpen.txt") gtest.Case(t, func() { f, _ := gfpool.Open(testFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666) @@ -28,20 +31,19 @@ func TestOpen(t *testing.T) { }) - stop() + stop(testFile) } func TestOpenErr(t *testing.T) { - testFile := "errorPath" gtest.Case(t, func() { - _, err := gfpool.Open(testFile, os.O_RDWR, 0666) + _, err := gfpool.Open(testErrFile, os.O_RDWR, 0666) gtest.AssertNE(err, nil) }) } func TestOpenExipre(t *testing.T) { - testFile := start() + testFile := start("TestOpenExipre.txt") gtest.Case(t, func() { f, _ := gfpool.Open(testFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666, 100) @@ -58,12 +60,30 @@ func TestOpenExipre(t *testing.T) { f3.Close() }) - stop() + stop(testFile) } -func start() string { - testFile := os.TempDir() + string(os.PathSeparator) + "testGfpool.txt" - fmt.Println(testFile) +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) +} + +func start(name string) string { + testFile := os.TempDir() + string(os.PathSeparator) + name if gfile.Exists(testFile) { gfile.Remove(testFile) } @@ -72,8 +92,7 @@ func start() string { return testFile } -func stop() { - testFile := os.TempDir() + string(os.PathSeparator) + "testGfpool.txt" +func stop(testFile string) { if gfile.Exists(testFile) { gfile.Remove(testFile) } From 68d8e25bc43b678e2e48023b88a42bdc59862880 Mon Sep 17 00:00:00 2001 From: "zcool321@sina.com" <1234qwer> Date: Sun, 16 Jun 2019 00:07:06 +0800 Subject: [PATCH 3/6] update gfpool test2 --- g/os/gfpool/gfpool_z_unit_test.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/g/os/gfpool/gfpool_z_unit_test.go b/g/os/gfpool/gfpool_z_unit_test.go index ab0506110..79a200321 100644 --- a/g/os/gfpool/gfpool_z_unit_test.go +++ b/g/os/gfpool/gfpool_z_unit_test.go @@ -9,10 +9,6 @@ import ( "time" ) -var ( - testErrFile = "errorPath" -) - func TestOpen(t *testing.T) { testFile := start("TestOpen.txt") @@ -35,8 +31,8 @@ func TestOpen(t *testing.T) { } func TestOpenErr(t *testing.T) { - gtest.Case(t, func() { + testErrFile := "errorPath" _, err := gfpool.Open(testErrFile, os.O_RDWR, 0666) gtest.AssertNE(err, nil) }) From 4713739d1aed674bf1ce25a76354e8f5e2ab3dd3 Mon Sep 17 00:00:00 2001 From: zhangbiao Date: Mon, 17 Jun 2019 17:52:10 +0800 Subject: [PATCH 4/6] update gfpool test:delete err --- g/os/gfpool/gfpool_z_unit_test.go | 48 +++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/g/os/gfpool/gfpool_z_unit_test.go b/g/os/gfpool/gfpool_z_unit_test.go index 79a200321..e916175f3 100644 --- a/g/os/gfpool/gfpool_z_unit_test.go +++ b/g/os/gfpool/gfpool_z_unit_test.go @@ -3,12 +3,14 @@ package gfpool_test import ( "github.com/gogf/gf/g/os/gfile" "github.com/gogf/gf/g/os/gfpool" + "github.com/gogf/gf/g/os/glog" "github.com/gogf/gf/g/test/gtest" "os" "testing" "time" ) +// TestOpen test open file cache func TestOpen(t *testing.T) { testFile := start("TestOpen.txt") @@ -30,16 +32,50 @@ func TestOpen(t *testing.T) { stop(testFile) } +// TestOpenErr test open file error func TestOpenErr(t *testing.T) { gtest.Case(t, func() { testErrFile := "errorPath" _, err := gfpool.Open(testErrFile, os.O_RDWR, 0666) gtest.AssertNE(err, nil) + + // 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) }) } -func TestOpenExipre(t *testing.T) { - testFile := start("TestOpenExipre.txt") +// TestOpenExpire test open file cache expire +func TestOpenExpire(t *testing.T) { + testFile := start("TestOpenExpire.txt") gtest.Case(t, func() { f, _ := gfpool.Open(testFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666, 100) @@ -59,6 +95,7 @@ func TestOpenExipre(t *testing.T) { stop(testFile) } +// TestNewPool test gfpool new function func TestNewPool(t *testing.T) { testFile := start("TestNewPool.txt") @@ -78,6 +115,7 @@ func TestNewPool(t *testing.T) { stop(testFile) } +// test before func start(name string) string { testFile := os.TempDir() + string(os.PathSeparator) + name if gfile.Exists(testFile) { @@ -88,8 +126,12 @@ func start(name string) string { return testFile } +// test after func stop(testFile string) { if gfile.Exists(testFile) { - gfile.Remove(testFile) + err := gfile.Remove(testFile) + if err != nil { + glog.Error(err) + } } } From b3b0ba775c7e11a1c4454f5d8cf6fbf62c8591b4 Mon Sep 17 00:00:00 2001 From: zhangbiao Date: Mon, 17 Jun 2019 18:09:56 +0800 Subject: [PATCH 5/6] update gfpool test:error --- g/os/gfpool/gfpool_z_unit_test.go | 61 +++++++++++++++++++------------ 1 file changed, 37 insertions(+), 24 deletions(-) diff --git a/g/os/gfpool/gfpool_z_unit_test.go b/g/os/gfpool/gfpool_z_unit_test.go index e916175f3..0cc827de1 100644 --- a/g/os/gfpool/gfpool_z_unit_test.go +++ b/g/os/gfpool/gfpool_z_unit_test.go @@ -15,15 +15,19 @@ func TestOpen(t *testing.T) { testFile := start("TestOpen.txt") gtest.Case(t, func() { - f, _ := gfpool.Open(testFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666) + f, err := gfpool.Open(testFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666) + gtest.AssertEQ(gfile.GetContents(f.Name()), "123") + gtest.AssertEQ(err, nil) f.Close() - f2, _ := gfpool.Open(testFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666) + f2, err1 := gfpool.Open(testFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666) + gtest.AssertEQ(err1, nil) 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) + f3, err2 := gfpool.OpenFile(testFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666) + gtest.AssertEQ(err2, nil) gtest.AssertEQ(f, f3) f3.Close() @@ -41,35 +45,39 @@ func TestOpenErr(t *testing.T) { // delete file error testFile := start("TestOpenDeleteErr.txt") - f, _ := gfpool.Open(testFile, os.O_RDWR, 0666) - f.Close() + pool := gfpool.New(testFile, os.O_RDWR, 0666) + _, err1 := pool.File() + gtest.AssertEQ(err1, nil) stop(testFile) - _, err = gfpool.Open(testFile, os.O_RDWR, 0666) - gtest.AssertNE(err, nil) + _, err1 = pool.File() + gtest.AssertNE(err1, nil) // append mode delete file error testFile = start("TestOpenCreateErr.txt") - f, _ = gfpool.Open(testFile, os.O_CREATE, 0666) - f.Close() + pool = gfpool.New(testFile, os.O_CREATE, 0666) + _, err1 = pool.File() + gtest.AssertEQ(err1, nil) stop(testFile) - _, err = gfpool.Open(testFile, os.O_CREATE, 0666) - gtest.AssertNE(err, nil) + _, err1 = pool.File() + gtest.AssertNE(err1, nil) // append mode delete file error testFile = start("TestOpenAppendErr.txt") - f, _ = gfpool.Open(testFile, os.O_APPEND, 0666) - f.Close() + pool = gfpool.New(testFile, os.O_APPEND, 0666) + _, err1 = pool.File() + gtest.AssertEQ(err1, nil) stop(testFile) - _, err = gfpool.Open(testFile, os.O_APPEND, 0666) - gtest.AssertNE(err, nil) + _, err1 = pool.File() + gtest.AssertNE(err1, nil) // trunc mode delete file error testFile = start("TestOpenTruncErr.txt") - f, _ = gfpool.Open(testFile, os.O_TRUNC, 0666) - f.Close() + pool = gfpool.New(testFile, os.O_TRUNC, 0666) + _, err1 = pool.File() + gtest.AssertEQ(err1, nil) stop(testFile) - _, err = gfpool.Open(testFile, os.O_TRUNC, 0666) - gtest.AssertNE(err, nil) + _, err1 = pool.File() + gtest.AssertNE(err1, nil) }) } @@ -78,16 +86,19 @@ func TestOpenExpire(t *testing.T) { testFile := start("TestOpenExpire.txt") gtest.Case(t, func() { - f, _ := gfpool.Open(testFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666, 100) + f, err := gfpool.Open(testFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666, 100) + gtest.AssertEQ(err, nil) 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) + f2, err1 := gfpool.Open(testFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666, 100) + gtest.AssertEQ(err1, nil) 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) + f3, err2 := gfpool.Open(testFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666, 100) + gtest.AssertEQ(err2, nil) gtest.AssertEQ(f2, f3) f3.Close() }) @@ -100,12 +111,14 @@ 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, err := gfpool.Open(testFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666) + gtest.AssertEQ(err, nil) f.Close() pool := gfpool.New(testFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666) - f2, _ := pool.File() + f2, err1 := pool.File() // pool not equal + gtest.AssertEQ(err1, nil) gtest.AssertNE(f, f2) f2.Close() From 80655bce50a6ffbe4f3cb39aab5b38ce4ebb5962 Mon Sep 17 00:00:00 2001 From: zhangbiao Date: Mon, 17 Jun 2019 18:39:27 +0800 Subject: [PATCH 6/6] add gfpool test:delete error --- g/os/gfpool/gfpool_z_unit_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/g/os/gfpool/gfpool_z_unit_test.go b/g/os/gfpool/gfpool_z_unit_test.go index 0cc827de1..71587fadd 100644 --- a/g/os/gfpool/gfpool_z_unit_test.go +++ b/g/os/gfpool/gfpool_z_unit_test.go @@ -16,7 +16,6 @@ func TestOpen(t *testing.T) { gtest.Case(t, func() { f, err := gfpool.Open(testFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666) - gtest.AssertEQ(gfile.GetContents(f.Name()), "123") gtest.AssertEQ(err, nil) f.Close() @@ -52,14 +51,14 @@ func TestOpenErr(t *testing.T) { _, err1 = pool.File() gtest.AssertNE(err1, nil) - // append mode delete file error + // append mode delete file error and create again testFile = start("TestOpenCreateErr.txt") pool = gfpool.New(testFile, os.O_CREATE, 0666) _, err1 = pool.File() gtest.AssertEQ(err1, nil) stop(testFile) _, err1 = pool.File() - gtest.AssertNE(err1, nil) + gtest.AssertEQ(err1, nil) // append mode delete file error testFile = start("TestOpenAppendErr.txt") @@ -77,6 +76,7 @@ func TestOpenErr(t *testing.T) { gtest.AssertEQ(err1, nil) stop(testFile) _, err1 = pool.File() + glog.Error(err1) gtest.AssertNE(err1, nil) }) }