status_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // Copyright 2018 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 "testing"
  16. type counterOutput Counts
  17. func (c *counterOutput) StartAction(action *Action, counts Counts) {
  18. *c = counterOutput(counts)
  19. }
  20. func (c *counterOutput) FinishAction(result ActionResult, counts Counts) {
  21. *c = counterOutput(counts)
  22. }
  23. func (c counterOutput) Message(level MsgLevel, msg string) {}
  24. func (c counterOutput) Flush() {}
  25. func (c counterOutput) Write(p []byte) (int, error) {
  26. // Discard writes
  27. return len(p), nil
  28. }
  29. func (c counterOutput) Expect(t *testing.T, counts Counts) {
  30. if Counts(c) == counts {
  31. return
  32. }
  33. t.Helper()
  34. if c.TotalActions != counts.TotalActions {
  35. t.Errorf("Expected %d total edges, but got %d", counts.TotalActions, c.TotalActions)
  36. }
  37. if c.RunningActions != counts.RunningActions {
  38. t.Errorf("Expected %d running edges, but got %d", counts.RunningActions, c.RunningActions)
  39. }
  40. if c.StartedActions != counts.StartedActions {
  41. t.Errorf("Expected %d started edges, but got %d", counts.StartedActions, c.StartedActions)
  42. }
  43. if c.FinishedActions != counts.FinishedActions {
  44. t.Errorf("Expected %d finished edges, but got %d", counts.FinishedActions, c.FinishedActions)
  45. }
  46. }
  47. func TestBasicUse(t *testing.T) {
  48. status := &Status{}
  49. counts := &counterOutput{}
  50. status.AddOutput(counts)
  51. s := status.StartTool()
  52. s.SetTotalActions(2)
  53. a := &Action{}
  54. s.StartAction(a)
  55. counts.Expect(t, Counts{
  56. TotalActions: 2,
  57. RunningActions: 1,
  58. StartedActions: 1,
  59. FinishedActions: 0,
  60. })
  61. s.FinishAction(ActionResult{Action: a})
  62. counts.Expect(t, Counts{
  63. TotalActions: 2,
  64. RunningActions: 0,
  65. StartedActions: 1,
  66. FinishedActions: 1,
  67. })
  68. a = &Action{}
  69. s.StartAction(a)
  70. counts.Expect(t, Counts{
  71. TotalActions: 2,
  72. RunningActions: 1,
  73. StartedActions: 2,
  74. FinishedActions: 1,
  75. })
  76. s.FinishAction(ActionResult{Action: a})
  77. counts.Expect(t, Counts{
  78. TotalActions: 2,
  79. RunningActions: 0,
  80. StartedActions: 2,
  81. FinishedActions: 2,
  82. })
  83. }
  84. // For when a tool claims to have 2 actions, but finishes after one.
  85. func TestFinishEarly(t *testing.T) {
  86. status := &Status{}
  87. counts := &counterOutput{}
  88. status.AddOutput(counts)
  89. s := status.StartTool()
  90. s.SetTotalActions(2)
  91. a := &Action{}
  92. s.StartAction(a)
  93. s.FinishAction(ActionResult{Action: a})
  94. s.Finish()
  95. s = status.StartTool()
  96. s.SetTotalActions(2)
  97. a = &Action{}
  98. s.StartAction(a)
  99. counts.Expect(t, Counts{
  100. TotalActions: 3,
  101. RunningActions: 1,
  102. StartedActions: 2,
  103. FinishedActions: 1,
  104. })
  105. }
  106. // For when a tool claims to have 1 action, but starts two.
  107. func TestExtraActions(t *testing.T) {
  108. status := &Status{}
  109. counts := &counterOutput{}
  110. status.AddOutput(counts)
  111. s := status.StartTool()
  112. s.SetTotalActions(1)
  113. s.StartAction(&Action{})
  114. s.StartAction(&Action{})
  115. counts.Expect(t, Counts{
  116. TotalActions: 2,
  117. RunningActions: 2,
  118. StartedActions: 2,
  119. FinishedActions: 0,
  120. })
  121. }
  122. // When a tool calls Finish() with a running Action
  123. func TestRunningWhenFinished(t *testing.T) {
  124. status := &Status{}
  125. counts := &counterOutput{}
  126. status.AddOutput(counts)
  127. s := status.StartTool()
  128. s.SetTotalActions(1)
  129. s.StartAction(&Action{})
  130. s.Finish()
  131. s = status.StartTool()
  132. s.SetTotalActions(1)
  133. s.StartAction(&Action{})
  134. counts.Expect(t, Counts{
  135. TotalActions: 2,
  136. RunningActions: 2,
  137. StartedActions: 2,
  138. FinishedActions: 0,
  139. })
  140. }