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