fix sonar Part3
This commit is contained in:
parent
6f98a01bfd
commit
62aa0188c4
@ -26,3 +26,4 @@
|
||||
## Fix Sonar
|
||||
- [4473](https://github.com/beego/beego/pull/4473)
|
||||
- [4474](https://github.com/beego/beego/pull/4474)
|
||||
- [4479](https://github.com/beego/beego/pull/4479)
|
||||
@ -22,6 +22,8 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
const setCookieKey = "Set-Cookie"
|
||||
|
||||
func TestCookie(t *testing.T) {
|
||||
config := `{"cookieName":"gosessionid","enableSetCookie":false,"gclifetime":3600,"ProviderConfig":"{\"cookieName\":\"gosessionid\",\"securityKey\":\"beegocookiehashkey\"}"}`
|
||||
conf := new(ManagerConfig)
|
||||
@ -46,7 +48,8 @@ func TestCookie(t *testing.T) {
|
||||
t.Fatal("get username error")
|
||||
}
|
||||
sess.SessionRelease(w)
|
||||
if cookiestr := w.Header().Get("Set-Cookie"); cookiestr == "" {
|
||||
|
||||
if cookiestr := w.Header().Get(setCookieKey); cookiestr == "" {
|
||||
t.Fatal("setcookie error")
|
||||
} else {
|
||||
parts := strings.Split(strings.TrimSpace(cookiestr), ";")
|
||||
@ -79,7 +82,7 @@ func TestDestorySessionCookie(t *testing.T) {
|
||||
|
||||
// request again ,will get same sesssion id .
|
||||
r1, _ := http.NewRequest("GET", "/", nil)
|
||||
r1.Header.Set("Cookie", w.Header().Get("Set-Cookie"))
|
||||
r1.Header.Set("Cookie", w.Header().Get(setCookieKey))
|
||||
w = httptest.NewRecorder()
|
||||
newSession, err := globalSessions.SessionStart(w, r1)
|
||||
if err != nil {
|
||||
@ -92,7 +95,7 @@ func TestDestorySessionCookie(t *testing.T) {
|
||||
// After destroy session , will get a new session id .
|
||||
globalSessions.SessionDestroy(w, r1)
|
||||
r2, _ := http.NewRequest("GET", "/", nil)
|
||||
r2.Header.Set("Cookie", w.Header().Get("Set-Cookie"))
|
||||
r2.Header.Set("Cookie", w.Header().Get(setCookieKey))
|
||||
|
||||
w = httptest.NewRecorder()
|
||||
newSession, err = globalSessions.SessionStart(w, r2)
|
||||
|
||||
@ -19,19 +19,15 @@ import (
|
||||
"net/url"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestSubstr(t *testing.T) {
|
||||
s := `012345`
|
||||
if Substr(s, 0, 2) != "01" {
|
||||
t.Error("should be equal")
|
||||
}
|
||||
if Substr(s, 0, 100) != "012345" {
|
||||
t.Error("should be equal")
|
||||
}
|
||||
if Substr(s, 12, 100) != "012345" {
|
||||
t.Error("should be equal")
|
||||
}
|
||||
assert.Equal(t, "01", Substr(s, 0, 2))
|
||||
assert.Equal(t, "012345", Substr(s, 0, 100))
|
||||
assert.Equal(t, "012345", Substr(s, 12, 100))
|
||||
}
|
||||
|
||||
func TestHtml2str(t *testing.T) {
|
||||
@ -39,73 +35,51 @@ func TestHtml2str(t *testing.T) {
|
||||
|
||||
|
||||
\n`
|
||||
if HTML2str(h) != "123\\n\n\\n" {
|
||||
t.Error("should be equal")
|
||||
}
|
||||
assert.Equal(t, "123\\n\n\\n", HTML2str(h))
|
||||
}
|
||||
|
||||
func TestDateFormat(t *testing.T) {
|
||||
ts := "Mon, 01 Jul 2013 13:27:42 CST"
|
||||
tt, _ := time.Parse(time.RFC1123, ts)
|
||||
|
||||
if ss := DateFormat(tt, "2006-01-02 15:04:05"); ss != "2013-07-01 13:27:42" {
|
||||
t.Errorf("2013-07-01 13:27:42 does not equal %v", ss)
|
||||
}
|
||||
assert.Equal(t, "2013-07-01 13:27:42", DateFormat(tt, "2006-01-02 15:04:05"))
|
||||
}
|
||||
|
||||
func TestDate(t *testing.T) {
|
||||
ts := "Mon, 01 Jul 2013 13:27:42 CST"
|
||||
tt, _ := time.Parse(time.RFC1123, ts)
|
||||
|
||||
if ss := Date(tt, "Y-m-d H:i:s"); ss != "2013-07-01 13:27:42" {
|
||||
t.Errorf("2013-07-01 13:27:42 does not equal %v", ss)
|
||||
}
|
||||
if ss := Date(tt, "y-n-j h:i:s A"); ss != "13-7-1 01:27:42 PM" {
|
||||
t.Errorf("13-7-1 01:27:42 PM does not equal %v", ss)
|
||||
}
|
||||
if ss := Date(tt, "D, d M Y g:i:s a"); ss != "Mon, 01 Jul 2013 1:27:42 pm" {
|
||||
t.Errorf("Mon, 01 Jul 2013 1:27:42 pm does not equal %v", ss)
|
||||
}
|
||||
if ss := Date(tt, "l, d F Y G:i:s"); ss != "Monday, 01 July 2013 13:27:42" {
|
||||
t.Errorf("Monday, 01 July 2013 13:27:42 does not equal %v", ss)
|
||||
}
|
||||
assert.Equal(t, "2013-07-01 13:27:42", Date(tt, "Y-m-d H:i:s"))
|
||||
|
||||
assert.Equal(t, "13-7-1 01:27:42 PM", Date(tt, "y-n-j h:i:s A"))
|
||||
assert.Equal(t, "Mon, 01 Jul 2013 1:27:42 pm", Date(tt, "D, d M Y g:i:s a"))
|
||||
assert.Equal(t, "Monday, 01 July 2013 13:27:42", Date(tt, "l, d F Y G:i:s"))
|
||||
}
|
||||
|
||||
func TestCompareRelated(t *testing.T) {
|
||||
if !Compare("abc", "abc") {
|
||||
t.Error("should be equal")
|
||||
}
|
||||
if Compare("abc", "aBc") {
|
||||
t.Error("should be not equal")
|
||||
}
|
||||
if !Compare("1", 1) {
|
||||
t.Error("should be equal")
|
||||
}
|
||||
if CompareNot("abc", "abc") {
|
||||
t.Error("should be equal")
|
||||
}
|
||||
if !CompareNot("abc", "aBc") {
|
||||
t.Error("should be not equal")
|
||||
}
|
||||
if !NotNil("a string") {
|
||||
t.Error("should not be nil")
|
||||
}
|
||||
assert.True(t, Compare("abc", "abc"))
|
||||
|
||||
assert.False(t, Compare("abc", "aBc"))
|
||||
|
||||
assert.True(t, Compare("1", 1))
|
||||
|
||||
assert.False(t, CompareNot("abc", "abc"))
|
||||
|
||||
assert.True(t, CompareNot("abc", "aBc"))
|
||||
assert.True(t, NotNil("a string"))
|
||||
}
|
||||
|
||||
func TestHtmlquote(t *testing.T) {
|
||||
h := `<' ”“&">`
|
||||
s := `<' ”“&">`
|
||||
if Htmlquote(s) != h {
|
||||
t.Error("should be equal")
|
||||
}
|
||||
assert.Equal(t, h, Htmlquote(s))
|
||||
}
|
||||
|
||||
func TestHtmlunquote(t *testing.T) {
|
||||
h := `<' ”“&">`
|
||||
s := `<' ”“&">`
|
||||
if Htmlunquote(h) != s {
|
||||
t.Error("should be equal")
|
||||
}
|
||||
assert.Equal(t, s, Htmlunquote(h))
|
||||
|
||||
}
|
||||
|
||||
func TestParseForm(t *testing.T) {
|
||||
@ -148,55 +122,42 @@ func TestParseForm(t *testing.T) {
|
||||
"hobby": []string{"", "Basketball", "Football"},
|
||||
"memo": []string{"nothing"},
|
||||
}
|
||||
if err := ParseForm(form, u); err == nil {
|
||||
t.Fatal("nothing will be changed")
|
||||
}
|
||||
if err := ParseForm(form, &u); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if u.ID != 0 {
|
||||
t.Errorf("ID should equal 0 but got %v", u.ID)
|
||||
}
|
||||
if len(u.tag) != 0 {
|
||||
t.Errorf("tag's length should equal 0 but got %v", len(u.tag))
|
||||
}
|
||||
if u.Name.(string) != "test" {
|
||||
t.Errorf("Name should equal `test` but got `%v`", u.Name.(string))
|
||||
}
|
||||
if u.Age != 40 {
|
||||
t.Errorf("Age should equal 40 but got %v", u.Age)
|
||||
}
|
||||
if u.Email != "test@gmail.com" {
|
||||
t.Errorf("Email should equal `test@gmail.com` but got `%v`", u.Email)
|
||||
}
|
||||
if u.Intro != "I am an engineer!" {
|
||||
t.Errorf("Intro should equal `I am an engineer!` but got `%v`", u.Intro)
|
||||
}
|
||||
if !u.StrBool {
|
||||
t.Errorf("strboll should equal `true`, but got `%v`", u.StrBool)
|
||||
}
|
||||
|
||||
assert.NotNil(t, ParseForm(form, u))
|
||||
|
||||
assert.Nil(t, ParseForm(form, &u))
|
||||
|
||||
assert.Equal(t, 0, u.ID)
|
||||
|
||||
assert.Equal(t, 0, len(u.tag))
|
||||
|
||||
assert.Equal(t, "test", u.Name)
|
||||
|
||||
assert.Equal(t, 40, u.Age)
|
||||
|
||||
assert.Equal(t, "test@gmail.com", u.Email)
|
||||
|
||||
assert.Equal(t, "I am an engineer!", u.Intro)
|
||||
|
||||
assert.True(t, u.StrBool)
|
||||
|
||||
y, m, d := u.Date.Date()
|
||||
if y != 2014 || m.String() != "November" || d != 12 {
|
||||
t.Errorf("Date should equal `2014-11-12`, but got `%v`", u.Date.String())
|
||||
}
|
||||
if u.Organization != "beego" {
|
||||
t.Errorf("Organization should equal `beego`, but got `%v`", u.Organization)
|
||||
}
|
||||
if u.Title != "CXO" {
|
||||
t.Errorf("Title should equal `CXO`, but got `%v`", u.Title)
|
||||
}
|
||||
if u.Hobby[0] != "" {
|
||||
t.Errorf("Hobby should equal ``, but got `%v`", u.Hobby[0])
|
||||
}
|
||||
if u.Hobby[1] != "Basketball" {
|
||||
t.Errorf("Hobby should equal `Basketball`, but got `%v`", u.Hobby[1])
|
||||
}
|
||||
if u.Hobby[2] != "Football" {
|
||||
t.Errorf("Hobby should equal `Football`, but got `%v`", u.Hobby[2])
|
||||
}
|
||||
if len(u.Memo) != 0 {
|
||||
t.Errorf("Memo's length should equal 0 but got %v", len(u.Memo))
|
||||
}
|
||||
|
||||
assert.Equal(t, 2014, y)
|
||||
assert.Equal(t, "November", m.String())
|
||||
assert.Equal(t, 12, d)
|
||||
|
||||
assert.Equal(t, "beego", u.Organization)
|
||||
|
||||
assert.Equal(t, "CXO", u.Title)
|
||||
|
||||
assert.Equal(t, "", u.Hobby[0])
|
||||
|
||||
assert.Equal(t, "Basketball", u.Hobby[1])
|
||||
|
||||
assert.Equal(t, "Football", u.Hobby[2])
|
||||
|
||||
assert.Equal(t, 0, len(u.Memo))
|
||||
}
|
||||
|
||||
func TestRenderForm(t *testing.T) {
|
||||
@ -212,18 +173,14 @@ func TestRenderForm(t *testing.T) {
|
||||
|
||||
u := user{Name: "test", Intro: "Some Text"}
|
||||
output := RenderForm(u)
|
||||
if output != template.HTML("") {
|
||||
t.Errorf("output should be empty but got %v", output)
|
||||
}
|
||||
assert.Equal(t, template.HTML(""), output)
|
||||
output = RenderForm(&u)
|
||||
result := template.HTML(
|
||||
`Name: <input name="username" type="text" value="test"></br>` +
|
||||
`年龄:<input name="age" type="text" value="0"></br>` +
|
||||
`Sex: <input name="Sex" type="text" value=""></br>` +
|
||||
`Intro: <textarea name="Intro">Some Text</textarea>`)
|
||||
if output != result {
|
||||
t.Errorf("output should equal `%v` but got `%v`", result, output)
|
||||
}
|
||||
assert.Equal(t, result, output)
|
||||
}
|
||||
|
||||
func TestMapGet(t *testing.T) {
|
||||
@ -233,29 +190,18 @@ func TestMapGet(t *testing.T) {
|
||||
"1": 2,
|
||||
}
|
||||
|
||||
if res, err := MapGet(m1, "a"); err == nil {
|
||||
if res.(int64) != 1 {
|
||||
t.Errorf("Should return 1, but return %v", res)
|
||||
}
|
||||
} else {
|
||||
t.Errorf("Error happens %v", err)
|
||||
}
|
||||
res, err := MapGet(m1, "a")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, int64(1), res)
|
||||
|
||||
if res, err := MapGet(m1, "1"); err == nil {
|
||||
if res.(int64) != 2 {
|
||||
t.Errorf("Should return 2, but return %v", res)
|
||||
}
|
||||
} else {
|
||||
t.Errorf("Error happens %v", err)
|
||||
}
|
||||
res, err = MapGet(m1, "1")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, int64(2), res)
|
||||
|
||||
if res, err := MapGet(m1, 1); err == nil {
|
||||
if res.(int64) != 2 {
|
||||
t.Errorf("Should return 2, but return %v", res)
|
||||
}
|
||||
} else {
|
||||
t.Errorf("Error happens %v", err)
|
||||
}
|
||||
|
||||
res, err = MapGet(m1, 1)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, int64(2), res)
|
||||
|
||||
// test 2 level map
|
||||
m2 := M{
|
||||
@ -264,13 +210,9 @@ func TestMapGet(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
if res, err := MapGet(m2, 1, 2); err == nil {
|
||||
if res.(float64) != 3.5 {
|
||||
t.Errorf("Should return 3.5, but return %v", res)
|
||||
}
|
||||
} else {
|
||||
t.Errorf("Error happens %v", err)
|
||||
}
|
||||
res, err = MapGet(m2, 1, 2)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, 3.5, res)
|
||||
|
||||
// test 5 level map
|
||||
m5 := M{
|
||||
@ -285,20 +227,13 @@ func TestMapGet(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
if res, err := MapGet(m5, 1, 2, 3, 4, 5); err == nil {
|
||||
if res.(float64) != 1.2 {
|
||||
t.Errorf("Should return 1.2, but return %v", res)
|
||||
}
|
||||
} else {
|
||||
t.Errorf("Error happens %v", err)
|
||||
}
|
||||
res, err = MapGet(m5, 1, 2, 3, 4, 5)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, 1.2, res)
|
||||
|
||||
// check whether element not exists in map
|
||||
if res, err := MapGet(m5, 5, 4, 3, 2, 1); err == nil {
|
||||
if res != nil {
|
||||
t.Errorf("Should return nil, but return %v", res)
|
||||
}
|
||||
} else {
|
||||
t.Errorf("Error happens %v", err)
|
||||
}
|
||||
res, err = MapGet(m5, 5, 4, 3, 2, 1)
|
||||
assert.Nil(t, err)
|
||||
assert.Nil(t, res)
|
||||
|
||||
}
|
||||
|
||||
@ -21,13 +21,16 @@ import (
|
||||
)
|
||||
|
||||
func TestStatics(t *testing.T) {
|
||||
StatisticsMap.AddStatistics("POST", "/api/user", "&admin.user", time.Duration(2000))
|
||||
StatisticsMap.AddStatistics("POST", "/api/user", "&admin.user", time.Duration(120000))
|
||||
StatisticsMap.AddStatistics("GET", "/api/user", "&admin.user", time.Duration(13000))
|
||||
StatisticsMap.AddStatistics("POST", "/api/admin", "&admin.user", time.Duration(14000))
|
||||
StatisticsMap.AddStatistics("POST", "/api/user/astaxie", "&admin.user", time.Duration(12000))
|
||||
StatisticsMap.AddStatistics("POST", "/api/user/xiemengjun", "&admin.user", time.Duration(13000))
|
||||
StatisticsMap.AddStatistics("DELETE", "/api/user", "&admin.user", time.Duration(1400))
|
||||
userApi := "/api/user"
|
||||
post := "POST"
|
||||
adminUser := "&admin.user"
|
||||
StatisticsMap.AddStatistics(post, userApi, adminUser, time.Duration(2000))
|
||||
StatisticsMap.AddStatistics(post, userApi, adminUser, time.Duration(120000))
|
||||
StatisticsMap.AddStatistics("GET", userApi, adminUser, time.Duration(13000))
|
||||
StatisticsMap.AddStatistics(post, "/api/admin", adminUser, time.Duration(14000))
|
||||
StatisticsMap.AddStatistics(post, "/api/user/astaxie", adminUser, time.Duration(12000))
|
||||
StatisticsMap.AddStatistics(post, "/api/user/xiemengjun", adminUser, time.Duration(13000))
|
||||
StatisticsMap.AddStatistics("DELETE", userApi, adminUser, time.Duration(1400))
|
||||
t.Log(StatisticsMap.GetMap())
|
||||
|
||||
data := StatisticsMap.GetMapData()
|
||||
|
||||
@ -16,7 +16,7 @@ package utils
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestRand_01(t *testing.T) {
|
||||
func TestRand01(t *testing.T) {
|
||||
bs0 := RandomCreateBytes(16)
|
||||
bs1 := RandomCreateBytes(16)
|
||||
|
||||
|
||||
@ -18,131 +18,83 @@ import (
|
||||
"regexp"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestRequired(t *testing.T) {
|
||||
valid := Validation{}
|
||||
|
||||
if valid.Required(nil, "nil").Ok {
|
||||
t.Error("nil object should be false")
|
||||
}
|
||||
if !valid.Required(true, "bool").Ok {
|
||||
t.Error("Bool value should always return true")
|
||||
}
|
||||
if !valid.Required(false, "bool").Ok {
|
||||
t.Error("Bool value should always return true")
|
||||
}
|
||||
if valid.Required("", "string").Ok {
|
||||
t.Error("\"'\" string should be false")
|
||||
}
|
||||
if valid.Required(" ", "string").Ok {
|
||||
t.Error("\" \" string should be false") // For #2361
|
||||
}
|
||||
if valid.Required("\n", "string").Ok {
|
||||
t.Error("new line string should be false") // For #2361
|
||||
}
|
||||
if !valid.Required("astaxie", "string").Ok {
|
||||
t.Error("string should be true")
|
||||
}
|
||||
if valid.Required(0, "zero").Ok {
|
||||
t.Error("Integer should not be equal 0")
|
||||
}
|
||||
if !valid.Required(1, "int").Ok {
|
||||
t.Error("Integer except 0 should be true")
|
||||
}
|
||||
if !valid.Required(time.Now(), "time").Ok {
|
||||
t.Error("time should be true")
|
||||
}
|
||||
if valid.Required([]string{}, "emptySlice").Ok {
|
||||
t.Error("empty slice should be false")
|
||||
}
|
||||
if !valid.Required([]interface{}{"ok"}, "slice").Ok {
|
||||
t.Error("slice should be true")
|
||||
}
|
||||
assert.False(t, valid.Required(nil, "nil").Ok)
|
||||
assert.True(t, valid.Required(true, "bool").Ok)
|
||||
|
||||
assert.True(t, valid.Required(false, "bool").Ok)
|
||||
assert.False(t, valid.Required("", "string").Ok)
|
||||
assert.False(t, valid.Required(" ", "string").Ok)
|
||||
assert.False(t, valid.Required("\n", "string").Ok)
|
||||
|
||||
assert.True(t, valid.Required("astaxie", "string").Ok)
|
||||
assert.False(t, valid.Required(0, "zero").Ok)
|
||||
|
||||
assert.True(t, valid.Required(1, "int").Ok)
|
||||
|
||||
assert.True(t, valid.Required(time.Now(), "time").Ok)
|
||||
|
||||
assert.False(t, valid.Required([]string{}, "emptySlice").Ok)
|
||||
|
||||
assert.True(t, valid.Required([]interface{}{"ok"}, "slice").Ok)
|
||||
}
|
||||
|
||||
func TestMin(t *testing.T) {
|
||||
valid := Validation{}
|
||||
|
||||
if valid.Min(-1, 0, "min0").Ok {
|
||||
t.Error("-1 is less than the minimum value of 0 should be false")
|
||||
}
|
||||
if !valid.Min(1, 0, "min0").Ok {
|
||||
t.Error("1 is greater or equal than the minimum value of 0 should be true")
|
||||
}
|
||||
assert.False(t, valid.Min(-1, 0, "min0").Ok)
|
||||
assert.True(t, valid.Min(1, 0, "min0").Ok)
|
||||
|
||||
}
|
||||
|
||||
func TestMax(t *testing.T) {
|
||||
valid := Validation{}
|
||||
|
||||
if valid.Max(1, 0, "max0").Ok {
|
||||
t.Error("1 is greater than the minimum value of 0 should be false")
|
||||
}
|
||||
if !valid.Max(-1, 0, "max0").Ok {
|
||||
t.Error("-1 is less or equal than the maximum value of 0 should be true")
|
||||
}
|
||||
assert.False(t, valid.Max(1, 0, "max0").Ok)
|
||||
assert.True(t, valid.Max(-1, 0, "max0").Ok)
|
||||
}
|
||||
|
||||
func TestRange(t *testing.T) {
|
||||
valid := Validation{}
|
||||
|
||||
if valid.Range(-1, 0, 1, "range0_1").Ok {
|
||||
t.Error("-1 is between 0 and 1 should be false")
|
||||
}
|
||||
if !valid.Range(1, 0, 1, "range0_1").Ok {
|
||||
t.Error("1 is between 0 and 1 should be true")
|
||||
}
|
||||
assert.False(t, valid.Range(-1, 0, 1, "range0_1").Ok)
|
||||
|
||||
assert.True(t, valid.Range(1, 0, 1, "range0_1").Ok)
|
||||
}
|
||||
|
||||
func TestMinSize(t *testing.T) {
|
||||
valid := Validation{}
|
||||
|
||||
if valid.MinSize("", 1, "minSize1").Ok {
|
||||
t.Error("the length of \"\" is less than the minimum value of 1 should be false")
|
||||
}
|
||||
if !valid.MinSize("ok", 1, "minSize1").Ok {
|
||||
t.Error("the length of \"ok\" is greater or equal than the minimum value of 1 should be true")
|
||||
}
|
||||
if valid.MinSize([]string{}, 1, "minSize1").Ok {
|
||||
t.Error("the length of empty slice is less than the minimum value of 1 should be false")
|
||||
}
|
||||
if !valid.MinSize([]interface{}{"ok"}, 1, "minSize1").Ok {
|
||||
t.Error("the length of [\"ok\"] is greater or equal than the minimum value of 1 should be true")
|
||||
}
|
||||
assert.False(t, valid.MinSize("", 1, "minSize1").Ok)
|
||||
|
||||
assert.True(t, valid.MinSize("ok", 1, "minSize1").Ok)
|
||||
assert.False(t, valid.MinSize([]string{}, 1, "minSize1").Ok)
|
||||
assert.True(t, valid.MinSize([]interface{}{"ok"}, 1, "minSize1").Ok)
|
||||
}
|
||||
|
||||
func TestMaxSize(t *testing.T) {
|
||||
valid := Validation{}
|
||||
|
||||
if valid.MaxSize("ok", 1, "maxSize1").Ok {
|
||||
t.Error("the length of \"ok\" is greater than the maximum value of 1 should be false")
|
||||
}
|
||||
if !valid.MaxSize("", 1, "maxSize1").Ok {
|
||||
t.Error("the length of \"\" is less or equal than the maximum value of 1 should be true")
|
||||
}
|
||||
if valid.MaxSize([]interface{}{"ok", false}, 1, "maxSize1").Ok {
|
||||
t.Error("the length of [\"ok\", false] is greater than the maximum value of 1 should be false")
|
||||
}
|
||||
if !valid.MaxSize([]string{}, 1, "maxSize1").Ok {
|
||||
t.Error("the length of empty slice is less or equal than the maximum value of 1 should be true")
|
||||
}
|
||||
assert.False(t, valid.MaxSize("ok", 1, "maxSize1").Ok)
|
||||
assert.True(t, valid.MaxSize("", 1, "maxSize1").Ok)
|
||||
assert.False(t, valid.MaxSize([]interface{}{"ok", false}, 1, "maxSize1").Ok)
|
||||
assert.True(t, valid.MaxSize([]string{}, 1, "maxSize1").Ok)
|
||||
}
|
||||
|
||||
func TestLength(t *testing.T) {
|
||||
valid := Validation{}
|
||||
|
||||
if valid.Length("", 1, "length1").Ok {
|
||||
t.Error("the length of \"\" must equal 1 should be false")
|
||||
}
|
||||
if !valid.Length("1", 1, "length1").Ok {
|
||||
t.Error("the length of \"1\" must equal 1 should be true")
|
||||
}
|
||||
if valid.Length([]string{}, 1, "length1").Ok {
|
||||
t.Error("the length of empty slice must equal 1 should be false")
|
||||
}
|
||||
if !valid.Length([]interface{}{"ok"}, 1, "length1").Ok {
|
||||
t.Error("the length of [\"ok\"] must equal 1 should be true")
|
||||
}
|
||||
assert.False(t, valid.Length("", 1, "length1").Ok)
|
||||
assert.True(t, valid.Length("1", 1, "length1").Ok)
|
||||
|
||||
assert.False(t, valid.Length([]string{}, 1, "length1").Ok)
|
||||
assert.True(t, valid.Length([]interface{}{"ok"}, 1, "length1").Ok)
|
||||
}
|
||||
|
||||
func TestAlpha(t *testing.T) {
|
||||
@ -178,13 +130,16 @@ func TestAlphaNumeric(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
const email = "suchuangji@gmail.com"
|
||||
|
||||
func TestMatch(t *testing.T) {
|
||||
valid := Validation{}
|
||||
|
||||
if valid.Match("suchuangji@gmail", regexp.MustCompile(`^\w+@\w+\.\w+$`), "match").Ok {
|
||||
t.Error("\"suchuangji@gmail\" match \"^\\w+@\\w+\\.\\w+$\" should be false")
|
||||
}
|
||||
if !valid.Match("suchuangji@gmail.com", regexp.MustCompile(`^\w+@\w+\.\w+$`), "match").Ok {
|
||||
|
||||
if !valid.Match(email, regexp.MustCompile(`^\w+@\w+\.\w+$`), "match").Ok {
|
||||
t.Error("\"suchuangji@gmail\" match \"^\\w+@\\w+\\.\\w+$\" should be true")
|
||||
}
|
||||
}
|
||||
@ -217,7 +172,7 @@ func TestEmail(t *testing.T) {
|
||||
if valid.Email("not@a email", "email").Ok {
|
||||
t.Error("\"not@a email\" is a valid email address should be false")
|
||||
}
|
||||
if !valid.Email("suchuangji@gmail.com", "email").Ok {
|
||||
if !valid.Email(email, "email").Ok {
|
||||
t.Error("\"suchuangji@gmail.com\" is a valid email address should be true")
|
||||
}
|
||||
if valid.Email("@suchuangji@gmail.com", "email").Ok {
|
||||
@ -242,7 +197,7 @@ func TestIP(t *testing.T) {
|
||||
func TestBase64(t *testing.T) {
|
||||
valid := Validation{}
|
||||
|
||||
if valid.Base64("suchuangji@gmail.com", "base64").Ok {
|
||||
if valid.Base64(email, "base64").Ok {
|
||||
t.Error("\"suchuangji@gmail.com\" are a valid base64 characters should be false")
|
||||
}
|
||||
if !valid.Base64("c3VjaHVhbmdqaUBnbWFpbC5jb20=", "base64").Ok {
|
||||
@ -370,44 +325,25 @@ func TestValid(t *testing.T) {
|
||||
|
||||
u := user{Name: "test@/test/;com", Age: 40}
|
||||
b, err := valid.Valid(u)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !b {
|
||||
t.Error("validation should be passed")
|
||||
}
|
||||
assert.Nil(t, err)
|
||||
assert.True(t, b)
|
||||
|
||||
uptr := &user{Name: "test", Age: 40}
|
||||
valid.Clear()
|
||||
b, err = valid.Valid(uptr)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if b {
|
||||
t.Error("validation should not be passed")
|
||||
}
|
||||
if len(valid.Errors) != 1 {
|
||||
t.Fatalf("valid errors len should be 1 but got %d", len(valid.Errors))
|
||||
}
|
||||
if valid.Errors[0].Key != "Name.Match" {
|
||||
t.Errorf("Message key should be `Name.Match` but got %s", valid.Errors[0].Key)
|
||||
}
|
||||
|
||||
assert.Nil(t, err)
|
||||
assert.False(t, b)
|
||||
assert.Equal(t, 1, len(valid.Errors))
|
||||
assert.Equal(t, "Name.Match", valid.Errors[0].Key)
|
||||
|
||||
u = user{Name: "test@/test/;com", Age: 180}
|
||||
valid.Clear()
|
||||
b, err = valid.Valid(u)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if b {
|
||||
t.Error("validation should not be passed")
|
||||
}
|
||||
if len(valid.Errors) != 1 {
|
||||
t.Fatalf("valid errors len should be 1 but got %d", len(valid.Errors))
|
||||
}
|
||||
if valid.Errors[0].Key != "Age.Range." {
|
||||
t.Errorf("Message key should be `Age.Range` but got %s", valid.Errors[0].Key)
|
||||
}
|
||||
assert.Nil(t, err)
|
||||
assert.False(t, b)
|
||||
assert.Equal(t, 1, len(valid.Errors))
|
||||
assert.Equal(t, "Age.Range.", valid.Errors[0].Key)
|
||||
}
|
||||
|
||||
func TestRecursiveValid(t *testing.T) {
|
||||
@ -432,12 +368,8 @@ func TestRecursiveValid(t *testing.T) {
|
||||
|
||||
u := Account{Password: "abc123_", U: User{}}
|
||||
b, err := valid.RecursiveValid(u)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if b {
|
||||
t.Error("validation should not be passed")
|
||||
}
|
||||
assert.Nil(t, err)
|
||||
assert.False(t, b)
|
||||
}
|
||||
|
||||
func TestSkipValid(t *testing.T) {
|
||||
@ -474,21 +406,13 @@ func TestSkipValid(t *testing.T) {
|
||||
|
||||
valid := Validation{}
|
||||
b, err := valid.Valid(u)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if b {
|
||||
t.Fatal("validation should not be passed")
|
||||
}
|
||||
assert.Nil(t, err)
|
||||
assert.False(t, b)
|
||||
|
||||
valid = Validation{RequiredFirst: true}
|
||||
b, err = valid.Valid(u)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !b {
|
||||
t.Fatal("validation should be passed")
|
||||
}
|
||||
assert.Nil(t, err)
|
||||
assert.True(t, b)
|
||||
}
|
||||
|
||||
func TestPointer(t *testing.T) {
|
||||
@ -506,12 +430,8 @@ func TestPointer(t *testing.T) {
|
||||
|
||||
valid := Validation{}
|
||||
b, err := valid.Valid(u)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if b {
|
||||
t.Fatal("validation should not be passed")
|
||||
}
|
||||
assert.Nil(t, err)
|
||||
assert.False(t, b)
|
||||
|
||||
validEmail := "a@a.com"
|
||||
u = User{
|
||||
@ -521,12 +441,8 @@ func TestPointer(t *testing.T) {
|
||||
|
||||
valid = Validation{RequiredFirst: true}
|
||||
b, err = valid.Valid(u)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !b {
|
||||
t.Fatal("validation should be passed")
|
||||
}
|
||||
assert.Nil(t, err)
|
||||
assert.True(t, b)
|
||||
|
||||
u = User{
|
||||
ReqEmail: &validEmail,
|
||||
@ -535,12 +451,8 @@ func TestPointer(t *testing.T) {
|
||||
|
||||
valid = Validation{}
|
||||
b, err = valid.Valid(u)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if b {
|
||||
t.Fatal("validation should not be passed")
|
||||
}
|
||||
assert.Nil(t, err)
|
||||
assert.False(t, b)
|
||||
|
||||
invalidEmail := "a@a"
|
||||
u = User{
|
||||
@ -550,12 +462,8 @@ func TestPointer(t *testing.T) {
|
||||
|
||||
valid = Validation{RequiredFirst: true}
|
||||
b, err = valid.Valid(u)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if b {
|
||||
t.Fatal("validation should not be passed")
|
||||
}
|
||||
assert.Nil(t, err)
|
||||
assert.False(t, b)
|
||||
|
||||
u = User{
|
||||
ReqEmail: &validEmail,
|
||||
@ -564,12 +472,8 @@ func TestPointer(t *testing.T) {
|
||||
|
||||
valid = Validation{}
|
||||
b, err = valid.Valid(u)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if b {
|
||||
t.Fatal("validation should not be passed")
|
||||
}
|
||||
assert.Nil(t, err)
|
||||
assert.False(t, b)
|
||||
}
|
||||
|
||||
func TestCanSkipAlso(t *testing.T) {
|
||||
@ -589,21 +493,14 @@ func TestCanSkipAlso(t *testing.T) {
|
||||
|
||||
valid := Validation{RequiredFirst: true}
|
||||
b, err := valid.Valid(u)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if b {
|
||||
t.Fatal("validation should not be passed")
|
||||
}
|
||||
assert.Nil(t, err)
|
||||
assert.False(t, b)
|
||||
|
||||
valid = Validation{RequiredFirst: true}
|
||||
valid.CanSkipAlso("Range")
|
||||
b, err = valid.Valid(u)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !b {
|
||||
t.Fatal("validation should be passed")
|
||||
}
|
||||
|
||||
assert.Nil(t, err)
|
||||
assert.True(t, b)
|
||||
|
||||
}
|
||||
|
||||
193
client/cache/cache_test.go
vendored
193
client/cache/cache_test.go
vendored
@ -21,13 +21,13 @@ import (
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestCacheIncr(t *testing.T) {
|
||||
bm, err := NewCache("memory", `{"interval":20}`)
|
||||
if err != nil {
|
||||
t.Error("init err")
|
||||
}
|
||||
assert.Nil(t, err)
|
||||
// timeoutDuration := 10 * time.Second
|
||||
|
||||
bm.Put(context.Background(), "edwardhey", 0, time.Second*20)
|
||||
@ -48,9 +48,7 @@ func TestCacheIncr(t *testing.T) {
|
||||
|
||||
func TestCache(t *testing.T) {
|
||||
bm, err := NewCache("memory", `{"interval":1}`)
|
||||
if err != nil {
|
||||
t.Error("init err")
|
||||
}
|
||||
assert.Nil(t, err)
|
||||
timeoutDuration := 5 * time.Second
|
||||
if err = bm.Put(context.Background(), "astaxie", 1, timeoutDuration); err != nil {
|
||||
t.Error("set Error", err)
|
||||
@ -81,70 +79,48 @@ func TestCache(t *testing.T) {
|
||||
testDecrOverFlow(t, bm, timeoutDuration)
|
||||
|
||||
bm.Delete(context.Background(), "astaxie")
|
||||
if res, _ := bm.IsExist(context.Background(), "astaxie"); res {
|
||||
t.Error("delete err")
|
||||
}
|
||||
res, _ := bm.IsExist(context.Background(), "astaxie")
|
||||
assert.False(t, res)
|
||||
|
||||
// test GetMulti
|
||||
if err = bm.Put(context.Background(), "astaxie", "author", timeoutDuration); err != nil {
|
||||
t.Error("set Error", err)
|
||||
}
|
||||
if res, _ := bm.IsExist(context.Background(), "astaxie"); !res {
|
||||
t.Error("check err")
|
||||
}
|
||||
if v, _ := bm.Get(context.Background(), "astaxie"); v.(string) != "author" {
|
||||
t.Error("get err")
|
||||
}
|
||||
assert.Nil(t, bm.Put(context.Background(), "astaxie", "author", timeoutDuration))
|
||||
|
||||
if err = bm.Put(context.Background(), "astaxie1", "author1", timeoutDuration); err != nil {
|
||||
t.Error("set Error", err)
|
||||
}
|
||||
if res, _ := bm.IsExist(context.Background(), "astaxie1"); !res {
|
||||
t.Error("check err")
|
||||
}
|
||||
res, _ = bm.IsExist(context.Background(), "astaxie")
|
||||
assert.True(t, res)
|
||||
|
||||
v, _ := bm.Get(context.Background(), "astaxie")
|
||||
assert.Equal(t, "author", v)
|
||||
|
||||
assert.Nil(t, bm.Put(context.Background(), "astaxie1", "author1", timeoutDuration))
|
||||
|
||||
res, _ = bm.IsExist(context.Background(), "astaxie1")
|
||||
assert.True(t, res)
|
||||
|
||||
vv, _ := bm.GetMulti(context.Background(), []string{"astaxie", "astaxie1"})
|
||||
if len(vv) != 2 {
|
||||
t.Error("GetMulti ERROR")
|
||||
}
|
||||
if vv[0].(string) != "author" {
|
||||
t.Error("GetMulti ERROR")
|
||||
}
|
||||
if vv[1].(string) != "author1" {
|
||||
t.Error("GetMulti ERROR")
|
||||
}
|
||||
assert.Equal(t, 2, len(vv))
|
||||
assert.Equal(t, "author", vv[0])
|
||||
assert.Equal(t,"author1", vv[1])
|
||||
|
||||
|
||||
|
||||
vv, err = bm.GetMulti(context.Background(), []string{"astaxie0", "astaxie1"})
|
||||
if len(vv) != 2 {
|
||||
t.Error("GetMulti ERROR")
|
||||
}
|
||||
if vv[0] != nil {
|
||||
t.Error("GetMulti ERROR")
|
||||
}
|
||||
if vv[1].(string) != "author1" {
|
||||
t.Error("GetMulti ERROR")
|
||||
}
|
||||
if err != nil && err.Error() != "key [astaxie0] error: the key isn't exist" {
|
||||
t.Error("GetMulti ERROR")
|
||||
}
|
||||
assert.Equal(t, 2, len(vv))
|
||||
assert.Nil(t, vv[0])
|
||||
assert.Equal(t, "author1", vv[1])
|
||||
|
||||
assert.NotNil(t, err)
|
||||
assert.Equal(t, "key [astaxie0] error: key not exist", err.Error())
|
||||
}
|
||||
|
||||
func TestFileCache(t *testing.T) {
|
||||
bm, err := NewCache("file", `{"CachePath":"cache","FileSuffix":".bin","DirectoryLevel":"2","EmbedExpiry":"0"}`)
|
||||
if err != nil {
|
||||
t.Error("init err")
|
||||
}
|
||||
assert.Nil(t, err)
|
||||
timeoutDuration := 10 * time.Second
|
||||
if err = bm.Put(context.Background(), "astaxie", 1, timeoutDuration); err != nil {
|
||||
t.Error("set Error", err)
|
||||
}
|
||||
if res, _ := bm.IsExist(context.Background(), "astaxie"); !res {
|
||||
t.Error("check err")
|
||||
}
|
||||
assert.Nil(t, bm.Put(context.Background(), "astaxie", 1, timeoutDuration))
|
||||
|
||||
if v, _ := bm.Get(context.Background(), "astaxie"); v.(int) != 1 {
|
||||
t.Error("get err")
|
||||
}
|
||||
res, _ := bm.IsExist(context.Background(), "astaxie")
|
||||
assert.True(t, res)
|
||||
v, _ := bm.Get(context.Background(), "astaxie")
|
||||
assert.Equal(t, 1, v)
|
||||
|
||||
// test different integer type for incr & decr
|
||||
testMultiTypeIncrDecr(t, bm, timeoutDuration)
|
||||
@ -154,54 +130,35 @@ func TestFileCache(t *testing.T) {
|
||||
testDecrOverFlow(t, bm, timeoutDuration)
|
||||
|
||||
bm.Delete(context.Background(), "astaxie")
|
||||
if res, _ := bm.IsExist(context.Background(), "astaxie"); res {
|
||||
t.Error("delete err")
|
||||
}
|
||||
res, _ = bm.IsExist(context.Background(), "astaxie")
|
||||
assert.False(t, res)
|
||||
|
||||
// test string
|
||||
if err = bm.Put(context.Background(), "astaxie", "author", timeoutDuration); err != nil {
|
||||
t.Error("set Error", err)
|
||||
}
|
||||
if res, _ := bm.IsExist(context.Background(), "astaxie"); !res {
|
||||
t.Error("check err")
|
||||
}
|
||||
if v, _ := bm.Get(context.Background(), "astaxie"); v.(string) != "author" {
|
||||
t.Error("get err")
|
||||
}
|
||||
assert.Nil(t, bm.Put(context.Background(), "astaxie", "author", timeoutDuration))
|
||||
res, _ = bm.IsExist(context.Background(), "astaxie")
|
||||
assert.True(t, res)
|
||||
|
||||
v, _ = bm.Get(context.Background(), "astaxie")
|
||||
assert.Equal(t, "author", v)
|
||||
|
||||
// test GetMulti
|
||||
if err = bm.Put(context.Background(), "astaxie1", "author1", timeoutDuration); err != nil {
|
||||
t.Error("set Error", err)
|
||||
}
|
||||
if res, _ := bm.IsExist(context.Background(), "astaxie1"); !res {
|
||||
t.Error("check err")
|
||||
}
|
||||
assert.Nil(t, bm.Put(context.Background(), "astaxie1", "author1", timeoutDuration))
|
||||
|
||||
res, _ = bm.IsExist(context.Background(), "astaxie1")
|
||||
assert.True(t, res)
|
||||
|
||||
vv, _ := bm.GetMulti(context.Background(), []string{"astaxie", "astaxie1"})
|
||||
if len(vv) != 2 {
|
||||
t.Error("GetMulti ERROR")
|
||||
}
|
||||
if vv[0].(string) != "author" {
|
||||
t.Error("GetMulti ERROR")
|
||||
}
|
||||
if vv[1].(string) != "author1" {
|
||||
t.Error("GetMulti ERROR")
|
||||
}
|
||||
assert.Equal(t, 2, len(vv))
|
||||
assert.Equal(t, "author", vv[0])
|
||||
assert.Equal(t, "author1", vv[1])
|
||||
|
||||
vv, err = bm.GetMulti(context.Background(), []string{"astaxie0", "astaxie1"})
|
||||
if len(vv) != 2 {
|
||||
t.Error("GetMulti ERROR")
|
||||
}
|
||||
if vv[0] != nil {
|
||||
t.Error("GetMulti ERROR")
|
||||
}
|
||||
if vv[1].(string) != "author1" {
|
||||
t.Error("GetMulti ERROR")
|
||||
}
|
||||
if err == nil {
|
||||
t.Error("GetMulti ERROR")
|
||||
}
|
||||
assert.Equal(t, 2, len(vv))
|
||||
|
||||
assert.Nil(t, vv[0])
|
||||
|
||||
assert.Equal(t, "author1", vv[1])
|
||||
assert.NotNil(t, err)
|
||||
os.RemoveAll("cache")
|
||||
}
|
||||
|
||||
@ -215,53 +172,33 @@ func testMultiTypeIncrDecr(t *testing.T, c Cache, timeout time.Duration) {
|
||||
}
|
||||
|
||||
func testIncrDecr(t *testing.T, c Cache, beforeIncr interface{}, afterIncr interface{}, timeout time.Duration) {
|
||||
var err error
|
||||
ctx := context.Background()
|
||||
key := "incDecKey"
|
||||
if err = c.Put(ctx, key, beforeIncr, timeout); err != nil {
|
||||
t.Error("Get Error", err)
|
||||
}
|
||||
|
||||
if err = c.Incr(ctx, key); err != nil {
|
||||
t.Error("Incr Error", err)
|
||||
}
|
||||
assert.Nil(t, c.Put(ctx, key, beforeIncr, timeout))
|
||||
assert.Nil(t, c.Incr(ctx, key))
|
||||
|
||||
if v, _ := c.Get(ctx, key); v != afterIncr {
|
||||
t.Error("Get Error")
|
||||
}
|
||||
|
||||
if err = c.Decr(ctx, key); err != nil {
|
||||
t.Error("Decr Error", err)
|
||||
}
|
||||
v, _ := c.Get(ctx, key)
|
||||
assert.Equal(t, afterIncr, v)
|
||||
|
||||
if v, _ := c.Get(ctx, key); v != beforeIncr {
|
||||
t.Error("Get Error")
|
||||
}
|
||||
assert.Nil(t, c.Decr(ctx, key))
|
||||
|
||||
if err := c.Delete(ctx, key); err != nil {
|
||||
t.Error("Delete Error")
|
||||
}
|
||||
v, _ = c.Get(ctx, key)
|
||||
assert.Equal(t, v, beforeIncr)
|
||||
assert.Nil(t, c.Delete(ctx, key))
|
||||
}
|
||||
|
||||
func testIncrOverFlow(t *testing.T, c Cache, timeout time.Duration) {
|
||||
var err error
|
||||
ctx := context.Background()
|
||||
key := "incKey"
|
||||
|
||||
assert.Nil(t, c.Put(ctx, key, int64(math.MaxInt64), timeout))
|
||||
// int64
|
||||
if err = c.Put(ctx, key, int64(math.MaxInt64), timeout); err != nil {
|
||||
t.Error("Put Error: ", err.Error())
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
if err = c.Delete(ctx, key); err != nil {
|
||||
t.Errorf("Delete error: %s", err.Error())
|
||||
}
|
||||
assert.Nil(t, c.Delete(ctx, key))
|
||||
}()
|
||||
if err = c.Incr(ctx, key); err == nil {
|
||||
t.Error("Incr error")
|
||||
return
|
||||
}
|
||||
assert.NotNil(t, c.Incr(ctx, key))
|
||||
}
|
||||
|
||||
func testDecrOverFlow(t *testing.T, c Cache, timeout time.Duration) {
|
||||
|
||||
202
client/cache/calc_utils_test.go
vendored
202
client/cache/calc_utils_test.go
vendored
@ -4,6 +4,8 @@ import (
|
||||
"math"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestIncr(t *testing.T) {
|
||||
@ -11,116 +13,62 @@ func TestIncr(t *testing.T) {
|
||||
var originVal interface{} = int(1)
|
||||
var updateVal interface{} = int(2)
|
||||
val, err := incr(originVal)
|
||||
if err != nil {
|
||||
t.Errorf("incr failed, err: %s", err.Error())
|
||||
return
|
||||
}
|
||||
if val != updateVal {
|
||||
t.Errorf("incr failed, expect %v, but %v actually", updateVal, val)
|
||||
return
|
||||
}
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, updateVal, val)
|
||||
_, err = incr(int(1<<(strconv.IntSize-1) - 1))
|
||||
if err == nil {
|
||||
t.Error("incr failed")
|
||||
return
|
||||
}
|
||||
assert.NotNil(t, err)
|
||||
|
||||
// int32
|
||||
originVal = int32(1)
|
||||
updateVal = int32(2)
|
||||
val, err = incr(originVal)
|
||||
if err != nil {
|
||||
t.Errorf("incr failed, err: %s", err.Error())
|
||||
return
|
||||
}
|
||||
if val != updateVal {
|
||||
t.Errorf("incr failed, expect %v, but %v actually", updateVal, val)
|
||||
return
|
||||
}
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, updateVal, val)
|
||||
_, err = incr(int32(math.MaxInt32))
|
||||
if err == nil {
|
||||
t.Error("incr failed")
|
||||
return
|
||||
}
|
||||
|
||||
assert.NotNil(t, err)
|
||||
// int64
|
||||
originVal = int64(1)
|
||||
updateVal = int64(2)
|
||||
val, err = incr(originVal)
|
||||
if err != nil {
|
||||
t.Errorf("incr failed, err: %s", err.Error())
|
||||
return
|
||||
}
|
||||
if val != updateVal {
|
||||
t.Errorf("incr failed, expect %v, but %v actually", updateVal, val)
|
||||
return
|
||||
}
|
||||
assert.Nil(t, err)
|
||||
|
||||
assert.Equal(t, updateVal, val)
|
||||
|
||||
_, err = incr(int64(math.MaxInt64))
|
||||
if err == nil {
|
||||
t.Error("incr failed")
|
||||
return
|
||||
}
|
||||
assert.NotNil(t, err)
|
||||
|
||||
// uint
|
||||
originVal = uint(1)
|
||||
updateVal = uint(2)
|
||||
val, err = incr(originVal)
|
||||
if err != nil {
|
||||
t.Errorf("incr failed, err: %s", err.Error())
|
||||
return
|
||||
}
|
||||
if val != updateVal {
|
||||
t.Errorf("incr failed, expect %v, but %v actually", updateVal, val)
|
||||
return
|
||||
}
|
||||
_, err = incr(uint(1<<(strconv.IntSize) - 1))
|
||||
if err == nil {
|
||||
t.Error("incr failed")
|
||||
return
|
||||
}
|
||||
assert.Nil(t, err)
|
||||
|
||||
assert.Equal(t, updateVal, val)
|
||||
|
||||
_, err = incr(uint(1<<(strconv.IntSize) - 1))
|
||||
assert.NotNil(t, err)
|
||||
// uint32
|
||||
originVal = uint32(1)
|
||||
updateVal = uint32(2)
|
||||
val, err = incr(originVal)
|
||||
if err != nil {
|
||||
t.Errorf("incr failed, err: %s", err.Error())
|
||||
return
|
||||
}
|
||||
if val != updateVal {
|
||||
t.Errorf("incr failed, expect %v, but %v actually", updateVal, val)
|
||||
return
|
||||
}
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, updateVal, val)
|
||||
|
||||
_, err = incr(uint32(math.MaxUint32))
|
||||
if err == nil {
|
||||
t.Error("incr failed")
|
||||
return
|
||||
}
|
||||
assert.NotNil(t, err)
|
||||
|
||||
// uint64
|
||||
originVal = uint64(1)
|
||||
updateVal = uint64(2)
|
||||
val, err = incr(originVal)
|
||||
if err != nil {
|
||||
t.Errorf("incr failed, err: %s", err.Error())
|
||||
return
|
||||
}
|
||||
if val != updateVal {
|
||||
t.Errorf("incr failed, expect %v, but %v actually", updateVal, val)
|
||||
return
|
||||
}
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, updateVal, val)
|
||||
_, err = incr(uint64(math.MaxUint64))
|
||||
if err == nil {
|
||||
t.Error("incr failed")
|
||||
return
|
||||
}
|
||||
assert.NotNil(t, err)
|
||||
|
||||
// other type
|
||||
_, err = incr("string")
|
||||
if err == nil {
|
||||
t.Error("incr failed")
|
||||
return
|
||||
}
|
||||
assert.NotNil(t, err)
|
||||
}
|
||||
|
||||
func TestDecr(t *testing.T) {
|
||||
@ -128,114 +76,58 @@ func TestDecr(t *testing.T) {
|
||||
var originVal interface{} = int(2)
|
||||
var updateVal interface{} = int(1)
|
||||
val, err := decr(originVal)
|
||||
if err != nil {
|
||||
t.Errorf("decr failed, err: %s", err.Error())
|
||||
return
|
||||
}
|
||||
if val != updateVal {
|
||||
t.Errorf("decr failed, expect %v, but %v actually", updateVal, val)
|
||||
return
|
||||
}
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, updateVal, val)
|
||||
_, err = decr(int(-1 << (strconv.IntSize - 1)))
|
||||
if err == nil {
|
||||
t.Error("decr failed")
|
||||
return
|
||||
}
|
||||
|
||||
assert.NotNil(t, err)
|
||||
|
||||
// int32
|
||||
originVal = int32(2)
|
||||
updateVal = int32(1)
|
||||
val, err = decr(originVal)
|
||||
if err != nil {
|
||||
t.Errorf("decr failed, err: %s", err.Error())
|
||||
return
|
||||
}
|
||||
if val != updateVal {
|
||||
t.Errorf("decr failed, expect %v, but %v actually", updateVal, val)
|
||||
return
|
||||
}
|
||||
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, updateVal, val)
|
||||
_, err = decr(int32(math.MinInt32))
|
||||
if err == nil {
|
||||
t.Error("decr failed")
|
||||
return
|
||||
}
|
||||
assert.NotNil(t, err)
|
||||
|
||||
// int64
|
||||
originVal = int64(2)
|
||||
updateVal = int64(1)
|
||||
val, err = decr(originVal)
|
||||
if err != nil {
|
||||
t.Errorf("decr failed, err: %s", err.Error())
|
||||
return
|
||||
}
|
||||
if val != updateVal {
|
||||
t.Errorf("decr failed, expect %v, but %v actually", updateVal, val)
|
||||
return
|
||||
}
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, updateVal, val)
|
||||
_, err = decr(int64(math.MinInt64))
|
||||
if err == nil {
|
||||
t.Error("decr failed")
|
||||
return
|
||||
}
|
||||
|
||||
assert.NotNil(t, err)
|
||||
// uint
|
||||
originVal = uint(2)
|
||||
updateVal = uint(1)
|
||||
val, err = decr(originVal)
|
||||
if err != nil {
|
||||
t.Errorf("decr failed, err: %s", err.Error())
|
||||
return
|
||||
}
|
||||
if val != updateVal {
|
||||
t.Errorf("decr failed, expect %v, but %v actually", updateVal, val)
|
||||
return
|
||||
}
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, updateVal, val)
|
||||
_, err = decr(uint(0))
|
||||
if err == nil {
|
||||
t.Error("decr failed")
|
||||
return
|
||||
}
|
||||
assert.NotNil(t, err)
|
||||
|
||||
// uint32
|
||||
originVal = uint32(2)
|
||||
updateVal = uint32(1)
|
||||
val, err = decr(originVal)
|
||||
if err != nil {
|
||||
t.Errorf("decr failed, err: %s", err.Error())
|
||||
return
|
||||
}
|
||||
if val != updateVal {
|
||||
t.Errorf("decr failed, expect %v, but %v actually", updateVal, val)
|
||||
return
|
||||
}
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, updateVal, val)
|
||||
_, err = decr(uint32(0))
|
||||
if err == nil {
|
||||
t.Error("decr failed")
|
||||
return
|
||||
}
|
||||
assert.NotNil(t, err)
|
||||
|
||||
// uint64
|
||||
originVal = uint64(2)
|
||||
updateVal = uint64(1)
|
||||
val, err = decr(originVal)
|
||||
if err != nil {
|
||||
t.Errorf("decr failed, err: %s", err.Error())
|
||||
return
|
||||
}
|
||||
if val != updateVal {
|
||||
t.Errorf("decr failed, expect %v, but %v actually", updateVal, val)
|
||||
return
|
||||
}
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, updateVal, val)
|
||||
_, err = decr(uint64(0))
|
||||
if err == nil {
|
||||
t.Error("decr failed")
|
||||
return
|
||||
}
|
||||
assert.NotNil(t, err)
|
||||
|
||||
// other type
|
||||
_, err = decr("string")
|
||||
if err == nil {
|
||||
t.Error("decr failed")
|
||||
return
|
||||
}
|
||||
assert.NotNil(t, err)
|
||||
}
|
||||
|
||||
120
client/cache/conv_test.go
vendored
120
client/cache/conv_test.go
vendored
@ -16,128 +16,74 @@ package cache
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestGetString(t *testing.T) {
|
||||
var t1 = "test1"
|
||||
if "test1" != GetString(t1) {
|
||||
t.Error("get string from string error")
|
||||
}
|
||||
var t2 = []byte("test2")
|
||||
if "test2" != GetString(t2) {
|
||||
t.Error("get string from byte array error")
|
||||
}
|
||||
var t3 = 1
|
||||
if "1" != GetString(t3) {
|
||||
t.Error("get string from int error")
|
||||
}
|
||||
var t4 int64 = 1
|
||||
if "1" != GetString(t4) {
|
||||
t.Error("get string from int64 error")
|
||||
}
|
||||
var t5 = 1.1
|
||||
if "1.1" != GetString(t5) {
|
||||
t.Error("get string from float64 error")
|
||||
}
|
||||
|
||||
if "" != GetString(nil) {
|
||||
t.Error("get string from nil error")
|
||||
}
|
||||
assert.Equal(t, "test1", GetString(t1))
|
||||
var t2 = []byte("test2")
|
||||
assert.Equal(t, "test2", GetString(t2))
|
||||
var t3 = 1
|
||||
assert.Equal(t, "1", GetString(t3))
|
||||
var t4 int64 = 1
|
||||
assert.Equal(t, "1", GetString(t4))
|
||||
var t5 = 1.1
|
||||
assert.Equal(t, "1.1", GetString(t5))
|
||||
assert.Equal(t, "", GetString(nil))
|
||||
}
|
||||
|
||||
func TestGetInt(t *testing.T) {
|
||||
var t1 = 1
|
||||
if 1 != GetInt(t1) {
|
||||
t.Error("get int from int error")
|
||||
}
|
||||
assert.Equal(t, 1, GetInt(t1))
|
||||
var t2 int32 = 32
|
||||
if 32 != GetInt(t2) {
|
||||
t.Error("get int from int32 error")
|
||||
}
|
||||
assert.Equal(t, 32, GetInt(t2))
|
||||
|
||||
var t3 int64 = 64
|
||||
if 64 != GetInt(t3) {
|
||||
t.Error("get int from int64 error")
|
||||
}
|
||||
assert.Equal(t, 64, GetInt(t3))
|
||||
var t4 = "128"
|
||||
if 128 != GetInt(t4) {
|
||||
t.Error("get int from num string error")
|
||||
}
|
||||
if 0 != GetInt(nil) {
|
||||
t.Error("get int from nil error")
|
||||
}
|
||||
|
||||
assert.Equal(t, 128, GetInt(t4))
|
||||
assert.Equal(t, 0, GetInt(nil))
|
||||
}
|
||||
|
||||
func TestGetInt64(t *testing.T) {
|
||||
var i int64 = 1
|
||||
var t1 = 1
|
||||
if i != GetInt64(t1) {
|
||||
t.Error("get int64 from int error")
|
||||
}
|
||||
assert.Equal(t, i, GetInt64(t1))
|
||||
var t2 int32 = 1
|
||||
if i != GetInt64(t2) {
|
||||
t.Error("get int64 from int32 error")
|
||||
}
|
||||
|
||||
assert.Equal(t, i, GetInt64(t2))
|
||||
var t3 int64 = 1
|
||||
if i != GetInt64(t3) {
|
||||
t.Error("get int64 from int64 error")
|
||||
}
|
||||
assert.Equal(t, i, GetInt64(t3))
|
||||
var t4 = "1"
|
||||
if i != GetInt64(t4) {
|
||||
t.Error("get int64 from num string error")
|
||||
}
|
||||
if 0 != GetInt64(nil) {
|
||||
t.Error("get int64 from nil")
|
||||
}
|
||||
assert.Equal(t, i, GetInt64(t4))
|
||||
assert.Equal(t, int64(0), GetInt64(nil))
|
||||
}
|
||||
|
||||
func TestGetFloat64(t *testing.T) {
|
||||
var f = 1.11
|
||||
var t1 float32 = 1.11
|
||||
if f != GetFloat64(t1) {
|
||||
t.Error("get float64 from float32 error")
|
||||
}
|
||||
assert.Equal(t, f, GetFloat64(t1))
|
||||
var t2 = 1.11
|
||||
if f != GetFloat64(t2) {
|
||||
t.Error("get float64 from float64 error")
|
||||
}
|
||||
assert.Equal(t, f, GetFloat64(t2))
|
||||
var t3 = "1.11"
|
||||
if f != GetFloat64(t3) {
|
||||
t.Error("get float64 from string error")
|
||||
}
|
||||
assert.Equal(t, f, GetFloat64(t3))
|
||||
|
||||
var f2 float64 = 1
|
||||
var t4 = 1
|
||||
if f2 != GetFloat64(t4) {
|
||||
t.Error("get float64 from int error")
|
||||
}
|
||||
assert.Equal(t, f2, GetFloat64(t4))
|
||||
|
||||
if 0 != GetFloat64(nil) {
|
||||
t.Error("get float64 from nil error")
|
||||
}
|
||||
assert.Equal(t, float64(0), GetFloat64(nil))
|
||||
}
|
||||
|
||||
func TestGetBool(t *testing.T) {
|
||||
var t1 = true
|
||||
if !GetBool(t1) {
|
||||
t.Error("get bool from bool error")
|
||||
}
|
||||
assert.True(t, GetBool(t1))
|
||||
var t2 = "true"
|
||||
if !GetBool(t2) {
|
||||
t.Error("get bool from string error")
|
||||
}
|
||||
if GetBool(nil) {
|
||||
t.Error("get bool from nil error")
|
||||
}
|
||||
}
|
||||
assert.True(t, GetBool(t2))
|
||||
|
||||
func byteArrayEquals(a []byte, b []byte) bool {
|
||||
if len(a) != len(b) {
|
||||
return false
|
||||
}
|
||||
for i, v := range a {
|
||||
if v != b[i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
assert.False(t, GetBool(nil))
|
||||
}
|
||||
|
||||
2
client/cache/memcache/memcache.go
vendored
2
client/cache/memcache/memcache.go
vendored
@ -84,7 +84,7 @@ func (rc *Cache) GetMulti(ctx context.Context, keys []string) ([]interface{}, er
|
||||
keysErr := make([]string, 0)
|
||||
for i, ki := range keys {
|
||||
if _, ok := mv[ki]; !ok {
|
||||
keysErr = append(keysErr, fmt.Sprintf("key [%s] error: %s", ki, "the key isn't exist"))
|
||||
keysErr = append(keysErr, fmt.Sprintf("key [%s] error: %s", ki, "key not exist"))
|
||||
continue
|
||||
}
|
||||
rv[i] = mv[ki].Value
|
||||
|
||||
111
client/cache/memcache/memcache_test.go
vendored
111
client/cache/memcache/memcache_test.go
vendored
@ -23,6 +23,7 @@ import (
|
||||
"time"
|
||||
|
||||
_ "github.com/bradfitz/gomemcache/memcache"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/beego/beego/v2/client/cache"
|
||||
)
|
||||
@ -34,78 +35,63 @@ func TestMemcacheCache(t *testing.T) {
|
||||
}
|
||||
|
||||
bm, err := cache.NewCache("memcache", fmt.Sprintf(`{"conn": "%s"}`, addr))
|
||||
if err != nil {
|
||||
t.Error("init err")
|
||||
}
|
||||
assert.Nil(t, err)
|
||||
|
||||
timeoutDuration := 10 * time.Second
|
||||
if err = bm.Put(context.Background(), "astaxie", "1", timeoutDuration); err != nil {
|
||||
t.Error("set Error", err)
|
||||
}
|
||||
if res, _ := bm.IsExist(context.Background(), "astaxie"); !res {
|
||||
t.Error("check err")
|
||||
}
|
||||
|
||||
assert.Nil(t, bm.Put(context.Background(), "astaxie", "1", timeoutDuration))
|
||||
res, _ := bm.IsExist(context.Background(), "astaxie")
|
||||
assert.True(t, res)
|
||||
|
||||
time.Sleep(11 * time.Second)
|
||||
|
||||
if res, _ := bm.IsExist(context.Background(), "astaxie"); res {
|
||||
t.Error("check err")
|
||||
}
|
||||
if err = bm.Put(context.Background(), "astaxie", "1", timeoutDuration); err != nil {
|
||||
t.Error("set Error", err)
|
||||
}
|
||||
res, _ = bm.IsExist(context.Background(), "astaxie")
|
||||
assert.False(t, res)
|
||||
|
||||
assert.Nil(t, bm.Put(context.Background(), "astaxie", "1", timeoutDuration))
|
||||
|
||||
val, _ := bm.Get(context.Background(), "astaxie")
|
||||
if v, err := strconv.Atoi(string(val.([]byte))); err != nil || v != 1 {
|
||||
t.Error("get err")
|
||||
}
|
||||
v, err := strconv.Atoi(string(val.([]byte)))
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, 1, v)
|
||||
|
||||
if err = bm.Incr(context.Background(), "astaxie"); err != nil {
|
||||
t.Error("Incr Error", err)
|
||||
}
|
||||
assert.Nil(t, bm.Incr(context.Background(), "astaxie"))
|
||||
|
||||
val, _ = bm.Get(context.Background(), "astaxie")
|
||||
if v, err := strconv.Atoi(string(val.([]byte))); err != nil || v != 2 {
|
||||
t.Error("get err")
|
||||
}
|
||||
v, err = strconv.Atoi(string(val.([]byte)))
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, 2, v)
|
||||
|
||||
if err = bm.Decr(context.Background(), "astaxie"); err != nil {
|
||||
t.Error("Decr Error", err)
|
||||
}
|
||||
assert.Nil(t, bm.Decr(context.Background(), "astaxie"))
|
||||
|
||||
val, _ = bm.Get(context.Background(), "astaxie")
|
||||
if v, err := strconv.Atoi(string(val.([]byte))); err != nil || v != 1 {
|
||||
t.Error("get err")
|
||||
}
|
||||
v, err = strconv.Atoi(string(val.([]byte)))
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, 1, v)
|
||||
bm.Delete(context.Background(), "astaxie")
|
||||
if res, _ := bm.IsExist(context.Background(), "astaxie"); res {
|
||||
t.Error("delete err")
|
||||
}
|
||||
|
||||
res, _ = bm.IsExist(context.Background(), "astaxie")
|
||||
assert.False(t, res)
|
||||
|
||||
assert.Nil(t,bm.Put(context.Background(), "astaxie", "author", timeoutDuration) )
|
||||
// test string
|
||||
if err = bm.Put(context.Background(), "astaxie", "author", timeoutDuration); err != nil {
|
||||
t.Error("set Error", err)
|
||||
}
|
||||
if res, _ := bm.IsExist(context.Background(), "astaxie"); !res {
|
||||
t.Error("check err")
|
||||
}
|
||||
res, _ = bm.IsExist(context.Background(), "astaxie")
|
||||
assert.True(t, res)
|
||||
|
||||
val, _ = bm.Get(context.Background(), "astaxie")
|
||||
if v := val.([]byte); string(v) != "author" {
|
||||
t.Error("get err")
|
||||
}
|
||||
vs := val.([]byte)
|
||||
assert.Equal(t, "author", string(vs))
|
||||
|
||||
// test GetMulti
|
||||
if err = bm.Put(context.Background(), "astaxie1", "author1", timeoutDuration); err != nil {
|
||||
t.Error("set Error", err)
|
||||
}
|
||||
if res, _ := bm.IsExist(context.Background(), "astaxie1"); !res {
|
||||
t.Error("check err")
|
||||
}
|
||||
assert.Nil(t, bm.Put(context.Background(), "astaxie1", "author1", timeoutDuration))
|
||||
|
||||
|
||||
res, _ = bm.IsExist(context.Background(), "astaxie1")
|
||||
assert.True(t, res)
|
||||
|
||||
vv, _ := bm.GetMulti(context.Background(), []string{"astaxie", "astaxie1"})
|
||||
if len(vv) != 2 {
|
||||
t.Error("GetMulti ERROR")
|
||||
}
|
||||
assert.Equal(t, 2, len(vv))
|
||||
|
||||
if string(vv[0].([]byte)) != "author" && string(vv[0].([]byte)) != "author1" {
|
||||
t.Error("GetMulti ERROR")
|
||||
}
|
||||
@ -114,21 +100,14 @@ func TestMemcacheCache(t *testing.T) {
|
||||
}
|
||||
|
||||
vv, err = bm.GetMulti(context.Background(), []string{"astaxie0", "astaxie1"})
|
||||
if len(vv) != 2 {
|
||||
t.Error("GetMulti ERROR")
|
||||
}
|
||||
if vv[0] != nil {
|
||||
t.Error("GetMulti ERROR")
|
||||
}
|
||||
if string(vv[1].([]byte)) != "author1" {
|
||||
t.Error("GetMulti ERROR")
|
||||
}
|
||||
if err != nil && err.Error() == "key [astaxie0] error: key isn't exist" {
|
||||
t.Error("GetMulti ERROR")
|
||||
}
|
||||
assert.Equal(t, 2, len(vv))
|
||||
assert.Nil(t, vv[0])
|
||||
|
||||
assert.Equal(t, "author1", string(vv[1].([]byte)))
|
||||
|
||||
assert.NotNil(t, err)
|
||||
assert.Equal(t, "key [astaxie0] error: key not exist", err.Error())
|
||||
|
||||
assert.Nil(t, bm.ClearAll(context.Background()))
|
||||
// test clear all
|
||||
if err = bm.ClearAll(context.Background()); err != nil {
|
||||
t.Error("clear all err")
|
||||
}
|
||||
}
|
||||
|
||||
11
client/cache/memory.go
vendored
11
client/cache/memory.go
vendored
@ -29,6 +29,8 @@ var (
|
||||
DefaultEvery = 60 // 1 minute
|
||||
)
|
||||
|
||||
const keyNotExistMsg = "key not exist"
|
||||
|
||||
// MemoryItem stores memory cache item.
|
||||
type MemoryItem struct {
|
||||
val interface{}
|
||||
@ -70,7 +72,7 @@ func (bc *MemoryCache) Get(ctx context.Context, key string) (interface{}, error)
|
||||
}
|
||||
return itm.val, nil
|
||||
}
|
||||
return nil, errors.New("the key isn't exist")
|
||||
return nil, errors.New(keyNotExistMsg)
|
||||
}
|
||||
|
||||
// GetMulti gets caches from memory.
|
||||
@ -112,7 +114,8 @@ func (bc *MemoryCache) Delete(ctx context.Context, key string) error {
|
||||
bc.Lock()
|
||||
defer bc.Unlock()
|
||||
if _, ok := bc.items[key]; !ok {
|
||||
return errors.New("key not exist")
|
||||
|
||||
return errors.New(keyNotExistMsg)
|
||||
}
|
||||
delete(bc.items, key)
|
||||
if _, ok := bc.items[key]; ok {
|
||||
@ -128,7 +131,7 @@ func (bc *MemoryCache) Incr(ctx context.Context, key string) error {
|
||||
defer bc.Unlock()
|
||||
itm, ok := bc.items[key]
|
||||
if !ok {
|
||||
return errors.New("key not exist")
|
||||
return errors.New(keyNotExistMsg)
|
||||
}
|
||||
|
||||
val, err := incr(itm.val)
|
||||
@ -145,7 +148,7 @@ func (bc *MemoryCache) Decr(ctx context.Context, key string) error {
|
||||
defer bc.Unlock()
|
||||
itm, ok := bc.items[key]
|
||||
if !ok {
|
||||
return errors.New("key not exist")
|
||||
return errors.New(keyNotExistMsg)
|
||||
}
|
||||
|
||||
val, err := decr(itm.val)
|
||||
|
||||
139
client/cache/redis/redis_test.go
vendored
139
client/cache/redis/redis_test.go
vendored
@ -35,96 +35,74 @@ func TestRedisCache(t *testing.T) {
|
||||
}
|
||||
|
||||
bm, err := cache.NewCache("redis", fmt.Sprintf(`{"conn": "%s"}`, redisAddr))
|
||||
if err != nil {
|
||||
t.Error("init err")
|
||||
}
|
||||
timeoutDuration := 10 * time.Second
|
||||
if err = bm.Put(context.Background(), "astaxie", 1, timeoutDuration); err != nil {
|
||||
t.Error("set Error", err)
|
||||
}
|
||||
if res, _ := bm.IsExist(context.Background(), "astaxie"); !res {
|
||||
t.Error("check err")
|
||||
}
|
||||
assert.Nil(t, err)
|
||||
timeoutDuration := 3 * time.Second
|
||||
|
||||
time.Sleep(11 * time.Second)
|
||||
assert.Nil(t, bm.Put(context.Background(), "astaxie", 1, timeoutDuration))
|
||||
|
||||
|
||||
res, _ := bm.IsExist(context.Background(), "astaxie")
|
||||
assert.True(t, res)
|
||||
|
||||
time.Sleep(5 * time.Second)
|
||||
|
||||
res, _ = bm.IsExist(context.Background(), "astaxie")
|
||||
assert.False(t, res)
|
||||
|
||||
assert.Nil(t, bm.Put(context.Background(), "astaxie", 1, timeoutDuration))
|
||||
|
||||
if res, _ := bm.IsExist(context.Background(), "astaxie"); res {
|
||||
t.Error("check err")
|
||||
}
|
||||
if err = bm.Put(context.Background(), "astaxie", 1, timeoutDuration); err != nil {
|
||||
t.Error("set Error", err)
|
||||
}
|
||||
|
||||
val, _ := bm.Get(context.Background(), "astaxie")
|
||||
if v, _ := redis.Int(val, err); v != 1 {
|
||||
t.Error("get err")
|
||||
}
|
||||
v, _ := redis.Int(val, err)
|
||||
assert.Equal(t, 1, v)
|
||||
|
||||
if err = bm.Incr(context.Background(), "astaxie"); err != nil {
|
||||
t.Error("Incr Error", err)
|
||||
}
|
||||
assert.Nil(t, bm.Incr(context.Background(), "astaxie"))
|
||||
val, _ = bm.Get(context.Background(), "astaxie")
|
||||
if v, _ := redis.Int(val, err); v != 2 {
|
||||
t.Error("get err")
|
||||
}
|
||||
v, _ = redis.Int(val, err)
|
||||
assert.Equal(t, 2, v)
|
||||
|
||||
if err = bm.Decr(context.Background(), "astaxie"); err != nil {
|
||||
t.Error("Decr Error", err)
|
||||
}
|
||||
assert.Nil(t, bm.Decr(context.Background(), "astaxie"))
|
||||
|
||||
val, _ = bm.Get(context.Background(), "astaxie")
|
||||
if v, _ := redis.Int(val, err); v != 1 {
|
||||
t.Error("get err")
|
||||
}
|
||||
v, _ = redis.Int(val, err)
|
||||
assert.Equal(t, 1, v)
|
||||
bm.Delete(context.Background(), "astaxie")
|
||||
if res, _ := bm.IsExist(context.Background(), "astaxie"); res {
|
||||
t.Error("delete err")
|
||||
}
|
||||
|
||||
res, _ = bm.IsExist(context.Background(), "astaxie")
|
||||
assert.False(t, res)
|
||||
|
||||
assert.Nil(t, bm.Put(context.Background(), "astaxie", "author", timeoutDuration))
|
||||
// test string
|
||||
if err = bm.Put(context.Background(), "astaxie", "author", timeoutDuration); err != nil {
|
||||
t.Error("set Error", err)
|
||||
}
|
||||
if res, _ := bm.IsExist(context.Background(), "astaxie"); !res {
|
||||
t.Error("check err")
|
||||
}
|
||||
|
||||
res, _ = bm.IsExist(context.Background(), "astaxie")
|
||||
assert.True(t, res)
|
||||
|
||||
val, _ = bm.Get(context.Background(), "astaxie")
|
||||
if v, _ := redis.String(val, err); v != "author" {
|
||||
t.Error("get err")
|
||||
}
|
||||
vs, _ := redis.String(val, err)
|
||||
assert.Equal(t, "author", vs)
|
||||
|
||||
// test GetMulti
|
||||
if err = bm.Put(context.Background(), "astaxie1", "author1", timeoutDuration); err != nil {
|
||||
t.Error("set Error", err)
|
||||
}
|
||||
if res, _ := bm.IsExist(context.Background(), "astaxie1"); !res {
|
||||
t.Error("check err")
|
||||
}
|
||||
assert.Nil(t, bm.Put(context.Background(), "astaxie1", "author1", timeoutDuration))
|
||||
|
||||
res, _ = bm.IsExist(context.Background(), "astaxie1")
|
||||
assert.True(t, res)
|
||||
|
||||
vv, _ := bm.GetMulti(context.Background(), []string{"astaxie", "astaxie1"})
|
||||
if len(vv) != 2 {
|
||||
t.Error("GetMulti ERROR")
|
||||
}
|
||||
if v, _ := redis.String(vv[0], nil); v != "author" {
|
||||
t.Error("GetMulti ERROR")
|
||||
}
|
||||
if v, _ := redis.String(vv[1], nil); v != "author1" {
|
||||
t.Error("GetMulti ERROR")
|
||||
}
|
||||
assert.Equal(t, 2, len(vv))
|
||||
vs, _ = redis.String(vv[0], nil)
|
||||
assert.Equal(t, "author", vs)
|
||||
|
||||
vs, _ = redis.String(vv[1], nil)
|
||||
assert.Equal(t, "author1", vs)
|
||||
|
||||
vv, _ = bm.GetMulti(context.Background(), []string{"astaxie0", "astaxie1"})
|
||||
if vv[0] != nil {
|
||||
t.Error("GetMulti ERROR")
|
||||
}
|
||||
if v, _ := redis.String(vv[1], nil); v != "author1" {
|
||||
t.Error("GetMulti ERROR")
|
||||
}
|
||||
assert.Nil(t, vv[0])
|
||||
|
||||
vs, _ = redis.String(vv[1], nil)
|
||||
assert.Equal(t, "author1", vs)
|
||||
|
||||
// test clear all
|
||||
if err = bm.ClearAll(context.Background()); err != nil {
|
||||
t.Error("clear all err")
|
||||
}
|
||||
assert.Nil(t, bm.ClearAll(context.Background()))
|
||||
}
|
||||
|
||||
func TestCache_Scan(t *testing.T) {
|
||||
@ -137,35 +115,24 @@ func TestCache_Scan(t *testing.T) {
|
||||
|
||||
// init
|
||||
bm, err := cache.NewCache("redis", fmt.Sprintf(`{"conn": "%s"}`, addr))
|
||||
if err != nil {
|
||||
t.Error("init err")
|
||||
}
|
||||
|
||||
assert.Nil(t, err)
|
||||
// insert all
|
||||
for i := 0; i < 100; i++ {
|
||||
if err = bm.Put(context.Background(), fmt.Sprintf("astaxie%d", i), fmt.Sprintf("author%d", i), timeoutDuration); err != nil {
|
||||
t.Error("set Error", err)
|
||||
}
|
||||
assert.Nil(t, bm.Put(context.Background(), fmt.Sprintf("astaxie%d", i), fmt.Sprintf("author%d", i), timeoutDuration))
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
// scan all for the first time
|
||||
keys, err := bm.(*Cache).Scan(DefaultKey + ":*")
|
||||
if err != nil {
|
||||
t.Error("scan Error", err)
|
||||
}
|
||||
assert.Nil(t, err)
|
||||
|
||||
assert.Equal(t, 100, len(keys), "scan all error")
|
||||
|
||||
// clear all
|
||||
if err = bm.ClearAll(context.Background()); err != nil {
|
||||
t.Error("clear all err")
|
||||
}
|
||||
assert.Nil(t, bm.ClearAll(context.Background()))
|
||||
|
||||
// scan all for the second time
|
||||
keys, err = bm.(*Cache).Scan(DefaultKey + ":*")
|
||||
if err != nil {
|
||||
t.Error("scan Error", err)
|
||||
}
|
||||
if len(keys) != 0 {
|
||||
t.Error("scan all err")
|
||||
}
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, 0, len(keys))
|
||||
}
|
||||
|
||||
2
client/cache/ssdb/ssdb.go
vendored
2
client/cache/ssdb/ssdb.go
vendored
@ -63,7 +63,7 @@ func (rc *Cache) GetMulti(ctx context.Context, keys []string) ([]interface{}, er
|
||||
keysErr := make([]string, 0)
|
||||
for i, ki := range keys {
|
||||
if _, ok := keyIdx[ki]; !ok {
|
||||
keysErr = append(keysErr, fmt.Sprintf("key [%s] error: %s", ki, "the key isn't exist"))
|
||||
keysErr = append(keysErr, fmt.Sprintf("key [%s] error: %s", ki, "key not exist"))
|
||||
continue
|
||||
}
|
||||
values[i] = res[keyIdx[ki]+1]
|
||||
|
||||
136
client/cache/ssdb/ssdb_test.go
vendored
136
client/cache/ssdb/ssdb_test.go
vendored
@ -8,6 +8,8 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/beego/beego/v2/client/cache"
|
||||
)
|
||||
|
||||
@ -19,114 +21,80 @@ func TestSsdbcacheCache(t *testing.T) {
|
||||
}
|
||||
|
||||
ssdb, err := cache.NewCache("ssdb", fmt.Sprintf(`{"conn": "%s"}`, ssdbAddr))
|
||||
if err != nil {
|
||||
t.Error("init err")
|
||||
}
|
||||
assert.Nil(t, err)
|
||||
|
||||
// test put and exist
|
||||
if res, _ := ssdb.IsExist(context.Background(), "ssdb"); res {
|
||||
t.Error("check err")
|
||||
}
|
||||
timeoutDuration := 10 * time.Second
|
||||
res, _ := ssdb.IsExist(context.Background(), "ssdb")
|
||||
assert.False(t, res)
|
||||
timeoutDuration := 3 * time.Second
|
||||
// timeoutDuration := -10*time.Second if timeoutDuration is negtive,it means permanent
|
||||
if err = ssdb.Put(context.Background(), "ssdb", "ssdb", timeoutDuration); err != nil {
|
||||
t.Error("set Error", err)
|
||||
}
|
||||
if res, _ := ssdb.IsExist(context.Background(), "ssdb"); !res {
|
||||
t.Error("check err")
|
||||
}
|
||||
|
||||
assert.Nil(t, ssdb.Put(context.Background(), "ssdb", "ssdb", timeoutDuration))
|
||||
|
||||
res, _ = ssdb.IsExist(context.Background(), "ssdb")
|
||||
assert.True(t, res)
|
||||
|
||||
// Get test done
|
||||
if err = ssdb.Put(context.Background(), "ssdb", "ssdb", timeoutDuration); err != nil {
|
||||
t.Error("set Error", err)
|
||||
}
|
||||
assert.Nil(t, ssdb.Put(context.Background(), "ssdb", "ssdb", timeoutDuration))
|
||||
|
||||
if v, _ := ssdb.Get(context.Background(), "ssdb"); v != "ssdb" {
|
||||
t.Error("get Error")
|
||||
}
|
||||
v, _ := ssdb.Get(context.Background(), "ssdb")
|
||||
assert.Equal(t, "ssdb", v)
|
||||
|
||||
// inc/dec test done
|
||||
if err = ssdb.Put(context.Background(), "ssdb", "2", timeoutDuration); err != nil {
|
||||
t.Error("set Error", err)
|
||||
}
|
||||
if err = ssdb.Incr(context.Background(), "ssdb"); err != nil {
|
||||
t.Error("incr Error", err)
|
||||
}
|
||||
assert.Nil(t, ssdb.Put(context.Background(), "ssdb", "2", timeoutDuration))
|
||||
|
||||
assert.Nil(t, ssdb.Incr(context.Background(), "ssdb"))
|
||||
|
||||
val, _ := ssdb.Get(context.Background(), "ssdb")
|
||||
if v, err := strconv.Atoi(val.(string)); err != nil || v != 3 {
|
||||
t.Error("get err")
|
||||
}
|
||||
v, err = strconv.Atoi(val.(string))
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, 3, v)
|
||||
|
||||
if err = ssdb.Decr(context.Background(), "ssdb"); err != nil {
|
||||
t.Error("decr error")
|
||||
}
|
||||
assert.Nil(t, ssdb.Decr(context.Background(), "ssdb"))
|
||||
|
||||
// test del
|
||||
if err = ssdb.Put(context.Background(), "ssdb", "3", timeoutDuration); err != nil {
|
||||
t.Error("set Error", err)
|
||||
}
|
||||
assert.Nil(t, ssdb.Put(context.Background(), "ssdb", "3", timeoutDuration))
|
||||
|
||||
val, _ = ssdb.Get(context.Background(), "ssdb")
|
||||
if v, err := strconv.Atoi(val.(string)); err != nil || v != 3 {
|
||||
t.Error("get err")
|
||||
}
|
||||
if err := ssdb.Delete(context.Background(), "ssdb"); err == nil {
|
||||
if e, _ := ssdb.IsExist(context.Background(), "ssdb"); e {
|
||||
t.Error("delete err")
|
||||
}
|
||||
}
|
||||
v, err = strconv.Atoi(val.(string))
|
||||
assert.Equal(t, 3, v)
|
||||
assert.Nil(t, err)
|
||||
|
||||
assert.NotNil(t, ssdb.Delete(context.Background(), "ssdb"))
|
||||
assert.Nil(t, ssdb.Put(context.Background(), "ssdb", "ssdb", -10*time.Second))
|
||||
// test string
|
||||
if err = ssdb.Put(context.Background(), "ssdb", "ssdb", -10*time.Second); err != nil {
|
||||
t.Error("set Error", err)
|
||||
}
|
||||
if res, _ := ssdb.IsExist(context.Background(), "ssdb"); !res {
|
||||
t.Error("check err")
|
||||
}
|
||||
if v, _ := ssdb.Get(context.Background(), "ssdb"); v.(string) != "ssdb" {
|
||||
t.Error("get err")
|
||||
}
|
||||
|
||||
res, _ = ssdb.IsExist(context.Background(), "ssdb")
|
||||
assert.True(t, res)
|
||||
|
||||
v, _ = ssdb.Get(context.Background(), "ssdb")
|
||||
assert.Equal(t, "ssdb", v.(string))
|
||||
|
||||
// test GetMulti done
|
||||
if err = ssdb.Put(context.Background(), "ssdb1", "ssdb1", -10*time.Second); err != nil {
|
||||
t.Error("set Error", err)
|
||||
}
|
||||
if res, _ := ssdb.IsExist(context.Background(), "ssdb1"); !res {
|
||||
t.Error("check err")
|
||||
}
|
||||
assert.Nil(t, ssdb.Put(context.Background(), "ssdb1", "ssdb1", -10*time.Second))
|
||||
|
||||
res, _ = ssdb.IsExist(context.Background(), "ssdb1")
|
||||
assert.True(t, res)
|
||||
vv, _ := ssdb.GetMulti(context.Background(), []string{"ssdb", "ssdb1"})
|
||||
if len(vv) != 2 {
|
||||
t.Error("getmulti error")
|
||||
}
|
||||
if vv[0].(string) != "ssdb" {
|
||||
t.Error("getmulti error")
|
||||
}
|
||||
if vv[1].(string) != "ssdb1" {
|
||||
t.Error("getmulti error")
|
||||
}
|
||||
assert.Equal(t, 2, len(vv))
|
||||
|
||||
assert.Equal(t, "ssdb", vv[0])
|
||||
assert.Equal(t, "ssdb1", vv[1])
|
||||
|
||||
vv, err = ssdb.GetMulti(context.Background(), []string{"ssdb", "ssdb11"})
|
||||
if len(vv) != 2 {
|
||||
t.Error("getmulti error")
|
||||
}
|
||||
if vv[0].(string) != "ssdb" {
|
||||
t.Error("getmulti error")
|
||||
}
|
||||
if vv[1] != nil {
|
||||
t.Error("getmulti error")
|
||||
}
|
||||
if err != nil && err.Error() != "key [ssdb11] error: the key isn't exist" {
|
||||
t.Error("getmulti error")
|
||||
}
|
||||
|
||||
assert.Equal(t, 2, len(vv))
|
||||
|
||||
assert.Equal(t, "ssdb", vv[0])
|
||||
assert.Nil(t, vv[1])
|
||||
|
||||
assert.NotNil(t, err)
|
||||
assert.Equal(t, "key [ssdb11] error: key not exist", err.Error())
|
||||
|
||||
// test clear all done
|
||||
if err = ssdb.ClearAll(context.Background()); err != nil {
|
||||
t.Error("clear all err")
|
||||
}
|
||||
assert.Nil(t, ssdb.ClearAll(context.Background()))
|
||||
e1, _ := ssdb.IsExist(context.Background(), "ssdb")
|
||||
e2, _ := ssdb.IsExist(context.Background(), "ssdb1")
|
||||
if e1 || e2 {
|
||||
t.Error("check err")
|
||||
}
|
||||
assert.False(t, e1)
|
||||
assert.False(t, e2)
|
||||
}
|
||||
|
||||
@ -26,7 +26,7 @@ import (
|
||||
"github.com/beego/beego/v2/client/httplib"
|
||||
)
|
||||
|
||||
func TestFilterChainBuilder_FilterChain(t *testing.T) {
|
||||
func TestFilterChainBuilderFilterChain(t *testing.T) {
|
||||
next := func(ctx context.Context, req *httplib.BeegoHTTPRequest) (*http.Response, error) {
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
return &http.Response{
|
||||
|
||||
@ -25,7 +25,7 @@ import (
|
||||
"github.com/beego/beego/v2/client/httplib"
|
||||
)
|
||||
|
||||
func TestFilterChainBuilder_FilterChain(t *testing.T) {
|
||||
func TestFilterChainBuilderFilterChain(t *testing.T) {
|
||||
next := func(ctx context.Context, req *httplib.BeegoHTTPRequest) (*http.Response, error) {
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
return &http.Response{
|
||||
|
||||
@ -55,6 +55,7 @@ import (
|
||||
"github.com/beego/beego/v2/core/logs"
|
||||
)
|
||||
|
||||
const contentTypeKey = "Content-Type"
|
||||
// it will be the last filter and execute request.Do
|
||||
var doRequestFilter = func(ctx context.Context, req *BeegoHTTPRequest) (*http.Response, error) {
|
||||
return req.doRequest(ctx)
|
||||
@ -311,7 +312,7 @@ func (b *BeegoHTTPRequest) XMLBody(obj interface{}) (*BeegoHTTPRequest, error) {
|
||||
return ioutil.NopCloser(bytes.NewReader(byts)), nil
|
||||
}
|
||||
b.req.ContentLength = int64(len(byts))
|
||||
b.req.Header.Set("Content-Type", "application/xml")
|
||||
b.req.Header.Set(contentTypeKey, "application/xml")
|
||||
}
|
||||
return b, nil
|
||||
}
|
||||
@ -325,7 +326,7 @@ func (b *BeegoHTTPRequest) YAMLBody(obj interface{}) (*BeegoHTTPRequest, error)
|
||||
}
|
||||
b.req.Body = ioutil.NopCloser(bytes.NewReader(byts))
|
||||
b.req.ContentLength = int64(len(byts))
|
||||
b.req.Header.Set("Content-Type", "application/x+yaml")
|
||||
b.req.Header.Set(contentTypeKey, "application/x+yaml")
|
||||
}
|
||||
return b, nil
|
||||
}
|
||||
@ -339,7 +340,7 @@ func (b *BeegoHTTPRequest) JSONBody(obj interface{}) (*BeegoHTTPRequest, error)
|
||||
}
|
||||
b.req.Body = ioutil.NopCloser(bytes.NewReader(byts))
|
||||
b.req.ContentLength = int64(len(byts))
|
||||
b.req.Header.Set("Content-Type", "application/json")
|
||||
b.req.Header.Set(contentTypeKey, "application/json")
|
||||
}
|
||||
return b, nil
|
||||
}
|
||||
@ -359,6 +360,19 @@ func (b *BeegoHTTPRequest) buildURL(paramBody string) {
|
||||
if (b.req.Method == "POST" || b.req.Method == "PUT" || b.req.Method == "PATCH" || b.req.Method == "DELETE") && b.req.Body == nil {
|
||||
// with files
|
||||
if len(b.files) > 0 {
|
||||
b.handleFiles()
|
||||
return
|
||||
}
|
||||
|
||||
// with params
|
||||
if len(paramBody) > 0 {
|
||||
b.Header(contentTypeKey, "application/x-www-form-urlencoded")
|
||||
b.Body(paramBody)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (b *BeegoHTTPRequest) handleFiles() {
|
||||
pr, pw := io.Pipe()
|
||||
bodyWriter := multipart.NewWriter(pw)
|
||||
go func() {
|
||||
@ -373,18 +387,9 @@ func (b *BeegoHTTPRequest) buildURL(paramBody string) {
|
||||
_ = bodyWriter.Close()
|
||||
_ = pw.Close()
|
||||
}()
|
||||
b.Header("Content-Type", bodyWriter.FormDataContentType())
|
||||
b.Header(contentTypeKey, bodyWriter.FormDataContentType())
|
||||
b.req.Body = ioutil.NopCloser(pr)
|
||||
b.Header("Transfer-Encoding", "chunked")
|
||||
return
|
||||
}
|
||||
|
||||
// with params
|
||||
if len(paramBody) > 0 {
|
||||
b.Header("Content-Type", "application/x-www-form-urlencoded")
|
||||
b.Body(paramBody)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (b *BeegoHTTPRequest) handleFileToBody(bodyWriter *multipart.Writer, formname string, filename string) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user