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