|
| 1 | +defmodule LiveDebugger.GenServers.EtsTableServerTest do |
| 2 | + @moduledoc false |
| 3 | + use ExUnit.Case, async: true |
| 4 | + |
| 5 | + alias LiveDebugger.Utils.PubSub, as: PubSubUtils |
| 6 | + alias LiveDebugger.GenServers.EtsTableServer |
| 7 | + |
| 8 | + test "start_link/1" do |
| 9 | + assert {:ok, _pid} = EtsTableServer.start_link() |
| 10 | + GenServer.stop(EtsTableServer) |
| 11 | + end |
| 12 | + |
| 13 | + test "init/1" do |
| 14 | + assert {:ok, %{}} = EtsTableServer.init([]) |
| 15 | + end |
| 16 | + |
| 17 | + describe "gen server api" do |
| 18 | + test "table!/1" do |
| 19 | + pid = :c.pid(0, 0, 1) |
| 20 | + |
| 21 | + LiveDebugger.MockEtsTableServer |
| 22 | + |> Mox.expect(:table!, fn ^pid -> :some_ref end) |
| 23 | + |
| 24 | + assert :some_ref = EtsTableServer.table!(pid) |
| 25 | + end |
| 26 | + |
| 27 | + test "delete_table!/1" do |
| 28 | + pid = :c.pid(0, 0, 1) |
| 29 | + |
| 30 | + LiveDebugger.MockEtsTableServer |
| 31 | + |> Mox.expect(:delete_table!, fn ^pid -> :ok end) |
| 32 | + |
| 33 | + assert :ok = EtsTableServer.delete_table!(pid) |
| 34 | + end |
| 35 | + end |
| 36 | + |
| 37 | + describe "handle_info/2" do |
| 38 | + test "deletes table ref after process down" do |
| 39 | + pid = :c.pid(0, 0, 1) |
| 40 | + ref = :ets.new(:test_table, []) |
| 41 | + |
| 42 | + other_pid = :c.pid(0, 0, 2) |
| 43 | + other_ref = :ets.new(:test_table, []) |
| 44 | + |
| 45 | + table_refs = %{pid => ref, other_pid => other_ref} |
| 46 | + |
| 47 | + topic = PubSubUtils.process_status_topic(pid) |
| 48 | + |
| 49 | + LiveDebugger.MockPubSubUtils |
| 50 | + |> Mox.expect(:broadcast, fn ^topic, {:process_status, :dead} -> :ok end) |
| 51 | + |
| 52 | + assert {:noreply, new_table_refs} = |
| 53 | + EtsTableServer.handle_info({:DOWN, :_, :process, pid, :_}, table_refs) |
| 54 | + |
| 55 | + assert :undefined == :ets.info(ref) |
| 56 | + assert nil == Map.get(new_table_refs, pid) |
| 57 | + |
| 58 | + assert [{:id, ^other_ref} | _] = :ets.info(other_ref) |
| 59 | + assert other_ref == Map.get(new_table_refs, other_pid) |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + describe "handle_call/3" do |
| 64 | + test "deletes table on event {:delete_table, pid}" do |
| 65 | + pid = :c.pid(0, 0, 1) |
| 66 | + ref = :ets.new(:test_table, []) |
| 67 | + |
| 68 | + other_pid = :c.pid(0, 0, 2) |
| 69 | + other_ref = :ets.new(:test_table, []) |
| 70 | + |
| 71 | + table_refs = %{pid => ref, other_pid => other_ref} |
| 72 | + |
| 73 | + assert {:reply, :ok, new_table_refs} = |
| 74 | + EtsTableServer.handle_call({:delete_table, pid}, self(), table_refs) |
| 75 | + |
| 76 | + assert :undefined == :ets.info(ref) |
| 77 | + assert nil == Map.get(new_table_refs, pid) |
| 78 | + |
| 79 | + assert [{:id, ^other_ref} | _] = :ets.info(other_ref) |
| 80 | + assert other_ref == Map.get(new_table_refs, other_pid) |
| 81 | + end |
| 82 | + |
| 83 | + test "ignores delete table on event {:delete_table, pid} when table does not exist" do |
| 84 | + pid = :c.pid(0, 0, 1) |
| 85 | + ref = :ets.new(:test_table, []) |
| 86 | + |
| 87 | + other_pid = :c.pid(0, 0, 2) |
| 88 | + |
| 89 | + table_refs = %{pid => ref} |
| 90 | + |
| 91 | + assert {:reply, :ok, new_table_refs} = |
| 92 | + EtsTableServer.handle_call({:delete_table, other_pid}, self(), table_refs) |
| 93 | + |
| 94 | + assert [{:id, ^ref} | _] = :ets.info(ref) |
| 95 | + assert ref == Map.get(new_table_refs, pid) |
| 96 | + |
| 97 | + assert nil == Map.get(new_table_refs, other_pid) |
| 98 | + end |
| 99 | + |
| 100 | + test "creates table on event {:get_or_create_table, pid}" do |
| 101 | + pid = :c.pid(0, 0, 1) |
| 102 | + table_refs = %{} |
| 103 | + |
| 104 | + assert {:reply, ref, new_table_refs} = |
| 105 | + EtsTableServer.handle_call({:get_or_create_table, pid}, self(), table_refs) |
| 106 | + |
| 107 | + assert [{:id, ^ref} | _] = :ets.info(ref) |
| 108 | + assert ref == Map.get(new_table_refs, pid) |
| 109 | + end |
| 110 | + |
| 111 | + test "returns existing table on event {:get_or_create_table, pid}" do |
| 112 | + pid = :c.pid(0, 0, 1) |
| 113 | + ref = :ets.new(:test_table, []) |
| 114 | + table_refs = %{pid => ref} |
| 115 | + |
| 116 | + assert {:reply, ^ref, new_table_refs} = |
| 117 | + EtsTableServer.handle_call({:get_or_create_table, pid}, self(), table_refs) |
| 118 | + |
| 119 | + assert ref == Map.get(new_table_refs, pid) |
| 120 | + end |
| 121 | + end |
| 122 | +end |
0 commit comments