-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Support :group
option in ExUnit
#13897
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
josevalim
merged 15 commits into
elixir-lang:main
from
tellerhq:async-test-partitioning
Oct 13, 2024
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
83e8e65
Support async_partition_key option in ExUnit
reisub c74322f
Add tests for :async and :async_partition_key
reisub 7c6ece6
Add test to ensure async partition order is predictable
reisub b0dd42f
Document :async_partition_key option
reisub 07e5429
Macro.escape() key option
reisub 8707d42
Avoid raising
reisub ade7605
Change option name to :group
reisub 3b9d737
Refactor for consistency, simplify state
reisub c1cfa3f
Remove unneeded spawn_modules/4 function head
reisub 27b463d
Simplify ExUnit.Server
reisub 55f38a4
Remove :group from reserved list
reisub fe3f66c
Update unique code to match new state format
reisub 9882f65
Apply suggestions from code review
reisub fa5e52c
Fix references after applying suggestions
reisub ad42780
Merge branch 'main' into async-test-partitioning
reisub 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
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 |
---|---|---|
|
@@ -9,15 +9,21 @@ defmodule ExUnit.Server do | |
GenServer.start_link(__MODULE__, :ok, name: @name) | ||
end | ||
|
||
def add_module(name, {async?, parameterize}) do | ||
def add_module(name, config) do | ||
%{ | ||
async?: async?, | ||
group: group, | ||
parameterize: parameterize | ||
} = config | ||
|
||
modules = | ||
if parameterize do | ||
Enum.map(parameterize, &{name, &1}) | ||
else | ||
[{name, %{}}] | ||
end | ||
|
||
case GenServer.call(@name, {:add, async?, modules}, @timeout) do | ||
case GenServer.call(@name, {:add, {async?, group}, modules}, @timeout) do | ||
:ok -> | ||
:ok | ||
|
||
|
@@ -51,6 +57,7 @@ defmodule ExUnit.Server do | |
state = %{ | ||
loaded: System.monotonic_time(), | ||
waiting: nil, | ||
async_groups: %{}, | ||
async_modules: :queue.new(), | ||
sync_modules: :queue.new() | ||
} | ||
|
@@ -74,10 +81,20 @@ defmodule ExUnit.Server do | |
|
||
# Called by the runner when --repeat-until-failure is used. | ||
def handle_call({:restore_modules, async_modules, sync_modules}, _from, state) do | ||
{async_modules, async_groups} = | ||
Enum.reduce(async_modules, {[], []}, fn | ||
{nil, [module]}, {modules, groups} -> | ||
{[{:module, module} | modules], groups} | ||
|
||
{group, group_modules}, {modules, groups} -> | ||
{[{:group, group} | modules], Map.put(groups, group, group_modules)} | ||
end) | ||
|
||
{:reply, :ok, | ||
%{ | ||
state | ||
| loaded: :done, | ||
async_groups: async_groups, | ||
async_modules: :queue.from_list(async_modules), | ||
reisub marked this conversation as resolved.
Show resolved
Hide resolved
|
||
sync_modules: :queue.from_list(sync_modules) | ||
josevalim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}} | ||
|
@@ -91,12 +108,18 @@ defmodule ExUnit.Server do | |
when is_integer(loaded) do | ||
state = | ||
if uniq? do | ||
async_groups = | ||
Map.new(state.async_groups, fn {group, modules} -> | ||
{group, Enum.uniq(modules)} | ||
end) | ||
|
||
async_modules = :queue.to_list(state.async_modules) |> Enum.uniq() |> :queue.from_list() | ||
sync_modules = :queue.to_list(state.sync_modules) |> Enum.uniq() |> :queue.from_list() | ||
|
||
%{ | ||
state | ||
| async_modules: async_modules, | ||
| async_groups: async_groups, | ||
async_modules: async_modules, | ||
sync_modules: sync_modules | ||
} | ||
else | ||
|
@@ -107,26 +130,40 @@ defmodule ExUnit.Server do | |
{:reply, diff, take_modules(%{state | loaded: :done})} | ||
end | ||
|
||
def handle_call({:add, true, names}, _from, %{loaded: loaded} = state) | ||
def handle_call({:add, {false = _async, _group}, names}, _from, %{loaded: loaded} = state) | ||
when is_integer(loaded) do | ||
state = | ||
update_in(state.sync_modules, &Enum.reduce(names, &1, fn name, q -> :queue.in(name, q) end)) | ||
|
||
{:reply, :ok, state} | ||
end | ||
|
||
def handle_call({:add, {true = _async, nil = _group}, names}, _from, %{loaded: loaded} = state) | ||
when is_integer(loaded) do | ||
state = | ||
update_in( | ||
state.async_modules, | ||
&Enum.reduce(names, &1, fn name, q -> :queue.in(name, q) end) | ||
&Enum.reduce(names, &1, fn name, q -> :queue.in({:module, name}, q) end) | ||
) | ||
|
||
{:reply, :ok, take_modules(state)} | ||
{:reply, :ok, state} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We lost a call to |
||
end | ||
|
||
def handle_call({:add, false, names}, _from, %{loaded: loaded} = state) | ||
def handle_call({:add, {true = _async, group}, names}, _from, %{loaded: loaded} = state) | ||
when is_integer(loaded) do | ||
state = | ||
update_in(state.sync_modules, &Enum.reduce(names, &1, fn name, q -> :queue.in(name, q) end)) | ||
case state.async_groups do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are setting the |
||
%{^group => entries} = async_groups -> | ||
{%{async_groups | group => names ++ entries}, state.async_modules} | ||
|
||
%{} = async_groups -> | ||
{Map.put(async_groups, group, names), :queue.in({:group, group}, state.async_modules)} | ||
end | ||
|
||
{:reply, :ok, state} | ||
end | ||
|
||
def handle_call({:add, _async?, _names}, _from, state), | ||
def handle_call({:add, {_async?, _group}, _names}, _from, state), | ||
do: {:reply, :already_running, state} | ||
|
||
defp take_modules(%{waiting: nil} = state) do | ||
|
@@ -145,9 +182,26 @@ defmodule ExUnit.Server do | |
state | ||
|
||
true -> | ||
{modules, async_modules} = take_until(count, state.async_modules) | ||
GenServer.reply(from, modules) | ||
%{state | async_modules: async_modules, waiting: nil} | ||
{async_modules, remaining_modules} = take_until(count, state.async_modules) | ||
|
||
{async_modules, remaining_groups} = | ||
Enum.reduce(async_modules, {[], state.async_groups}, fn | ||
{:module, module}, {collected_modules, async_groups} -> | ||
{[{nil, [module]} | collected_modules], async_groups} | ||
|
||
{:group, group}, {collected_modules, async_groups} -> | ||
{group_modules, async_groups} = Map.pop!(async_groups, group) | ||
{[{group, Enum.reverse(group_modules)} | collected_modules], async_groups} | ||
end) | ||
|
||
GenServer.reply(from, Enum.reverse(async_modules)) | ||
|
||
%{ | ||
state | ||
| async_groups: remaining_groups, | ||
async_modules: remaining_modules, | ||
waiting: nil | ||
} | ||
end | ||
end | ||
|
||
|
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The sync modules are not arriving in this shape and they are being skipped (because the group modules is treated as an empty map). I will add a pattern to help us catch it. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Damn, that masked the other issues!
Thanks for fixing it.
And bummer about the tests being flaky, it would have been nice to have tests for this functionality.