critical_path_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Copyright 2019 Google Inc. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package status
  15. import (
  16. "reflect"
  17. "testing"
  18. "time"
  19. )
  20. type testCriticalPath struct {
  21. *CriticalPath
  22. Counts
  23. actions map[int]*Action
  24. }
  25. type testClock time.Time
  26. func (t testClock) Now() time.Time { return time.Time(t) }
  27. func (t *testCriticalPath) start(id int, startTime time.Duration, outputs, inputs []string) {
  28. t.clock = testClock(time.Unix(0, 0).Add(startTime))
  29. action := &Action{
  30. Description: outputs[0],
  31. Outputs: outputs,
  32. Inputs: inputs,
  33. }
  34. t.actions[id] = action
  35. t.StartAction(action)
  36. }
  37. func (t *testCriticalPath) finish(id int, endTime time.Duration) {
  38. t.clock = testClock(time.Unix(0, 0).Add(endTime))
  39. t.FinishAction(t.actions[id])
  40. }
  41. func TestCriticalPath(t *testing.T) {
  42. tests := []struct {
  43. name string
  44. msgs func(*testCriticalPath)
  45. want []string
  46. wantTime time.Duration
  47. }{
  48. {
  49. name: "empty",
  50. msgs: func(cp *testCriticalPath) {},
  51. },
  52. {
  53. name: "duplicate",
  54. msgs: func(cp *testCriticalPath) {
  55. cp.start(0, 0, []string{"a"}, nil)
  56. cp.start(1, 0, []string{"a"}, nil)
  57. cp.finish(0, 1000)
  58. cp.finish(0, 2000)
  59. },
  60. want: []string{"a"},
  61. wantTime: 1000,
  62. },
  63. {
  64. name: "linear",
  65. // a
  66. // |
  67. // b
  68. // |
  69. // c
  70. msgs: func(cp *testCriticalPath) {
  71. cp.start(0, 0, []string{"a"}, nil)
  72. cp.finish(0, 1000)
  73. cp.start(1, 1000, []string{"b"}, []string{"a"})
  74. cp.finish(1, 2000)
  75. cp.start(2, 3000, []string{"c"}, []string{"b"})
  76. cp.finish(2, 4000)
  77. },
  78. want: []string{"c", "b", "a"},
  79. wantTime: 3000,
  80. },
  81. {
  82. name: "diamond",
  83. // a
  84. // |\
  85. // b c
  86. // |/
  87. // d
  88. msgs: func(cp *testCriticalPath) {
  89. cp.start(0, 0, []string{"a"}, nil)
  90. cp.finish(0, 1000)
  91. cp.start(1, 1000, []string{"b"}, []string{"a"})
  92. cp.start(2, 1000, []string{"c"}, []string{"a"})
  93. cp.finish(1, 2000)
  94. cp.finish(2, 3000)
  95. cp.start(3, 3000, []string{"d"}, []string{"b", "c"})
  96. cp.finish(3, 4000)
  97. },
  98. want: []string{"d", "c", "a"},
  99. wantTime: 4000,
  100. },
  101. {
  102. name: "multiple",
  103. // a d
  104. // | |
  105. // b e
  106. // |
  107. // c
  108. msgs: func(cp *testCriticalPath) {
  109. cp.start(0, 0, []string{"a"}, nil)
  110. cp.start(3, 0, []string{"d"}, nil)
  111. cp.finish(0, 1000)
  112. cp.finish(3, 1000)
  113. cp.start(1, 1000, []string{"b"}, []string{"a"})
  114. cp.start(4, 1000, []string{"e"}, []string{"d"})
  115. cp.finish(1, 2000)
  116. cp.start(2, 2000, []string{"c"}, []string{"b"})
  117. cp.finish(2, 3000)
  118. cp.finish(4, 4000)
  119. },
  120. want: []string{"e", "d"},
  121. wantTime: 4000,
  122. },
  123. }
  124. for _, tt := range tests {
  125. t.Run(tt.name, func(t *testing.T) {
  126. cp := &testCriticalPath{
  127. CriticalPath: NewCriticalPath(),
  128. actions: make(map[int]*Action),
  129. }
  130. tt.msgs(cp)
  131. criticalPath, _, _ := cp.CriticalPath.criticalPath()
  132. var descs []string
  133. for _, x := range criticalPath {
  134. descs = append(descs, x.action.Description)
  135. }
  136. if !reflect.DeepEqual(descs, tt.want) {
  137. t.Errorf("criticalPath.criticalPath() = %v, want %v", descs, tt.want)
  138. }
  139. var gotTime time.Duration
  140. if len(criticalPath) > 0 {
  141. gotTime = criticalPath[0].cumulativeDuration
  142. }
  143. if gotTime != tt.wantTime {
  144. t.Errorf("cumulativeDuration[0].cumulativeDuration = %v, want %v", gotTime, tt.wantTime)
  145. }
  146. })
  147. }
  148. }