redis_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package dao
  2. import (
  3. "context"
  4. "testing"
  5. "go-common/app/service/main/broadcast/model"
  6. "github.com/smartystreets/goconvey/convey"
  7. )
  8. func TestDaokeyMidServer(t *testing.T) {
  9. var (
  10. mid = int64(1)
  11. )
  12. convey.Convey("keyMidServer", t, func(ctx convey.C) {
  13. p1 := keyMidServer(mid)
  14. ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
  15. ctx.So(p1, convey.ShouldNotBeNil)
  16. })
  17. })
  18. }
  19. func TestDaokeyKeyServer(t *testing.T) {
  20. var (
  21. key = "key"
  22. )
  23. convey.Convey("keyKeyServer", t, func(ctx convey.C) {
  24. p1 := keyKeyServer(key)
  25. ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
  26. ctx.So(p1, convey.ShouldNotBeNil)
  27. })
  28. })
  29. }
  30. func TestDaokeyServerOnline(t *testing.T) {
  31. var (
  32. key = "key"
  33. )
  34. convey.Convey("keyServerOnline", t, func(ctx convey.C) {
  35. p1 := keyServerOnline(key)
  36. ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
  37. ctx.So(p1, convey.ShouldNotBeNil)
  38. })
  39. })
  40. }
  41. func TestDaopingRedis(t *testing.T) {
  42. var (
  43. c = context.Background()
  44. )
  45. convey.Convey("pingRedis", t, func(ctx convey.C) {
  46. err := d.pingRedis(c)
  47. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  48. ctx.So(err, convey.ShouldBeNil)
  49. })
  50. })
  51. }
  52. func TestDaoAddMapping(t *testing.T) {
  53. var (
  54. c = context.Background()
  55. mid = int64(1)
  56. key = "key"
  57. server = "server"
  58. )
  59. convey.Convey("AddMapping", t, func(ctx convey.C) {
  60. err := d.AddMapping(c, mid, key, server)
  61. ctx.So(err, convey.ShouldBeNil)
  62. has, err := d.ExpireMapping(c, mid, key)
  63. ctx.So(err, convey.ShouldBeNil)
  64. ctx.So(has, convey.ShouldBeTrue)
  65. has, err = d.DelMapping(c, mid, key, server)
  66. ctx.So(err, convey.ShouldBeNil)
  67. ctx.So(has, convey.ShouldBeTrue)
  68. // false
  69. has, err = d.ExpireMapping(c, mid, key)
  70. ctx.So(err, convey.ShouldBeNil)
  71. ctx.So(has, convey.ShouldBeFalse)
  72. has, err = d.DelMapping(c, mid, key, server)
  73. ctx.So(err, convey.ShouldBeNil)
  74. ctx.So(has, convey.ShouldBeFalse)
  75. })
  76. }
  77. func TestDaoServersByKeys(t *testing.T) {
  78. var (
  79. c = context.Background()
  80. keys = []string{"key"}
  81. )
  82. convey.Convey("ServersByKeys", t, func(ctx convey.C) {
  83. res, err := d.ServersByKeys(c, keys)
  84. ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
  85. ctx.So(err, convey.ShouldBeNil)
  86. ctx.So(res, convey.ShouldNotBeNil)
  87. })
  88. })
  89. }
  90. func TestDaoKeysByMids(t *testing.T) {
  91. var (
  92. c = context.Background()
  93. mids = []int64{1, 2, 3}
  94. )
  95. convey.Convey("KeysByMids", t, func(ctx convey.C) {
  96. ress, _, err := d.KeysByMids(c, mids)
  97. ctx.Convey("Then err should be nil.ress should not be nil.", func(ctx convey.C) {
  98. ctx.So(err, convey.ShouldBeNil)
  99. ctx.So(ress, convey.ShouldNotBeNil)
  100. })
  101. })
  102. }
  103. func TestDaoAddServerOnline(t *testing.T) {
  104. var (
  105. c = context.Background()
  106. server = "key"
  107. shard = 1
  108. online = &model.Online{RoomCount: map[string]int32{
  109. "test1": 100,
  110. "test2": 200,
  111. "test3": 300,
  112. }, Updated: 1}
  113. )
  114. convey.Convey("AddServerOnline", t, func(ctx convey.C) {
  115. err := d.AddServerOnline(c, server, int32(shard), online)
  116. ctx.So(err, convey.ShouldBeNil)
  117. res, err := d.ServerOnline(c, server, shard)
  118. ctx.So(err, convey.ShouldBeNil)
  119. ctx.So(res, convey.ShouldResemble, online)
  120. err = d.DelServerOnline(c, server, shard)
  121. ctx.So(err, convey.ShouldBeNil)
  122. })
  123. }
  124. func TestDaoSetServers(t *testing.T) {
  125. var (
  126. c = context.Background()
  127. servers = []*model.ServerInfo{}
  128. )
  129. convey.Convey("SetServers", t, func(ctx convey.C) {
  130. ctx.So(d.SetServers(c, servers), convey.ShouldBeNil)
  131. res, err := d.Servers(c)
  132. ctx.So(err, convey.ShouldBeNil)
  133. ctx.So(res, convey.ShouldResemble, servers)
  134. _, _, err = d.MigrateServers(c)
  135. ctx.So(err, convey.ShouldBeNil)
  136. _, err = d.MigrateRooms(c, 0)
  137. ctx.So(err, convey.ShouldBeNil)
  138. })
  139. }