Skip to content

Commit

Permalink
SMS check expired messages
Browse files Browse the repository at this point in the history
  • Loading branch information
bogh committed Mar 21, 2017
1 parent 8f6ad3f commit 1aec698
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 2 deletions.
2 changes: 1 addition & 1 deletion protocol/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (m *Message) SetFilter(key, value string) {
// Checks are made using `Expires` field timezone
func (m *Message) IsExpired() bool {
if m.Expires == nil {
return true
return false
}
return m.Expires != nil && m.Expires.Before(time.Now().In(m.Expires.Location()))
}
Expand Down
7 changes: 7 additions & 0 deletions protocol/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,10 @@ func TestMessage_IsExpired(t *testing.T) {
}

}

func TestMessage_IsExpired_WithNilExpires(t *testing.T) {
a := assert.New(t)

a.Equal(false, (&Message{}).IsExpired())

}
11 changes: 11 additions & 0 deletions server/sms/nexmo_sms_sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,17 @@ func (ns *NexmoSender) Send(msg *protocol.Message) error {
return ErrRetryFailed
}

if msg.IsExpired() {
log.WithFields(log.Fields{
"ID": msg.ID,
"To": nexmoSMS.To,
"Expires": msg.Expires.Format(time.RFC3339),
"Created": time.Unix(msg.Time, 0).Format(time.RFC3339),
}).Info("Expired message received")
mTotalExpiredMessages.Add(1)
return nil
}

sendSms := func() (*NexmoMessageResponse, error) {
return ns.sendSms(nexmoSMS)
}
Expand Down
37 changes: 37 additions & 0 deletions server/sms/nexmo_sms_sender_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sms

import (
"net/http"
"testing"
"time"

Expand Down Expand Up @@ -35,3 +36,39 @@ func TestNexmoSender_SendWithError(t *testing.T) {
a.Error(err)
a.Equal(ErrRetryFailed, err)
}

func TestNexmoSender_SendExpiredMessage(t *testing.T) {
a := assert.New(t)

port := createRandomPort(7000, 8000)
URL = "http://127.0.0.1" + port

sender := createNexmoSender(t)
countCh := make(chan bool)
doneCh := make(chan struct{})
// no request should be made in case the sms is expired
go dummyNexmoEndpointWithHandlerFunc(t, countCh, port, func(t *testing.T, countCh chan bool) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
countCh <- true
response := composeNexmoMessageResponse(decodeSMSMessage(t, r), ResponseSuccess, 1)
writeNexmoResponse(response, t, w)
}
})
go func() {
select {
case <-countCh:
a.FailNow("Nexmo call not expected.")
case <-doneCh:
return
}
}()

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

err := sender.Send(&msg)
time.Sleep(3 * timeInterval)
a.NoError(err)
close(doneCh)
}
4 changes: 3 additions & 1 deletion server/sms/sms_metrics.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package sms

import (
"github.com/cosminrentea/gobbler/server/metrics"
"time"

"github.com/cosminrentea/gobbler/server/metrics"
)

var (
Expand All @@ -11,6 +12,7 @@ var (
mTotalSendErrors = ns.NewInt("total_sent_message_errors")
mTotalResponseErrors = ns.NewInt("total_response_errors")
mTotalResponseInternalErrors = ns.NewInt("total_response_internal_errors")
mTotalExpiredMessages = ns.NewInt("total_expires_messages")
mMinute = ns.NewMap("minute")
mHour = ns.NewMap("hour")
mDay = ns.NewMap("day")
Expand Down

0 comments on commit 1aec698

Please sign in to comment.