discovery_test.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. package discovery
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "os"
  7. "testing"
  8. "time"
  9. "go-common/library/conf/env"
  10. "go-common/library/ecode"
  11. "go-common/library/exp/feature"
  12. "go-common/library/naming"
  13. "go-common/library/net/http/blademaster"
  14. . "github.com/smartystreets/goconvey/convey"
  15. )
  16. var appdID = "main.arch.test66"
  17. var appID2 = "main.arch.test22"
  18. func TestMain(m *testing.M) {
  19. feature.DefaultGate.AddFlag(flag.CommandLine)
  20. flag.Set("feature-gates", fmt.Sprintf("%s=true", _selfDiscoveryFeatrue))
  21. os.Exit(m.Run())
  22. }
  23. var c = &Config{
  24. Nodes: []string{"172.18.33.51:7171"},
  25. Zone: "sh001",
  26. Env: "pre",
  27. Key: "0c4b8fe3ff35a4b6",
  28. Secret: "b370880d1aca7d3a289b9b9a7f4d6812",
  29. Host: "host_1",
  30. }
  31. var indis = &naming.Instance{
  32. AppID: appdID,
  33. Zone: env.Zone,
  34. Addrs: []string{
  35. "grpc://172.18.33.51:8080",
  36. "http://172.18.33.51:7171",
  37. },
  38. Version: "1",
  39. Metadata: map[string]string{
  40. "test": "1",
  41. "weight": "12",
  42. "color": "blue",
  43. },
  44. }
  45. var in = &naming.Instance{
  46. AppID: appdID,
  47. Addrs: []string{
  48. "grpc://127.0.0.1:8080",
  49. },
  50. Version: "1",
  51. Metadata: map[string]string{
  52. "test": "1",
  53. "weight": "12",
  54. "color": "blue",
  55. },
  56. }
  57. var intest2 = &naming.Instance{
  58. AppID: appID2,
  59. Addrs: []string{
  60. "grpc://127.0.0.1:8080",
  61. },
  62. Version: "1",
  63. Metadata: map[string]string{
  64. "test": "1",
  65. "weight": "12",
  66. "color": "blue",
  67. },
  68. }
  69. var in2 = &naming.Instance{
  70. AppID: appdID,
  71. Addrs: []string{
  72. "grpc://127.0.0.1:8081",
  73. },
  74. Version: "1",
  75. Metadata: map[string]string{
  76. "test": "2",
  77. "weight": "6",
  78. "color": "red",
  79. },
  80. }
  81. func TestDiscoverySelf(t *testing.T) {
  82. Convey("test TestDiscoverySelf ", t, func() {
  83. So(feature.DefaultGate.Enabled(_selfDiscoveryFeatrue), ShouldBeTrue)
  84. d := New(c)
  85. So(len(d.node.Load().([]string)), ShouldNotEqual, 0)
  86. })
  87. }
  88. func TestRegister(t *testing.T) {
  89. Convey("test register and cancel", t, func() {
  90. env.Hostname = "host_1"
  91. d := New(c)
  92. defer d.Close()
  93. ctx := context.TODO()
  94. cancel, err := d.Register(ctx, in)
  95. defer cancel()
  96. So(err, ShouldBeNil)
  97. rs := d.Build(appdID)
  98. ch := rs.Watch()
  99. So(ch, ShouldNotBeNil)
  100. <-ch
  101. ins, ok := rs.Fetch(ctx)
  102. So(ok, ShouldBeTrue)
  103. So(err, ShouldBeNil)
  104. var count int
  105. for _, data := range ins {
  106. count += len(data)
  107. }
  108. So(count, ShouldEqual, 1)
  109. c.Host = "host_2"
  110. env.Hostname = "host_2"
  111. d2 := New(c)
  112. defer d2.Close()
  113. cancel2, err := d2.Register(ctx, in2)
  114. So(err, ShouldBeNil)
  115. <-ch
  116. ins, _ = rs.Fetch(ctx)
  117. So(err, ShouldBeNil)
  118. count = 0
  119. for _, data := range ins {
  120. count += len(data)
  121. }
  122. So(count, ShouldEqual, 2)
  123. time.Sleep(time.Millisecond * 500)
  124. cancel2()
  125. <-ch
  126. ins, _ = rs.Fetch(ctx)
  127. So(err, ShouldBeNil)
  128. count = 0
  129. for _, data := range ins {
  130. count += len(data)
  131. }
  132. So(count, ShouldEqual, 1)
  133. Convey("test discovery set", func() {
  134. c.Host = "host_1"
  135. inSet := &naming.Instance{
  136. AppID: appdID,
  137. Addrs: []string{
  138. "grpc://127.0.0.1:8080",
  139. },
  140. Status: 1,
  141. Metadata: map[string]string{
  142. "test": "1",
  143. "weight": "111",
  144. "color": "blue",
  145. },
  146. }
  147. ins, _ = rs.Fetch(context.TODO())
  148. fmt.Println("ins", ins["sh001"][0])
  149. err = d2.Set(inSet)
  150. // So(err, ShouldBeNil)
  151. <-ch
  152. ins, _ = rs.Fetch(context.TODO())
  153. fmt.Println("ins1", ins["sh001"][0])
  154. So(ins["sh001"][0].Metadata["weight"], ShouldResemble, "111")
  155. })
  156. })
  157. }
  158. func TestMultiZone(t *testing.T) {
  159. Convey("test multi zone", t, func() {
  160. env.Hostname = "host_1"
  161. d := New(c)
  162. defer d.Close()
  163. ctx := context.TODO()
  164. cancel, err := d.Register(ctx, in)
  165. So(err, ShouldBeNil)
  166. defer cancel()
  167. rs := d.Build(appdID)
  168. ch := rs.Watch()
  169. So(ch, ShouldNotBeNil)
  170. <-ch
  171. ins, ok := rs.Fetch(ctx)
  172. So(ok, ShouldBeTrue)
  173. count := 0
  174. for _, data := range ins {
  175. count += len(data)
  176. }
  177. So(count, ShouldEqual, 1)
  178. env.Hostname = "host_2"
  179. env.Zone = "other_zone"
  180. d2 := New(c)
  181. defer d2.Close()
  182. cancel2, err := d2.Register(ctx, in2)
  183. So(err, ShouldBeNil)
  184. defer func() {
  185. cancel2()
  186. time.Sleep(time.Millisecond * 300)
  187. }()
  188. <-ch
  189. ins, ok = rs.Fetch(ctx)
  190. So(ok, ShouldBeTrue)
  191. count = 0
  192. zoneCount := 0
  193. for _, data := range ins {
  194. zoneCount++
  195. count += len(data)
  196. }
  197. So(count, ShouldEqual, 2)
  198. So(zoneCount, ShouldEqual, 2)
  199. })
  200. }
  201. func TestDiscoveryFailOver(t *testing.T) {
  202. Convey("test failover", t, func() {
  203. var conf = &Config{
  204. Nodes: []string{"127.0.0.1:8080"},
  205. Zone: "sh001",
  206. Env: "pre",
  207. Key: "0c4b8fe3ff35a4b6",
  208. Secret: "b370880d1aca7d3a289b9b9a7f4d6812",
  209. Host: "host_1",
  210. }
  211. for _, a := range []string{":8080"} {
  212. go func(addr string) {
  213. e := blademaster.DefaultServer(nil)
  214. e.GET("/discovery/nodes", func(ctx *blademaster.Context) {
  215. type v struct {
  216. Addr string `json:"addr"`
  217. }
  218. ctx.JSON([]v{{Addr: "127.0.0.1:8080"}}, nil)
  219. })
  220. e.GET("/discovery/polls", func(ctx *blademaster.Context) {
  221. params := ctx.Request.Form
  222. ts := params.Get("latest_timestamp")
  223. if ts == "0" {
  224. ctx.JSON(map[string]appData{
  225. appdID: {LastTs: time.Now().UnixNano(), ZoneInstances: map[string][]*naming.Instance{"zone": {in}}},
  226. "infra.discovery": {LastTs: time.Now().UnixNano(), ZoneInstances: map[string][]*naming.Instance{conf.Zone: {indis}}},
  227. }, nil)
  228. } else {
  229. ctx.JSON(nil, ecode.ServerErr)
  230. }
  231. })
  232. e.Run(addr)
  233. }(a)
  234. }
  235. time.Sleep(time.Millisecond * 30)
  236. d := New(conf)
  237. defer d.Close()
  238. rs := d.Build(appdID)
  239. ch := rs.Watch()
  240. <-ch
  241. ins, _ := rs.Fetch(context.TODO())
  242. count := 0
  243. zoneCount := 0
  244. for _, data := range ins {
  245. zoneCount++
  246. count += len(data)
  247. }
  248. So(count, ShouldEqual, 1)
  249. So(zoneCount, ShouldEqual, 1)
  250. })
  251. }
  252. func TestWatchContinuosly(t *testing.T) {
  253. Convey("test TestWatchContinuosly ", t, func() {
  254. env.Hostname = "host_1"
  255. d := New(c)
  256. defer d.Close()
  257. in1 := *in
  258. in1.AppID = "test.test"
  259. ctx := context.TODO()
  260. cancel, err := d.Register(ctx, &in1)
  261. So(err, ShouldBeNil)
  262. defer cancel()
  263. in2 := *in
  264. in2.AppID = "test.test2"
  265. cancel2, err := d.Register(ctx, &in2)
  266. So(err, ShouldBeNil)
  267. defer cancel2()
  268. in3 := *in
  269. in3.AppID = "test.test3"
  270. cancel3, err := d.Register(ctx, &in3)
  271. So(err, ShouldBeNil)
  272. defer cancel3()
  273. rs := d.Build("test.test")
  274. ch := rs.Watch()
  275. <-ch
  276. ins, ok := rs.Fetch(ctx)
  277. So(ok, ShouldBeTrue)
  278. count := 0
  279. for _, data := range ins {
  280. count += len(data)
  281. }
  282. So(count, ShouldBeGreaterThanOrEqualTo, 1)
  283. time.Sleep(time.Millisecond * 10)
  284. rs = d.Build("test.test2")
  285. ch2 := rs.Watch()
  286. <-ch2
  287. ins, ok = rs.Fetch(ctx)
  288. So(ok, ShouldBeTrue)
  289. count = 0
  290. for _, data := range ins {
  291. count += len(data)
  292. }
  293. So(count, ShouldBeGreaterThanOrEqualTo, 1)
  294. rs = d.Build("test.test3")
  295. ch3 := rs.Watch()
  296. <-ch3
  297. ins, ok = rs.Fetch(ctx)
  298. So(ok, ShouldBeTrue)
  299. count = 0
  300. for _, data := range ins {
  301. count += len(data)
  302. }
  303. So(count, ShouldBeGreaterThanOrEqualTo, 1)
  304. })
  305. }
  306. func TestSameBuilder(t *testing.T) {
  307. Convey("test multi watch", t, func() {
  308. env.Hostname = "host_1"
  309. d := New(c)
  310. defer d.Close()
  311. ctx := context.TODO()
  312. cancel, err := d.Register(ctx, in)
  313. d.Register(ctx, intest2)
  314. defer cancel()
  315. So(err, ShouldBeNil)
  316. // first builder
  317. rs := d.Build(appdID)
  318. ch := rs.Watch()
  319. So(ch, ShouldNotBeNil)
  320. <-ch
  321. _, ok := rs.Fetch(ctx)
  322. So(ok, ShouldBeTrue)
  323. So(err, ShouldBeNil)
  324. var count int
  325. // for _, data := range ins {
  326. // count += len(data)
  327. // }
  328. // So(count, ShouldEqual, 1)
  329. // same appd builder
  330. rs2 := d.Build(appdID)
  331. ch2 := rs2.Watch()
  332. <-ch2
  333. _, ok = rs2.Fetch(ctx)
  334. So(ok, ShouldBeTrue)
  335. So(err, ShouldBeNil)
  336. rs3 := d.Build(appID2)
  337. ch3 := rs3.Watch()
  338. <-ch3
  339. ins, _ := rs3.Fetch(ctx)
  340. So(ok, ShouldBeTrue)
  341. So(err, ShouldBeNil)
  342. count = 0
  343. for _, data := range ins {
  344. count += len(data)
  345. }
  346. So(count, ShouldEqual, 1)
  347. })
  348. }