gvalid and gfcache test 20190709 15:06

This commit is contained in:
陈国潮 2019-07-09 15:10:02 +08:00
parent 1c71340719
commit 125af33941
2 changed files with 48 additions and 21 deletions

View File

@ -9,6 +9,7 @@
package gfcache_test package gfcache_test
import ( import (
"io/ioutil"
"os" "os"
"testing" "testing"
"time" "time"
@ -23,43 +24,69 @@ func TestGetContents(t *testing.T) {
var f *os.File var f *os.File
var err error var err error
fileName := "test.txt" fileName := "test"
strTest := "123" strTest := "123"
if !gfile.Exists(fileName) { if !gfile.Exists(fileName) {
f, err = gfile.Create(fileName) f, err = ioutil.TempFile("", fileName)
if err != nil { if err != nil {
t.Error("create file fail") t.Error("create file fail")
} }
} }
cache := gfcache.GetContents(fileName, 2) defer f.Close()
defer os.Remove(f.Name())
if gfile.Exists(fileName) { if gfile.Exists(f.Name()) {
f, err = gfile.OpenFile(fileName, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
f, err = gfile.OpenFile(f.Name(), os.O_APPEND|os.O_WRONLY, os.ModeAppend)
if err != nil { if err != nil {
t.Error("file open fail", err) t.Error("file open fail", err)
} }
err = gfile.PutContents(f.Name(), strTest)
if err != nil {
t.Error("write error", err)
}
cache := gfcache.GetContents(f.Name(), 1)
gtest.Assert(cache, strTest)
}
})
gtest.Case(t, func() {
var f *os.File
var err error
fileName := "test2"
strTest := "123"
if !gfile.Exists(fileName) {
f, err = ioutil.TempFile("", fileName)
if err != nil {
t.Error("create file fail")
}
} }
defer f.Close() defer f.Close()
defer os.Remove(f.Name())
_, err = f.Write([]byte(strTest)) if gfile.Exists(f.Name()) {
if err != nil { cache := gfcache.GetContents(f.Name())
t.Error("write error", err)
}
cache = gfcache.GetContents(fileName) f, err = gfile.OpenFile(f.Name(), os.O_APPEND|os.O_WRONLY, os.ModeAppend)
gtest.Assert(cache, "")
time.Sleep(time.Duration(4) * time.Second)
if gfile.Exists(fileName) {
err = gfile.Remove(fileName)
if err != nil { if err != nil {
t.Error("file remove fail", err) t.Error("file open fail", err)
} }
err = gfile.PutContents(f.Name(), strTest)
if err != nil {
t.Error("write error", err)
}
gtest.Assert(cache, "")
time.Sleep(100 * time.Millisecond)
} }
}) })
} }

View File

@ -24,9 +24,9 @@ func Test_Check(t *testing.T) {
err1 := gvalid.Check(val1, rule, nil) err1 := gvalid.Check(val1, rule, nil)
err2 := gvalid.Check(val2, rule, nil) err2 := gvalid.Check(val2, rule, nil)
err3 := gvalid.Check(val3, rule, nil) err3 := gvalid.Check(val3, rule, nil)
gtest.AssertNE(err1, nil) gtest.Assert(err1, "invalid rules:abc:6,16")
gtest.AssertNE(err2, nil) gtest.Assert(err2, "invalid rules:abc:6,16")
gtest.AssertNE(err3, nil) gtest.Assert(err3, "invalid rules:abc:6,16")
}) })
} }