Skip to content

Commit

Permalink
added race test and addtl. log output
Browse files Browse the repository at this point in the history
  • Loading branch information
rschmied committed Oct 24, 2022
1 parent 4726bfc commit 6d77fab
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 8 deletions.
26 changes: 18 additions & 8 deletions mockresponder.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ type MockResp struct {
served bool
}

func (mr MockResp) String() string {
// return fmt.Sprintf("%s/%d/%v/%s", mr.URL, mr.Code, mr.Err, string(mr.Data))
return fmt.Sprintf("%s/%d/%v/%v", mr.URL, mr.Code, mr.Err, mr.served)
}

// MockRespList is a list of mocked responses, these are the responses that the
// MockResponder serves, either sequentially or because of a RegEx match.
type MockRespList []MockResp
Expand Down Expand Up @@ -99,9 +104,19 @@ func defaultDoFunc(req *http.Request) (*http.Response, error) {
break
}

if !found {
// default to 200/OK
statusCode := data.Code
if statusCode == 0 {
statusCode = http.StatusOK
}

if found {
// log.Printf("%s <%v>, %d: %v\n", req.Method, req.URL, statusCode, string(data.Data))
log.Printf("%s <%v>, %d: %s\n", req.Method, req.URL, statusCode, data)
} else {
for k, v := range mc.mockData {
fmt.Printf("%d: %v/%v/%v/%v\n", k, v.served, v.URL, v.Code, string(v.Data))
log.Printf("%d: %v %v %v\n%v\n%v\n", k, v.served, v.URL, v.Code, sanitizeURL(req.URL.String()), string(v.Data))
log.Println("**********")
}
panic("ran out of data")
}
Expand All @@ -110,12 +125,6 @@ func defaultDoFunc(req *http.Request) (*http.Response, error) {
return nil, data.Err
}

// default to 200/OK
statusCode := data.Code
if statusCode == 0 {
statusCode = http.StatusOK
}

resp := &http.Response{
StatusCode: statusCode,
Body: io.NopCloser(bytes.NewReader([]byte(data.Data))),
Expand Down Expand Up @@ -169,6 +178,7 @@ func (m *MockResponder) LastData() []byte {
func (m *MockResponder) Empty() bool {
for _, d := range m.mockData {
if !d.served {
log.Println(d)
return false
}
}
Expand Down
51 changes: 51 additions & 0 deletions mockresponder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"net/http"
"reflect"
"runtime"
"sync"
"testing"
"time"

"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -183,3 +185,52 @@ func Test_sanitizeURL(t *testing.T) {
})
}
}

func Test_Race(t *testing.T) {

mrClient, ctx := NewMockResponder()
data := MockRespList{
MockResp{Code: 200},
MockResp{Code: 200},
}
mrClient.SetData(data)

done := false
wg := sync.WaitGroup{}
wg.Add(2)

mu := sync.Mutex{}

get := func() {
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, "/bla", nil)
resp, _ := mrClient.Do(req)
defer resp.Body.Close()
io.ReadAll(resp.Body)
}

go func() {
get()
wg.Done()
}()

go func() {
get()
wg.Done()
}()

go func() {
wg.Wait()
mu.Lock()
done = true
mu.Unlock()
}()

doneCheck := func() bool {
mu.Lock()
defer mu.Unlock()
return done
}

assert.Eventually(t, doneCheck, time.Second*5, time.Microsecond*50)
assert.True(t, mrClient.Empty())
}

0 comments on commit 6d77fab

Please sign in to comment.