File size: 4,433 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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()

	/*
		Sent:
		- Test to User C
		- Verify to User H (< 60 seconds ago)
		- Alert to Slack for Service B

		Pending:
		- Alert to User A, Service A (first alert)
		- Alert to User E, Service B (created 2nd -- SMS so Slack doesn't affect first alert status)
		- Alert to User H, Service C (created 3rd) -- Not sent, user H already notified
		- Verify to User F
		- Verify to User A -- Not sent, (will get an alert for Service A)
		- Test to User B
		- Alert to User C, Service A

		Throttled:
		- Status to User D
		- Status to User G (created 2nd)
	*/

	messages := []Message{
		// Sent
		{
			Type:   notification.MessageTypeTest,
			UserID: "User C",

			// Sent messages are considered regardless of the Dest.Type
			// as the user has been notified *somehow*. That way if we need
			// to make a choice, a user who has gotten no message of any kind
			// would take priority (all other criteria being equal).
			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),
		},

		// Pending
		{
			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),
		},
		{
			// no ID, this message should not be sent this cycle
			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"),
		},
		{
			// no ID, this message should not be sent this cycle
			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"),
		},

		// ThrottleConfig limits 5 messages to be sent in 15 min for DestTypeSMS
		{
			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)
	}

	// shuffle order for testing
	rand.Shuffle(len(messages), func(i, j int) { messages[i], messages[j] = messages[j], messages[i] })

	q := newQueue(messages, n)

	// limit the number expected messages to the number allowed to be sent in 15 min
	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)
	}

	// no more expected messages
	msg := q.NextByType(twilio.DestTypeTwilioSMS)
	assert.Nil(t, msg)
}