boot_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. package boot
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/Shopify/sarama"
  7. "git.shuncheng.lu/bigthing/gocommon/pkg/cache/localcache"
  8. "git.shuncheng.lu/bigthing/gocommon/pkg/cache/redis"
  9. "git.shuncheng.lu/bigthing/gocommon/pkg/conf"
  10. "git.shuncheng.lu/bigthing/gocommon/pkg/database/db"
  11. "git.shuncheng.lu/bigthing/gocommon/pkg/internal/application"
  12. "git.shuncheng.lu/bigthing/gocommon/pkg/logger"
  13. "git.shuncheng.lu/bigthing/gocommon/pkg/mq/kafka"
  14. "git.shuncheng.lu/bigthing/gocommon/pkg/net/httpclent"
  15. "git.shuncheng.lu/bigthing/gocommon/pkg/net/nacos"
  16. "net/http"
  17. "testing"
  18. "time"
  19. )
  20. func TestBoot(t *testing.T) {
  21. //TestAllowMultiInit(t)
  22. //TestInitLogger(t)
  23. //TestMultiDb(t)
  24. TestHttp(t)
  25. //TestInitConfig(t)
  26. //TestInitNacosServer(t)
  27. //TestRedis(t)
  28. //TestNacosRegister(t)
  29. //TestLocalCache(t)
  30. //TestInitConfig(t)
  31. //Init(KafkaProducer, KafkaConsumer)
  32. //go TestKafkaProducer(t)
  33. //
  34. //TestKafkaConsumer(t)
  35. }
  36. func BenchmarkLogger(b *testing.B) {
  37. Init()
  38. for i := 0; i < b.N; i++ {
  39. logger.Infof(fmt.Sprintf("hello world %s\n", time.Now().Format("2006-01-02 15:04:05.000")))
  40. }
  41. }
  42. func TestSingle(t *testing.T) {
  43. go func() {
  44. <-application.GetSignal()
  45. fmt.Println("single ........")
  46. }()
  47. time.Sleep(time.Second * 1000)
  48. }
  49. func TestInitConfig(t *testing.T) {
  50. Init(Config)
  51. fmt.Println(conf.GetStringV2("log.access_log"))
  52. fmt.Println(conf.GetString("log", "access_log"))
  53. go func() {
  54. for {
  55. time.Sleep(time.Second)
  56. fmt.Println(conf.GetStringV2("test.key1"))
  57. }
  58. }()
  59. time.Sleep(time.Second * 100)
  60. }
  61. func TestInitNacosServer(t *testing.T) {
  62. Init(NacosRegister, NacosDiscover)
  63. s, e := nacos.GetHost("ebike-factory-api")
  64. if e != nil {
  65. t.Fatal(e)
  66. }
  67. fmt.Println("配置: ", s)
  68. }
  69. func TestInitLogger(t *testing.T) {
  70. Init()
  71. ctx := logger.NewTraceIdContext()
  72. logger.Infof("hello, %s!", "Infof")
  73. logger.Errorf("hello, %s!", "Errorf")
  74. logger.Debugf("hello, %s!", "Debugf")
  75. logger.Warnf("hello, %s!", "Warnf")
  76. logger.Infoc(ctx, "hello, %s!", "Infof")
  77. logger.Errorc(ctx, "hello, %s!", "Errorf")
  78. logger.Debugc(ctx, "hello, %s!", "Debugf")
  79. logger.Warnc(ctx, "hello, %s!", "Warnf")
  80. logger.Fatalc(ctx, "hello, %s!", "Fatalf")
  81. //logger.Fatalf("hello, %s!", "Fatalf")
  82. }
  83. func TestDb(t *testing.T) {
  84. Init(DB)
  85. for {
  86. <-time.After(time.Second)
  87. strings, err := db.NewSlaveSession(context.Background()).SQL("select * from peccancy_template_config").QueryString()
  88. if err != nil {
  89. t.Fatal(err)
  90. }
  91. fmt.Println(strings)
  92. }
  93. }
  94. func TestNacosRegister(t *testing.T) {
  95. Init(NacosRegister)
  96. }
  97. func TestNacosDiscover(t *testing.T) {
  98. Init(NacosDiscover)
  99. host, err := nacos.GetHost("ebike-factory-api")
  100. if err != nil {
  101. t.Fatal(err)
  102. }
  103. fmt.Println(host)
  104. }
  105. func TestLocalCache(t *testing.T) {
  106. Init(LocalCache)
  107. localcache.Register("demo", func(context context.Context, cache localcache.Cache) {
  108. fmt.Println("refresh ............")
  109. cache.Put("k1", time.Now())
  110. })
  111. for {
  112. <-time.After(time.Second * 5)
  113. fmt.Println(localcache.Get("demo", "k1"))
  114. }
  115. }
  116. func TestKafkaProducer(t *testing.T) {
  117. type BusinessEventMsg struct {
  118. Data struct {
  119. BikeSn string `json:"bike_sn"`
  120. }
  121. LbsServerTs int64 `json:"lbs_server_ts"`
  122. Event string `json:"event"`
  123. Ts int64 `json:"ts"`
  124. }
  125. for {
  126. <-time.After(time.Second)
  127. msg := BusinessEventMsg{}
  128. msg.Data.BikeSn = "804615442"
  129. msg.Ts = time.Now().Unix()
  130. msg.Event = "bike_in_use"
  131. bytes, err := json.Marshal(msg)
  132. if err != nil {
  133. t.Fatal(err)
  134. }
  135. err = kafka.GetKafkaProducer().ProducerMsg(application.NewContext(), "business_event_dev", string(bytes))
  136. if err != nil {
  137. t.Fatal(err)
  138. }
  139. }
  140. }
  141. func TestKafkaConsumer(t *testing.T) {
  142. err := kafka.SetConsumerHandler(func(ctx context.Context, msg *sarama.ConsumerMessage) error {
  143. fmt.Printf("value: %s, key: %s, Offset:%d, Partition:%d\n", msg.Value, msg.Key, msg.Offset, msg.Partition)
  144. return nil
  145. })
  146. if err != nil {
  147. t.Fatal(err)
  148. }
  149. time.Sleep(time.Second * 10000)
  150. }
  151. func TestRedis(t *testing.T) {
  152. Init(Redis)
  153. s, e := redis.Instance().GetString("k1")
  154. if !redis.CheckIsNilErr(e) {
  155. fmt.Println("k1 不为空, 值为: ", s)
  156. }
  157. if redis.CheckIsNilErr(e) {
  158. fmt.Println("k1 为空")
  159. }
  160. {
  161. s, e := redis.Instance().GetString("k2")
  162. if !redis.CheckIsNilErr(e) {
  163. fmt.Println("k2 不为空, 值为: ", s)
  164. }
  165. if redis.CheckIsNilErr(e) {
  166. fmt.Println("k2 为空")
  167. }
  168. }
  169. }
  170. func TestAllowMultiInit(t *testing.T) {
  171. Init()
  172. Init(DB, Redis, NacosDiscover, NacosRegister)
  173. Start()
  174. Start()
  175. }
  176. func TestMultiDb(t *testing.T) {
  177. Init(DB)
  178. //conn, err := db.GetDb("mysql")/**/
  179. //if err != nil {
  180. // panic(err)
  181. //}
  182. strings, err := db.NewSlaveSession(context.Background()).SQL("select * from peccancy_template_config").QueryString()
  183. if err != nil {
  184. panic(err)
  185. }
  186. fmt.Println(strings)
  187. }
  188. func TestHttp(t *testing.T) {
  189. //{
  190. // Init(Qconf, Trace)
  191. // resp := httpclent.RequestParams{}
  192. // err := httpclent.HttpRequestAndDecode(context.Background(), "city-config", "/city-config/city/info", httpclent.RequestParams{
  193. // "city_id": 1,
  194. // }, &resp)
  195. // if err != nil {
  196. // t.Fatal(err)
  197. // }
  198. // fmt.Println(resp)
  199. //}
  200. {
  201. Init(NacosDiscover)
  202. resp := httpclent.RequestParams{}
  203. err := httpclent.HttpRequestAndDecode(context.Background(), "ebike-factory-api", "/service/v1/op-worker/search-by-ids", httpclent.RequestParams{
  204. "ids": []uint64{10010},
  205. }, &resp, httpclent.NacosServerHostOption, func(options *httpclent.Options) {
  206. options.AddRequestCookies = func(ctx context.Context, requestCookie []*http.Cookie) []*http.Cookie {
  207. return nil
  208. }
  209. })
  210. if err != nil {
  211. t.Fatal(err)
  212. }
  213. fmt.Println(resp)
  214. }
  215. }