Skip to content

Commit

Permalink
Fixed issue zammad#2353 - Chat does not work due to errors in WebSock…
Browse files Browse the repository at this point in the history
…et (Database connection). Credits to @d--j for providing the solution.
  • Loading branch information
znuny-robo committed Nov 17, 2018
1 parent ff60c57 commit d0a36c3
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/sessions/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def self.run(params)
begin
backend = load_adapter(adapter)
rescue => e
Rails.logger.error e
return { event: 'error', data: { error: "No such event #{params[:event]}: #{e.inspect}", payload: params[:payload] } }
end

Expand All @@ -16,6 +17,7 @@ def self.run(params)
instance.destroy
result
rescue => e
Rails.logger.error e
return { event: 'error', data: { error: e.message, payload: params[:payload] } }
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/sessions/event/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ def initialize(params)
ActiveRecord::Base.establish_connection
end

def self.inherited(subclass)
subclass.instance_variable_set(:@database_connection, @database_connection)
end

def websocket_send(recipient_client_id, data)
msg = if data.class != Array
"[#{data.to_json}]"
Expand Down
85 changes: 85 additions & 0 deletions test/unit/chat_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,91 @@ class ChatTest < ActiveSupport::TestCase
Setting.set('chat', false)
end

test 'instance_variable test' do
assert_nil(Sessions::Event::Base.instance_variable_get(:@database_connection))
assert_equal(Sessions::Event::ChatBase.instance_variable_get(:@database_connection), true)
assert_equal(Sessions::Event::ChatStatusAgent.instance_variable_get(:@database_connection), true)
end

# check if db connection is available for chat events
# see: https://github.com/zammad/zammad/issues/2353
test 'chat event db connection test' do

class DummyWs
def send(msg)
Rails.logger.info "WS send: #{msg}"
end
end

# with websockets
assert(User.first)

message = Sessions::Event.run(
event: 'login',
payload: {},
session: 123,
remote_ip: '127.0.0.1',
client_id: '123',
clients: {
'123' => {
websocket: DummyWs.new # to simulate a ws connection
}
},
options: {},
)
assert_equal(message, false)

assert_raises(ActiveRecord::ConnectionNotEstablished) do
User.first
end

message = Sessions::Event.run(
event: 'chat_status_customer',
payload: {},
session: 123,
remote_ip: '127.0.0.1',
client_id: '123',
clients: {
'123' => DummyWs.new # to simulate a ws connection
},
options: {},
)
assert_equal(message[:event], 'chat_error')

assert_raises(ActiveRecord::ConnectionNotEstablished) do
User.first
end

# re-establish connection
ActiveRecord::Base.establish_connection

# with ajax long polling
assert(User.first)
message = Sessions::Event.run(
event: 'login',
payload: {},
session: 123,
remote_ip: '127.0.0.1',
client_id: '123',
clients: {},
options: {},
)
assert_equal(message, false)
assert(User.first)

message = Sessions::Event.run(
event: 'chat_status_customer',
payload: {},
session: 123,
remote_ip: '127.0.0.1',
client_id: '123',
clients: {},
options: {},
)
assert_equal(message[:event], 'chat_error')
assert(User.first)
end

test 'default test' do

chat = Chat.create_or_update(
Expand Down

0 comments on commit d0a36c3

Please sign in to comment.