From 9a51a0113f9a7c31deb07fdc2d36df8af3ffad14 Mon Sep 17 00:00:00 2001 From: Yousaf Nabi Date: Thu, 18 May 2023 00:53:14 +0100 Subject: [PATCH] fix: use native lockfile, rather than ruby gem, tested on ruby 3.3.0-dev --- .../consumer_contract_writer.rb | 9 +- pact-mock_service.gemspec | 3 +- .../log/mock_multiple_responses_spec.log | 160 +++++++++--------- 3 files changed, 86 insertions(+), 86 deletions(-) diff --git a/lib/pact/consumer_contract/consumer_contract_writer.rb b/lib/pact/consumer_contract/consumer_contract_writer.rb index dd7c4d3..287a78f 100644 --- a/lib/pact/consumer_contract/consumer_contract_writer.rb +++ b/lib/pact/consumer_contract/consumer_contract_writer.rb @@ -5,7 +5,6 @@ require 'pact/consumer_contract/consumer_contract_decorator' require 'pact/shared/active_support_support' require 'fileutils' -require 'filelock' module Pact @@ -60,8 +59,10 @@ 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 @@ -69,7 +70,7 @@ def update_pactfile pact_file.write new_contents pact_file.flush pact_file.truncate(pact_file.pos) - end + } end def pact_json diff --git a/pact-mock_service.gemspec b/pact-mock_service.gemspec index 146fc0e..69e5281 100644 --- a/pact-mock_service.gemspec +++ b/pact-mock_service.gemspec @@ -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' diff --git a/spec/features/log/mock_multiple_responses_spec.log b/spec/features/log/mock_multiple_responses_spec.log index 2b15a5a..90d1bff 100644 --- a/spec/features/log/mock_multiple_responses_spec.log +++ b/spec/features/log/mock_multiple_responses_spec.log @@ -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", @@ -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" } @@ -40,9 +40,7 @@ DEBUG -- : { "Content-Type": "application/json" }, "body": [ - { - "name": "Xena Zebra" - } + ] }, "metadata": null @@ -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", @@ -123,7 +82,6 @@ DEBUG -- : { }, "metadata": null } -INFO -- : Registered expected interaction GET /alligators DEBUG -- : { "description": "a request for alligators", "providerState": "there are no alligators", @@ -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", @@ -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" } @@ -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"