| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- package boot
- import (
- "context"
- "encoding/json"
- "fmt"
- "github.com/Shopify/sarama"
- "git.shuncheng.lu/bigthing/gocommon/pkg/cache/localcache"
- "git.shuncheng.lu/bigthing/gocommon/pkg/cache/redis"
- "git.shuncheng.lu/bigthing/gocommon/pkg/conf"
- "git.shuncheng.lu/bigthing/gocommon/pkg/database/db"
- "git.shuncheng.lu/bigthing/gocommon/pkg/internal/application"
- "git.shuncheng.lu/bigthing/gocommon/pkg/logger"
- "git.shuncheng.lu/bigthing/gocommon/pkg/mq/kafka"
- "git.shuncheng.lu/bigthing/gocommon/pkg/net/httpclent"
- "git.shuncheng.lu/bigthing/gocommon/pkg/net/nacos"
- "net/http"
- "testing"
- "time"
- )
- func TestBoot(t *testing.T) {
- //TestAllowMultiInit(t)
- //TestInitLogger(t)
- //TestMultiDb(t)
- TestHttp(t)
- //TestInitConfig(t)
- //TestInitNacosServer(t)
- //TestRedis(t)
- //TestNacosRegister(t)
- //TestLocalCache(t)
- //TestInitConfig(t)
- //Init(KafkaProducer, KafkaConsumer)
- //go TestKafkaProducer(t)
- //
- //TestKafkaConsumer(t)
- }
- func BenchmarkLogger(b *testing.B) {
- Init()
- for i := 0; i < b.N; i++ {
- logger.Infof(fmt.Sprintf("hello world %s\n", time.Now().Format("2006-01-02 15:04:05.000")))
- }
- }
- func TestSingle(t *testing.T) {
- go func() {
- <-application.GetSignal()
- fmt.Println("single ........")
- }()
- time.Sleep(time.Second * 1000)
- }
- func TestInitConfig(t *testing.T) {
- Init(Config)
- fmt.Println(conf.GetStringV2("log.access_log"))
- fmt.Println(conf.GetString("log", "access_log"))
- go func() {
- for {
- time.Sleep(time.Second)
- fmt.Println(conf.GetStringV2("test.key1"))
- }
- }()
- time.Sleep(time.Second * 100)
- }
- func TestInitNacosServer(t *testing.T) {
- Init(NacosRegister, NacosDiscover)
- s, e := nacos.GetHost("ebike-factory-api")
- if e != nil {
- t.Fatal(e)
- }
- fmt.Println("配置: ", s)
- }
- func TestInitLogger(t *testing.T) {
- Init()
- ctx := logger.NewTraceIdContext()
- logger.Infof("hello, %s!", "Infof")
- logger.Errorf("hello, %s!", "Errorf")
- logger.Debugf("hello, %s!", "Debugf")
- logger.Warnf("hello, %s!", "Warnf")
- logger.Infoc(ctx, "hello, %s!", "Infof")
- logger.Errorc(ctx, "hello, %s!", "Errorf")
- logger.Debugc(ctx, "hello, %s!", "Debugf")
- logger.Warnc(ctx, "hello, %s!", "Warnf")
- logger.Fatalc(ctx, "hello, %s!", "Fatalf")
- //logger.Fatalf("hello, %s!", "Fatalf")
- }
- func TestDb(t *testing.T) {
- Init(DB)
- for {
- <-time.After(time.Second)
- strings, err := db.NewSlaveSession(context.Background()).SQL("select * from peccancy_template_config").QueryString()
- if err != nil {
- t.Fatal(err)
- }
- fmt.Println(strings)
- }
- }
- func TestNacosRegister(t *testing.T) {
- Init(NacosRegister)
- }
- func TestNacosDiscover(t *testing.T) {
- Init(NacosDiscover)
- host, err := nacos.GetHost("ebike-factory-api")
- if err != nil {
- t.Fatal(err)
- }
- fmt.Println(host)
- }
- func TestLocalCache(t *testing.T) {
- Init(LocalCache)
- localcache.Register("demo", func(context context.Context, cache localcache.Cache) {
- fmt.Println("refresh ............")
- cache.Put("k1", time.Now())
- })
- for {
- <-time.After(time.Second * 5)
- fmt.Println(localcache.Get("demo", "k1"))
- }
- }
- func TestKafkaProducer(t *testing.T) {
- type BusinessEventMsg struct {
- Data struct {
- BikeSn string `json:"bike_sn"`
- }
- LbsServerTs int64 `json:"lbs_server_ts"`
- Event string `json:"event"`
- Ts int64 `json:"ts"`
- }
- for {
- <-time.After(time.Second)
- msg := BusinessEventMsg{}
- msg.Data.BikeSn = "804615442"
- msg.Ts = time.Now().Unix()
- msg.Event = "bike_in_use"
- bytes, err := json.Marshal(msg)
- if err != nil {
- t.Fatal(err)
- }
- err = kafka.GetKafkaProducer().ProducerMsg(application.NewContext(), "business_event_dev", string(bytes))
- if err != nil {
- t.Fatal(err)
- }
- }
- }
- func TestKafkaConsumer(t *testing.T) {
- err := kafka.SetConsumerHandler(func(ctx context.Context, msg *sarama.ConsumerMessage) error {
- fmt.Printf("value: %s, key: %s, Offset:%d, Partition:%d\n", msg.Value, msg.Key, msg.Offset, msg.Partition)
- return nil
- })
- if err != nil {
- t.Fatal(err)
- }
- time.Sleep(time.Second * 10000)
- }
- func TestRedis(t *testing.T) {
- Init(Redis)
- s, e := redis.Instance().GetString("k1")
- if !redis.CheckIsNilErr(e) {
- fmt.Println("k1 不为空, 值为: ", s)
- }
- if redis.CheckIsNilErr(e) {
- fmt.Println("k1 为空")
- }
- {
- s, e := redis.Instance().GetString("k2")
- if !redis.CheckIsNilErr(e) {
- fmt.Println("k2 不为空, 值为: ", s)
- }
- if redis.CheckIsNilErr(e) {
- fmt.Println("k2 为空")
- }
- }
- }
- func TestAllowMultiInit(t *testing.T) {
- Init()
- Init(DB, Redis, NacosDiscover, NacosRegister)
- Start()
- Start()
- }
- func TestMultiDb(t *testing.T) {
- Init(DB)
- //conn, err := db.GetDb("mysql")/**/
- //if err != nil {
- // panic(err)
- //}
- strings, err := db.NewSlaveSession(context.Background()).SQL("select * from peccancy_template_config").QueryString()
- if err != nil {
- panic(err)
- }
- fmt.Println(strings)
- }
- func TestHttp(t *testing.T) {
- //{
- // Init(Qconf, Trace)
- // resp := httpclent.RequestParams{}
- // err := httpclent.HttpRequestAndDecode(context.Background(), "city-config", "/city-config/city/info", httpclent.RequestParams{
- // "city_id": 1,
- // }, &resp)
- // if err != nil {
- // t.Fatal(err)
- // }
- // fmt.Println(resp)
- //}
- {
- Init(NacosDiscover)
- resp := httpclent.RequestParams{}
- err := httpclent.HttpRequestAndDecode(context.Background(), "ebike-factory-api", "/service/v1/op-worker/search-by-ids", httpclent.RequestParams{
- "ids": []uint64{10010},
- }, &resp, httpclent.NacosServerHostOption, func(options *httpclent.Options) {
- options.AddRequestCookies = func(ctx context.Context, requestCookie []*http.Cookie) []*http.Cookie {
- return nil
- }
- })
- if err != nil {
- t.Fatal(err)
- }
- fmt.Println(resp)
- }
- }
|