From b5753d682b52800f8793fb8ac24e9233c34dfd64 Mon Sep 17 00:00:00 2001 From: "e.zhirov" Date: Thu, 4 Jul 2019 11:55:08 +0200 Subject: [PATCH] Test which reproduce the problem --- runner/reporter.go | 2 +- runner/reporter_test.go | 29 +++++++++++++++++++++++++++++ runner/requester.go | 7 ++++--- runner/stats_handler.go | 2 +- 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/runner/reporter.go b/runner/reporter.go index 2d648663..027832c3 100644 --- a/runner/reporter.go +++ b/runner/reporter.go @@ -154,7 +154,7 @@ func (r *Reporter) Run() { r.lats = append(r.lats, res.duration.Seconds()) r.errors = append(r.errors, "") r.statuses = append(r.statuses, res.status) - r.timestamps = append(r.timestamps, time.Now()) + r.timestamps = append(r.timestamps, res.timestamp) } } } diff --git a/runner/reporter_test.go b/runner/reporter_test.go index fc1c8db8..9c102ac0 100644 --- a/runner/reporter_test.go +++ b/runner/reporter_test.go @@ -1,6 +1,7 @@ package runner import ( + "context" "encoding/json" "testing" "time" @@ -26,3 +27,31 @@ func TestReport_MarshalJSON(t *testing.T) { expected := `{"date":"2006-01-02T15:04:00-07:00","options":{"insecure":false,"binary":false,"CPUs":0},"count":1000,"total":10000000000,"average":500000000,"fastest":10000000,"slowest":1000000000,"rps":34567.89,"errorDistribution":null,"statusCodeDistribution":null,"latencyDistribution":null,"histogram":null,"details":null}` assert.Equal(t, expected, string(json)) } + +func TestReport_CorrectDetails(t *testing.T) { + callResultsChan := make(chan *callResult) + config, _ := newConfig("call", "host") + reporter := newReporter(callResultsChan, config) + + go reporter.Run() + + cr1 := callResult{ + status: "OK", + duration: time.Millisecond * 100, + err: nil, + timestamp: time.Now(), + } + callResultsChan <- &cr1 + cr2 := callResult{ + status: "DeadlineExceeded", + duration: time.Millisecond * 500, + err: context.DeadlineExceeded, + timestamp: time.Now(), + } + callResultsChan <- &cr2 + + report := reporter.Finalize("stop reason", time.Second) + assert.Equal(t, 2, len(report.Details)) + assert.Equal(t, ResultDetail{Error: "", Latency: cr1.duration, Status: cr1.status, Timestamp: cr1.timestamp}, report.Details[0]) + assert.Equal(t, ResultDetail{Error: cr2.err.Error(), Latency: cr2.duration, Status: cr2.status, Timestamp: cr2.timestamp}, report.Details[1]) +} diff --git a/runner/requester.go b/runner/requester.go index 4878fcf4..ee8211ef 100644 --- a/runner/requester.go +++ b/runner/requester.go @@ -28,9 +28,10 @@ const maxResult = 1000000 // result of a call type callResult struct { - err error - status string - duration time.Duration + err error + status string + duration time.Duration + timestamp time.Time } // Requester is used for doing the requests diff --git a/runner/stats_handler.go b/runner/stats_handler.go index c3a35fc6..22100d9b 100644 --- a/runner/stats_handler.go +++ b/runner/stats_handler.go @@ -38,7 +38,7 @@ func (c *statsHandler) HandleRPC(ctx context.Context, rs stats.RPCStats) { st = s.Code().String() } - c.results <- &callResult{rpcStats.Error, st, duration} + c.results <- &callResult{rpcStats.Error, st, duration, end} } }