Skip to content

Commit

Permalink
Fix Flaky ActiveStorage test (rails#41225)
Browse files Browse the repository at this point in the history
Fixes a flaky Active Storage test introduced by [rails#41065][],
and improves the documentation.

It seems that the test is covering the backwards compatibility of an
older interface for retrieving records through
`ActiveStorage::Record#find_signed!`. The test itself would pass
unpredictably. To isolate the failure and reproduce it consistently, a
see value was found after some trial and error:

```
SEED=59729 bin/test test/fixture_set_test.rb test/models/attachment_test.rb
```

This _used_ to pass consistently because [rails/rails][rails#41065]
introduced a call to `fixtures :all`, which introduces more variation in
the database's ID generation sequence. Without that line, `id` values
start at `1`, so the fact that calls to
`ActiveStorage::Attached::One#id` and `ActiveStorage::Blob#id` **both
return `1`** is purely coincidence.

The proposed resolution changes the test slightly. Prior to this change,
the identifier used during retrieval and verification fetched from
`@user.avatar.id`, where `@user.avatar` is an instance of
`ActiveStorage::Attached::One`. The verifier/retriever combination in
that test expected a signed identifier for an `ActiveStorage::Blob`
instance. The change involved retrieving an instance through
`@user.avatar.blob`.

To better emphasize how global the `fixtures :all` declaration is, move
it from the `test/fixture_set_test.rb` file to the `test/test_helper.rb`
file.

[rails#41065]: rails#41065
  • Loading branch information
seanpdoyle authored Jan 24, 2021
1 parent 0ad777c commit 948e9d7
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 4 deletions.
5 changes: 5 additions & 0 deletions activestorage/lib/active_storage/fixture_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@ class FixtureSet
# # tests/fixtures/action_text/blobs.yml
# second_thumbnail_blob: <%= ActiveStorage::FixtureSet.blob(
# filename: "second.svg",
# ) %>
#
# third_thumbnail_blob: <%= ActiveStorage::FixtureSet.blob(
# filename: "third.svg",
# content_type: "image/svg+xml",
# service_name: "public"
# ) %>
#
def self.blob(filename:, **attributes)
Expand Down
3 changes: 0 additions & 3 deletions activestorage/test/fixture_set_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
require "database/setup"

class ActiveStorage::FixtureSetTest < ActiveSupport::TestCase
self.fixture_path = File.expand_path("fixtures", __dir__)
self.use_transactional_tests = true

fixtures :all

def test_active_storage_blob
Expand Down
2 changes: 1 addition & 1 deletion activestorage/test/models/attachment_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class ActiveStorage::AttachmentTest < ActiveSupport::TestCase
blob = create_blob
@user.avatar.attach(blob)

signed_id_generated_old_way = ActiveStorage.verifier.generate(@user.avatar.id, purpose: :blob_id)
signed_id_generated_old_way = ActiveStorage.verifier.generate(@user.avatar.blob.id, purpose: :blob_id)
assert_equal blob, ActiveStorage::Blob.find_signed!(signed_id_generated_old_way)
end

Expand Down
2 changes: 2 additions & 0 deletions activestorage/test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class ActiveSupport::TestCase

include ActiveRecord::TestFixtures

self.fixture_path = File.expand_path("fixtures", __dir__)

setup do
ActiveStorage::Current.host = "https://example.com"
end
Expand Down

0 comments on commit 948e9d7

Please sign in to comment.