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.
Merge pull request rapidpro#320 from nyaruka/throttle_queue_fix
Fix throttle queue task
- Loading branch information
Showing
3 changed files
with
57 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package starts_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/nyaruka/mailroom/core/models" | ||
"github.com/nyaruka/mailroom/core/tasks/starts" | ||
"github.com/nyaruka/mailroom/testsuite" | ||
"github.com/nyaruka/mailroom/utils/queues" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestThrottleQueue(t *testing.T) { | ||
ctx, rt := testsuite.Runtime() | ||
rc := rt.RP.Get() | ||
defer rc.Close() | ||
|
||
defer testsuite.Reset(testsuite.ResetRedis | testsuite.ResetData) | ||
|
||
queue := queues.NewFairSorted("test") | ||
cron := &starts.ThrottleQueueCron{Queue: queue} | ||
res, err := cron.Run(ctx, rt) | ||
require.NoError(t, err) | ||
assert.Equal(t, map[string]any{"paused": 0, "resumed": 0}, res) | ||
|
||
queue.Push(rc, "type1", 1, "task1", queues.DefaultPriority) | ||
|
||
res, err = cron.Run(ctx, rt) | ||
require.NoError(t, err) | ||
assert.Equal(t, map[string]any{"paused": 0, "resumed": 1}, res) | ||
|
||
// make it look like org 1 has 20,000 messages in its outbox | ||
rt.DB.MustExec(`INSERT INTO msgs_systemlabelcount(org_id, label_type, count, is_squashed) VALUES (1, 'O', 10050, FALSE)`) | ||
|
||
models.FlushCache() | ||
|
||
res, err = cron.Run(ctx, rt) | ||
require.NoError(t, err) | ||
assert.Equal(t, map[string]any{"paused": 1, "resumed": 0}, res) | ||
|
||
// make it look like most of the inbox has cleared | ||
rt.DB.MustExec(`INSERT INTO msgs_systemlabelcount(org_id, label_type, count, is_squashed) VALUES (1, 'O', -10000, FALSE)`) | ||
|
||
models.FlushCache() | ||
|
||
res, err = cron.Run(ctx, rt) | ||
require.NoError(t, err) | ||
assert.Equal(t, map[string]any{"paused": 0, "resumed": 1}, res) | ||
} |
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