From 7f277f1c59e9395bda9b1be990f1bb8e761d3562 Mon Sep 17 00:00:00 2001 From: Rowan Seymour Date: Mon, 18 Nov 2024 12:14:08 -0500 Subject: [PATCH] Add dummy handler type for load testing --- cmd/courier/main.go | 1 + handlers/test/handler.go | 40 +++++++++++++++++++++++++++++++++++ handlers/test/handler_test.go | 30 ++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 handlers/test/handler.go create mode 100644 handlers/test/handler_test.go diff --git a/cmd/courier/main.go b/cmd/courier/main.go index df5f273a8..e673fb131 100644 --- a/cmd/courier/main.go +++ b/cmd/courier/main.go @@ -64,6 +64,7 @@ import ( _ "github.com/nyaruka/courier/handlers/start" _ "github.com/nyaruka/courier/handlers/telegram" _ "github.com/nyaruka/courier/handlers/telesom" + _ "github.com/nyaruka/courier/handlers/test" _ "github.com/nyaruka/courier/handlers/thinq" _ "github.com/nyaruka/courier/handlers/twiml" _ "github.com/nyaruka/courier/handlers/twitter" diff --git a/handlers/test/handler.go b/handlers/test/handler.go new file mode 100644 index 000000000..f1ef94117 --- /dev/null +++ b/handlers/test/handler.go @@ -0,0 +1,40 @@ +package test + +import ( + "context" + "time" + + "github.com/nyaruka/courier" + "github.com/nyaruka/courier/handlers" + "github.com/nyaruka/gocommon/random" +) + +func init() { + courier.RegisterHandler(newHandler()) +} + +type handler struct { + handlers.BaseHandler +} + +func newHandler() courier.ChannelHandler { + return &handler{handlers.NewBaseHandler(courier.ChannelType("TST"), "Test")} +} + +func (h *handler) Initialize(s courier.Server) error { + h.SetServer(s) + return nil +} + +func (h *handler) Send(ctx context.Context, msg courier.MsgOut, res *courier.SendResult, clog *courier.ChannelLog) error { + sendDelayMs := msg.Channel().IntConfigForKey("send_delay_ms", 10) + errorPercent := msg.Channel().IntConfigForKey("error_percent", 5) + + time.Sleep(time.Duration(sendDelayMs) * time.Millisecond) + + if random.IntN(100) < errorPercent { + return courier.ErrConnectionFailed + } + + return nil +} diff --git a/handlers/test/handler_test.go b/handlers/test/handler_test.go new file mode 100644 index 000000000..a5aedd45b --- /dev/null +++ b/handlers/test/handler_test.go @@ -0,0 +1,30 @@ +package test + +import ( + "testing" + + . "github.com/nyaruka/courier/handlers" + "github.com/nyaruka/courier/test" + "github.com/nyaruka/gocommon/random" + "github.com/nyaruka/gocommon/urns" +) + +var sendTestCases = []OutgoingTestCase{ + { + Label: "Plain Send", + MsgText: "Simple Message ☺", + MsgURN: "tel:+12067791234", + ExpectedRequests: []ExpectedRequest{}, + }, +} + +func TestOutgoing(t *testing.T) { + random.SetGenerator(random.NewSeededGenerator(123)) + defer random.SetGenerator(random.DefaultGenerator) + + var channel = test.NewMockChannel("8eb23e93-5ecb-45ba-b726-3b064e0c56ab", "TST", "+12065551212", "US", + []string{urns.Phone.Prefix}, + map[string]any{}, + ) + RunOutgoingTestCases(t, channel, newHandler(), sendTestCases, nil, nil) +}