memcache_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package memcache
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "os"
  6. "testing"
  7. "time"
  8. "go-common/library/container/pool"
  9. xtime "go-common/library/time"
  10. )
  11. var testMemcacheAddr = "127.0.0.1:11211"
  12. var testConfig = &Config{
  13. Config: &pool.Config{
  14. Active: 10,
  15. Idle: 10,
  16. IdleTimeout: xtime.Duration(time.Second),
  17. WaitTimeout: xtime.Duration(time.Second),
  18. Wait: false,
  19. },
  20. Proto: "tcp",
  21. DialTimeout: xtime.Duration(time.Second),
  22. ReadTimeout: xtime.Duration(time.Second),
  23. WriteTimeout: xtime.Duration(time.Second),
  24. }
  25. func init() {
  26. if addr := os.Getenv("TEST_MEMCACHE_ADDR"); addr != "" {
  27. testMemcacheAddr = addr
  28. }
  29. testConfig.Addr = testMemcacheAddr
  30. }
  31. func TestMain(m *testing.M) {
  32. testClient = New(testConfig)
  33. m.Run()
  34. testClient.Close()
  35. os.Exit(0)
  36. }
  37. func ExampleConn_set() {
  38. var (
  39. err error
  40. value []byte
  41. conn Conn
  42. expire int32 = 100
  43. p = struct {
  44. Name string
  45. Age int64
  46. }{"golang", 10}
  47. )
  48. cnop := DialConnectTimeout(time.Duration(time.Second))
  49. rdop := DialReadTimeout(time.Duration(time.Second))
  50. wrop := DialWriteTimeout(time.Duration(time.Second))
  51. if value, err = json.Marshal(p); err != nil {
  52. fmt.Println(err)
  53. return
  54. }
  55. if conn, err = Dial("tcp", testMemcacheAddr, cnop, rdop, wrop); err != nil {
  56. fmt.Println(err)
  57. return
  58. }
  59. // FlagRAW test
  60. itemRaw := &Item{
  61. Key: "test_raw",
  62. Value: value,
  63. Expiration: expire,
  64. }
  65. if err = conn.Set(itemRaw); err != nil {
  66. fmt.Println(err)
  67. return
  68. }
  69. // FlagGzip
  70. itemGZip := &Item{
  71. Key: "test_gzip",
  72. Value: value,
  73. Flags: FlagGzip,
  74. Expiration: expire,
  75. }
  76. if err = conn.Set(itemGZip); err != nil {
  77. fmt.Println(err)
  78. return
  79. }
  80. // FlagGOB
  81. itemGOB := &Item{
  82. Key: "test_gob",
  83. Object: p,
  84. Flags: FlagGOB,
  85. Expiration: expire,
  86. }
  87. if err = conn.Set(itemGOB); err != nil {
  88. fmt.Println(err)
  89. return
  90. }
  91. // FlagJSON
  92. itemJSON := &Item{
  93. Key: "test_json",
  94. Object: p,
  95. Flags: FlagJSON,
  96. Expiration: expire,
  97. }
  98. if err = conn.Set(itemJSON); err != nil {
  99. fmt.Println(err)
  100. return
  101. }
  102. // FlagJSON | FlagGzip
  103. itemJSONGzip := &Item{
  104. Key: "test_jsonGzip",
  105. Object: p,
  106. Flags: FlagJSON | FlagGzip,
  107. Expiration: expire,
  108. }
  109. if err = conn.Set(itemJSONGzip); err != nil {
  110. fmt.Println(err)
  111. return
  112. }
  113. // Output:
  114. }
  115. func ExampleConn_get() {
  116. var (
  117. err error
  118. item2 *Item
  119. conn Conn
  120. p struct {
  121. Name string
  122. Age int64
  123. }
  124. )
  125. cnop := DialConnectTimeout(time.Duration(time.Second))
  126. rdop := DialReadTimeout(time.Duration(time.Second))
  127. wrop := DialWriteTimeout(time.Duration(time.Second))
  128. if conn, err = Dial("tcp", testMemcacheAddr, cnop, rdop, wrop); err != nil {
  129. fmt.Println(err)
  130. return
  131. }
  132. if item2, err = conn.Get("test_raw"); err != nil {
  133. fmt.Println(err)
  134. } else {
  135. if err = conn.Scan(item2, &p); err != nil {
  136. fmt.Printf("FlagRAW conn.Scan error(%v)\n", err)
  137. return
  138. }
  139. }
  140. // FlagGZip
  141. if item2, err = conn.Get("test_gzip"); err != nil {
  142. fmt.Println(err)
  143. } else {
  144. if err = conn.Scan(item2, &p); err != nil {
  145. fmt.Printf("FlagGZip conn.Scan error(%v)\n", err)
  146. return
  147. }
  148. }
  149. // FlagGOB
  150. if item2, err = conn.Get("test_gob"); err != nil {
  151. fmt.Println(err)
  152. } else {
  153. if err = conn.Scan(item2, &p); err != nil {
  154. fmt.Printf("FlagGOB conn.Scan error(%v)\n", err)
  155. return
  156. }
  157. }
  158. // FlagJSON
  159. if item2, err = conn.Get("test_json"); err != nil {
  160. fmt.Println(err)
  161. } else {
  162. if err = conn.Scan(item2, &p); err != nil {
  163. fmt.Printf("FlagJSON conn.Scan error(%v)\n", err)
  164. return
  165. }
  166. }
  167. // Output:
  168. }
  169. func ExampleConn_getMulti() {
  170. var (
  171. err error
  172. conn Conn
  173. res map[string]*Item
  174. keys = []string{"test_raw", "test_gzip"}
  175. p struct {
  176. Name string
  177. Age int64
  178. }
  179. )
  180. cnop := DialConnectTimeout(time.Duration(time.Second))
  181. rdop := DialReadTimeout(time.Duration(time.Second))
  182. wrop := DialWriteTimeout(time.Duration(time.Second))
  183. if conn, err = Dial("tcp", testMemcacheAddr, cnop, rdop, wrop); err != nil {
  184. fmt.Println(err)
  185. return
  186. }
  187. if res, err = conn.GetMulti(keys); err != nil {
  188. fmt.Printf("conn.GetMulti(%v) error(%v)", keys, err)
  189. return
  190. }
  191. for _, v := range res {
  192. if err = conn.Scan(v, &p); err != nil {
  193. fmt.Printf("conn.Scan error(%v)\n", err)
  194. return
  195. }
  196. fmt.Println(p)
  197. }
  198. // Output:
  199. //{golang 10}
  200. //{golang 10}
  201. }