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) } }