-
Notifications
You must be signed in to change notification settings - Fork 16
thread safety: reporters and docs #175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cfa66e4
Make snap_diff reporter notifications safer
pftg ad46203
Update thread safety doc for snap_diff
pftg 330d068
Rename thread safety doc
pftg 3b564f5
Cover reporters snapshot mutation
pftg 18c9641
Fix thread safety issues found in review
pftg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| # Thread Safety Guide for Parallel Testing | ||
|
|
||
| This document explains how `snap_diff` behaves under Rails parallel tests with the `:thread` strategy. | ||
|
|
||
| ## Overview | ||
|
|
||
| `snap_diff` is thread safe for parallel test execution as long as global configuration is set before tests run. Per-thread state is isolated, and shared state is protected where it matters. | ||
|
|
||
| ## Architecture Summary | ||
|
|
||
| ### Per-thread Assertion Registry | ||
|
|
||
| Each thread gets its own `AssertionRegistry` stored in thread-local storage: | ||
|
|
||
| ```ruby | ||
| def registry | ||
| Thread.current[:capybara_screenshot_diff_registry] ||= AssertionRegistry.new | ||
| end | ||
| ``` | ||
|
|
||
| This prevents cross-thread leakage for assertions and screenshot naming. | ||
|
|
||
| ### Reporters Snapshot on Notify | ||
|
|
||
| Reporters are notified using a snapshot protected by an eagerly initialized mutex: | ||
|
|
||
| ```ruby | ||
| @reporters_mutex = Mutex.new | ||
|
|
||
| def notify_reporters(assertions) | ||
| reporters_snapshot = reporters_mutex.synchronize { reporters.dup } | ||
| reporters_snapshot.each { |reporter| reporter.record(assertions) } | ||
| end | ||
| ``` | ||
|
|
||
| This ensures a stable list while notifying without forcing a global lock around reporter work. | ||
|
|
||
| ### HTML Reporter Internal Lock | ||
|
|
||
| The HTML reporter protects `@failures`, `@total`, and `@finalized` with a mutex so `record` and `finalize` can run safely: | ||
|
|
||
| ```ruby | ||
| @mutex.synchronize do | ||
| return if @finalized | ||
| @total += total | ||
| @failures.concat(failures) | ||
| end | ||
| ``` | ||
|
|
||
| `@finalized` is set only after `write_report` succeeds, so a failed write can be retried. | ||
|
|
||
| ### Screenshot Naming Isolation | ||
|
|
||
| Each thread gets its own `ScreenshotNamer` via the per-thread registry, so counters, sections, and groups do not collide. | ||
|
|
||
| ### SnapManager Per Call | ||
|
|
||
| `SnapManager` returns a new instance for each call, avoiding shared mutable state. | ||
|
|
||
| ## Global Configuration | ||
|
|
||
| Configuration uses `mattr_accessor` and should be set once before tests run. Do not mutate config during parallel execution. | ||
|
|
||
| ## Parallel Test Lifecycle | ||
|
|
||
| - Setup: per-thread registry is created, config is read | ||
| - Execution: assertions are added to the thread-local registry | ||
| - Teardown: `verify` and `reset` operate on the thread-local registry, reporters are notified | ||
| - Exit: reporters finalize once per process (using mutex-protected snapshot) | ||
|
|
||
| ## Usage Examples | ||
|
|
||
| ```ruby | ||
| parallelize(workers: :number_of_processors, with: :threads) | ||
|
|
||
| Capybara::Screenshot::Diff.configure do |screenshot, diff| | ||
| screenshot.window_size = [1280, 1024] | ||
| screenshot.save_path = "doc/screenshots" | ||
| diff.tolerance = 0.001 | ||
| end | ||
| ``` | ||
|
|
||
| ## Do and Do Not | ||
|
|
||
| Do: | ||
| - Set config once in test helper | ||
| - Pass per-screenshot options in the call | ||
|
|
||
| Do not: | ||
| - Change global config inside tests | ||
| - Manually mutate registry internals | ||
|
|
||
| ## File System Notes | ||
|
|
||
| - Paths are unique per screenshot name and counter | ||
| - `FileUtils.mv` is atomic on most file systems | ||
| - Directory creation uses `mkpath` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "test_helper" | ||
|
|
||
| module CapybaraScreenshotDiff | ||
| class ReportersMutexTest < ActiveSupport::TestCase | ||
| setup do | ||
| @original_reporters = CapybaraScreenshotDiff.reporters.dup | ||
| CapybaraScreenshotDiff.reporters.clear | ||
| end | ||
|
|
||
| teardown do | ||
| CapybaraScreenshotDiff.reporters.clear | ||
| CapybaraScreenshotDiff.reporters.concat(@original_reporters) | ||
| end | ||
|
|
||
| test "reporters_mutex is eagerly initialized" do | ||
| assert_instance_of Mutex, CapybaraScreenshotDiff.reporters_mutex | ||
| end | ||
|
|
||
| test "reporters_mutex returns the same instance" do | ||
| assert_same CapybaraScreenshotDiff.reporters_mutex, CapybaraScreenshotDiff.reporters_mutex | ||
| end | ||
|
|
||
| test "reporters notification iterates over snapshot" do | ||
| received = [] | ||
|
|
||
| mutating_reporter = Class.new do | ||
| define_method :record do |assertions| | ||
| received << [:original, assertions] | ||
| CapybaraScreenshotDiff.reporters.clear | ||
| CapybaraScreenshotDiff.reporters << Class.new { | ||
| define_method(:record) { |a| received << [:added, a] } | ||
| }.new | ||
| end | ||
| end.new | ||
|
|
||
| CapybaraScreenshotDiff.reporters << mutating_reporter | ||
|
|
||
| assertions = [:some, :assertions] | ||
|
|
||
| assert_nothing_raised do | ||
| CapybaraScreenshotDiff.send(:notify_reporters, assertions) | ||
| end | ||
|
|
||
| assert_equal [[:original, assertions]], received | ||
| end | ||
| end | ||
| end | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.