Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Synchronize access to Server#next_uuid #88

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions lib/couchrest/server.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'securerandom'

module CouchRest module CouchRest
class Server class Server
attr_accessor :uri, :uuid_batch_count, :available_databases attr_accessor :uri, :uuid_batch_count, :available_databases
Expand Down Expand Up @@ -80,12 +82,9 @@ def restart!
end end


# Retrive an unused UUID from CouchDB. Server instances manage caching a list of unused UUIDs. # Retrive an unused UUID from CouchDB. Server instances manage caching a list of unused UUIDs.
# The count argument is deprecated and has no effect anymore.
def next_uuid(count = @uuid_batch_count) def next_uuid(count = @uuid_batch_count)
@uuids ||= [] SecureRandom.uuid
if @uuids.empty?
@uuids = CouchRest.get("#{@uri}/_uuids?count=#{count}")["uuids"]
end
@uuids.pop
end end


end end
Expand Down
11 changes: 11 additions & 0 deletions spec/couchrest/server_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@
@couch.default_database = 'cr-server-test-default-db' @couch.default_database = 'cr-server-test-default-db'
@couch.available_database?(:default).should be_true @couch.available_database?(:default).should be_true
end end

it "should synchronize access to #next_uuid" do
uuids1 = []
uuids2 = []
Thread.new @couch do |server|
500.times { uuids1 << server.next_uuid }
end
500.times { uuids2 << @couch.next_uuid }
sleep 0.001 until uuids1.size == 500
(uuids1 + uuids2).uniq.size.should be_equal (uuids1 + uuids2).size
end
end end


end end