infoc_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package infoc
  2. import (
  3. "bytes"
  4. "context"
  5. "os"
  6. "sync"
  7. "testing"
  8. "time"
  9. "go-common/library/net/metadata"
  10. )
  11. var (
  12. once sync.Once
  13. i1 *Infoc
  14. )
  15. func TestMain(m *testing.M) {
  16. once.Do(createInfoc)
  17. defer i1.Close()
  18. os.Exit(m.Run())
  19. }
  20. func createInfoc() {
  21. i1 = New(&Config{
  22. TaskID: "000146",
  23. Addr: "172.16.0.204:514",
  24. Proto: "tcp",
  25. ChanSize: 1,
  26. })
  27. }
  28. func Test_Infoc(b *testing.T) {
  29. err := i1.Info("infoc-test", "ip", "mid", 222)
  30. time.Sleep(2 * time.Second)
  31. if err != nil {
  32. b.Fatalf("err %+v", err)
  33. }
  34. }
  35. func Test_Infocv(b *testing.T) {
  36. i1.Infov(context.Background(), "infoc-test", "ip", "mid", 222)
  37. ctx := metadata.NewContext(context.Background(), metadata.MD{metadata.Mirror: true})
  38. i1.Infov(ctx, "infoc-test", "ip", "mid", 222)
  39. ctx = metadata.NewContext(context.Background(), metadata.MD{metadata.Mirror: "1"})
  40. err := i1.Infov(ctx, "infoc-test", "ip", "mid", 222)
  41. time.Sleep(2 * time.Second)
  42. if err != nil {
  43. b.Fatalf("err %+v", err)
  44. }
  45. var args []interface{}
  46. args = append(args, "infoc-test")
  47. args = append(args, "ip")
  48. args = append(args, "mid")
  49. args = append(args, 222)
  50. err1, buf1 := i1.info(args...)
  51. err2, buf2 := i1.info(args)
  52. if bytes.Equal(buf1.Bytes(), buf2.Bytes()) {
  53. b.Fatalf("err %+v,%+v,%+v,%+v", err1, err2, buf1.Bytes(), buf2.Bytes())
  54. }
  55. args = append([]interface{}{})
  56. args = append(args, "infoc-test")
  57. err1, buf1 = i1.info(args...)
  58. err2, buf2 = i1.info(args)
  59. if bytes.Equal(buf1.Bytes(), buf2.Bytes()) {
  60. b.Fatalf("err %+v,%+v,%+v,%+v", err1, err2, buf1.Bytes(), buf2.Bytes())
  61. }
  62. }
  63. func BenchmarkInfoc(b *testing.B) {
  64. once.Do(createInfoc)
  65. b.RunParallel(func(pb *testing.PB) {
  66. var f float32 = 3.55051
  67. var i8 int8 = 2
  68. var u8 uint8 = 2
  69. for pb.Next() {
  70. i1.Info("infoc-test", "ip", "mid", i8, u8, f)
  71. }
  72. })
  73. }