Use ExUnit’s test supervisor to start a fresh process for each test, exercise a GenServer through its public API, and verify supervision behavior by triggering a controlled exit and checking the configured restart outcome. Choose the startup helper, exit reason, and assertions to match the process’s contract; avoid arbitrary sleeps and account for shared resources when enabling asynchronous tests.
How do I start a process in ExUnit and clean it up?
Prefer start_supervised!/2 over starting a linked process directly in each test. ExUnit’s test supervisor owns the child and stops it before the next test begins, preventing a process from one test from lingering into another. The official ExUnit.Callbacks documentation describes these helpers and their lifecycle.
use ExUnit.Case, async: true
setup do
server = start_supervised!({MyApp.Counter, 0})
%{server: server}
end
test "increments the counter", %{server: server} do
assert MyApp.Counter.value(server) == 0
assert MyApp.Counter.increment(server) == 1
end
The module and argument tuple must fit the process’s child specification and start_link contract. Use the raising helper when startup failure should fail setup immediately and return the child PID.
Choose the helper based on the failure you need to observe
| Test need | Mechanism | What it establishes |
|---|---|---|
| Start a process and clean it up at test completion | start_supervised!/2 |
Returns the PID on success; raises if startup fails. The child is managed by ExUnit’s test supervisor, not linked to the test process. |
| Inspect a startup success or error result | start_supervised/2 |
Lets the test examine the returned {:ok, pid} or {:error, reason}. |
| Make a child crash propagate to the test | start_link_supervised!/2 |
Starts a linked child so an unexpected failure can fail the test. |
| Stop a test-supervised child before the test ends | stop_supervised/1 |
Stops the child by its child ID. Simply terminating a restartable child may prompt its supervisor to restart it. |
These helpers do not make every process failure equivalent: a monitor lets you assert termination as an expected event, while a link makes a linked process’s unexpected failure propagate. Choose according to whether the test should observe a crash or fail because of it.
Quick wins for a faster PC:
Clear out junk files and repair common Windows errorsFree Scan →Scan for outdated or missing drivers - takes under a minuteDriver Scan →#1 Best Overall
- Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
- Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
- Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
- Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
- 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.
How do I test a GenServer in Elixir?
Test ordinary behavior through the GenServer’s public client API. Call its synchronous or asynchronous functions and assert replies or state transitions that callers depend on. The GenServer guide demonstrates client-server testing and test-supervised startup.
Assert observable outcomes, not incidental implementation
- For a synchronous operation, assert its returned value and, if relevant, the next result visible through the API.
- For an asynchronous contract, use
assert_receivewith a bounded timeout to check the expected message. - If termination is part of the contract, monitor the PID and assert the
:DOWNmessage and reason. - Avoid reading internal state or testing callback details unless that internal detail is itself part of the required contract.
Use replies, messages, and monitor signals as synchronization. An arbitrary sleep only waits for a guessed interval; it does not prove the process reached the condition the test needs.
Rank #2
- 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
- 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
- Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
- 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
- What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.
How do I test that a supervisor restarts a process?
Start the supervisor or relevant subtree under ExUnit’s test supervisor, identify the child by its child ID, and induce a controlled failure. Then assert the configured result: whether the child returns, whether its PID changes, and whether affected siblings restart. A child specification defines the child’s start, shutdown, and restart behavior; the supervisor strategy defines how a failure affects the rest of the tree. See the versioned Supervisor documentation for the applicable configuration details.
Account for restart mode and exit reason
| Child restart mode | Expected response to exit |
|---|---|
:permanent |
The child is restarted regardless of exit reason. |
:transient |
The child is restarted after an abnormal exit, not a normal termination. |
:temporary |
The child is not restarted. |
Assert the configured supervision strategy
| Strategy | Expected effect of a child failure | Useful assertion |
|---|---|---|
:one_for_one |
The failed child is the restart focus. | Check the failed child’s new PID and initialized behavior; check that unrelated siblings retain their PIDs if that matters to the contract. |
:one_for_all |
All children in the group are restarted. | Compare captured PIDs before and after for the failed child and each sibling in the group. |
:rest_for_one |
The failed child and children started after it are restarted. | Check that the failed child and later siblings change PID, while earlier siblings retain theirs where relevant. |
Use a test-only message, deliberately failing input, or another controlled trigger suitable for the child. Synchronize on an explicit message, a supervisor API result, or monitoring rather than waiting an arbitrary duration. A new PID alone proves that a process restarted, not that it returned to the required state; call its public API to check that state as well.
Recommended Free Tools
Rank #3
- Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
- Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
- Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
- Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
- What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.
test "restarts a permanent worker after an abnormal exit" do
supervisor = start_supervised!({MyApp.WorkerSupervisor, []})
old_pid = MyApp.WorkerSupervisor.worker_pid(supervisor)
send(old_pid, :crash_for_test)
assert_receive {:worker_restarted, new_pid}
refute old_pid == new_pid
assert MyApp.Worker.get_state(new_pid) == :initial_state
end
This is a pattern, not a drop-in implementation: adapt the child ID, crash trigger, restart mode, and synchronization signal to the application. The child must be configured to restart for the induced exit; for example, a transient child requires an abnormal exit. Avoid identifying a child by module alone if the tree can contain multiple children of that module. Supervisor details can vary by Elixir version, so consult documentation matching the project’s pinned version.
How do I test a DynamicSupervisor?
Start a fresh DynamicSupervisor for the test, then add and remove children through its API. Assert that a child is present after a successful start and absent after a stop or termination consistent with its restart mode. ExUnit’s outer test supervisor provides lifecycle cleanup for the test’s processes. The DynamicSupervisor guide covers the dynamic-child model.
Rank #4
- Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
- Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
- Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
- Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
- Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft
Should I use async tests for OTP processes?
Use async: true only if concurrently running tests cannot interfere through shared mutable state or external resources. Per-test process ownership isolates those supervised processes, but it does not isolate registered names, files, ports, external services, or other global resources.
- Give registered processes unique names when tests run concurrently.
- Use resources scoped to the individual test where possible.
- Disable async execution for tests that share state or cannot safely isolate an external resource.
ExUnit documentation also covers test grouping and parameterized runs in newer releases; verify the features available in the Elixir version pinned by the project. The current Elixir documentation index reported Elixir v1.20.4 as stable on October 4, 2026, with Erlang/OTP 27, 28, and 29 listed as supported. The linked ExUnit and Supervisor API pages here are versioned v1.18.0 and v1.15.8 references, respectively; match implementation details to the versions your application uses.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Quick Recap
Best Value
- 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
- Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
- Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
- HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
- What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

