dao_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package dao
  2. import (
  3. "context"
  4. "flag"
  5. "log"
  6. "math/rand"
  7. "os"
  8. "strconv"
  9. "testing"
  10. "time"
  11. . "github.com/smartystreets/goconvey/convey"
  12. "go-common/app/service/main/dapper/conf"
  13. "go-common/app/service/main/dapper/model"
  14. )
  15. func init() {
  16. rand.Seed(time.Now().UnixNano())
  17. }
  18. var cfg *conf.Config
  19. var flagMap = map[string]string{
  20. "app_id": "main.common-arch.dapper-service",
  21. "conf_token": "528dd7e00bb411e894c14a552f48fef8",
  22. "tree_id": "5172",
  23. "conf_version": "server-1",
  24. "deploy_env": "uat",
  25. "conf_host": "config.bilibili.co",
  26. "conf_path": os.TempDir(),
  27. "region": "sh",
  28. "zone": "sh001",
  29. }
  30. func TestMain(m *testing.M) {
  31. for key, val := range flagMap {
  32. flag.Set(key, val)
  33. }
  34. flag.Parse()
  35. if err := conf.Init(); err != nil {
  36. log.Printf("init config from remote error: %s", err)
  37. }
  38. cfg = conf.Conf
  39. if cfg.InfluxDB != nil {
  40. cfg.InfluxDB.Database = "dapper_ut"
  41. }
  42. if cfg.HBase != nil {
  43. cfg.HBase.Namespace = "dapperut"
  44. }
  45. if hbaseAddrs := os.Getenv("TEST_HBASE_ADDRS"); hbaseAddrs != "" {
  46. cfg.HBase = &conf.HBaseConfig{Addrs: hbaseAddrs, Namespace: "dapperut"}
  47. if influxdbAddr := os.Getenv("TEST_INFLUXDB_ADDR"); influxdbAddr != "" {
  48. cfg.InfluxDB = &conf.InfluxDBConfig{Addr: influxdbAddr, Database: "dapper_ut"}
  49. }
  50. }
  51. os.Exit(m.Run())
  52. }
  53. func TestDao(t *testing.T) {
  54. if cfg == nil {
  55. t.Skipf("no config provide skipped")
  56. }
  57. daoImpl, err := New(cfg)
  58. if err != nil {
  59. t.Fatalf("new dao error: %s", err)
  60. }
  61. ctx := context.Background()
  62. Convey("test fetch serviceName and operationName", t, func() {
  63. serviceNames, err := daoImpl.FetchServiceName(ctx)
  64. So(err, ShouldBeNil)
  65. So(serviceNames, ShouldNotBeEmpty)
  66. for _, serviceName := range serviceNames {
  67. operationNames, err := daoImpl.FetchOperationName(ctx, serviceName)
  68. So(err, ShouldBeNil)
  69. t.Logf("%s operationNames: %v", serviceName, operationNames)
  70. }
  71. })
  72. Convey("test write rawtrace", t, func() {
  73. if err := daoImpl.WriteRawTrace(
  74. context.Background(),
  75. strconv.FormatUint(rand.Uint64(), 16),
  76. map[string][]byte{strconv.FormatUint(rand.Uint64(), 16): []byte("hello world")},
  77. ); err != nil {
  78. t.Error(err)
  79. }
  80. })
  81. Convey("test batchwrite span point", t, func() {
  82. points := []*model.SpanPoint{
  83. &model.SpanPoint{
  84. ServiceName: "service_a",
  85. OperationName: "opt1",
  86. PeerService: "peer_service_a",
  87. SpanKind: "client",
  88. Timestamp: time.Now().Unix() - rand.Int63n(3600),
  89. MaxDuration: model.SamplePoint{
  90. SpanID: rand.Uint64(),
  91. TraceID: rand.Uint64(),
  92. Value: rand.Int63n(1024),
  93. },
  94. MinDuration: model.SamplePoint{
  95. SpanID: rand.Uint64(),
  96. TraceID: rand.Uint64(),
  97. Value: rand.Int63n(1024),
  98. },
  99. AvgDuration: model.SamplePoint{
  100. SpanID: rand.Uint64(),
  101. TraceID: rand.Uint64(),
  102. Value: rand.Int63n(1024),
  103. },
  104. Errors: []model.SamplePoint{
  105. model.SamplePoint{
  106. SpanID: rand.Uint64(),
  107. TraceID: rand.Uint64(),
  108. Value: 1,
  109. },
  110. model.SamplePoint{
  111. SpanID: rand.Uint64(),
  112. TraceID: rand.Uint64(),
  113. Value: 1,
  114. },
  115. },
  116. },
  117. &model.SpanPoint{
  118. ServiceName: "service_b",
  119. OperationName: "opt2",
  120. PeerService: "peer_service_b",
  121. SpanKind: "server",
  122. Timestamp: time.Now().Unix() - rand.Int63n(3600),
  123. },
  124. &model.SpanPoint{
  125. ServiceName: "service_c",
  126. OperationName: "opt3",
  127. PeerService: "peer_service_c",
  128. SpanKind: "client",
  129. Timestamp: time.Now().Unix() - rand.Int63n(3600),
  130. },
  131. }
  132. err := daoImpl.BatchWriteSpanPoint(context.Background(), points)
  133. if err != nil {
  134. t.Error(err)
  135. }
  136. })
  137. }