When the test is complete, the adapter does not commit the transaction but rolls it back, abandoning all changes made within it and leaving other tests happily unaware. This means that the adapter starts a database transaction for your test, making all changes to the database tentative and therefore invisible to other connections. This works by taking advantage of database transactions.īy default, when you checkout/1 a connection, it’s set to sandbox: true. Whatever one test does with the database will have no effect on another test. The basic idea is that whenever a test is running it can pretend it has its own database which no other test uses. SQL SandboxĮ is “a pool for concurrent transactional tests” per Repo in your system. It works as a connection pool, but its main purpose is to help tests run cleanly. In dev or production we typically configure Ecto to use Poolboy. Whether your database is configured to allow 10 simultaneous connections or 5,000, Elixir can run a much larger number of processes.īecause of this Elixir processes must take turns. This is because they each require some memory for things like transaction state and local settings. Like paper books, database connections are limited resources. If every copy of a printed book has been checked out, you won’t be able to read it until someone checks in a copy. Database Connection PoolsĪ database connection pool is a bit like a local library. Now let’s take a look at how database access works in concurrent tests. end end defmodule SubtractionTests do use ExUnit.Case, async : true test "it subtracts" do #. They could even be submodules within the same file:ĭefmodule MyApp.MathTest do defmodule AdditionTests do use ExUnit.Case, async : true test "it adds" do #. On the other hand, if test A and test B should run concurrently, we should put them in separate test modules and mark both modules async: true.īreaking apart a large test module into smaller ones will automatically increase test concurrency. If we have two tests which should not run at the same time, we can ensure that by either marking both of their test modules as async: false or by putting them in the same test module. We can use this knowledge to control test concurrency. This fact is worth highlighting: tests within the same test module always run one at a time, even if that module is marked async. In an umbrella project, the timeline above repeats once per application, serially. This time, tests are run for one module at a time.Īs before, tests within the same test module run serially. However, tests within the same test module run serially.įinally, using one process per test, ExUnit runs the tests for each module marked async: false. The tests in one async module will run concurrently with the tests in another async module. Next, using one process per test, ExUnit runs the tests for all modules marked async: true. test_helper.exs ModuleATests (file) test1 test2… ModuleDTests test1 test2… ModuleETests test1 test2… ModuleBTests test1 test2… ModuleCTests test1 test2… Test modules marked async: true Test modules marked async: false Setupįirst, test_helper.exs is run, with nothing else running concurrently. The timeline of an ExUnit test run looks something like this: test_concurrency Created with Sketch. In Elixir, the fastest applications take full advantage of concurrency, and the same thing can be said about test suites.īecause database access is a common test performance bottleneck, Ecto SQL provides tools for concurrent database tests.īut to use them well, you need to understand how they work. With no special effort, the entire Phoenix application’s test suite ran in two seconds. Years later when I had the same experience on an Elixir web development project, I was floored. The fast feedback cycle kept me happy and productive. There was never a pause to interrupt my train of thought, and I always knew everything my change affected. I typed in the test command, pressed enter, and before I could blink, the tests were finished.Įvery time I made a change, I ran the whole suite. I had created a small library which unlike the applications I’d worked on, didn’t touch a database. I remember the first time I ran a really fast software test suite. This post refers to Elixir 1.8 and Ecto 3.0, which are the latest versions at publication time.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |