|
| 1 | +package channels |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/nyaruka/gocommon/dbutil/assertdb" |
| 8 | + "github.com/nyaruka/mailroom/core/models" |
| 9 | + "github.com/nyaruka/mailroom/core/tasks/msgs" |
| 10 | + "github.com/nyaruka/mailroom/testsuite" |
| 11 | + "github.com/nyaruka/mailroom/testsuite/testdata" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | +) |
| 15 | + |
| 16 | +func TestInterruptChannel(t *testing.T) { |
| 17 | + ctx, rt, db, rp := testsuite.Get() |
| 18 | + rc := rp.Get() |
| 19 | + defer rc.Close() |
| 20 | + |
| 21 | + defer testsuite.Reset(testsuite.ResetData) |
| 22 | + |
| 23 | + insertSession := func(org *testdata.Org, contact *testdata.Contact, flow *testdata.Flow, connectionID models.CallID) models.SessionID { |
| 24 | + sessionID := testdata.InsertWaitingSession(db, org, contact, models.FlowTypeMessaging, flow, connectionID, time.Now(), time.Now(), false, nil) |
| 25 | + |
| 26 | + // give session one waiting run too |
| 27 | + testdata.InsertFlowRun(db, org, sessionID, contact, flow, models.RunStatusWaiting) |
| 28 | + return sessionID |
| 29 | + } |
| 30 | + |
| 31 | + // twilio call |
| 32 | + twilioCallID := testdata.InsertCall(db, testdata.Org1, testdata.TwilioChannel, testdata.Alexandria) |
| 33 | + |
| 34 | + // vonage call |
| 35 | + vonageCallID := testdata.InsertCall(db, testdata.Org1, testdata.VonageChannel, testdata.George) |
| 36 | + |
| 37 | + sessionID1 := insertSession(testdata.Org1, testdata.Cathy, testdata.Favorites, models.NilCallID) |
| 38 | + sessionID2 := insertSession(testdata.Org1, testdata.George, testdata.Favorites, vonageCallID) |
| 39 | + sessionID3 := insertSession(testdata.Org1, testdata.Alexandria, testdata.Favorites, twilioCallID) |
| 40 | + |
| 41 | + testdata.InsertOutgoingMsg(db, testdata.Org1, testdata.TwilioChannel, testdata.Cathy, "how can we help", nil, models.MsgStatusPending, false) |
| 42 | + testdata.InsertOutgoingMsg(db, testdata.Org1, testdata.VonageChannel, testdata.Bob, "this failed", nil, models.MsgStatusQueued, false) |
| 43 | + testdata.InsertOutgoingMsg(db, testdata.Org1, testdata.VonageChannel, testdata.George, "no URN", nil, models.MsgStatusPending, false) |
| 44 | + testdata.InsertOutgoingMsg(db, testdata.Org1, testdata.VonageChannel, testdata.George, "no URN", nil, models.MsgStatusErrored, false) |
| 45 | + testdata.InsertOutgoingMsg(db, testdata.Org1, testdata.VonageChannel, testdata.George, "no URN", nil, models.MsgStatusFailed, false) |
| 46 | + |
| 47 | + assertdb.Query(t, db, `SELECT status FROM flows_flowsession WHERE id = $1`, sessionID1).Returns("W") |
| 48 | + assertdb.Query(t, db, `SELECT status FROM flows_flowsession WHERE id = $1`, sessionID2).Returns("W") |
| 49 | + assertdb.Query(t, db, `SELECT status FROM flows_flowsession WHERE id = $1`, sessionID3).Returns("W") |
| 50 | + |
| 51 | + assertdb.Query(t, db, `SELECT count(*) FROM msgs_msg WHERE status = 'F' and channel_id = $1`, testdata.VonageChannel.ID).Returns(1) |
| 52 | + assertdb.Query(t, db, `SELECT count(*) FROM msgs_msg WHERE status = 'F' and channel_id = $1`, testdata.TwilioChannel.ID).Returns(0) |
| 53 | + |
| 54 | + // twilio channel task |
| 55 | + task := &InterruptChannelTask{ |
| 56 | + ChannelID: testdata.TwilioChannel.ID, |
| 57 | + } |
| 58 | + |
| 59 | + // execute it |
| 60 | + err := task.Perform(ctx, rt, testdata.Org1.ID) |
| 61 | + assert.NoError(t, err) |
| 62 | + |
| 63 | + assertdb.Query(t, db, `SELECT count(*) FROM msgs_msg WHERE status = 'F' and channel_id = $1`, testdata.VonageChannel.ID).Returns(1) |
| 64 | + assertdb.Query(t, db, `SELECT count(*) FROM msgs_msg WHERE status = 'F' and channel_id = $1`, testdata.TwilioChannel.ID).Returns(1) |
| 65 | + |
| 66 | + assertdb.Query(t, db, `SELECT status FROM flows_flowsession WHERE id = $1`, sessionID1).Returns("W") |
| 67 | + assertdb.Query(t, db, `SELECT status FROM flows_flowsession WHERE id = $1`, sessionID2).Returns("W") |
| 68 | + assertdb.Query(t, db, `SELECT status FROM flows_flowsession WHERE id = $1`, sessionID3).Returns("I") |
| 69 | + |
| 70 | + testdata.InsertErroredOutgoingMsg(db, testdata.Org1, testdata.TwilioChannel, testdata.Cathy, "Hi", 1, time.Now().Add(-time.Hour), false) |
| 71 | + testdata.InsertErroredOutgoingMsg(db, testdata.Org1, testdata.VonageChannel, testdata.Bob, "Hi", 2, time.Now().Add(-time.Minute), false) |
| 72 | + testdata.InsertErroredOutgoingMsg(db, testdata.Org1, testdata.VonageChannel, testdata.Bob, "Hi", 2, time.Now().Add(-time.Minute), false) |
| 73 | + testdata.InsertErroredOutgoingMsg(db, testdata.Org1, testdata.VonageChannel, testdata.Bob, "Hi", 2, time.Now().Add(-time.Minute), true) // high priority |
| 74 | + |
| 75 | + // just to create courier queues |
| 76 | + err = msgs.RetryErroredMessages(ctx, rt) |
| 77 | + require.NoError(t, err) |
| 78 | + |
| 79 | + testsuite.AssertCourierQueues(t, map[string][]int{ |
| 80 | + "msgs:74729f45-7f29-4868-9dc4-90e491e3c7d8|10/0": {1}, // twilio, bulk priority |
| 81 | + "msgs:19012bfd-3ce3-4cae-9bb9-76cf92c73d49|10/0": {2}, // vonage, bulk priority |
| 82 | + "msgs:19012bfd-3ce3-4cae-9bb9-76cf92c73d49|10/1": {1}, // vonage, high priority |
| 83 | + }) |
| 84 | + |
| 85 | + // vonage channel task |
| 86 | + task = &InterruptChannelTask{ |
| 87 | + ChannelID: testdata.VonageChannel.ID, |
| 88 | + } |
| 89 | + |
| 90 | + // execute it |
| 91 | + err = task.Perform(ctx, rt, testdata.Org1.ID) |
| 92 | + assert.NoError(t, err) |
| 93 | + |
| 94 | + assertdb.Query(t, db, `SELECT count(*) FROM msgs_msg WHERE status = 'F' and channel_id = $1`, testdata.VonageChannel.ID).Returns(7) |
| 95 | + assertdb.Query(t, db, `SELECT count(*) FROM msgs_msg WHERE status = 'F' and channel_id = $1`, testdata.TwilioChannel.ID).Returns(1) |
| 96 | + |
| 97 | + assertdb.Query(t, db, `SELECT status FROM flows_flowsession WHERE id = $1`, sessionID1).Returns("W") |
| 98 | + assertdb.Query(t, db, `SELECT status FROM flows_flowsession WHERE id = $1`, sessionID2).Returns("I") |
| 99 | + assertdb.Query(t, db, `SELECT status FROM flows_flowsession WHERE id = $1`, sessionID3).Returns("I") |
| 100 | + |
| 101 | + // vonage queues should be cleared |
| 102 | + testsuite.AssertCourierQueues(t, map[string][]int{ |
| 103 | + "msgs:74729f45-7f29-4868-9dc4-90e491e3c7d8|10/0": {1}, // twilio, bulk priority |
| 104 | + }) |
| 105 | + |
| 106 | +} |
0 commit comments