logging_test.go 993 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package warden
  2. import (
  3. "context"
  4. "reflect"
  5. "testing"
  6. "time"
  7. "go-common/library/log"
  8. )
  9. func Test_logFn(t *testing.T) {
  10. type args struct {
  11. code int
  12. dt time.Duration
  13. }
  14. tests := []struct {
  15. name string
  16. args args
  17. want func(context.Context, ...log.D)
  18. }{
  19. {
  20. name: "ok",
  21. args: args{code: 0, dt: time.Millisecond},
  22. want: log.Infov,
  23. },
  24. {
  25. name: "slowlog",
  26. args: args{code: 0, dt: time.Second},
  27. want: log.Warnv,
  28. },
  29. {
  30. name: "business error",
  31. args: args{code: 2233, dt: time.Millisecond},
  32. want: log.Warnv,
  33. },
  34. {
  35. name: "system error",
  36. args: args{code: -1, dt: 0},
  37. want: log.Errorv,
  38. },
  39. {
  40. name: "system error and slowlog",
  41. args: args{code: -1, dt: time.Second},
  42. want: log.Errorv,
  43. },
  44. }
  45. for _, tt := range tests {
  46. t.Run(tt.name, func(t *testing.T) {
  47. if got := logFn(tt.args.code, tt.args.dt); reflect.ValueOf(got).Pointer() != reflect.ValueOf(tt.want).Pointer() {
  48. t.Errorf("unexpect log function!")
  49. }
  50. })
  51. }
  52. }