client_test.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. package memcache
  2. import (
  3. "context"
  4. "fmt"
  5. "reflect"
  6. "testing"
  7. "time"
  8. )
  9. var testClient *Memcache
  10. func Test_client_Set(t *testing.T) {
  11. type args struct {
  12. c context.Context
  13. item *Item
  14. }
  15. tests := []struct {
  16. name string
  17. args args
  18. wantErr bool
  19. }{
  20. {name: "set value", args: args{c: context.Background(), item: &Item{Key: "Test_client_Set", Value: []byte("abc")}}, wantErr: false},
  21. }
  22. for _, tt := range tests {
  23. t.Run(tt.name, func(t *testing.T) {
  24. if err := testClient.Set(tt.args.c, tt.args.item); (err != nil) != tt.wantErr {
  25. t.Errorf("client.Set() error = %v, wantErr %v", err, tt.wantErr)
  26. }
  27. })
  28. }
  29. }
  30. func Test_client_Add(t *testing.T) {
  31. type args struct {
  32. c context.Context
  33. item *Item
  34. }
  35. key := fmt.Sprintf("Test_client_Add_%d", time.Now().Unix())
  36. tests := []struct {
  37. name string
  38. args args
  39. wantErr bool
  40. }{
  41. {name: "add not exist value", args: args{c: context.Background(), item: &Item{Key: key, Value: []byte("abc")}}, wantErr: false},
  42. {name: "add exist value", args: args{c: context.Background(), item: &Item{Key: key, Value: []byte("abc")}}, wantErr: true},
  43. }
  44. for _, tt := range tests {
  45. t.Run(tt.name, func(t *testing.T) {
  46. if err := testClient.Add(tt.args.c, tt.args.item); (err != nil) != tt.wantErr {
  47. t.Errorf("client.Add() error = %v, wantErr %v", err, tt.wantErr)
  48. }
  49. })
  50. }
  51. }
  52. func Test_client_Replace(t *testing.T) {
  53. key := fmt.Sprintf("Test_client_Replace_%d", time.Now().Unix())
  54. ekey := "Test_client_Replace_exist"
  55. testClient.Set(context.Background(), &Item{Key: ekey, Value: []byte("ok")})
  56. type args struct {
  57. c context.Context
  58. item *Item
  59. }
  60. tests := []struct {
  61. name string
  62. args args
  63. wantErr bool
  64. }{
  65. {name: "not exist value", args: args{c: context.Background(), item: &Item{Key: key, Value: []byte("abc")}}, wantErr: true},
  66. {name: "exist value", args: args{c: context.Background(), item: &Item{Key: ekey, Value: []byte("abc")}}, wantErr: false},
  67. }
  68. for _, tt := range tests {
  69. t.Run(tt.name, func(t *testing.T) {
  70. if err := testClient.Replace(tt.args.c, tt.args.item); (err != nil) != tt.wantErr {
  71. t.Errorf("client.Replace() error = %v, wantErr %v", err, tt.wantErr)
  72. }
  73. })
  74. }
  75. }
  76. func Test_client_CompareAndSwap(t *testing.T) {
  77. key := fmt.Sprintf("Test_client_CompareAndSwap_%d", time.Now().Unix())
  78. ekey := "Test_client_CompareAndSwap_k"
  79. testClient.Set(context.Background(), &Item{Key: ekey, Value: []byte("old")})
  80. cas := testClient.Get(context.Background(), ekey).Item().cas
  81. type args struct {
  82. c context.Context
  83. item *Item
  84. }
  85. tests := []struct {
  86. name string
  87. args args
  88. wantErr bool
  89. }{
  90. {name: "not exist value", args: args{c: context.Background(), item: &Item{Key: key, Value: []byte("abc")}}, wantErr: true},
  91. {name: "exist value", args: args{c: context.Background(), item: &Item{Key: ekey, cas: cas, Value: []byte("abc")}}, wantErr: false},
  92. }
  93. for _, tt := range tests {
  94. t.Run(tt.name, func(t *testing.T) {
  95. if err := testClient.CompareAndSwap(tt.args.c, tt.args.item); (err != nil) != tt.wantErr {
  96. t.Errorf("client.CompareAndSwap() error = %v, wantErr %v", err, tt.wantErr)
  97. }
  98. })
  99. }
  100. }
  101. func Test_client_Get(t *testing.T) {
  102. key := fmt.Sprintf("Test_client_Get_%d", time.Now().Unix())
  103. ekey := "Test_client_Get_k"
  104. testClient.Set(context.Background(), &Item{Key: ekey, Value: []byte("old")})
  105. type args struct {
  106. c context.Context
  107. key string
  108. }
  109. tests := []struct {
  110. name string
  111. args args
  112. want string
  113. wantErr bool
  114. }{
  115. {name: "not exist value", args: args{c: context.Background(), key: key}, wantErr: true},
  116. {name: "exist value", args: args{c: context.Background(), key: ekey}, wantErr: false, want: "old"},
  117. }
  118. for _, tt := range tests {
  119. t.Run(tt.name, func(t *testing.T) {
  120. var res string
  121. if err := testClient.Get(tt.args.c, tt.args.key).Scan(&res); (err != nil) != tt.wantErr || res != tt.want {
  122. t.Errorf("client.Get() = %v, want %v, got err: %v, want err: %v", err, tt.want, err, tt.wantErr)
  123. }
  124. })
  125. }
  126. }
  127. func Test_client_Touch(t *testing.T) {
  128. key := fmt.Sprintf("Test_client_Touch_%d", time.Now().Unix())
  129. ekey := "Test_client_Touch_k"
  130. testClient.Set(context.Background(), &Item{Key: ekey, Value: []byte("old")})
  131. type args struct {
  132. c context.Context
  133. key string
  134. timeout int32
  135. }
  136. tests := []struct {
  137. name string
  138. args args
  139. wantErr bool
  140. }{
  141. {name: "not exist value", args: args{c: context.Background(), key: key, timeout: 100000}, wantErr: true},
  142. {name: "exist value", args: args{c: context.Background(), key: ekey, timeout: 100000}, wantErr: false},
  143. }
  144. for _, tt := range tests {
  145. t.Run(tt.name, func(t *testing.T) {
  146. if err := testClient.Touch(tt.args.c, tt.args.key, tt.args.timeout); (err != nil) != tt.wantErr {
  147. t.Errorf("client.Touch() error = %v, wantErr %v", err, tt.wantErr)
  148. }
  149. })
  150. }
  151. }
  152. func Test_client_Delete(t *testing.T) {
  153. key := fmt.Sprintf("Test_client_Delete_%d", time.Now().Unix())
  154. ekey := "Test_client_Delete_k"
  155. testClient.Set(context.Background(), &Item{Key: ekey, Value: []byte("old")})
  156. type args struct {
  157. c context.Context
  158. key string
  159. }
  160. tests := []struct {
  161. name string
  162. args args
  163. wantErr bool
  164. }{
  165. {name: "not exist value", args: args{c: context.Background(), key: key}, wantErr: true},
  166. {name: "exist value", args: args{c: context.Background(), key: ekey}, wantErr: false},
  167. }
  168. for _, tt := range tests {
  169. t.Run(tt.name, func(t *testing.T) {
  170. if err := testClient.Delete(tt.args.c, tt.args.key); (err != nil) != tt.wantErr {
  171. t.Errorf("client.Delete() error = %v, wantErr %v", err, tt.wantErr)
  172. }
  173. })
  174. }
  175. }
  176. func Test_client_Increment(t *testing.T) {
  177. key := fmt.Sprintf("Test_client_Increment_%d", time.Now().Unix())
  178. ekey := "Test_client_Increment_k"
  179. testClient.Set(context.Background(), &Item{Key: ekey, Value: []byte("1")})
  180. type args struct {
  181. c context.Context
  182. key string
  183. delta uint64
  184. }
  185. tests := []struct {
  186. name string
  187. args args
  188. wantNewValue uint64
  189. wantErr bool
  190. }{
  191. {name: "not exist value", args: args{c: context.Background(), key: key, delta: 10}, wantErr: true},
  192. {name: "exist value", args: args{c: context.Background(), key: ekey, delta: 10}, wantErr: false, wantNewValue: 11},
  193. }
  194. for _, tt := range tests {
  195. t.Run(tt.name, func(t *testing.T) {
  196. gotNewValue, err := testClient.Increment(tt.args.c, tt.args.key, tt.args.delta)
  197. if (err != nil) != tt.wantErr {
  198. t.Errorf("client.Increment() error = %v, wantErr %v", err, tt.wantErr)
  199. return
  200. }
  201. if gotNewValue != tt.wantNewValue {
  202. t.Errorf("client.Increment() = %v, want %v", gotNewValue, tt.wantNewValue)
  203. }
  204. })
  205. }
  206. }
  207. func Test_client_Decrement(t *testing.T) {
  208. key := fmt.Sprintf("Test_client_Decrement_%d", time.Now().Unix())
  209. ekey := "Test_client_Decrement_k"
  210. testClient.Set(context.Background(), &Item{Key: ekey, Value: []byte("100")})
  211. type args struct {
  212. c context.Context
  213. key string
  214. delta uint64
  215. }
  216. tests := []struct {
  217. name string
  218. args args
  219. wantNewValue uint64
  220. wantErr bool
  221. }{
  222. {name: "not exist value", args: args{c: context.Background(), key: key, delta: 10}, wantErr: true},
  223. {name: "exist value", args: args{c: context.Background(), key: ekey, delta: 10}, wantErr: false, wantNewValue: 90},
  224. }
  225. for _, tt := range tests {
  226. t.Run(tt.name, func(t *testing.T) {
  227. gotNewValue, err := testClient.Decrement(tt.args.c, tt.args.key, tt.args.delta)
  228. if (err != nil) != tt.wantErr {
  229. t.Errorf("client.Decrement() error = %v, wantErr %v", err, tt.wantErr)
  230. return
  231. }
  232. if gotNewValue != tt.wantNewValue {
  233. t.Errorf("client.Decrement() = %v, want %v", gotNewValue, tt.wantNewValue)
  234. }
  235. })
  236. }
  237. }
  238. func Test_client_GetMulti(t *testing.T) {
  239. key := fmt.Sprintf("Test_client_GetMulti_%d", time.Now().Unix())
  240. ekey1 := "Test_client_GetMulti_k1"
  241. ekey2 := "Test_client_GetMulti_k2"
  242. testClient.Set(context.Background(), &Item{Key: ekey1, Value: []byte("1")})
  243. testClient.Set(context.Background(), &Item{Key: ekey2, Value: []byte("2")})
  244. keys := []string{key, ekey1, ekey2}
  245. rows, err := testClient.GetMulti(context.Background(), keys)
  246. if err != nil {
  247. t.Errorf("client.GetMulti() error = %v, wantErr %v", err, nil)
  248. }
  249. tests := []struct {
  250. key string
  251. wantNewValue string
  252. wantErr bool
  253. nilItem bool
  254. }{
  255. {key: key, wantErr: true, nilItem: true},
  256. {key: ekey1, wantErr: false, wantNewValue: "1", nilItem: false},
  257. {key: ekey2, wantErr: false, wantNewValue: "2", nilItem: false},
  258. }
  259. if reflect.DeepEqual(keys, rows.Keys()) {
  260. t.Errorf("got %v, expect: %v", rows.Keys(), keys)
  261. }
  262. for _, tt := range tests {
  263. t.Run(tt.key, func(t *testing.T) {
  264. var gotNewValue string
  265. err = rows.Scan(tt.key, &gotNewValue)
  266. if (err != nil) != tt.wantErr {
  267. t.Errorf("rows.Scan() error = %v, wantErr %v", err, tt.wantErr)
  268. return
  269. }
  270. if gotNewValue != tt.wantNewValue {
  271. t.Errorf("rows.Value() = %v, want %v", gotNewValue, tt.wantNewValue)
  272. }
  273. if (rows.Item(tt.key) == nil) != tt.nilItem {
  274. t.Errorf("rows.Item() = %v, want %v", rows.Item(tt.key) == nil, tt.nilItem)
  275. }
  276. })
  277. }
  278. err = rows.Close()
  279. if err != nil {
  280. t.Errorf("client.Replies.Close() error = %v, wantErr %v", err, nil)
  281. }
  282. }
  283. func Test_client_Conn(t *testing.T) {
  284. conn := testClient.Conn(context.Background())
  285. defer conn.Close()
  286. if conn == nil {
  287. t.Errorf("expect get conn, get nil")
  288. }
  289. }