RPN Calculator Inspection - intermitent test failure

On the task rpn-calculator-inspection, there is a test :

test reliability_check returns a map when input list has 1000 elements and the calculator takes 50ms for each calculation

For me intermittently fails, although always seems pass the first time you run it

I think this may be a bug. I’ve searched for a solution to the task online and mine is the same idea. Even is the approach is wrong, I think the test should be setup so the test results are consistent.

There is some timing involved, the test reports

This test shouldn’t take this long to complete. Make sure to start all tasks first before awaiting them.

however when the test fails it takes seconds longer than when it passes. I dont think it’s just that the run time is on the threshold of the timeout, there seems to be something else going on that I dont understand.

Does anyone have any thoughts as to what might be going on?

One possibility is the testing infrastructure or environment happens to be slow at the exact right time. That’s a bit tricky to reproduce and debug. Another possibility is there’s something wrong in your code. Maybe something that alters state which persists across tests. That’s a lot easier to reproduce and debug. You can open a code review request or share code here (as text in a code black and not as an image) Ave someone may spot something.

Yes, it would have made sense to show the code, it’s below
I’m dont think I’m doing anything weird that should cause state persist, but any observations are appreciated

defmodule RPNCalculatorInspection do
  def start_reliability_check(calculator, input) do
    {:ok, pid} = Task.start_link(fn -> calculator.(input) end)
    %{input: input, pid: pid}
  end

  def await_reliability_check_result(%{pid: pid, input: input}, results) do
    Process.link(pid)

    receive do
      {:EXIT, ^pid, :normal} -> Map.put(results, input, :ok)
      {:EXIT, ^pid, _results} -> Map.put(results, input, :error)
    after
      100 -> Map.put(results, input, :timeout)
    end
  end

  def reliability_check(calculator, inputs) do
    flag = Process.flag(:trap_exit, true)

    results =
      inputs
      |> Enum.map(&start_reliability_check(calculator, &1))
      |> Enum.reduce(%{}, &await_reliability_check_result(&1, &2))

    Process.flag(:trap_exit, flag)

    results
  end

Remove the unnecessary call to Process.link(pid) from await_reliability_check_result and you should find that tests now reliably pass.