discovery_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package discovery
  2. import (
  3. "context"
  4. "os"
  5. "testing"
  6. "go-common/app/service/main/bns/agent/backend"
  7. "go-common/library/log"
  8. )
  9. func init() {
  10. log.Init(&log.Config{
  11. Stdout: true,
  12. })
  13. }
  14. var (
  15. // test discovery
  16. testURL = "http://api.bilibili.co"
  17. testSecret = "b370880d1aca7d3a289b9b9a7f4d6812"
  18. testAppKey = "0c4b8fe3ff35a4b6"
  19. // test app
  20. testAppID = "middleware.databus"
  21. testApplicationEnv = "uat"
  22. testZone = "sh001"
  23. testRegion = "sh"
  24. )
  25. var dis *discovery
  26. func TestMain(m *testing.M) {
  27. config := map[string]interface{}{
  28. "url": testURL,
  29. "secret": testSecret,
  30. "appKey": testAppKey,
  31. }
  32. backend, err := New(config)
  33. if err != nil {
  34. log.Error("new discovery error %s", err)
  35. os.Exit(1)
  36. }
  37. dis = backend.(*discovery)
  38. os.Exit(m.Run())
  39. }
  40. func TestNodes(t *testing.T) {
  41. ctx := context.Background()
  42. nodes, err := dis.Nodes(ctx)
  43. if err != nil {
  44. t.Fatal(err)
  45. }
  46. t.Logf("%v", nodes)
  47. }
  48. func TestQuery(t *testing.T) {
  49. ctx := context.Background()
  50. appID, sel, err := backend.ParseName(testAppID, backend.Selector{Env: testApplicationEnv, Region: testRegion, Zone: testZone})
  51. if err != nil {
  52. t.Fatal(err)
  53. }
  54. instances, err := dis.Query(ctx, appID, sel, backend.Metadata{
  55. ClientHost: "locahost",
  56. })
  57. if err != nil {
  58. t.Fatal(err)
  59. }
  60. t.Logf("%v", instances)
  61. }
  62. func BenchmarkQuery(b *testing.B) {
  63. ctx := context.Background()
  64. appID, sel, err := backend.ParseName(testAppID, backend.Selector{Env: testApplicationEnv, Region: testRegion, Zone: testZone})
  65. if err != nil {
  66. b.Fatal(err)
  67. }
  68. for i := 0; i < b.N; i++ {
  69. data, err := dis.Query(ctx, appID, sel, backend.Metadata{ClientHost: "locahost"})
  70. if err != nil {
  71. b.Error(err)
  72. }
  73. if len(data) == 0 {
  74. b.Error("not data found")
  75. }
  76. }
  77. }