memcache_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package dao
  2. import (
  3. "context"
  4. "reflect"
  5. "testing"
  6. "go-common/app/service/main/secure/model"
  7. "go-common/library/cache/memcache"
  8. "github.com/bouk/monkey"
  9. . "github.com/smartystreets/goconvey/convey"
  10. )
  11. func TestAddLocsCache(t *testing.T) {
  12. Convey("TestAddLocsCache", t, func() {
  13. err := d.AddLocsCache(context.TODO(), 3, &model.Locs{LocsCount: map[int64]int64{3: 2, 4: 3}})
  14. So(err, ShouldBeNil)
  15. })
  16. Convey("TestAddLocsCacheErr", t, func() {
  17. connGuard := monkey.PatchInstanceMethod(reflect.TypeOf(d.mc), "Get", func(_ *memcache.Pool, _ context.Context) memcache.Conn {
  18. return memcache.MockWith(memcache.ErrItemObject)
  19. })
  20. defer connGuard.Unpatch()
  21. err := d.AddLocsCache(context.TODO(), 3, &model.Locs{LocsCount: map[int64]int64{3: 2, 4: 3}})
  22. So(err, ShouldEqual, memcache.ErrItemObject)
  23. })
  24. }
  25. func TestLocsCache(t *testing.T) {
  26. Convey("TestGetLocsCache", t, func() {
  27. locs, err := d.LocsCache(context.TODO(), 3)
  28. So(err, ShouldBeNil)
  29. So(locs, ShouldNotBeNil)
  30. })
  31. Convey("TestGetLocsCacheGetErr", t, func() {
  32. connGuard := monkey.PatchInstanceMethod(reflect.TypeOf(d.mc), "Get", func(_ *memcache.Pool, _ context.Context) memcache.Conn {
  33. return memcache.MockWith(memcache.ErrNotFound)
  34. })
  35. defer connGuard.Unpatch()
  36. locs, err := d.LocsCache(context.TODO(), 3)
  37. So(err, ShouldBeNil)
  38. So(locs, ShouldBeNil)
  39. })
  40. Convey("TestGetLocsCacheScanErr", t, func() {
  41. connGuard := monkey.PatchInstanceMethod(reflect.TypeOf(d.mc), "Get", func(_ *memcache.Pool, _ context.Context) memcache.Conn {
  42. return memcache.MockWith(memcache.ErrItemObject)
  43. })
  44. defer connGuard.Unpatch()
  45. locs, err := d.LocsCache(context.TODO(), 3)
  46. So(err, ShouldEqual, memcache.ErrItemObject)
  47. So(locs, ShouldBeNil)
  48. })
  49. }
  50. func TestMcPing(t *testing.T) {
  51. Convey("TestMcPing", t, func() {
  52. err := d.pingMC(context.Background())
  53. So(err, ShouldBeNil)
  54. })
  55. Convey("TestMcPingErr", t, func() {
  56. connGuard := monkey.PatchInstanceMethod(reflect.TypeOf(d.mc), "Get", func(_ *memcache.Pool, _ context.Context) memcache.Conn {
  57. return memcache.MockWith(memcache.ErrConnClosed)
  58. })
  59. defer connGuard.Unpatch()
  60. err := d.pingMC(context.Background())
  61. So(err, ShouldNotBeNil)
  62. So(err, ShouldEqual, memcache.ErrConnClosed)
  63. })
  64. }