2015-10-11 17:41:23 +08:00
|
|
|
require 'rails_helper'
|
2015-02-25 14:19:12 +08:00
|
|
|
|
|
|
|
describe RandomTopicSelector do
|
|
|
|
|
|
|
|
it 'can correctly use cache' do
|
|
|
|
key = RandomTopicSelector.cache_key
|
|
|
|
|
|
|
|
$redis.del key
|
|
|
|
|
|
|
|
4.times do |t|
|
|
|
|
$redis.rpush key, t
|
|
|
|
end
|
|
|
|
|
2016-03-03 08:26:45 +08:00
|
|
|
expect(RandomTopicSelector.next(0)).to eq([])
|
2017-07-28 09:20:09 +08:00
|
|
|
expect(RandomTopicSelector.next(2)).to eq([0, 1])
|
2016-03-03 08:26:45 +08:00
|
|
|
|
|
|
|
$redis.expects(:multi).returns(Discourse.received_readonly!)
|
2017-07-28 09:20:09 +08:00
|
|
|
expect(RandomTopicSelector.next(2)).to eq([2, 3])
|
2016-03-03 08:26:45 +08:00
|
|
|
$redis.unstub(:multi)
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
expect(RandomTopicSelector.next(2)).to eq([2, 3])
|
2016-03-03 08:26:45 +08:00
|
|
|
expect(RandomTopicSelector.next(2)).to eq([])
|
2015-02-25 14:19:12 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'can correctly backfill' do
|
2016-12-27 16:08:54 +08:00
|
|
|
category = Fabricate(:category, sort_order: 'op_likes')
|
2015-02-25 14:19:12 +08:00
|
|
|
t1 = Fabricate(:topic, category_id: category.id)
|
|
|
|
_t2 = Fabricate(:topic, category_id: category.id, visible: false)
|
|
|
|
_t3 = Fabricate(:topic, category_id: category.id, deleted_at: 1.minute.ago)
|
|
|
|
t4 = Fabricate(:topic, category_id: category.id)
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
expect(RandomTopicSelector.next(5, category).sort).to eq([t1.id, t4.id].sort)
|
2015-02-25 14:19:12 +08:00
|
|
|
end
|
|
|
|
end
|