Skip to content

Commit

Permalink
Merge pull request #803 from nyaruka/test_handler
Browse files Browse the repository at this point in the history
Add dummy handler type for load testing
  • Loading branch information
rowanseymour authored Nov 18, 2024
2 parents 029f9a3 + 7f277f1 commit b9cc901
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/courier/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
40 changes: 40 additions & 0 deletions handlers/test/handler.go
Original file line number Diff line number Diff line change
@@ -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
}
30 changes: 30 additions & 0 deletions handlers/test/handler_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit b9cc901

Please sign in to comment.