From a1a71479d0933b98622ef1e3516d9e3c8f61138d Mon Sep 17 00:00:00 2001 From: Jack Murdock Date: Fri, 31 May 2019 16:41:00 -0700 Subject: [PATCH] add unit tests --- src/caduceus/outboundSender.go | 2 +- src/caduceus/outboundSender_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/caduceus/outboundSender.go b/src/caduceus/outboundSender.go index 4c008645..f6d983d7 100644 --- a/src/caduceus/outboundSender.go +++ b/src/caduceus/outboundSender.go @@ -516,7 +516,7 @@ func (obs *CaduceusOutboundSender) send(secret, acceptType string, msg *wrp.Mess // Always retry on failures up to the max count. ShouldRetry: func(error) bool { return true }, ShouldRetryStatus: func(code int) bool { - return code < 200 && code > 299 + return code < 200 || code > 299 }, } diff --git a/src/caduceus/outboundSender_test.go b/src/caduceus/outboundSender_test.go index 8a31556f..761d64aa 100644 --- a/src/caduceus/outboundSender_test.go +++ b/src/caduceus/outboundSender_test.go @@ -104,6 +104,7 @@ func simpleFactorySetup(trans *transport, cutOffPeriod time.Duration, matcher [] On("With", []string{"url", w.Config.URL, "code", "201"}).Return(fakeDC). On("With", []string{"url", w.Config.URL, "code", "202"}).Return(fakeDC). On("With", []string{"url", w.Config.URL, "code", "204"}).Return(fakeDC). + On("With", []string{"url", w.Config.URL, "code", "429", "event", "iot"}).Return(fakeDC). On("With", []string{"url", w.Config.URL, "code", "failure"}).Return(fakeDC) fakeDC.On("Add", 1.0).Return() fakeDC.On("Add", 0.0).Return() @@ -218,6 +219,31 @@ func TestSimpleRetry(t *testing.T) { assert.Equal(int32(2), trans.i) } +func Test429Retry(t *testing.T) { + + assert := assert.New(t) + + trans := &transport{} + trans.fn = func(req *http.Request, count int) (*http.Response, error) { + return &http.Response{StatusCode: 429}, nil + } + + obs, err := simpleSetup(trans, time.Second, nil) + + assert.NotNil(obs) + assert.Nil(err) + + req := simpleRequest() + req.Source = "mac:112233445566" + req.TransactionUUID = "1234" + req.Destination = "event:iot" + obs.Queue(req) + + obs.Shutdown(true) + + assert.Equal(int32(2), trans.i) +} + // Simple test that covers the normal successful case with extra matchers func TestSimpleWrpWithMatchers(t *testing.T) {