Skip to content

Commit

Permalink
fix: gracefully handle two requests coming in at the same time to cre…
Browse files Browse the repository at this point in the history
…ate the same pacticipant
  • Loading branch information
bethesque committed Jun 11, 2019
1 parent ba1f6bc commit 78d92ad
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/pact_broker/api/resources/pacticipant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def from_json
if pacticipant
@pacticipant = pacticipant_service.update params_with_string_keys.merge('name' => pacticipant_name)
else
@pacticipant = pacticipant_service.create params_with_string_keys.merge('name' => pacticipant_name)
@pacticipant = pacticipant_service.create params.merge(:name => pacticipant_name)
response.headers["Location"] = pacticipant_url(base_url, pacticipant)
end
response.body = to_json
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/api/resources/pacticipants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def post_is_create?
end

def from_json
created_model = pacticipant_service.create params_with_string_keys
created_model = pacticipant_service.create(params)
response.body = decorator_for(created_model).to_json(user_options: decorator_context)
end

Expand Down
3 changes: 3 additions & 0 deletions lib/pact_broker/pacticipants/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ def find_by_name_or_create name
end
end

# Need to be able to handle two calls that make the pacticipant at the same time.
# TODO raise error if attributes apart from name are different, because this indicates that
# the second request is not at the same time.
def create args
PactBroker::Domain::Pacticipant.dataset.insert_ignore.insert(
name: args[:name],
Expand Down
6 changes: 2 additions & 4 deletions lib/pact_broker/pacticipants/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,15 @@ def self.find_pacticipant_repository_url_by_pacticipant_name name
end

def self.update params
# TODO move this to the repository!
pacticipant = pacticipant_repository.find_by_name(params.fetch('name'))
PactBroker::Api::Decorators::PacticipantDecorator.new(pacticipant).from_hash(params)
pacticipant.save
pacticipant_repository.find_by_name(params.fetch('name'))
end

def self.create params
pacticipant = PactBroker::Domain::Pacticipant.new
PactBroker::Api::Decorators::PacticipantDecorator.new(pacticipant).from_hash(params)
pacticipant.save
pacticipant
pacticipant_repository.create(params)
end

def self.delete name
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/pact_broker/api/resources/pacticipants_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ module Resources

context "with valid JSON" do
it "creates the pacticipant" do
expect(PactBroker::Pacticipants::Service).to receive(:create).with('name' => 'New Consumer')
expect(PactBroker::Pacticipants::Service).to receive(:create).with(:name => 'New Consumer')
subject
end

Expand Down

0 comments on commit 78d92ad

Please sign in to comment.