collector_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package app_test
  2. import (
  3. "testing"
  4. "time"
  5. "context"
  6. "github.com/weaveworks/common/mtime"
  7. "github.com/weaveworks/common/test"
  8. "github.com/weaveworks/scope/app"
  9. "github.com/weaveworks/scope/report"
  10. "github.com/weaveworks/scope/test/reflect"
  11. )
  12. func TestCollector(t *testing.T) {
  13. ctx := context.Background()
  14. window := 10 * time.Second
  15. c := app.NewCollector(window)
  16. now := time.Now()
  17. mtime.NowForce(now)
  18. defer mtime.NowReset()
  19. r1 := report.MakeReport()
  20. r1.Endpoint.AddNode(report.MakeNode("foo").WithTopology("bar"))
  21. r2 := report.MakeReport()
  22. r2.Endpoint.AddNode(report.MakeNode("foo").WithTopology("bar"))
  23. have, err := c.Report(ctx, mtime.Now())
  24. if err != nil {
  25. t.Error(err)
  26. }
  27. if want := report.MakeReport(); !reflect.DeepEqual(want, have) {
  28. t.Error(test.Diff(want, have))
  29. }
  30. c.Add(ctx, r1, nil)
  31. have, err = c.Report(ctx, mtime.Now())
  32. if err != nil {
  33. t.Error(err)
  34. }
  35. if want := r1; !reflect.DeepEqual(want, have) {
  36. t.Error(test.Diff(want, have))
  37. }
  38. timeBefore := mtime.Now()
  39. mtime.NowForce(now.Add(time.Second))
  40. c.Add(ctx, r2, nil)
  41. merged := report.MakeReport()
  42. merged.UnsafeMerge(r1)
  43. merged.UnsafeMerge(r2)
  44. merged.UnsafeRemovePartMergedNodes(context.Background())
  45. have, err = c.Report(ctx, mtime.Now())
  46. if err != nil {
  47. t.Error(err)
  48. }
  49. if want := merged; !reflect.DeepEqual(want, have) {
  50. t.Error(test.Diff(want, have))
  51. }
  52. // Since the timestamp given is before r2 was added,
  53. // it shouldn't be included in the final report.
  54. have, err = c.Report(ctx, timeBefore)
  55. if err != nil {
  56. t.Error(err)
  57. }
  58. if want := r1; !reflect.DeepEqual(want, have) {
  59. t.Error(test.Diff(want, have))
  60. }
  61. }
  62. func TestCollectorExpire(t *testing.T) {
  63. now := time.Now()
  64. mtime.NowForce(now)
  65. defer mtime.NowReset()
  66. ctx := context.Background()
  67. window := 10 * time.Second
  68. c := app.NewCollector(window)
  69. // 1st check the collector is empty
  70. have, err := c.Report(ctx, mtime.Now())
  71. if err != nil {
  72. t.Error(err)
  73. }
  74. if want := report.MakeReport(); !reflect.DeepEqual(want, have) {
  75. t.Error(test.Diff(want, have))
  76. }
  77. // Now check an added report is returned
  78. r1 := report.MakeReport()
  79. r1.Endpoint.AddNode(report.MakeNode("foo"))
  80. c.Add(ctx, r1, nil)
  81. have, err = c.Report(ctx, mtime.Now())
  82. if err != nil {
  83. t.Error(err)
  84. }
  85. if want := r1; !reflect.DeepEqual(want, have) {
  86. t.Error(test.Diff(want, have))
  87. }
  88. // Finally move time forward to expire the report
  89. mtime.NowForce(now.Add(window))
  90. have, err = c.Report(ctx, mtime.Now())
  91. if err != nil {
  92. t.Error(err)
  93. }
  94. if want := report.MakeReport(); !reflect.DeepEqual(want, have) {
  95. t.Error(test.Diff(want, have))
  96. }
  97. }
  98. func TestCollectorWait(t *testing.T) {
  99. ctx := context.Background()
  100. window := time.Millisecond
  101. c := app.NewCollector(window)
  102. waiter := make(chan struct{}, 1)
  103. c.WaitOn(ctx, waiter)
  104. defer c.UnWait(ctx, waiter)
  105. c.(interface {
  106. Broadcast()
  107. }).Broadcast()
  108. select {
  109. case <-waiter:
  110. default:
  111. t.Fatal("Didn't unblock")
  112. }
  113. }