Skip to content

Commit

Permalink
refactor: extract TestableServer
Browse files Browse the repository at this point in the history
  • Loading branch information
ArturT committed May 15, 2024
1 parent b977b3b commit b819328
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
20 changes: 13 additions & 7 deletions lib/knapsack_pro/store/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@ module Store
class Server
extend Forwardable

def self.reset
stop
set_store_server_uri(nil)
@assigned_store_server_uri = nil
@client = nil
end

def self.start
return unless @server_pid.nil?

Expand Down Expand Up @@ -111,5 +104,18 @@ def self.stop_dummy_service
DRb.stop_service
end
end

class TestableServer < Server
def self.assign_available_store_server_uri
super
end

def self.reset
stop
set_store_server_uri(nil)
@assigned_store_server_uri = nil
@client = nil
end
end
end
end
18 changes: 9 additions & 9 deletions spec/knapsack_pro/store/server_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
describe KnapsackPro::Store::Server do
before do
KnapsackPro::Store::Server.reset
KnapsackPro::Store::TestableServer.reset
end

describe 'Queue test batches' do
context 'when the queue mode flow' do
it do
KnapsackPro::Store::Server.start
store = KnapsackPro::Store::Server.client
KnapsackPro::Store::TestableServer.start
store = KnapsackPro::Store::TestableServer.client

expect(store.batches).to eq []

Expand Down Expand Up @@ -56,17 +56,17 @@

context 'when there is a delay in starting server in a forked process' do
before do
KnapsackPro::Store::Server.send(:assign_available_store_server_uri)
KnapsackPro::Store::TestableServer.assign_available_store_server_uri
end

context 'when the delay is below 3 seconds' do
it 'connects with the store server correctly' do
thread = Thread.new do
sleep 2
KnapsackPro::Store::Server.start
KnapsackPro::Store::TestableServer.start
end

store = KnapsackPro::Store::Server.client
store = KnapsackPro::Store::TestableServer.client
expect(store.ping).to be true

thread.join
Expand All @@ -77,11 +77,11 @@
it do
thread = Thread.new do
sleep 4
KnapsackPro::Store::Server.start
KnapsackPro::Store::TestableServer.start
end

expect {
KnapsackPro::Store::Server.client
KnapsackPro::Store::TestableServer.client
}.to raise_error DRb::DRbConnError

thread.join
Expand All @@ -92,7 +92,7 @@
context 'when the server has not been started' do
it do
expect {
KnapsackPro::Store::Server.client
KnapsackPro::Store::TestableServer.client
}.to raise_error RuntimeError, 'KNAPSACK_PRO_STORE_SERVER_URI must be set to available DRb port.'
end
end
Expand Down

0 comments on commit b819328

Please sign in to comment.