Skip to content

Commit

Permalink
fix: use native lockfile, rather than ruby gem, tested on ruby 3.3.0-dev
Browse files Browse the repository at this point in the history
  • Loading branch information
YOU54F committed May 17, 2023
1 parent e63774a commit 9a51a01
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 86 deletions.
9 changes: 5 additions & 4 deletions lib/pact/consumer_contract/consumer_contract_writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
require 'pact/consumer_contract/consumer_contract_decorator'
require 'pact/shared/active_support_support'
require 'fileutils'
require 'filelock'

module Pact

Expand Down Expand Up @@ -60,16 +59,18 @@ def update_pactfile_if_needed
def update_pactfile
logger.info log_message
FileUtils.mkdir_p File.dirname(pactfile_path)
Filelock(pactfile_path) do | pact_file |
# must be read after obtaining the lock, and must be read from the yielded file object, otherwise windows freaks out
# update a counter using write lock
# don't use "w" because it truncates the file before lock.
File.open(pactfile_path, File::RDWR|File::CREAT, 0644) {|pact_file|
pact_file.flock(File::LOCK_EX)
@existing_contents = pact_file.read
new_contents = pact_json
pact_file.rewind
pact_file.truncate 0
pact_file.write new_contents
pact_file.flush
pact_file.truncate(pact_file.pos)
end
}
end

def pact_json
Expand Down
3 changes: 1 addition & 2 deletions pact-mock_service.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ Gem::Specification.new do |gem|
gem.add_runtime_dependency 'find_a_port', '~> 1.0.1'
gem.add_runtime_dependency 'thor', '>= 0.19', '< 2.0'
gem.add_runtime_dependency 'json'
gem.add_runtime_dependency 'webrick', '~> 1.3'
gem.add_runtime_dependency 'webrick', '~> 1.8'
gem.add_runtime_dependency 'pact-support', '~> 1.16', '>= 1.16.4'
gem.add_runtime_dependency 'filelock', '~> 1.1'

gem.add_development_dependency 'rack-test', '~> 0.7'
gem.add_development_dependency 'rake', '~> 13.0', '>= 13.0.1'
Expand Down
160 changes: 80 additions & 80 deletions spec/features/log/mock_multiple_responses_spec.log
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
INFO -- : Cleared interactions for example "Pact::Consumer::MockService when more than one response has been mocked when the actual request matches one expected request returns the expected response"
INFO -- : Cleared interactions for example "Pact::Consumer::MockService when more than one response has been mocked when the actual request matches more than one expected request returns an error response"
INFO -- : Registered expected interaction GET /alligators
DEBUG -- : {
"description": "a request for alligators",
Expand All @@ -23,13 +23,13 @@ DEBUG -- : {
},
"metadata": null
}
INFO -- : Registered expected interaction GET /zebras
INFO -- : Registered expected interaction GET /alligators
DEBUG -- : {
"description": "a request for zebras",
"providerState": "there are zebras",
"description": "a request for alligators",
"providerState": "there are no alligators",
"request": {
"method": "get",
"path": "/zebras",
"path": "/alligators",
"headers": {
"Accept": "application/json"
}
Expand All @@ -40,9 +40,7 @@ DEBUG -- : {
"Content-Type": "application/json"
},
"body": [
{
"name": "Xena Zebra"
}

]
},
"metadata": null
Expand All @@ -60,46 +58,7 @@ DEBUG -- : {
"Cookie": ""
}
}
INFO -- : Found matching response for GET /alligators
DEBUG -- : {
"status": 200,
"headers": {
"Content-Type": "application/json"
},
"body": [
{
"name": "Mary"
}
]
}
INFO -- : Received request GET /zebras
DEBUG -- : {
"method": "get",
"query": "",
"path": "/zebras",
"headers": {
"Https": "off",
"Content-Length": "0",
"Accept": "application/json",
"Host": "example.org",
"Cookie": ""
}
}
INFO -- : Found matching response for GET /zebras
DEBUG -- : {
"status": 200,
"headers": {
"Content-Type": "application/json"
},
"body": [
{
"name": "Xena Zebra"
}
]
}
INFO -- : Verifying - interactions matched for example "Pact::Consumer::MockService when more than one response has been mocked when the actual request matches one expected request returns the expected response"
INFO -- : Cleared interactions for example "Pact::Consumer::MockService when more than one response has been mocked when the actual request matches more than one expected request returns an error response"
INFO -- : Registered expected interaction GET /alligators
ERROR -- : Multiple interactions found for GET /alligators:
DEBUG -- : {
"description": "a request for alligators",
"providerState": "alligators exist",
Expand All @@ -123,7 +82,6 @@ DEBUG -- : {
},
"metadata": null
}
INFO -- : Registered expected interaction GET /alligators
DEBUG -- : {
"description": "a request for alligators",
"providerState": "there are no alligators",
Expand All @@ -145,20 +103,21 @@ DEBUG -- : {
},
"metadata": null
}
INFO -- : Received request GET /alligators
DEBUG -- : {
"method": "get",
"query": "",
"path": "/alligators",
"headers": {
"Https": "off",
"Content-Length": "0",
"Accept": "application/json",
"Host": "example.org",
"Cookie": ""
}
}
ERROR -- : Multiple interactions found for GET /alligators:
WARN -- : Verifying - actual interactions do not match expected interactions for example "Pact::Consumer::MockService when more than one response has been mocked when the actual request matches more than one expected request returns an error response".
Missing requests:
GET /alligators
GET /alligators



