File size: 1,966 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
package message_test

import (
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
	"github.com/target/goalert/engine/message"
	"github.com/target/goalert/gadb"
	"github.com/target/goalert/notification"
	"github.com/target/goalert/notification/twilio"
)

func TestThrottleConfigBuilder(t *testing.T) {
	var b message.ThrottleConfigBuilder

	b.AddRules([]message.ThrottleRule{{Count: 1, Per: 2 * time.Minute}})

	b.WithDestTypes(twilio.DestTypeTwilioSMS).AddRules([]message.ThrottleRule{{Count: 2, Per: 3 * time.Minute}})

	b.WithMsgTypes(notification.MessageTypeAlert).AddRules([]message.ThrottleRule{{Count: 3, Per: 5 * time.Minute}})

	b.WithDestTypes(twilio.DestTypeTwilioVoice).WithMsgTypes(notification.MessageTypeTest).AddRules([]message.ThrottleRule{{Count: 5, Per: 7 * time.Minute}})

	cfg := b.Config()

	assert.Equal(t, 7*time.Minute, cfg.MaxDuration())

	check := func(dest string, msg gadb.EnumOutgoingMessagesType, expRules []message.ThrottleRule) {
		t.Helper()
		assert.EqualValues(t, expRules, cfg.Rules(message.Message{Type: msg, Dest: gadb.DestV1{Type: dest}}))
	}

	check(
		"",
		notification.MessageTypeUnknown,
		[]message.ThrottleRule{
			{Count: 1, Per: 2 * time.Minute},
		},
	)

	check(
		twilio.DestTypeTwilioSMS,
		notification.MessageTypeUnknown,
		[]message.ThrottleRule{
			{Count: 1, Per: 2 * time.Minute},
			{Count: 2, Per: 3 * time.Minute},
		},
	)

	check(
		twilio.DestTypeTwilioVoice,
		notification.MessageTypeAlert,
		[]message.ThrottleRule{
			{Count: 1, Per: 2 * time.Minute},
			{Count: 3, Per: 5 * time.Minute},
		},
	)

	check(
		twilio.DestTypeTwilioVoice,
		notification.MessageTypeTest,
		[]message.ThrottleRule{
			{Count: 1, Per: 2 * time.Minute},
			{Count: 5, Per: 7 * time.Minute},
		},
	)

	check(
		twilio.DestTypeTwilioSMS,
		notification.MessageTypeAlert,
		[]message.ThrottleRule{
			{Count: 1, Per: 2 * time.Minute},
			{Count: 2, Per: 3 * time.Minute},
			{Count: 3, Per: 5 * time.Minute},
		},
	)
}