forked from rapidpro/mailroom
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
172 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package ivr | ||
|
||
import ( | ||
"encoding/json" | ||
"sync" | ||
"testing" | ||
|
||
"github.com/nyaruka/mailroom/core/ivr" | ||
"github.com/nyaruka/mailroom/core/models" | ||
"github.com/nyaruka/mailroom/core/queue" | ||
"github.com/nyaruka/mailroom/core/tasks/starts" | ||
"github.com/nyaruka/mailroom/testsuite" | ||
"github.com/nyaruka/mailroom/testsuite/testdata" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestHandleWork(t *testing.T) { | ||
ctx, rt, db, rp := testsuite.Get() | ||
rc := rp.Get() | ||
defer rc.Close() | ||
|
||
defer testsuite.Reset(testsuite.ResetAll) | ||
|
||
// register our mock client | ||
ivr.RegisterServiceType(models.ChannelType("ZZ"), newMockProvider) | ||
|
||
// update our twilio channel to be of type 'ZZ' and set max_concurrent_events to 1 | ||
db.MustExec(`UPDATE channels_channel SET channel_type = 'ZZ', config = '{"max_concurrent_events": 1}' WHERE id = $1`, testdata.TwilioChannel.ID) | ||
|
||
// create a flow start for cathy | ||
start := models.NewFlowStart(testdata.Org1.ID, models.StartTypeTrigger, models.FlowTypeVoice, testdata.IVRFlow.ID, models.DoRestartParticipants, models.DoIncludeActive). | ||
WithContactIDs([]models.ContactID{testdata.Cathy.ID}) | ||
|
||
// call our master starter | ||
err := starts.CreateFlowBatches(ctx, rt, start) | ||
assert.NoError(t, err) | ||
|
||
// should have one task in our ivr queue | ||
task, err := queue.PopNextTask(rc, queue.HandlerQueue) | ||
assert.NoError(t, err) | ||
batch := &models.FlowStartBatch{} | ||
err = json.Unmarshal(task.Task, batch) | ||
assert.NoError(t, err) | ||
|
||
client.callError = nil | ||
client.callID = ivr.CallID("call1") | ||
err = HandleFlowStartBatch(ctx, rt, batch) | ||
assert.NoError(t, err) | ||
testsuite.AssertQuery(t, db, `SELECT COUNT(*) FROM channels_channelconnection WHERE contact_id = $1 AND status = $2 AND external_id = $3`, | ||
testdata.Cathy.ID, models.ConnectionStatusWired, "call1").Returns(1) | ||
|
||
// change our call to be errored instead of wired | ||
db.MustExec(`UPDATE channels_channelconnection SET status = 'E', next_attempt = NOW() WHERE external_id = 'call1';`) | ||
|
||
conns, err := models.LoadChannelConnectionsToRetry(ctx, rt.DB, 1) | ||
assert.NoError(t, err) | ||
assert.Equal(t, len(conns), 1) | ||
|
||
err = retryCall(0, rt, conns[0]) | ||
assert.NoError(t, err) | ||
|
||
// should now be in wired state | ||
testsuite.AssertQuery(t, db, `SELECT COUNT(*) FROM channels_channelconnection WHERE contact_id = $1 AND status = $2 AND external_id = $3`, | ||
testdata.Cathy.ID, models.ConnectionStatusWired, "call1").Returns(1) | ||
|
||
// back to retry and make the channel inactive | ||
db.MustExec(`UPDATE channels_channelconnection SET status = 'E', next_attempt = NOW() WHERE external_id = 'call1';`) | ||
db.MustExec(`UPDATE channels_channel SET is_active = FALSE WHERE id = $1`, testdata.TwilioChannel.ID) | ||
|
||
models.FlushCache() | ||
|
||
conns, err = models.LoadChannelConnectionsToRetry(ctx, rt.DB, 1) | ||
assert.NoError(t, err) | ||
assert.Equal(t, len(conns), 1) | ||
|
||
jobs := []Job{{Id: 1, conn: conns[0]}} | ||
|
||
var ( | ||
wg sync.WaitGroup | ||
jobChannel = make(chan Job) | ||
) | ||
wg.Add(1) | ||
|
||
go handleWork(0, rt, &wg, jobChannel) | ||
|
||
for _, job := range jobs { | ||
jobChannel <- job | ||
} | ||
|
||
close(jobChannel) | ||
wg.Wait() | ||
|
||
// this time should be failed | ||
testsuite.AssertQuery(t, db, `SELECT COUNT(*) FROM channels_channelconnection WHERE contact_id = $1 AND status = $2 AND external_id = $3`, | ||
testdata.Cathy.ID, models.ConnectionStatusFailed, "call1").Returns(1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters