github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/server/test/server_test.go (about)

     1  package test
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/isyscore/isc-gobase/cron"
     6  	"github.com/isyscore/isc-gobase/isc"
     7  	"github.com/isyscore/isc-gobase/listener"
     8  	"github.com/isyscore/isc-gobase/server/rsp"
     9  	"github.com/isyscore/isc-gobase/server/test/pojo"
    10  	"os"
    11  	"testing"
    12  
    13  	"github.com/gin-gonic/gin"
    14  	"github.com/isyscore/isc-gobase/logger"
    15  	"github.com/isyscore/isc-gobase/server"
    16  )
    17  
    18  func TestServer(t *testing.T) {
    19  	server.RegisterCustomHealthCheck("/api/sample",
    20  		func() string {
    21  			return "OK"
    22  		},
    23  		func() string {
    24  			return "OK"
    25  		},
    26  		func() string {
    27  			return "OK"
    28  		},
    29  	)
    30  
    31  	logger.Info("server started")
    32  	//panic("和水水水水")
    33  	////log.Fatal("")
    34  	go func() {
    35  		for i := 0; i < 100; i++ {
    36  			//panic("打我吗?")
    37  			go func(idx int) {
    38  				c_s := cron.New()
    39  				_ = c_s.AddFunc("*/1 * * * * ?", func() {
    40  					_, _ = fmt.Fprintf(os.Stderr, "我好帅,%s\n", "哈哈哈")
    41  					_, _ = fmt.Fprintf(os.Stdout, "是真的\n")
    42  					//logger.Debug("协程ID=:%d,我是库陈胜Debug", idx)
    43  					logger.Info("协程ID=:%d,我是库陈胜Info", idx)
    44  					//logger.Warn("协程ID=:%d,我是库陈胜Warn", idx)
    45  					//logger.Error("协程ID=:%d,我是库陈胜Error", idx)
    46  					//logger.Panic("我可以写入了吗?")
    47  					//logger.Fatal("我是fatal")
    48  					//panic("打我吗?")
    49  				})
    50  				c_s.Start()
    51  			}(i)
    52  		}
    53  	}()
    54  
    55  	server.StartServer()
    56  }
    57  
    58  func TestApiVersion(t *testing.T) {
    59  	fmt.Printf("step 1\n")
    60  	server.RegisterRouteWithHeaders("/api/sample", server.HmGet, []string{"isc-api-version"}, []string{"1.0"}, func(c *gin.Context) {
    61  		c.Data(200, "text/plain", []byte("hello 1.0"))
    62  	})
    63  	fmt.Printf("step 2\n")
    64  	server.RegisterRouteWithHeaders("/api/sample", server.HmGet, []string{"isc-api-version"}, []string{"2.0"}, func(c *gin.Context) {
    65  		c.Data(200, "text/plain", []byte("hello 2.0"))
    66  	})
    67  	fmt.Printf("step 3\n")
    68  	server.RegisterRouteWithHeaders("/api/sample", server.HmGet, []string{"isc-api-version"}, []string{"3.0"}, func(c *gin.Context) {
    69  		c.Data(200, "text/plain", []byte("hello 3.0"))
    70  	})
    71  	server.StartServer()
    72  }
    73  
    74  func TestErrorPrint(t *testing.T) {
    75  	server.RegisterRoute("/api/data", server.HmGet, func(c *gin.Context) {
    76  		c.Data(200, "text/plain", []byte("hello 3.0"))
    77  	})
    78  	server.StartServer()
    79  }
    80  
    81  func TestServerGet(t *testing.T) {
    82  	server.Get("/info", func(c *gin.Context) {
    83  		logger.Debug("debug的日志")
    84  		logger.Info("info的日志")
    85  		logger.Warn("warn的日志")
    86  		logger.Error("error的日志")
    87  		c.Data(200, "text/plain", []byte("hello"))
    88  	})
    89  
    90  	// 测试事件监听机制
    91  	listener.AddListener(listener.EventOfServerRunFinish, func(event listener.BaseEvent) {
    92  		logger.Info("应用启动完成")
    93  	})
    94  
    95  	server.StartServer()
    96  }
    97  
    98  func TestServer2(t *testing.T) {
    99  	server.Get("/test/req1", func(c *gin.Context) {
   100  		c.Data(200, "text/plain", []byte("hello"))
   101  	})
   102  
   103  	server.Get("/test/req2", func(c *gin.Context) {
   104  		rsp.SuccessOfStandard(c, "value")
   105  	})
   106  
   107  	server.Get("/test/req3/:key", func(c *gin.Context) {
   108  		rsp.SuccessOfStandard(c, c.Param("key"))
   109  	})
   110  
   111  	server.Post("/test/rsp1", func(c *gin.Context) {
   112  		testReq := pojo.TestReq{}
   113  		_ = isc.DataToObject(c.Request.Body, &testReq)
   114  		rsp.SuccessOfStandard(c, testReq)
   115  	})
   116  
   117  	server.Get("/test/err", func(c *gin.Context) {
   118  		rsp.FailedOfStandard(c, 500, "异常")
   119  	})
   120  
   121  	server.Run()
   122  }
   123  
   124  func init() {
   125  	// 添加服务器启动完成事件监听
   126  	listener.AddListener(listener.EventOfServerRunFinish, func(event listener.BaseEvent) {
   127  		logger.Info("应用启动完成")
   128  	})
   129  
   130  	// 添加服务器启动完成事件监听
   131  	listener.AddListener(listener.EventOfServerStop, func(event listener.BaseEvent) {
   132  		logger.Info("应用退出")
   133  	})
   134  }
   135  
   136  func TestServerOnProfileIsPprof(t *testing.T) {
   137  	server.Use(ApiVersionInterceptor())
   138  	server.Get("data", func(c *gin.Context) {
   139  		rsp.SuccessOfStandard(c, "data")
   140  	})
   141  
   142  	server.Run()
   143  }
   144  
   145  func ApiVersionInterceptor() gin.HandlerFunc {
   146  	return func(c *gin.Context) {
   147  		c.Next()
   148  	}
   149  }