Too forgiving test in `rpn-calculator-inspection`

未关闭
#1,133 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
需要澄清
活跃度
停滞
技术栈
elixir
领域
testing-qa

调研方向

从 RPNCalculatorInspection.reliability_check/2 的练习测试开始,并运行现有测试套件。将它们与提供的 200/150/0 回归案例进行比较,然后确定是否应更改测试或练习的范围。完成的标准是:已覆盖约定的 timeout 行为,同时没有让解决方案超出该练习的教学范围。

由索引模型根据 Issue 内容生成。

描述

Tests for RPNCalculatorInspection.reliability_check/2 are too forgiving for calculations that take longer than 100ms. If the solution starts the checks in parallel and awaits the results iteratively all the tests pass, but they should not. When the solution awaits the first check and it does not complete in 100ms, it adds :timeout to results. But when awaiting the second check, it already has that 100ms "bonus" from the previous await. So if the second check takes 150ms to finish, the await will receive the response after 50ms and add :ok to results, whereas it should add :timeout.

Here is a test which covers this case:

    test "returns :timeout for all calculations lasting longer than 100ms" do
      inputs = [200, 150, 0]
      calculator = &:timer.sleep(&1)

      outputs = %{
        200 => :timeout,
        150 => :timeout,
        0 => :ok
      }

      assert RPNCalculatorInspection.reliability_check(calculator, inputs) == outputs
    end

And here is a solution which passes the existing tests, but not the above one:

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

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

    Process.flag(:trap_exit, trap_orig)
    results
  end

Here's a solution which will pass the above test as well.

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

    results =
      inputs
      |> Enum.map(fn input ->
        await_pid =
          spawn_link(fn ->
            check = start_reliability_check(calculator, input)
            results = await_reliability_check_result(check, %{})
            [{^input, result}] = Map.to_list(results)
            send(caller, {self(), result})
          end)

        {input, await_pid}
      end)
      |> Enum.reduce(%{}, fn {input, await_pid}, results ->
        receive do
          {^await_pid, result} -> Map.put(results, input, result)
        after
          200 -> raise "Timeout"
        end
      end)

    Process.flag(:trap_exit, trap_orig)
    results
  end

Hardening the test would lead to more complicated solutions which are probably beyond the educational scope of this exercise. But the existing tests do not sufficiently cover the goals set in the exercise. So I'm not sure what exactly to do here.

主要语言
Elixir
星标
665
派生
415
平均合并
13 小时 3 分钟
30 天内合并 PR
12

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

exercism/elixir 的其他 Issue

查看 exercism/elixir 的全部 Issue

相似的 Issue

更多 Elixir Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。