WARN -- : Missing requests:
GET /alligators
GET /alligators



INFO -- : Cleared interactions for example "Pact::Consumer::MockService when more than one response has been mocked when the actual request matches one expected request returns the expected response"
INFO -- : Registered expected interaction GET /alligators
DEBUG -- : {
"description": "a request for alligators",
"providerState": "alligators exist",
Expand All @@ -182,12 +141,13 @@ DEBUG -- : {
},
"metadata": null
}
INFO -- : Registered expected interaction GET /zebras
DEBUG -- : {
"description": "a request for alligators",
"providerState": "there are no alligators",
"description": "a request for zebras",
"providerState": "there are zebras",
"request": {
"method": "get",
"path": "/alligators",
"path": "/zebras",
"headers": {
"Accept": "application/json"
}
Expand All @@ -198,21 +158,61 @@ DEBUG -- : {
"Content-Type": "application/json"
},
"body": [

{
"name": "Xena Zebra"
}
]
},
"metadata": null
}
WARN -- : Verifying - actual interactions do not match expected interactions for example "Pact::Consumer::MockService when more than one response has been mocked when the actual request matches more than one expected request returns an error response".
Missing requests:
GET /alligators
GET /alligators



WARN -- : Missing requests:
GET /alligators
GET /alligators



INFO -- : Received request GET /alligators
DEBUG -- : {
"method": "get",
"query": "",
"path": "/alligators",
"headers": {
"Https": "off",
"Content-Length": "0",
"Accept": "application/json",
"Host": "example.org",
"Cookie": ""
}
}
INFO -- : Found matching response for GET /alligators
DEBUG -- : {
"status": 200,
"headers": {
"Content-Type": "application/json"
},
"body": [
{
"name": "Mary"
}
]
}
INFO -- : Received request GET /zebras
DEBUG -- : {
"method": "get",
"query": "",
"path": "/zebras",
"headers": {
"Https": "off",
"Content-Length": "0",
"Accept": "application/json",
"Host": "example.org",
"Cookie": ""
}
}
INFO -- : Found matching response for GET /zebras
DEBUG -- : {
"status": 200,
"headers": {
"Content-Type": "application/json"
},
"body": [
{
"name": "Xena Zebra"
}
]
}
INFO -- : Verifying - interactions matched for example "Pact::Consumer::MockService when more than one response has been mocked when the actual request matches one expected request returns the expected response"

0 comments on commit 9a51a01

Please sign in to comment.