From 4dc3f9b7f2e72bc8831c5f8f932c352140e15f36 Mon Sep 17 00:00:00 2001 From: Shinto C V Date: Thu, 23 Nov 2023 17:43:13 +0530 Subject: [PATCH] Add test for race condition in community gardens (#1368) Co-authored-by: Angelika Tyborska --- CONTRIBUTING.md | 2 +- .../test/community_garden_test.exs | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 260905110..e54620660 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -40,7 +40,7 @@ Please keep the following in mind: - Each problem should have a test suite, an example solution, and a template file for the real implementation. Read about [the anatomy of practice exercises](https://github.com/exercism/docs/blob/main/building/tracks/practice-exercises.md) or [the anatomy of concept exercises](https://github.com/exercism/docs/blob/main/building/tracks/concept-exercises.md), depending on to which type of exercise you want to contribute. -- For practice exercises, `instructions.md` come from the [problem specifications](https://github.com/exercism/problem-specifications) repository. They cannot be changed without updating them in that repository first. If Elixir-specific changes are necessary, than can be appended to the instructions by creating a `instructions.append.md` file. +- For practice exercises, `instructions.md` come from the [problem specifications](https://github.com/exercism/problem-specifications) repository. They cannot be changed without updating them in that repository first. If Elixir-specific changes are necessary, they can be appended to the instructions by creating a `instructions.append.md` file. - For practice exercises, use typespecs in the example and template files as described [here](https://elixir-lang.org/getting-started/typespecs-and-behaviours.html). diff --git a/exercises/concept/community-garden/test/community_garden_test.exs b/exercises/concept/community-garden/test/community_garden_test.exs index 5594120e5..5de3344ee 100644 --- a/exercises/concept/community-garden/test/community_garden_test.exs +++ b/exercises/concept/community-garden/test/community_garden_test.exs @@ -45,6 +45,32 @@ defmodule CommunityGardenTest do assert plot_3.plot_id == 3 end + @tag task_id: 3 + test "registered plots have incremental unique id when registered concurrently" do + {:ok, pid} = CommunityGarden.start() + + total_plots = 20 + test_process_pid = self() + + Enum.each(1..total_plots, fn n -> + spawn(fn -> + plot = CommunityGarden.register(pid, "Mary Bumblebee #{n}") + send(test_process_pid, {n, plot}) + end) + end) + + plot_ids = + Enum.map(1..total_plots, fn n -> + receive do + {^n, plot} -> plot.plot_id + after + 100 -> nil + end + end) + + assert Enum.sort(plot_ids) == Enum.to_list(1..total_plots) + end + @tag task_id: 4 test "can release a plot" do assert {:ok, pid} = CommunityGarden.start()