Skip to content

Commit

Permalink
Specs: allow Server/Client specs to run concurrently
Browse files Browse the repository at this point in the history
  • Loading branch information
asppsa committed Sep 8, 2019
1 parent abdb813 commit e3a0c00
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 44 deletions.
13 changes: 0 additions & 13 deletions spec/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -323,19 +323,6 @@ def load_value value
end
end

def start_server(*args)
server = Moneta::Server.new(*args)
Thread.new { server.run }
sleep 0.1 until server.running?
server
rescue Exception => ex
puts "Failed to start server - #{ex.message}"
tries ||= 0
tries += 1
sleep Moneta::Server::TIMEOUT
tries < 3 ? retry : raise
end

def moneta_property_of(keys: 0, values: 0)
keys_meta = self.keys_meta
values_meta = self.values_meta
Expand Down
12 changes: 4 additions & 8 deletions spec/moneta/adapters/client/adapter_client_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
describe 'adapter_client', isolate: true, adapter: :Client do
before :all do
@server = start_server(Moneta::Adapters::Memory.new)
end
require_relative './client_helper.rb'

after :all do
@server.stop
end
describe 'adapter_client', adapter: :Client do
include_context :start_server, port: 9002, backend: ->{ Moneta::Adapters::Memory.new }

moneta_build do
Moneta::Adapters::Client.new
Moneta::Adapters::Client.new(port: 9002)
end

moneta_specs ADAPTER_SPECS.with_each_key
Expand Down
24 changes: 24 additions & 0 deletions spec/moneta/adapters/client/client_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
RSpec.shared_context :start_server do |**options|
before :context do
begin
options.each do |key, value|
options[key] = instance_exec(&value) if value.respond_to? :call
end
backend = options.delete(:backend)
@server = Moneta::Server.new(backend, options)
@thread = Thread.new { @server.run }
sleep 0.1 until @server.running?
rescue Exception => ex
puts "Failed to start server - #{ex.message}"
tries ||= 0
tries += 1
sleep Moneta::Server::TIMEOUT
tries < 3 ? retry : raise
end
end

after :context do
@server.stop
@thread.join
end
end
12 changes: 4 additions & 8 deletions spec/moneta/adapters/client/standard_client_tcp_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
describe "standard_client_tcp", isolate: true, adapter: :Client do
before :all do
@server = start_server(Moneta::Adapters::Memory.new)
end
require_relative './client_helper.rb'

after :all do
@server.stop
end
describe "standard_client_tcp", adapter: :Client do
include_context :start_server, port: 9003, backend: ->{ Moneta::Adapters::Memory.new }

moneta_store :Client
moneta_store :Client, port: 9003
moneta_specs STANDARD_SPECS

it 'supports multiple clients' do
Expand Down
15 changes: 6 additions & 9 deletions spec/moneta/adapters/client/standard_client_unix_spec.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
describe "standard_client_unix", isolate: true, adapter: :Client do
before :all do
@server = start_server Moneta::Adapters::Memory.new,
socket: File.join(tempdir, 'standard_client_unix')
end
require_relative './client_helper.rb'

after :all do
@server.stop
end
describe "standard_client_unix", adapter: :Client do
include_context :start_server,
backend: ->{ Moneta::Adapters::Memory.new },
socket: ->{ File.join(tempdir, 'standard_client_unix') }

moneta_store :Client do
{socket: File.join(tempdir, 'standard_client_unix')}
{ socket: File.join(tempdir, 'standard_client_unix') }
end

moneta_specs STANDARD_SPECS
Expand Down
4 changes: 2 additions & 2 deletions spec/moneta/proxies/shared/shared_tcp_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
describe "shared_tcp", isolate: true, proxy: :Shared do
describe "shared_tcp", proxy: :Shared do
moneta_build do
tempdir = self.tempdir
Moneta.build do
Expand Down Expand Up @@ -26,7 +26,7 @@

it 'has the underlying adapter' do
store.load('dummy')
expect(store.adapter.adapter).to be_a Moneta::Adapters::PStore
expect(store.adapter.adapter).to be_a Moneta::Adapters::GDBM
end
end

Expand Down
8 changes: 4 additions & 4 deletions spec/moneta/proxies/shared/shared_unix_spec.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
describe "shared_unix", isolate: true, proxy: :Shared do
describe "shared_unix", proxy: :Shared do
moneta_build do
tempdir = self.tempdir
Moneta.build do
use(:Shared, socket: File.join(tempdir, 'shared_unix.socket')) do
adapter :PStore, file: File.join(tempdir, 'shared_unix')
adapter :GDBM, file: File.join(tempdir, 'shared_unix')
end
end
end

shared_examples :shared_unix do
moneta_specs ADAPTER_SPECS
moneta_specs ADAPTER_SPECS.with_each_key

it 'shares values' do
store['shared_key'] = 'shared_value'
Expand All @@ -25,7 +25,7 @@

it "has the underlying adapter" do
store.load('dummy')
expect(store.adapter.adapter).to be_a Moneta::Adapters::PStore
expect(store.adapter.adapter).to be_a Moneta::Adapters::GDBM
end
end

Expand Down

0 comments on commit e3a0c00

Please sign in to comment.