gin_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package common_test
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "testing"
  6. "time"
  7. "github.com/gin-gonic/gin"
  8. "github.com/labring/aiproxy/core/common"
  9. "github.com/smartystreets/goconvey/convey"
  10. )
  11. func TestGetLogFields(t *testing.T) {
  12. convey.Convey("GetLogFields", t, func() {
  13. fields := common.GetLogFields()
  14. convey.So(fields, convey.ShouldNotBeNil)
  15. convey.So(len(fields), convey.ShouldEqual, 0)
  16. fields["test"] = "value"
  17. common.PutLogFields(fields)
  18. // Should get a cleared map (or at least we should be able to reuse it)
  19. fields2 := common.GetLogFields()
  20. convey.So(fields2, convey.ShouldNotBeNil)
  21. convey.So(len(fields2), convey.ShouldEqual, 0)
  22. })
  23. }
  24. func TestLogger(t *testing.T) {
  25. convey.Convey("Logger Context", t, func() {
  26. convey.Convey("GetLoggerFromReq should create new logger if missing", func() {
  27. req := httptest.NewRequest(http.MethodGet, "/", nil)
  28. logger := common.GetLoggerFromReq(req)
  29. convey.So(logger, convey.ShouldNotBeNil)
  30. convey.So(logger.Data, convey.ShouldNotBeNil)
  31. })
  32. convey.Convey("SetLogger should store logger in context", func() {
  33. req := httptest.NewRequest(http.MethodGet, "/", nil)
  34. entry := common.NewLogger()
  35. common.SetLogger(req, entry)
  36. logger := common.GetLoggerFromReq(req)
  37. convey.So(logger, convey.ShouldEqual, entry)
  38. })
  39. convey.Convey("GetLogger should work with Gin context", func() {
  40. w := httptest.NewRecorder()
  41. c, _ := gin.CreateTestContext(w)
  42. c.Request = httptest.NewRequest(http.MethodGet, "/", nil)
  43. logger := common.GetLogger(c)
  44. convey.So(logger, convey.ShouldNotBeNil)
  45. })
  46. })
  47. }
  48. func TestTruncateDuration(t *testing.T) {
  49. convey.Convey("TruncateDuration", t, func() {
  50. convey.Convey("should truncate > 1h to Minute", func() {
  51. d := time.Hour + 30*time.Minute + 30*time.Second
  52. res := common.TruncateDuration(d)
  53. convey.So(res, convey.ShouldEqual, time.Hour+30*time.Minute)
  54. })
  55. convey.Convey("should truncate > 1m to Second", func() {
  56. d := time.Minute + 30*time.Second + 500*time.Millisecond
  57. res := common.TruncateDuration(d)
  58. convey.So(res, convey.ShouldEqual, time.Minute+30*time.Second)
  59. })
  60. convey.Convey("should truncate > 1s to Millisecond", func() {
  61. d := time.Second + 500*time.Millisecond + 500*time.Microsecond
  62. res := common.TruncateDuration(d)
  63. convey.So(res, convey.ShouldEqual, time.Second+500*time.Millisecond)
  64. })
  65. convey.Convey("should truncate > 1ms to Microsecond", func() {
  66. d := time.Millisecond + 500*time.Microsecond + 500*time.Nanosecond
  67. res := common.TruncateDuration(d)
  68. convey.So(res, convey.ShouldEqual, time.Millisecond+500*time.Microsecond)
  69. })
  70. convey.Convey("should keep small durations", func() {
  71. d := 500 * time.Nanosecond
  72. res := common.TruncateDuration(d)
  73. convey.So(res, convey.ShouldEqual, d)
  74. })
  75. convey.Convey("should handle exact boundaries", func() {
  76. // 1h -> falls through to > 1m check (since 1h is not > 1h)
  77. // returns 1h truncated to Second -> 1h
  78. convey.So(common.TruncateDuration(time.Hour), convey.ShouldEqual, time.Hour)
  79. convey.So(common.TruncateDuration(time.Minute), convey.ShouldEqual, time.Minute)
  80. convey.So(common.TruncateDuration(time.Second), convey.ShouldEqual, time.Second)
  81. convey.So(
  82. common.TruncateDuration(time.Millisecond),
  83. convey.ShouldEqual,
  84. time.Millisecond,
  85. )
  86. })
  87. })
  88. }