pointwrite_test.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package pointwrite
  2. import (
  3. "context"
  4. "testing"
  5. "time"
  6. "go-common/app/service/main/dapper/model"
  7. )
  8. func TestPointWrite(t *testing.T) {
  9. var data []*model.SpanPoint
  10. mockFn := func(ctx context.Context, points []*model.SpanPoint) error {
  11. data = append(data, points...)
  12. return nil
  13. }
  14. pw := &pointwriter{
  15. fn: mockFn,
  16. current: make(map[string]*model.SpanPoint),
  17. precision: 5,
  18. timeout: time.Second,
  19. tk: time.NewTicker(time.Second * time.Duration(5)),
  20. }
  21. spans := []*model.Span{
  22. &model.Span{
  23. ServiceName: "test1",
  24. StartTime: time.Unix(100, 0),
  25. },
  26. &model.Span{
  27. ServiceName: "test1",
  28. StartTime: time.Unix(110, 0),
  29. },
  30. }
  31. for _, span := range spans {
  32. if err := pw.WriteSpan(span); err != nil {
  33. t.Error(err)
  34. }
  35. }
  36. if len(pw.current) != 2 {
  37. t.Errorf("expect 2 point get %d", len(pw.current))
  38. }
  39. pw.flush()
  40. if len(data) != 2 {
  41. t.Errorf("expect 2 point get %d", len(data))
  42. }
  43. }
  44. func TestPointWriteFlush(t *testing.T) {
  45. var data []*model.SpanPoint
  46. wait := make(chan bool, 1)
  47. mockFn := func(ctx context.Context, points []*model.SpanPoint) error {
  48. data = append(data, points...)
  49. wait <- true
  50. return nil
  51. }
  52. pw := New(mockFn, 1, time.Second)
  53. spans := []*model.Span{
  54. &model.Span{
  55. ServiceName: "test1",
  56. StartTime: time.Unix(100, 0),
  57. },
  58. &model.Span{
  59. ServiceName: "test1",
  60. StartTime: time.Unix(110, 0),
  61. },
  62. }
  63. for _, span := range spans {
  64. if err := pw.WriteSpan(span); err != nil {
  65. t.Error(err)
  66. }
  67. }
  68. <-wait
  69. if len(data) != 2 {
  70. t.Errorf("expect 2 point get %d", len(data))
  71. }
  72. }