Skip to content

Commit

Permalink
Return error when message is expired
Browse files Browse the repository at this point in the history
  • Loading branch information
bogh committed Mar 28, 2017
1 parent 88d3cae commit 9f6dec6
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
3 changes: 3 additions & 0 deletions protocol/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package protocol
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
Expand All @@ -11,6 +12,8 @@ import (
log "github.com/Sirupsen/logrus"
)

var ErrMessageExpired = errors.New("Message has expired.")

// Message is a struct that represents a message in the guble protocol, as the server sends it to the client.
type Message struct {

Expand Down
2 changes: 1 addition & 1 deletion server/sms/nexmo_sms_sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (ns *NexmoSender) Send(msg *protocol.Message) error {
}).Info("Expired message received")
mTotalExpiredMessages.Add(1)
pTotalExpiredMessages.Inc()
return nil
return protocol.ErrMessageExpired
}

sendSms := func() (*NexmoMessageResponse, error) {
Expand Down
7 changes: 6 additions & 1 deletion server/sms/sms_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package sms
import (
"context"
"encoding/json"
"time"

log "github.com/Sirupsen/logrus"
"github.com/cosminrentea/gobbler/protocol"
"github.com/cosminrentea/gobbler/server/connector"
"github.com/cosminrentea/gobbler/server/metrics"
"github.com/cosminrentea/gobbler/server/router"
"github.com/cosminrentea/gobbler/server/store"
"time"
)

const (
Expand Down Expand Up @@ -187,6 +188,10 @@ func (g *gateway) proxyLoop() error {
func (g *gateway) send(receivedMsg *protocol.Message) error {
err := g.sender.Send(receivedMsg)
if err != nil {
if err == protocol.ErrMessageExpired {
return nil
}

log.WithField("error", err.Error()).Error("Sending of message failed")
mTotalResponseErrors.Add(1)
pNexmoResponseErrors.Inc()
Expand Down
13 changes: 7 additions & 6 deletions server/sms/sms_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ package sms

import (
"encoding/json"
"expvar"
"fmt"
"github.com/cosminrentea/gobbler/server/router"
"github.com/stretchr/testify/assert"
"io/ioutil"
"net/http"
"os"
"strconv"
"testing"
"time"
"expvar"

"github.com/cosminrentea/gobbler/protocol"
"github.com/cosminrentea/gobbler/server/router"
"github.com/stretchr/testify/assert"
)

func Test_NexmoHTTPError(t *testing.T) {
Expand Down Expand Up @@ -152,12 +154,11 @@ func TestNexmoSender_SendExpiredMessage(t *testing.T) {
})

msg := encodeProtocolMessage(t, 0)
expires := time.Now().Add(-1 * time.Minute)
msg.Expires = &expires
msg.Expires = time.Now().Add(-1 * time.Minute).Unix()

err := sender.Send(&msg)
time.Sleep(3 * timeInterval)
a.NoError(err)
a.Equal(protocol.ErrMessageExpired, err)

expectedExpired := expvar.NewInt("total_expired_messages")
expectedExpired.Add(1)
Expand Down

0 comments on commit 9f6dec6

Please sign in to comment.