Skip to content

Commit

Permalink
Improve testing of statsd
Browse files Browse the repository at this point in the history
  • Loading branch information
hneiva committed Jul 23, 2024
1 parent 945ec7f commit 73d8690
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
28 changes: 26 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,10 @@ func TestStatsWriteSuccess(t *testing.T) {
if err != nil {
t.Errorf("Error setting up request.")
}
ag.statsWriteSuccess(req, "test")
statsErr := ag.statsWriteSuccess(req, "test")
if statsErr != nil {
t.Errorf("statsWriteSuccess should not have failed: %s", statsErr)
}
}

func TestStatsWriteSuccessNoStats(t *testing.T) {
Expand All @@ -478,5 +481,26 @@ func TestStatsWriteSuccessNoStats(t *testing.T) {
if err != nil {
t.Errorf("Error setting up request.")
}
tmpag.statsWriteSuccess(req, "test")
statsErr := tmpag.statsWriteSuccess(req, "test")
if statsErr != nil {
t.Errorf("statsWriteSuccess should not have failed: %s", statsErr)
}
}

func TestStatsWriteSuccessFailure(t *testing.T) {
t.Parallel()
// No stats set on autographer
tmpag := newAutographer(1)
req, err := http.NewRequest("GET", "https://foo.bar", nil)
if err != nil {
t.Errorf("Error setting up request.")
}
conf.loadFromFile("autograph.yaml")
// Buffer will always be full - causing an error on submission
conf.Statsd.Buflen = -1
tmpag.addStats(conf)
statsErr := tmpag.statsWriteSuccess(req, "test")
if statsErr == nil {
t.Error("statsWriteSuccess should have failed!")
}
}
5 changes: 3 additions & 2 deletions stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ func (a *autographer) addStats(conf configuration) (err error) {
}

// Send statsd success request
func (a *autographer) statsWriteSuccess(r *http.Request, key string) {
func (a *autographer) statsWriteSuccess(r *http.Request, key string) (err error) {
if a.stats != nil {
starttime := getRequestStartTime(r)
sendStatsErr := a.stats.Timing(key, time.Since(starttime), nil, 1.0)
if sendStatsErr != nil {
log.Warnf("Error sending statsd on success %s: %s", key, sendStatsErr)
return fmt.Errorf("error sending statsd on success %s: %w", key, sendStatsErr)
}
}
return nil
}

0 comments on commit 73d8690

Please sign in to comment.