|
package message |
|
|
|
import ( |
|
"math/rand" |
|
"strconv" |
|
"testing" |
|
"time" |
|
|
|
"github.com/stretchr/testify/assert" |
|
"github.com/stretchr/testify/require" |
|
"github.com/target/goalert/gadb" |
|
"github.com/target/goalert/notification" |
|
"github.com/target/goalert/notification/slack" |
|
"github.com/target/goalert/notification/twilio" |
|
) |
|
|
|
func voice(value string) gadb.DestV1 { |
|
return gadb.NewDestV1(twilio.DestTypeTwilioVoice, twilio.FieldPhoneNumber, value) |
|
} |
|
|
|
func sms(value string) gadb.DestV1 { |
|
return gadb.NewDestV1(twilio.DestTypeTwilioSMS, twilio.FieldPhoneNumber, value) |
|
} |
|
|
|
func slackCh(value string) gadb.DestV1 { |
|
return gadb.NewDestV1(slack.DestTypeSlackChannel, slack.FieldSlackChannelID, value) |
|
} |
|
|
|
func TestQueue_Sort(t *testing.T) { |
|
n := time.Now() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
messages := []Message{ |
|
|
|
{ |
|
Type: notification.MessageTypeTest, |
|
UserID: "User C", |
|
|
|
|
|
|
|
|
|
|
|
Dest: voice("Voice C"), |
|
SentAt: n.Add(-2 * time.Minute), |
|
}, |
|
{ |
|
Type: notification.MessageTypeTest, |
|
UserID: "User H", |
|
Dest: sms("SMS H"), |
|
SentAt: n.Add(-30 * time.Second), |
|
}, |
|
{ |
|
Type: notification.MessageTypeAlert, |
|
ServiceID: "Service B", |
|
Dest: slackCh("Slack B"), |
|
SentAt: n.Add(-30 * time.Second), |
|
}, |
|
|
|
|
|
{ |
|
ID: "0", |
|
Type: notification.MessageTypeAlert, |
|
UserID: "User A", |
|
ServiceID: "Service A", |
|
Dest: sms("SMS A"), |
|
CreatedAt: n, |
|
}, |
|
{ |
|
ID: "1", |
|
Type: notification.MessageTypeAlert, |
|
UserID: "User E", |
|
ServiceID: "Service B", |
|
Dest: sms("SMS E"), |
|
CreatedAt: n.Add(1), |
|
}, |
|
{ |
|
|
|
Type: notification.MessageTypeAlert, |
|
UserID: "User H", |
|
ServiceID: "Service C", |
|
Dest: sms("SMS H"), |
|
CreatedAt: n.Add(2), |
|
}, |
|
{ |
|
ID: "2", |
|
Type: notification.MessageTypeVerification, |
|
UserID: "User F", |
|
Dest: sms("SMS F"), |
|
}, |
|
{ |
|
|
|
Type: notification.MessageTypeVerification, |
|
UserID: "User A", |
|
Dest: sms("SMS A"), |
|
}, |
|
{ |
|
ID: "3", |
|
Type: notification.MessageTypeTest, |
|
UserID: "User B", |
|
Dest: sms("SMS B"), |
|
}, |
|
{ |
|
ID: "4", |
|
Type: notification.MessageTypeAlert, |
|
UserID: "User C", |
|
ServiceID: "Service A", |
|
Dest: sms("SMS C"), |
|
}, |
|
|
|
|
|
{ |
|
ID: "5", |
|
Type: notification.MessageTypeAlertStatus, |
|
UserID: "User D", |
|
Dest: sms("SMS D"), |
|
CreatedAt: n, |
|
}, |
|
{ |
|
ID: "6", |
|
Type: notification.MessageTypeAlertStatus, |
|
UserID: "User G", |
|
Dest: sms("SMS G"), |
|
CreatedAt: n.Add(1), |
|
}, |
|
} |
|
|
|
var expected []Message |
|
for _, m := range messages { |
|
if !m.SentAt.IsZero() || m.ID == "" { |
|
continue |
|
} |
|
if m.ID != strconv.Itoa(len(expected)) { |
|
t.Fatal("expected messages must be in order (starting with 0)") |
|
} |
|
expected = append(expected, m) |
|
} |
|
|
|
|
|
rand.Shuffle(len(messages), func(i, j int) { messages[i], messages[j] = messages[j], messages[i] }) |
|
|
|
q := newQueue(messages, n) |
|
|
|
|
|
rules := q.cmThrottle.cfg.Rules(Message{Type: notification.MessageTypeAlert, Dest: sms("")}) |
|
expected = expected[:rules[1].Count] |
|
|
|
for i, exp := range expected { |
|
msg := q.NextByType(twilio.DestTypeTwilioSMS) |
|
require.NotNilf(t, msg, "message #%d", i) |
|
assert.Equalf(t, exp, *msg, "message #%d", i) |
|
} |
|
|
|
|
|
msg := q.NextByType(twilio.DestTypeTwilioSMS) |
|
assert.Nil(t, msg) |
|
} |
|
|