Improve MessageBus.track_publish to allow filter by channel.

This commit is contained in:
Guo Xiang Tan 2017-10-02 11:34:57 +08:00
parent b295a39977
commit c872225762
7 changed files with 28 additions and 39 deletions

@ -15,9 +15,8 @@ describe PostMerger do
reply3 = create_post(topic: topic, raw: 'The third reply', post_number: 4, user: user) reply3 = create_post(topic: topic, raw: 'The third reply', post_number: 4, user: user)
replies = [reply3, reply2, reply1] replies = [reply3, reply2, reply1]
message = MessageBus.track_publish { PostMerger.new(admin, replies).merge }.last message = MessageBus.track_publish("/topic/#{topic.id}") { PostMerger.new(admin, replies).merge }.last
expect(message.channel).to eq("/topic/#{topic.id}")
expect(message.data[:type]).to eq(:revised) expect(message.data[:type]).to eq(:revised)
expect(message.data[:post_number]).to eq(reply3.post_number) expect(message.data[:post_number]).to eq(reply3.post_number)

@ -382,11 +382,10 @@ describe PostRevisor do
it "should publish topic changes to clients" do it "should publish topic changes to clients" do
revisor = described_class.new(topic.ordered_posts.first, topic) revisor = described_class.new(topic.ordered_posts.first, topic)
messages = MessageBus.track_publish do message = MessageBus.track_publish("/topic/#{topic.id}") do
revisor.revise!(newuser, title: 'this is a test topic') revisor.revise!(newuser, title: 'this is a test topic')
end end.first
message = messages.find { |m| m.channel == "/topic/#{topic.id}" }
payload = message.data payload = message.data
expect(payload[:reload_topic]).to eq(true) expect(payload[:reload_topic]).to eq(true)
end end

@ -145,11 +145,11 @@ describe SiteSettingExtension do
settings.setting("test_setting", 100) settings.setting("test_setting", 100)
settings.setting("test_setting", nil, client: true) settings.setting("test_setting", nil, client: true)
messages = MessageBus.track_publish do message = MessageBus.track_publish('/client_settings') do
settings.test_setting = 88 settings.test_setting = 88
end end.first
expect(messages.map(&:channel).include?('/client_settings')).to eq(true) expect(message).to be_present
end end
end end
end end

