Refactoring: Allow re-usage of existing DB connection instead of creating/resetting existing one.
This commit is contained in:
parent
7ffa03731e
commit
030f80e3eb
2 changed files with 10 additions and 1 deletions
|
@ -12,7 +12,12 @@ class Sessions::Event::Base
|
||||||
|
|
||||||
return if !self.class.instance_variable_get(:@database_connection)
|
return if !self.class.instance_variable_get(:@database_connection)
|
||||||
|
|
||||||
ActiveRecord::Base.establish_connection
|
if ActiveRecord::Base.connected?
|
||||||
|
@reused_connection = true
|
||||||
|
else
|
||||||
|
@reused_connection = false
|
||||||
|
ActiveRecord::Base.establish_connection
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.inherited(subclass)
|
def self.inherited(subclass)
|
||||||
|
@ -138,6 +143,7 @@ class Sessions::Event::Base
|
||||||
def destroy
|
def destroy
|
||||||
return if !@is_web_socket
|
return if !@is_web_socket
|
||||||
return if !self.class.instance_variable_get(:@database_connection)
|
return if !self.class.instance_variable_get(:@database_connection)
|
||||||
|
return if @reused_connection
|
||||||
|
|
||||||
ActiveRecord::Base.remove_connection
|
ActiveRecord::Base.remove_connection
|
||||||
end
|
end
|
||||||
|
|
|
@ -58,6 +58,9 @@ class ChatTest < ActiveSupport::TestCase
|
||||||
# with websockets
|
# with websockets
|
||||||
assert(User.first)
|
assert(User.first)
|
||||||
|
|
||||||
|
# make sure to emulate unconnected WS env
|
||||||
|
ActiveRecord::Base.remove_connection
|
||||||
|
|
||||||
message = Sessions::Event.run(
|
message = Sessions::Event.run(
|
||||||
event: 'login',
|
event: 'login',
|
||||||
payload: {},
|
payload: {},
|
||||||
|
|
Loading…
Reference in a new issue