From 030f80e3eb1ddb2c0222d0aee51aaf4b1d343324 Mon Sep 17 00:00:00 2001 From: Thorsten Eckel Date: Mon, 3 Dec 2018 12:34:56 +0100 Subject: [PATCH] Refactoring: Allow re-usage of existing DB connection instead of creating/resetting existing one. --- lib/sessions/event/base.rb | 8 +++++++- test/unit/chat_test.rb | 3 +++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/sessions/event/base.rb b/lib/sessions/event/base.rb index b63ea228b..dab060ed3 100644 --- a/lib/sessions/event/base.rb +++ b/lib/sessions/event/base.rb @@ -12,7 +12,12 @@ class Sessions::Event::Base 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 def self.inherited(subclass) @@ -138,6 +143,7 @@ class Sessions::Event::Base def destroy return if !@is_web_socket return if !self.class.instance_variable_get(:@database_connection) + return if @reused_connection ActiveRecord::Base.remove_connection end diff --git a/test/unit/chat_test.rb b/test/unit/chat_test.rb index 258b471f7..b3a53fbfd 100644 --- a/test/unit/chat_test.rb +++ b/test/unit/chat_test.rb @@ -58,6 +58,9 @@ class ChatTest < ActiveSupport::TestCase # with websockets assert(User.first) + # make sure to emulate unconnected WS env + ActiveRecord::Base.remove_connection + message = Sessions::Event.run( event: 'login', payload: {},