Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
67867187
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
67867187
编写于
3月 29, 2016
作者:
R
Rafael Mendonça França
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #23723 from mwear/action_cable_notifications
Add ActiveSupport::Notification to Channel::Base#perform_action
上级
8e8c57b4
09a1321d
变更
5
显示空白变更内容
内联
并排
Showing
5 changed file
with
204 addition
and
8 deletion
+204
-8
actioncable/CHANGELOG.md
actioncable/CHANGELOG.md
+4
-0
actioncable/lib/action_cable/channel/base.rb
actioncable/lib/action_cable/channel/base.rb
+16
-8
actioncable/lib/action_cable/channel/log_subscriber.rb
actioncable/lib/action_cable/channel/log_subscriber.rb
+40
-0
actioncable/test/channel/base_test.rb
actioncable/test/channel/base_test.rb
+75
-0
actioncable/test/channel/log_subscriber_test.rb
actioncable/test/channel/log_subscriber_test.rb
+69
-0
未找到文件。
actioncable/CHANGELOG.md
浏览文件 @
67867187
*
Add ActiveSupport::Notifications and ActiveSupport::LogSubscriber to ActionCable::Channel
*Matthew Wear*
*
Allow channel identifiers with no backslahes/escaping to be accepted
*
Allow channel identifiers with no backslahes/escaping to be accepted
by the subscription storer.
by the subscription storer.
...
...
actioncable/lib/action_cable/channel/base.rb
浏览文件 @
67867187
require
'action_cable/channel/log_subscriber'
require
'set'
require
'set'
module
ActionCable
module
ActionCable
...
@@ -160,7 +161,10 @@ def perform_action(data)
...
@@ -160,7 +161,10 @@ def perform_action(data)
action
=
extract_action
(
data
)
action
=
extract_action
(
data
)
if
processable_action?
(
action
)
if
processable_action?
(
action
)
payload
=
{
channel_class:
self
.
class
.
name
,
action:
action
,
data:
data
}
ActiveSupport
::
Notifications
.
instrument
(
"perform_action.action_cable"
,
payload
)
do
dispatch_action
(
action
,
data
)
dispatch_action
(
action
,
data
)
end
else
else
logger
.
error
"Unable to process
#{
action_signature
(
action
,
data
)
}
"
logger
.
error
"Unable to process
#{
action_signature
(
action
,
data
)
}
"
end
end
...
@@ -191,9 +195,11 @@ def unsubscribed
...
@@ -191,9 +195,11 @@ def unsubscribed
# Transmit a hash of data to the subscriber. The hash will automatically be wrapped in a JSON envelope with
# Transmit a hash of data to the subscriber. The hash will automatically be wrapped in a JSON envelope with
# the proper channel identifier marked as the recipient.
# the proper channel identifier marked as the recipient.
def
transmit
(
data
,
via:
nil
)
def
transmit
(
data
,
via:
nil
)
logger
.
info
"
#{
self
.
class
.
name
}
transmitting
#{
data
.
inspect
.
truncate
(
300
)
}
"
.
tap
{
|
m
|
m
<<
" (via
#{
via
}
)"
if
via
}
payload
=
{
channel_class:
self
.
class
.
name
,
data:
data
,
via:
via
}
ActiveSupport
::
Notifications
.
instrument
(
"transmit.action_cable"
,
payload
)
do
connection
.
transmit
ActiveSupport
::
JSON
.
encode
(
identifier:
@identifier
,
message:
data
)
connection
.
transmit
ActiveSupport
::
JSON
.
encode
(
identifier:
@identifier
,
message:
data
)
end
end
end
def
defer_subscription_confirmation!
def
defer_subscription_confirmation!
@defer_subscription_confirmation
=
true
@defer_subscription_confirmation
=
true
...
@@ -264,11 +270,12 @@ def action_signature(action, data)
...
@@ -264,11 +270,12 @@ def action_signature(action, data)
def
transmit_subscription_confirmation
def
transmit_subscription_confirmation
unless
subscription_confirmation_sent?
unless
subscription_confirmation_sent?
logger
.
info
"
#{
self
.
class
.
name
}
is transmitting the subscription confirmation"
ActiveSupport
::
Notifications
.
instrument
(
"transmit_subscription_confirmation.action_cable"
,
channel_class:
self
.
class
.
name
)
do
connection
.
transmit
ActiveSupport
::
JSON
.
encode
(
identifier:
@identifier
,
type:
ActionCable
::
INTERNAL
[
:message_types
][
:confirmation
])
connection
.
transmit
ActiveSupport
::
JSON
.
encode
(
identifier:
@identifier
,
type:
ActionCable
::
INTERNAL
[
:message_types
][
:confirmation
])
@subscription_confirmation_sent
=
true
@subscription_confirmation_sent
=
true
end
end
end
end
end
def
reject_subscription
def
reject_subscription
connection
.
subscriptions
.
remove_subscription
self
connection
.
subscriptions
.
remove_subscription
self
...
@@ -276,9 +283,10 @@ def reject_subscription
...
@@ -276,9 +283,10 @@ def reject_subscription
end
end
def
transmit_subscription_rejection
def
transmit_subscription_rejection
logger
.
info
"
#{
self
.
class
.
name
}
is transmitting the subscription rejection"
ActiveSupport
::
Notifications
.
instrument
(
"transmit_subscription_rejection.action_cable"
,
channel_class:
self
.
class
.
name
)
do
connection
.
transmit
ActiveSupport
::
JSON
.
encode
(
identifier:
@identifier
,
type:
ActionCable
::
INTERNAL
[
:message_types
][
:rejection
])
connection
.
transmit
ActiveSupport
::
JSON
.
encode
(
identifier:
@identifier
,
type:
ActionCable
::
INTERNAL
[
:message_types
][
:rejection
])
end
end
end
end
end
end
end
end
end
actioncable/lib/action_cable/channel/log_subscriber.rb
0 → 100644
浏览文件 @
67867187
require
'active_support/log_subscriber'
module
ActionCable
module
Channel
class
LogSubscriber
<
ActiveSupport
::
LogSubscriber
def
perform_action
(
event
)
info
do
channel_class
=
event
.
payload
[
:channel_class
]
action
=
event
.
payload
[
:action
]
"Completed
#{
channel_class
}
#
#{
action
}
in
#{
event
.
duration
.
round
}
ms"
end
end
def
transmit
(
event
)
info
do
channel_class
=
event
.
payload
[
:channel_class
]
data
=
event
.
payload
[
:data
]
via
=
event
.
payload
[
:via
]
"
#{
channel_class
}
transmitting
#{
data
.
inspect
.
truncate
(
300
)
}
"
.
tap
{
|
m
|
m
<<
" (via
#{
via
}
)"
if
via
}
end
end
def
transmit_subscription_confirmation
(
event
)
info
do
channel_class
=
event
.
payload
[
:channel_class
]
"
#{
channel_class
}
is transmitting the subscription confirmation"
end
end
def
transmit_subscription_rejection
(
event
)
info
do
channel_class
=
event
.
payload
[
:channel_class
]
"
#{
channel_class
}
is transmitting the subscription rejection"
end
end
end
end
end
ActionCable
::
Channel
::
LogSubscriber
.
attach_to
:action_cable
actioncable/test/channel/base_test.rb
浏览文件 @
67867187
...
@@ -166,6 +166,81 @@ def rm_rf
...
@@ -166,6 +166,81 @@ def rm_rf
end
end
end
end
test
"notification for perform_action"
do
begin
events
=
[]
ActiveSupport
::
Notifications
.
subscribe
"perform_action.action_cable"
do
|*
args
|
events
<<
ActiveSupport
::
Notifications
::
Event
.
new
(
*
args
)
end
data
=
{
'action'
=>
:speak
,
'content'
=>
'hello'
}
@channel
.
perform_action
data
assert_equal
1
,
events
.
length
assert_equal
'perform_action.action_cable'
,
events
[
0
].
name
assert_equal
'ActionCable::Channel::BaseTest::ChatChannel'
,
events
[
0
].
payload
[
:channel_class
]
assert_equal
:speak
,
events
[
0
].
payload
[
:action
]
assert_equal
data
,
events
[
0
].
payload
[
:data
]
ensure
ActiveSupport
::
Notifications
.
unsubscribe
"perform_action.action_cable"
end
end
test
"notification for transmit"
do
begin
events
=
[]
ActiveSupport
::
Notifications
.
subscribe
'transmit.action_cable'
do
|*
args
|
events
<<
ActiveSupport
::
Notifications
::
Event
.
new
(
*
args
)
end
@channel
.
perform_action
'action'
=>
:get_latest
expected_data
=
{
data:
'latest'
}
assert_equal
1
,
events
.
length
assert_equal
'transmit.action_cable'
,
events
[
0
].
name
assert_equal
'ActionCable::Channel::BaseTest::ChatChannel'
,
events
[
0
].
payload
[
:channel_class
]
assert_equal
expected_data
,
events
[
0
].
payload
[
:data
]
assert_nil
events
[
0
].
payload
[
:via
]
ensure
ActiveSupport
::
Notifications
.
unsubscribe
'transmit.action_cable'
end
end
test
"notification for transmit_subscription_confirmation"
do
begin
events
=
[]
ActiveSupport
::
Notifications
.
subscribe
'transmit_subscription_confirmation.action_cable'
do
|*
args
|
events
<<
ActiveSupport
::
Notifications
::
Event
.
new
(
*
args
)
end
@channel
.
stubs
(
:subscription_confirmation_sent?
).
returns
(
false
)
@channel
.
send
(
:transmit_subscription_confirmation
)
assert_equal
1
,
events
.
length
assert_equal
'transmit_subscription_confirmation.action_cable'
,
events
[
0
].
name
assert_equal
'ActionCable::Channel::BaseTest::ChatChannel'
,
events
[
0
].
payload
[
:channel_class
]
ensure
ActiveSupport
::
Notifications
.
unsubscribe
'transmit_subscription_confirmation.action_cable'
end
end
test
"notification for transmit_subscription_rejection"
do
begin
events
=
[]
ActiveSupport
::
Notifications
.
subscribe
'transmit_subscription_rejection.action_cable'
do
|*
args
|
events
<<
ActiveSupport
::
Notifications
::
Event
.
new
(
*
args
)
end
@channel
.
send
(
:transmit_subscription_rejection
)
assert_equal
1
,
events
.
length
assert_equal
'transmit_subscription_rejection.action_cable'
,
events
[
0
].
name
assert_equal
'ActionCable::Channel::BaseTest::ChatChannel'
,
events
[
0
].
payload
[
:channel_class
]
ensure
ActiveSupport
::
Notifications
.
unsubscribe
'transmit_subscription_rejection.action_cable'
end
end
private
private
def
assert_logged
(
message
)
def
assert_logged
(
message
)
old_logger
=
@connection
.
logger
old_logger
=
@connection
.
logger
...
...
actioncable/test/channel/log_subscriber_test.rb
0 → 100644
浏览文件 @
67867187
require
'test_helper'
require
'stubs/test_connection'
require
'active_support/log_subscriber/test_helper'
require
'action_cable/channel/log_subscriber'
class
ActionCable::Channel::LogSubscriberTest
<
ActiveSupport
::
TestCase
include
ActiveSupport
::
LogSubscriber
::
TestHelper
class
ChatChannel
<
ActionCable
::
Channel
::
Base
attr_reader
:last_action
def
speak
(
data
)
@last_action
=
[
:speak
,
data
]
end
def
get_latest
transmit
data:
'latest'
end
end
def
setup
super
@connection
=
TestConnection
.
new
@channel
=
ChatChannel
.
new
@connection
,
"{id: 1}"
,
{
id:
1
}
ActionCable
::
Channel
::
LogSubscriber
.
attach_to
:action_cable
end
def
test_perform_action
data
=
{
'action'
=>
:speak
,
'content'
=>
'hello'
}
@channel
.
perform_action
(
data
)
wait
assert_equal
(
1
,
logs
.
size
)
assert_match
(
/Completed
#{
channel_class
}
#speak in \d+ms/
,
logs
.
first
)
end
def
test_transmit
@channel
.
perform_action
(
'action'
=>
:get_latest
)
wait
assert_equal
(
2
,
logs
.
size
)
assert_match
(
/^
#{
channel_class
}
transmitting/
,
logs
.
first
)
end
def
test_transmit_subscription_confirmation
@channel
.
stubs
(
:subscription_confirmation_sent?
).
returns
(
false
)
@channel
.
send
(
:transmit_subscription_confirmation
)
wait
assert_equal
(
1
,
logs
.
size
)
assert_equal
(
"
#{
channel_class
}
is transmitting the subscription confirmation"
,
logs
.
first
)
end
def
test_transmit_subscription_rejection
@channel
.
send
(
:transmit_subscription_rejection
)
wait
assert_equal
(
1
,
logs
.
size
)
assert_equal
(
"
#{
channel_class
}
is transmitting the subscription rejection"
,
logs
.
first
)
end
def
channel_class
"ActionCable::Channel::LogSubscriberTest::ChatChannel"
end
def
logs
@logs
||=
@logger
.
logged
(
:info
)
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录