Skip to content

Commit

Permalink
Merge pull request #36 from MattSScott/update-diagnostics-engine
Browse files Browse the repository at this point in the history
Added divide by 0 protection to diagnostics engine
  • Loading branch information
MattSScott authored Oct 21, 2024
2 parents 0047f6a + 8c9d9c3 commit 493943c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions internal/diagnosticsEngine/diagnosticsEngine.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,15 @@ func (de *DiagnosticsEngine) GetNumberMessageDrops() int {
}

func (de *DiagnosticsEngine) GetMessagingSuccessRate() float32 {
if de.numMessages == 0 {
return 100
}
return 100 * float32(de.numMessageSuccesses) / float32(de.numMessages)
}

func (de *DiagnosticsEngine) GetEndMessagingSuccessRate(numAgents int) float32 {
if numAgents == 0 {
return 100
}
return 100 * float32(de.numEndMessagings) / float32(numAgents)
}
12 changes: 12 additions & 0 deletions internal/diagnosticsEngine/diagnosticsEngine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,15 @@ func TestResetDiagnostics(t *testing.T) {
}
}
}

func TestDivideByZeroProtection(t *testing.T) {
engine := diagnosticsEngine.CreateDiagnosticsEngine()
msgSuccessRate := engine.GetMessagingSuccessRate()
endMsgingSuccessRate := engine.GetEndMessagingSuccessRate(0)
if msgSuccessRate != 100.0 {
t.Errorf("Diagnostic Engine incorrectly reported Message Success rate when 0 messages sent. Expected 100%%, got %v%%", msgSuccessRate)
}
if endMsgingSuccessRate != 100.0 {
t.Errorf("Diagnostic Engine incorrectly reported Finished Messaging Success rate when 0 agents are present. Expected 100%%, got %v%%", endMsgingSuccessRate)
}
}

0 comments on commit 493943c

Please sign in to comment.