diff options
-rw-r--r-- | lib/active_storage/service/mirror_service.rb | 19 | ||||
-rw-r--r-- | test/service/mirror_service_test.rb | 18 |
2 files changed, 22 insertions, 15 deletions
diff --git a/lib/active_storage/service/mirror_service.rb b/lib/active_storage/service/mirror_service.rb index 1ec0930e6c..7ec166aace 100644 --- a/lib/active_storage/service/mirror_service.rb +++ b/lib/active_storage/service/mirror_service.rb @@ -1,18 +1,17 @@ require "active_support/core_ext/module/delegation" class ActiveStorage::Service::MirrorService < ActiveStorage::Service - attr_reader :services + attr_reader :primary, :mirrors - delegate :download, :exist?, :url, to: :primary_service + delegate :download, :exist?, :url, to: :primary - def initialize(services:) - @services = services + def initialize(primary:, mirrors:) + @primary, @mirrors = primary, mirrors end def upload(key, io, checksum: nil) - services.collect do |service| - service.upload key, io, checksum: checksum - io.rewind + each_service.collect do |service| + service.upload key, io.tap(&:rewind), checksum: checksum end end @@ -21,13 +20,13 @@ class ActiveStorage::Service::MirrorService < ActiveStorage::Service end private - def primary_service - services.first + def each_service(&block) + [ primary, *mirrors ].each(&block) end def perform_across_services(method, *args) # FIXME: Convert to be threaded - services.collect do |service| + each_service.collect do |service| service.public_send method, *args end end 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 |