From 6116313da4996ef99dcb45e2b9ac90ef073caabc Mon Sep 17 00:00:00 2001 From: Jeremy Daer Date: Sat, 8 Jul 2017 15:41:14 -0700 Subject: Mirror: explicit primary service and list of mirrors Pass separate primary service and list of mirrors rather than treating the first of the services list as the primary. Nice fit for keyword args, and something we've long wanted in the equivalent Basecamp file repository. Upload returns the results of the underlying service uploads rather than the io.rewind result. Rewind before uploading rather than afterward, and demonstrate that behavior with a test. Test that more than one mirror works. --- test/service/mirror_service_test.rb | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'test/service') diff --git a/test/service/mirror_service_test.rb b/test/service/mirror_service_test.rb index 45535c754e..10af41c0a8 100644 --- a/test/service/mirror_service_test.rb +++ b/test/service/mirror_service_test.rb @@ -3,9 +3,11 @@ require "service/shared_service_tests" class ActiveStorage::Service::MirrorServiceTest < ActiveSupport::TestCase PRIMARY_DISK_SERVICE = ActiveStorage::Service.configure(:Disk, root: File.join(Dir.tmpdir, "active_storage")) - SECONDARY_DISK_SERVICE = ActiveStorage::Service.configure(:Disk, root: File.join(Dir.tmpdir, "active_storage_mirror")) + MIRROR_SERVICES = (1..3).map do |i| + ActiveStorage::Service.configure(:Disk, root: File.join(Dir.tmpdir, "active_storage_mirror_#{i}")) + end - SERVICE = ActiveStorage::Service.configure :Mirror, services: [ PRIMARY_DISK_SERVICE, SECONDARY_DISK_SERVICE ] + SERVICE = ActiveStorage::Service.configure :Mirror, primary: PRIMARY_DISK_SERVICE, mirrors: MIRROR_SERVICES include ActiveStorage::Service::SharedServiceTests @@ -15,7 +17,9 @@ class ActiveStorage::Service::MirrorServiceTest < ActiveSupport::TestCase key = upload(data, to: @service) assert_equal data, PRIMARY_DISK_SERVICE.download(key) - assert_equal data, SECONDARY_DISK_SERVICE.download(key) + MIRROR_SERVICES.each do |mirror| + assert_equal data, mirror.download(key) + end ensure @service.delete key end @@ -31,7 +35,9 @@ class ActiveStorage::Service::MirrorServiceTest < ActiveSupport::TestCase test "deleting from all services" do @service.delete FIXTURE_KEY assert_not PRIMARY_DISK_SERVICE.exist?(FIXTURE_KEY) - assert_not SECONDARY_DISK_SERVICE.exist?(FIXTURE_KEY) + MIRROR_SERVICES.each do |mirror| + assert_not mirror.exist?(FIXTURE_KEY) + end end test "URL generation in primary service" do @@ -44,7 +50,9 @@ class ActiveStorage::Service::MirrorServiceTest < ActiveSupport::TestCase private def upload(data, to:) SecureRandom.base58(24).tap do |key| - @service.upload key, StringIO.new(data), checksum: Digest::MD5.base64digest(data) + io = StringIO.new(data).tap(&:read) + @service.upload key, io, checksum: Digest::MD5.base64digest(data) + assert io.eof? end end end -- cgit v1.2.3