Skip to content

Commit

Permalink
Improve error handling in test client (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop committed Dec 10, 2020
1 parent b329f49 commit f0c6083
Showing 1 changed file with 32 additions and 19 deletions.
51 changes: 32 additions & 19 deletions resttest/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package resttest
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -125,39 +126,42 @@ func (c *Client) do() (err error) {
c.reqConcurrency = 1
}

// A map of responses count by status code.
statusCodeCount := make(map[int]int, 2)
wg := sync.WaitGroup{}
mu := sync.Mutex{}
resps := make(map[int]*http.Response, 2)
bodies := make(map[int][]byte, 2)

// A map of responses count by status code.
statusCodeCount := make(map[int]int, 2)

errs := make([]error, 0)

for i := 0; i < c.reqConcurrency; i++ {
wg.Add(1)

go func() {
defer wg.Done()
var er error

defer func() {
if er != nil {
mu.Lock()
err = er
mu.Unlock()
}

resp, err := c.doOnce()
if err != nil {
mu.Lock()
errs = append(errs, err)
mu.Unlock()
wg.Done()
}()

resp, er := c.doOnce()
if er != nil {
return
}

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
body, er := ioutil.ReadAll(resp.Body)
if er != nil {
return
}

err = resp.Body.Close()
if err != nil {
panic(err)
er = resp.Body.Close()
if er != nil {
return
}

mu.Lock()
Expand All @@ -173,6 +177,10 @@ func (c *Client) do() (err error) {
}
wg.Wait()

if err != nil {
return err
}

return c.checkResponses(statusCodeCount, bodies, resps)
}

Expand Down Expand Up @@ -335,15 +343,20 @@ func (c *Client) checkBody(expected, received []byte) error {
return errEmptyBody
}

if json5.Valid(expected) {
if json5.Valid(expected) && json5.Valid(received) {
expected, err := json5.Downgrade(expected)
if err != nil {
return err
}

err = assertjson.FailNotEqual(expected, received)
if err != nil {
return fmt.Errorf("%w\nreceived: %s ", err, string(received))
recCompact, cerr := assertjson.MarshalIndentCompact(json.RawMessage(received), "", " ", 100)
if cerr == nil {
received = recCompact
}

return fmt.Errorf("%w\nreceived:\n%s ", err, string(received))
}

return nil
Expand Down

0 comments on commit f0c6083

Please sign in to comment.