Conversation
|
@ftes what do you think? I don't love it, but I love my |
455dc74 to
7c99987
Compare
| doc: | ||
| "Define custom Playwright [selector engines](https://playwright.dev/docs/extensibility#custom-selector-engines)." | ||
| ], | ||
| sandbox_owner_shutdown_delay: [ |
There was a problem hiding this comment.
If we add this key to setup_keys below (line 130), this could be overridden per test/describe/suite via exunit @tag sandbox_owner_shutdown_delay: x.
Wdyt?
There was a problem hiding this comment.
I love the idea. Will try it out.
There was a problem hiding this comment.
This works well and makes using this much nicer, since we don't have to keep increasing the global value for the test that has the longest-running dangling processes. 😌
|
Hey Nathan, this is a highly welcome PR! Can we avoid the slowdown by stopping the sandbox owner in a separate, unlinked, process? I guess the overall question is: Do we care if stopping the sandbox fails? |
|
The existing implementation was copied from wallaby. Given that wallaby has been around a lot longer, and the standard |
I did some tinkering, but every way I tried this created errors, and I think I finally realized why. The
If we use a Bottom line: we need the |
|
Regarding wallaby - we had lots of flakiness with our wallaby tests in the past and eventually abandoned them. (I say "we", it was mostly another team, so I can't say much in detail.) So maybe a different approach here is good. |
|
Since we seem to be on the same page and I'm up to 7 commits, I'm going to squash it down to one clean one. |
3942a2e to
a308bc4
Compare
lib/phoenix_test/playwright/case.ex
Outdated
| defp maybe_start_sandbox_owner(repo, context, config) do | ||
| case start_sandbox_owner(repo, context) do | ||
| {:ok, pid} -> | ||
| delay = context[:sandbox_shutdown_delay] || config[:sandbox_shutdown_delay] |
There was a problem hiding this comment.
It will be nil in the context if not specified via tag
There was a problem hiding this comment.
config[:sandbox_owner_shutdown_delay] should be sufficient.
It is filled with the tag value from ExUnit context, and falls back to global config otherwise.
Case.do_setup
a308bc4 to
7735c92
Compare
| Delay in milliseconds before shutting down the Ecto sandbox owner after a | ||
| test ends. Use this to allow LiveViews and other processes in your app | ||
| time to stop using database connections before the sandbox owner is | ||
| terminated. Default is 0 (immediate shutdown). |
|
|
||
| In that case, you may encounter ownership errors like: | ||
| ``` | ||
| ** (DBConnection.OwnershipError) cannot find owner for ... |
Is this only an issue in I'll try to find some time to tinker a bit myself. |
|
Can you reproduce the original error and validate the fix in an example repo (e.g. |
The issue of having a subsequent test re-use the old test's sandbox is, yes. So in
I'm not sure what you mean here. We do need sandboxing both in |
I'll see what I can do. |
|
@ftes see Query page for a demo |
Previously, the test process itself was the made owner of the sandbox process, which meant the sandbox would be terminated when the test terminated. Unlike some other kinds of tests, playwright tests are not linked to the LiveViews and other processes that are being tested, so those processes don't terminate with the test and may go on trying to use the sandbox connection after the test terminates. If they do, they will raise ownership errors. Switch to `Ecto.Adapters.SQL.Sandbox.html.start_owner!/2`, creating a separate process to own the sandbox connection, which is shut down after the test process terminates using `on_exit/1`. This reduces ownership errors that may occur after the test terminates. By default, this is done with no delay, but `sandbox_shutdown_delay` can be configured or specified via tag, which will wait that long before terminating the sandbox during `on_exit/1`.
32af1aa to
4390b8a
Compare
ftes
left a comment
There was a problem hiding this comment.
I've added some Ecto tests.
Maybe we can use improve those as a follow-up.
Either way: Thanks so much Nathan!
|
I've added some ecto based tests based your example (ftes/phoenix_test_playwright_example#5). If you have time to look at them, that would be great. |
|
Thank you! |
| pid = Sandbox.start_owner!(repo, shared: !context.async) | ||
| {:ok, pid} | ||
| rescue | ||
| _ -> {:error, :probably_already_started} |
There was a problem hiding this comment.
@nathanl Do you remember if this blanket rescue clause was just a guess, or handling actual errors you saw?
I've got a report of this rescue masking checkout errors.


Previously, I needed to add
Process.sleep/1calls to the ends of some of my tests to allow any LiveViews that were querying via the sandbox time to shut down before the test process ended; otherwise I would see connection ownership errors being logged. This PR makes two changes to remove that need.Ecto.Adapters.SQL.Sandbox.html.start_owner!/2to make the sandbox owner not be the test process, and use anon_exit/1to terminate the sandbox after the test process terminates, as shown here.sandbox_owner_shutdown_delayoption so users can specify a number of additional milliseconds to wait before shutting the sandbox down.Item 2 feels a bit smelly TBH, because it's a global (not per-test) option, and only guesswork can find the right value. But given that Playwright tests can't manage the LV processes in any way, I don't know a better solution, and in my case a
100ms delay seems fine.