Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dummy handler type for load testing #803

Merged
merged 1 commit into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}

Check warning on line 37 in handlers/test/handler.go

View check run for this annotation

Codecov / codecov/patch

handlers/test/handler.go#L36-L37

Added lines #L36 - L37 were not covered by tests

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)
}
Loading