Skip to content

Commit

Permalink
Merge pull request #88 from NETWAYS/fix/null-inflight-events
Browse files Browse the repository at this point in the history
Set inflight events to zero if below zero
  • Loading branch information
martialblog authored Sep 27, 2023
2 parents d1a5f98 + f1cdff0 commit b57093c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
14 changes: 13 additions & 1 deletion cmd/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ type PipelineThreshold struct {

var cliPipelineConfig PipelineConfig

// calculateInflightEvents calculates the current inflight events,
// returns 0 if the value is negative.
func calculateInflightEvents(in, out int) int {
r := in - out

if r < 0 {
return 0
}

return r
}

func parsePipeThresholds(config PipelineConfig) (PipelineThreshold, error) {
// Parses the CLI parameters
var t PipelineThreshold
Expand Down Expand Up @@ -107,7 +119,7 @@ var pipelineCmd = &cobra.Command{
var summary strings.Builder

for name, pipe := range pp.Pipelines {
inflightEvents := pipe.Events.In - pipe.Events.Out
inflightEvents := calculateInflightEvents(pipe.Events.In, pipe.Events.Out)

summary.WriteString("\n \\_")
if thresholds.Critical.DoesViolate(float64(inflightEvents)) {
Expand Down
17 changes: 17 additions & 0 deletions cmd/pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,23 @@ import (
"testing"
)

func TestCalculateInflightEvents(t *testing.T) {
var actual int

actual = calculateInflightEvents(0, 10)

if actual != 0 {
t.Error("\nActual: ", actual, "\nExpected: ", 0)
}

actual = calculateInflightEvents(20, 10)

if actual != 10 {
t.Error("\nActual: ", actual, "\nExpected: ", 10)
}

}

func TestPipeline_ConnectionRefused(t *testing.T) {

cmd := exec.Command("go", "run", "../main.go", "pipeline", "--port", "9999", "--inflight-events-warn", "10", "--inflight-events-crit", "20")
Expand Down

0 comments on commit b57093c

Please sign in to comment.