@ -52,13 +52,11 @@ describe UploadsController do
it 'is successful with an image' do it 'is successful with an image' do
Jobs.expects(:enqueue).with(:create_avatar_thumbnails, anything) Jobs.expects(:enqueue).with(:create_avatar_thumbnails, anything)
message = MessageBus.track_publish do message = MessageBus.track_publish('/uploads/avatar') do
post :create, params: { file: logo, type: "avatar", format: :json } post :create, params: { file: logo, type: "avatar", format: :json }
end.find { |m| m.channel == "/uploads/avatar" } end.first
expect(response.status).to eq 200 expect(response.status).to eq 200
expect(message.channel).to eq("/uploads/avatar")
expect(message.data["id"]).to be expect(message.data["id"]).to be
end end
@ -67,12 +65,11 @@ describe UploadsController do
Jobs.expects(:enqueue).never Jobs.expects(:enqueue).never
message = MessageBus.track_publish do message = MessageBus.track_publish('/uploads/composer') do
post :create, params: { file: text_file, type: "composer", format: :json } post :create, params: { file: text_file, type: "composer", format: :json }
end.find { |m| m.channel == "/uploads/composer" } end.first
expect(response.status).to eq 200 expect(response.status).to eq 200
expect(message.channel).to eq("/uploads/composer")
expect(message.data["id"]).to be expect(message.data["id"]).to be
end end
@ -103,7 +100,7 @@ describe UploadsController do
log_in :admin log_in :admin
Jobs.expects(:enqueue).with(:create_avatar_thumbnails, anything).never Jobs.expects(:enqueue).with(:create_avatar_thumbnails, anything).never
message = MessageBus.track_publish do message = MessageBus.track_publish('/uploads/profile_background') do
post :create, params: { post :create, params: {
file: logo, file: logo,
retain_hours: 100, retain_hours: 100,
@ -119,7 +116,7 @@ describe UploadsController do
it 'requires a file' do it 'requires a file' do
Jobs.expects(:enqueue).never Jobs.expects(:enqueue).never
message = MessageBus.track_publish do message = MessageBus.track_publish('/uploads/composer') do
post :create, params: { type: "composer", format: :json } post :create, params: { type: "composer", format: :json }
end.first end.first
@ -157,14 +154,14 @@ describe UploadsController do
SiteSetting.allow_staff_to_upload_any_file_in_pm = true SiteSetting.allow_staff_to_upload_any_file_in_pm = true
@user.update_columns(moderator: true) @user.update_columns(moderator: true)
message = MessageBus.track_publish do message = MessageBus.track_publish('/uploads/composer') do
post :create, params: { post :create, params: {
file: text_file, file: text_file,
type: "composer", type: "composer",
for_private_message: "true", for_private_message: "true",
format: :json format: :json
} }
end.find { |m| m.channel = '/uploads/composer' } end.first
expect(response).to be_success expect(response).to be_success
expect(message.data["id"]).to be expect(message.data["id"]).to be
@ -173,13 +170,11 @@ describe UploadsController do
it 'returns an error when it could not determine the dimensions of an image' do it 'returns an error when it could not determine the dimensions of an image' do
Jobs.expects(:enqueue).with(:create_avatar_thumbnails, anything).never Jobs.expects(:enqueue).with(:create_avatar_thumbnails, anything).never
message = MessageBus.track_publish do message = MessageBus.track_publish('/uploads/composer') do
post :create, params: { file: fake_jpg, type: "composer", format: :json } post :create, params: { file: fake_jpg, type: "composer", format: :json }
end.find { |m| m.channel == '/uploads/composer' } end.first
expect(response.status).to eq 200 expect(response.status).to eq 200
expect(message.channel).to eq("/uploads/composer")
expect(message.data["errors"]).to contain_exactly(I18n.t("upload.images.size_not_found")) expect(message.data["errors"]).to contain_exactly(I18n.t("upload.images.size_not_found"))
end end

@ -1420,9 +1420,8 @@ describe User do
let(:user) { Fabricate(:user) } let(:user) { Fabricate(:user) }
it 'should publish the right message' do it 'should publish the right message' do
message = MessageBus.track_publish { user.logged_out }.find { |m| m.channel == '/logout' } message = MessageBus.track_publish('/logout') { user.logged_out }.first
expect(message.channel).to eq('/logout')
expect(message.data).to eq(user.id) expect(message.data).to eq(user.id)
end end
end end
@ -1527,9 +1526,9 @@ describe User do
notification = Fabricate(:notification, user: user) notification = Fabricate(:notification, user: user)
notification2 = Fabricate(:notification, user: user, read: true) notification2 = Fabricate(:notification, user: user, read: true)
message = MessageBus.track_publish do message = MessageBus.track_publish("/notification/#{user.id}") do
user.publish_notifications_state user.publish_notifications_state
end.find { |m| m.channel = "/notification/#{user.id}" } end.first
expect(message.data[:recent]).to eq([ expect(message.data[:recent]).to eq([
[notification2.id, true], [notification.id, false] [notification2.id, true], [notification.id, false]

@ -11,14 +11,13 @@ RSpec.describe Admin::EmojisController do
describe "#create" do describe "#create" do
describe 'when upload is invalid' do describe 'when upload is invalid' do
it 'should publish the right error' do it 'should publish the right error' do
message = MessageBus.track_publish do message = MessageBus.track_publish("/uploads/emoji") do
post "/admin/customize/emojis.json", params: { post "/admin/customize/emojis.json", params: {
name: 'test', name: 'test',
file: fixture_file_upload("#{Rails.root}/spec/fixtures/images/fake.jpg") file: fixture_file_upload("#{Rails.root}/spec/fixtures/images/fake.jpg")
} }
end.find { |m| m.channel == "/uploads/emoji" } end.first
expect(message.channel).to eq("/uploads/emoji")
expect(message.data["errors"]).to eq([I18n.t('upload.images.size_not_found')]) expect(message.data["errors"]).to eq([I18n.t('upload.images.size_not_found')])
end end
end end
@ -27,14 +26,12 @@ RSpec.describe Admin::EmojisController do
it 'should publish the right error' do it 'should publish the right error' do
CustomEmoji.create!(name: 'test', upload: upload) CustomEmoji.create!(name: 'test', upload: upload)
message = MessageBus.track_publish do message = MessageBus.track_publish("/uploads/emoji") do
post "/admin/customize/emojis.json", params: { post "/admin/customize/emojis.json", params: {
name: 'test', name: 'test',
file: fixture_file_upload("#{Rails.root}/spec/fixtures/images/logo.png") file: fixture_file_upload("#{Rails.root}/spec/fixtures/images/logo.png")
} }
end.find { |m| m.channel == "/uploads/emoji" } end.first
expect(message.channel).to eq("/uploads/emoji")
expect(message.data["errors"]).to eq([ expect(message.data["errors"]).to eq([
"Name #{I18n.t('activerecord.errors.models.custom_emoji.attributes.name.taken')}" "Name #{I18n.t('activerecord.errors.models.custom_emoji.attributes.name.taken')}"
@ -45,18 +42,17 @@ RSpec.describe Admin::EmojisController do
it 'should allow an admin to add a custom emoji' do it 'should allow an admin to add a custom emoji' do
Emoji.expects(:clear_cache) Emoji.expects(:clear_cache)
message = MessageBus.track_publish do message = MessageBus.track_publish("/uploads/emoji") do
post "/admin/customize/emojis.json", params: { post "/admin/customize/emojis.json", params: {
name: 'test', name: 'test',
file: fixture_file_upload("#{Rails.root}/spec/fixtures/images/logo.png") file: fixture_file_upload("#{Rails.root}/spec/fixtures/images/logo.png")
} }
end.find { |m| m.channel == "/uploads/emoji" } end.first
custom_emoji = CustomEmoji.last custom_emoji = CustomEmoji.last
upload = custom_emoji.upload upload = custom_emoji.upload
expect(upload.original_filename).to eq('logo.png') expect(upload.original_filename).to eq('logo.png')
expect(message.channel).to eq("/uploads/emoji")
expect(message.data["errors"]).to eq(nil) expect(message.data["errors"]).to eq(nil)
expect(message.data["name"]).to eq(custom_emoji.name) expect(message.data["name"]).to eq(custom_emoji.name)
expect(message.data["url"]).to eq(upload.url) expect(message.data["url"]).to eq(upload.url)

@ -1,7 +1,7 @@
module MessageBus::DiagnosticsHelper module MessageBus::DiagnosticsHelper
def publish(channel, data, opts = nil) def publish(channel, data, opts = nil)
id = super(channel, data, opts) id = super(channel, data, opts)
if @tracking if @tracking && (@channel.nil? || @channel == channel)
m = MessageBus::Message.new(-1, id, channel, data) m = MessageBus::Message.new(-1, id, channel, data)
m.user_ids = opts[:user_ids] if opts m.user_ids = opts[:user_ids] if opts
m.group_ids = opts[:group_ids] if opts m.group_ids = opts[:group_ids] if opts
@ -10,7 +10,8 @@ module MessageBus::DiagnosticsHelper
id id
end end
def track_publish def track_publish(channel = nil)
@channel = channel
@tracking = tracking = [] @tracking = tracking = []
yield yield
tracking tracking