Lines Matching refs:registry

13     {:ok, %{registry: config.test, partitions: partitions}}
20 … test "starts configured number of partitions", %{registry: registry, partitions: partitions} do
21 assert length(Supervisor.which_children(registry)) == partitions
24 test "counts 0 keys in an empty registry", %{registry: registry} do
25 assert 0 == Registry.count(registry)
28 test "counts the number of keys in a registry", %{registry: registry} do
29 {:ok, _} = Registry.register(registry, "hello", :value)
30 {:ok, _} = Registry.register(registry, "world", :value)
32 assert 2 == Registry.count(registry)
35 test "has unique registrations", %{registry: registry} do
36 {:ok, pid} = Registry.register(registry, "hello", :value)
38 assert Registry.keys(registry, self()) == ["hello"]
39 assert Registry.values(registry, "hello", self()) == [:value]
41 assert {:error, {:already_registered, pid}} = Registry.register(registry, "hello", :value)
43 assert Registry.keys(registry, self()) == ["hello"]
44 assert Registry.values(registry, "hello", self()) == [:value]
46 {:ok, pid} = Registry.register(registry, "world", :value)
48 assert Registry.keys(registry, self()) |> Enum.sort() == ["hello", "world"]
51 test "has unique registrations across processes", %{registry: registry} do
52 {_, task} = register_task(registry, "hello", :value)
53 Process.link(Process.whereis(registry))
54 assert Registry.keys(registry, task) == ["hello"]
55 assert Registry.values(registry, "hello", task) == [:value]
58 Registry.register(registry, "hello", :recent)
60 assert Registry.keys(registry, self()) == []
61 assert Registry.values(registry, "hello", self()) == []
64 assert Process.whereis(registry) in links
67 test "has unique registrations even if partition is delayed", %{registry: registry} do
68 {owner, task} = register_task(registry, "hello", :value)
70 assert Registry.register(registry, "hello", :other) ==
75 Registry.register(registry, "hello", :other)
76 assert Registry.lookup(registry, "hello") == [{self(), :other}]
79 test "supports match patterns", %{registry: registry} do
81 {:ok, _} = Registry.register(registry, "hello", value)
82 assert Registry.match(registry, "hello", {1, :_, :_}) == [{self(), value}]
83 assert Registry.match(registry, "hello", {1.0, :_, :_}) == []
84 assert Registry.match(registry, "hello", {:_, :atom, :_}) == [{self(), value}]
85 assert Registry.match(registry, "hello", {:"$1", :_, :"$1"}) == [{self(), value}]
86 assert Registry.match(registry, "hello", :_) == [{self(), value}]
87 assert Registry.match(registry, :_, :_) == []
90 {:ok, _} = Registry.register(registry, "world", value2)
91 assert Registry.match(registry, "world", %{b: "b"}) == [{self(), value2}]
94 test "supports guard conditions", %{registry: registry} do
96 {:ok, _} = Registry.register(registry, "hello", value)
98 assert Registry.match(registry, "hello", {:_, :_, :"$1"}, [{:>, :"$1", 1}]) ==
101 assert Registry.match(registry, "hello", {:_, :_, :"$1"}, [{:>, :"$1", 2}]) == []
103 assert Registry.match(registry, "hello", {:_, :"$1", :_}, [{:is_atom, :"$1"}]) ==
107 test "count_match supports match patterns", %{registry: registry} do
109 {:ok, _} = Registry.register(registry, "hello", value)
110 assert 1 == Registry.count_match(registry, "hello", {1, :_, :_})
111 assert 0 == Registry.count_match(registry, "hello", {1.0, :_, :_})
112 assert 1 == Registry.count_match(registry, "hello", {:_, :atom, :_})
113 assert 1 == Registry.count_match(registry, "hello", {:"$1", :_, :"$1"})
114 assert 1 == Registry.count_match(registry, "hello", :_)
115 assert 0 == Registry.count_match(registry, :_, :_)
118 {:ok, _} = Registry.register(registry, "world", value2)
119 assert 1 == Registry.count_match(registry, "world", %{b: "b"})
122 test "count_match supports guard conditions", %{registry: registry} do
124 {:ok, _} = Registry.register(registry, "hello", value)
126 assert 1 == Registry.count_match(registry, "hello", {:_, :_, :"$1"}, [{:>, :"$1", 1}])
127 assert 0 == Registry.count_match(registry, "hello", {:_, :_, :"$1"}, [{:>, :"$1", 2}])
128 assert 1 == Registry.count_match(registry, "hello", {:_, :"$1", :_}, [{:is_atom, :"$1"}])
131 test "unregister_match supports patterns", %{registry: registry} do
133 {:ok, _} = Registry.register(registry, "hello", value)
135 Registry.unregister_match(registry, "hello", {2, :_, :_})
136 assert Registry.lookup(registry, "hello") == [{self(), value}]
137 Registry.unregister_match(registry, "hello", {1.0, :_, :_})
138 assert Registry.lookup(registry, "hello") == [{self(), value}]
139 Registry.unregister_match(registry, "hello", {:_, :atom, :_})
140 assert Registry.lookup(registry, "hello") == []
143 test "unregister_match supports guards", %{registry: registry} do
145 {:ok, _} = Registry.register(registry, "hello", value)
147 Registry.unregister_match(registry, "hello", {:"$1", :_, :_}, [{:<, :"$1", 2}])
148 assert Registry.lookup(registry, "hello") == []
151 test "unregister_match supports tricky keys", %{registry: registry} do
152 {:ok, _} = Registry.register(registry, :_, :foo)
153 {:ok, _} = Registry.register(registry, "hello", "b")
155 Registry.unregister_match(registry, :_, :foo)
156 assert Registry.lookup(registry, :_) == []
157 assert Registry.keys(registry, self()) |> Enum.sort() == ["hello"]
160 test "compares using ===", %{registry: registry} do
161 {:ok, _} = Registry.register(registry, 1.0, :value)
162 {:ok, _} = Registry.register(registry, 1, :value)
163 assert Registry.keys(registry, self()) |> Enum.sort() == [1, 1.0]
166 test "updates current process value", %{registry: registry} do
167 assert Registry.update_value(registry, "hello", &raise/1) == :error
168 register_task(registry, "hello", :value)
169 assert Registry.update_value(registry, "hello", &raise/1) == :error
171 Registry.register(registry, "world", 1)
172 assert Registry.lookup(registry, "world") == [{self(), 1}]
173 assert Registry.update_value(registry, "world", &(&1 + 1)) == {2, 1}
174 assert Registry.lookup(registry, "world") == [{self(), 2}]
177 test "dispatches to a single key", %{registry: registry} do
179 assert Registry.dispatch(registry, "hello", fun) == :ok
181 {:ok, _} = Registry.register(registry, "hello", :value)
184 assert Registry.dispatch(registry, "hello", fun)
189 test "unregisters process by key", %{registry: registry} do
190 :ok = Registry.unregister(registry, "hello")
192 {:ok, _} = Registry.register(registry, "hello", :value)
193 {:ok, _} = Registry.register(registry, "world", :value)
194 assert Registry.keys(registry, self()) |> Enum.sort() == ["hello", "world"]
196 :ok = Registry.unregister(registry, "hello")
197 assert Registry.keys(registry, self()) == ["world"]
199 :ok = Registry.unregister(registry, "world")
200 assert Registry.keys(registry, self()) == []
203 test "unregisters with no entries", %{registry: registry} do
204 assert Registry.unregister(registry, "hello") == :ok
207 test "unregisters with tricky keys", %{registry: registry} do
208 {:ok, _} = Registry.register(registry, :_, :foo)
209 {:ok, _} = Registry.register(registry, "hello", "b")
211 Registry.unregister(registry, :_)
212 assert Registry.lookup(registry, :_) == []
213 assert Registry.keys(registry, self()) |> Enum.sort() == ["hello"]
217 test "allows listeners", %{registry: registry, listener: listener} do
219 {_, task} = register_task(registry, "hello", :world)
220 assert_received {:register, ^registry, "hello", ^task, :world}
223 {:ok, _} = Registry.register(registry, "world", :value)
224 assert_received {:register, ^registry, "world", ^self, :value}
226 :ok = Registry.unregister(registry, "world")
227 assert_received {:unregister, ^registry, "world", ^self}
230 test "links and unlinks on register/unregister", %{registry: registry} do
231 {:ok, pid} = Registry.register(registry, "hello", :value)
235 {:ok, pid} = Registry.register(registry, "world", :value)
239 :ok = Registry.unregister(registry, "hello")
243 :ok = Registry.unregister(registry, "world")
248 test "raises on unknown registry name" do
249 assert_raise ArgumentError, ~r/unknown registry/, fn ->
254 test "via callbacks", %{registry: registry} do
255 name = {:via, Registry, {registry, "hello"}}
273 test "uses value provided in via", %{registry: registry} do
274 name = {:via, Registry, {registry, "hello", :value}}
276 assert Registry.lookup(registry, "hello") == [{pid, :value}]
279 test "empty list for empty registry", %{registry: registry} do
280 assert Registry.select(registry, [{{:_, :_, :_}, [], [:"$_"]}]) == []
283 test "select all", %{registry: registry} do
284 name = {:via, Registry, {registry, "hello"}}
286 {:ok, _} = Registry.register(registry, "world", :value)
288 assert Registry.select(registry, [{{:"$1", :"$2", :"$3"}, [], [{{:"$1", :"$2", :"$3"}}]}])
293 test "select supports full match specs", %{registry: registry} do
295 {:ok, _} = Registry.register(registry, "hello", value)
298 Registry.select(registry, [
303 Registry.select(registry, [
308 Registry.select(registry, [
313 Registry.select(registry, [
317 assert [] == Registry.select(registry, [{{:"$1", :"$2", {1.0, :_, :_}}, [], [:"$_"]}])
320 Registry.select(registry, [
326 Registry.select(registry, [
332 {:ok, _} = Registry.register(registry, "world", value2)
335 Registry.select(registry, [{{"world", self(), %{b: "b"}}, [], [:match]}])
338 Registry.select(registry, [{{:"$1", :_, :_}, [], [:"$1"]}]) |> Enum.sort()
341 test "select supports guard conditions", %{registry: registry} do
343 {:ok, _} = Registry.register(registry, "hello", value)
346 Registry.select(registry, [
352 Registry.select(registry, [
357 Registry.select(registry, [
362 test "select allows multiple specs", %{registry: registry} do
363 {:ok, _} = Registry.register(registry, "hello", :value)
364 {:ok, _} = Registry.register(registry, "world", :value)
367 Registry.select(registry, [
374 test "raises on incorrect shape of match spec", %{registry: registry} do
376 Registry.select(registry, [{:_, [], []}])
381 %{registry: registry, partitions: partitions} do
382 assert sum_pid_entries(registry, partitions) == 0
384 {:ok, pid} = Registry.register(registry, "hello", :value)
386 assert sum_pid_entries(registry, partitions) == 1
388 {:ok, pid} = Registry.register(registry, "world", :value)
390 assert sum_pid_entries(registry, partitions) == 2
393 Registry.register(registry, "hello", :value)
395 assert sum_pid_entries(registry, partitions) == 2
399 %{registry: registry, partitions: partitions} do
400 assert sum_pid_entries(registry, partitions) == 0
402 {_, task} = register_task(registry, "hello", :value)
403 Process.link(Process.whereis(registry))
405 assert sum_pid_entries(registry, partitions) == 1
407 {:ok, pid} = Registry.register(registry, "world", :value)
409 assert sum_pid_entries(registry, partitions) == 2
412 Registry.register(registry, "hello", :recent)
414 assert sum_pid_entries(registry, partitions) == 2
423 … test "starts configured number of partitions", %{registry: registry, partitions: partitions} do
424 assert length(Supervisor.which_children(registry)) == partitions
427 test "counts 0 keys in an empty registry", %{registry: registry} do
428 assert 0 == Registry.count(registry)
431 test "counts the number of keys in a registry", %{registry: registry} do
432 {:ok, _} = Registry.register(registry, "hello", :value)
433 {:ok, _} = Registry.register(registry, "hello", :value)
435 assert 2 == Registry.count(registry)
438 test "has duplicate registrations", %{registry: registry} do
439 {:ok, pid} = Registry.register(registry, "hello", :value)
441 assert Registry.keys(registry, self()) == ["hello"]
442 assert Registry.values(registry, "hello", self()) == [:value]
444 assert {:ok, pid} = Registry.register(registry, "hello", :value)
446 assert Registry.keys(registry, self()) == ["hello", "hello"]
447 assert Registry.values(registry, "hello", self()) == [:value, :value]
449 {:ok, pid} = Registry.register(registry, "world", :value)
451 assert Registry.keys(registry, self()) |> Enum.sort() == ["hello", "hello", "world"]
454 test "has duplicate registrations across processes", %{registry: registry} do
455 {_, task} = register_task(registry, "hello", :world)
456 assert Registry.keys(registry, self()) == []
457 assert Registry.keys(registry, task) == ["hello"]
458 assert Registry.values(registry, "hello", self()) == []
459 assert Registry.values(registry, "hello", task) == [:world]
461 assert {:ok, _pid} = Registry.register(registry, "hello", :value)
462 assert Registry.keys(registry, self()) == ["hello"]
463 assert Registry.values(registry, "hello", self()) == [:value]
466 test "compares using matches", %{registry: registry} do
467 {:ok, _} = Registry.register(registry, 1.0, :value)
468 {:ok, _} = Registry.register(registry, 1, :value)
469 assert Registry.keys(registry, self()) |> Enum.sort() == [1, 1.0]
472 test "dispatches to multiple keys in serial", %{registry: registry} do
477 assert Registry.dispatch(registry, "hello", fun, parallel: false) == :ok
479 {:ok, _} = Registry.register(registry, "hello", :value1)
480 {:ok, _} = Registry.register(registry, "hello", :value2)
481 {:ok, _} = Registry.register(registry, "world", :value3)
488 assert Registry.dispatch(registry, "hello", fun, parallel: false)
499 assert Registry.dispatch(registry, "world", fun, parallel: false)
509 %{registry: registry, partitions: partitions} = context
514 assert Registry.dispatch(registry, "hello", fun, parallel: true) == :ok
516 {:ok, _} = Registry.register(registry, "hello", :value1)
517 {:ok, _} = Registry.register(registry, "hello", :value2)
518 {:ok, _} = Registry.register(registry, "world", :value3)
530 assert Registry.dispatch(registry, "hello", fun, parallel: true)
546 assert Registry.dispatch(registry, "world", fun, parallel: true)
555 test "unregisters by key", %{registry: registry} do
556 {:ok, _} = Registry.register(registry, "hello", :value)
557 {:ok, _} = Registry.register(registry, "hello", :value)
558 {:ok, _} = Registry.register(registry, "world", :value)
559 assert Registry.keys(registry, self()) |> Enum.sort() == ["hello", "hello", "world"]
561 :ok = Registry.unregister(registry, "hello")
562 assert Registry.keys(registry, self()) == ["world"]
564 :ok = Registry.unregister(registry, "world")
565 assert Registry.keys(registry, self()) == []
568 test "unregisters with no entries", %{registry: registry} do
569 assert Registry.unregister(registry, "hello") == :ok
572 test "unregisters with tricky keys", %{registry: registry} do
573 {:ok, _} = Registry.register(registry, :_, :foo)
574 {:ok, _} = Registry.register(registry, :_, :bar)
575 {:ok, _} = Registry.register(registry, "hello", "a")
576 {:ok, _} = Registry.register(registry, "hello", "b")
578 Registry.unregister(registry, :_)
579 assert Registry.keys(registry, self()) |> Enum.sort() == ["hello", "hello"]
582 test "supports match patterns", %{registry: registry} do
586 {:ok, _} = Registry.register(registry, "hello", value1)
587 {:ok, _} = Registry.register(registry, "hello", value2)
589 assert Registry.match(registry, "hello", {1, :_, :_}) == [{self(), value1}]
590 assert Registry.match(registry, "hello", {1.0, :_, :_}) == []
592 assert Registry.match(registry, "hello", {:_, :atom, :_}) |> Enum.sort() ==
595 assert Registry.match(registry, "hello", {:"$1", :_, :"$1"}) |> Enum.sort() ==
598 assert Registry.match(registry, "hello", {2, :_, :_}) == [{self(), value2}]
599 assert Registry.match(registry, "hello", {2.0, :_, :_}) == []
602 test "supports guards", %{registry: registry} do
606 {:ok, _} = Registry.register(registry, "hello", value1)
607 {:ok, _} = Registry.register(registry, "hello", value2)
609 assert Registry.match(registry, "hello", {:"$1", :_, :_}, [{:<, :"$1", 2}]) ==
612 assert Registry.match(registry, "hello", {:"$1", :_, :_}, [{:>, :"$1", 3}]) == []
614 … assert Registry.match(registry, "hello", {:"$1", :_, :_}, [{:<, :"$1", 3}]) |> Enum.sort() ==
617 assert Registry.match(registry, "hello", {:_, :"$1", :_}, [{:is_atom, :"$1"}])
621 test "count_match supports match patterns", %{registry: registry} do
623 {:ok, _} = Registry.register(registry, "hello", value)
624 assert 1 == Registry.count_match(registry, "hello", {1, :_, :_})
625 assert 0 == Registry.count_match(registry, "hello", {1.0, :_, :_})
626 assert 1 == Registry.count_match(registry, "hello", {:_, :atom, :_})
627 assert 1 == Registry.count_match(registry, "hello", {:"$1", :_, :"$1"})
628 assert 1 == Registry.count_match(registry, "hello", :_)
629 assert 0 == Registry.count_match(registry, :_, :_)
632 {:ok, _} = Registry.register(registry, "world", value2)
633 assert 1 == Registry.count_match(registry, "world", %{b: "b"})
636 test "count_match supports guard conditions", %{registry: registry} do
638 {:ok, _} = Registry.register(registry, "hello", value)
640 assert 1 == Registry.count_match(registry, "hello", {:_, :_, :"$1"}, [{:>, :"$1", 1}])
641 assert 0 == Registry.count_match(registry, "hello", {:_, :_, :"$1"}, [{:>, :"$1", 2}])
642 assert 1 == Registry.count_match(registry, "hello", {:_, :"$1", :_}, [{:is_atom, :"$1"}])
645 test "unregister_match supports patterns", %{registry: registry} do
649 {:ok, _} = Registry.register(registry, "hello", value1)
650 {:ok, _} = Registry.register(registry, "hello", value2)
652 Registry.unregister_match(registry, "hello", {2, :_, :_})
653 assert Registry.lookup(registry, "hello") == [{self(), value1}]
655 {:ok, _} = Registry.register(registry, "hello", value2)
656 Registry.unregister_match(registry, "hello", {2.0, :_, :_})
657 assert Registry.lookup(registry, "hello") == [{self(), value1}, {self(), value2}]
658 Registry.unregister_match(registry, "hello", {:_, :atom, :_})
659 assert Registry.lookup(registry, "hello") == []
662 test "unregister_match supports guards", %{registry: registry} do
666 {:ok, _} = Registry.register(registry, "hello", value1)
667 {:ok, _} = Registry.register(registry, "hello", value2)
669 Registry.unregister_match(registry, "hello", {:"$1", :_, :_}, [{:<, :"$1", 2}])
670 assert Registry.lookup(registry, "hello") == [{self(), value2}]
673 test "unregister_match supports tricky keys", %{registry: registry} do
674 {:ok, _} = Registry.register(registry, :_, :foo)
675 {:ok, _} = Registry.register(registry, :_, :bar)
676 {:ok, _} = Registry.register(registry, "hello", "a")
677 {:ok, _} = Registry.register(registry, "hello", "b")
679 Registry.unregister_match(registry, :_, :foo)
680 assert Registry.lookup(registry, :_) == [{self(), :bar}]
682 assert Registry.keys(registry, self()) |> Enum.sort() == [:_, "hello", "hello"]
686 test "allows listeners", %{registry: registry, listener: listener} do
688 {_, task} = register_task(registry, "hello", :world)
689 assert_received {:register, ^registry, "hello", ^task, :world}
692 {:ok, _} = Registry.register(registry, "hello", :value)
693 assert_received {:register, ^registry, "hello", ^self, :value}
695 :ok = Registry.unregister(registry, "hello")
696 assert_received {:unregister, ^registry, "hello", ^self}
699 test "links and unlinks on register/unregister", %{registry: registry} do
700 {:ok, pid} = Registry.register(registry, "hello", :value)
704 {:ok, pid} = Registry.register(registry, "world", :value)
708 :ok = Registry.unregister(registry, "hello")
712 :ok = Registry.unregister(registry, "world")
717 test "raises on unknown registry name" do
718 assert_raise ArgumentError, ~r/unknown registry/, fn ->
723 test "raises if attempt to be used on via", %{registry: registry} do
725 name = {:via, Registry, {registry, "hello"}}
730 test "empty list for empty registry", %{registry: registry} do
731 assert Registry.select(registry, [{{:_, :_, :_}, [], [:"$_"]}]) == []
734 test "select all", %{registry: registry} do
735 {:ok, _} = Registry.register(registry, "hello", :value)
736 {:ok, _} = Registry.register(registry, "hello", :value)
738 assert Registry.select(registry, [{{:"$1", :"$2", :"$3"}, [], [{{:"$1", :"$2", :"$3"}}]}])
743 test "select supports full match specs", %{registry: registry} do
745 {:ok, _} = Registry.register(registry, "hello", value)
748 Registry.select(registry, [
753 Registry.select(registry, [
758 Registry.select(registry, [
763 Registry.select(registry, [
767 assert [] == Registry.select(registry, [{{:"$1", :"$2", {1.0, :_, :_}}, [], [:"$_"]}])
770 Registry.select(registry, [
776 Registry.select(registry, [
782 {:ok, _} = Registry.register(registry, "world", value2)
785 Registry.select(registry, [{{"world", self(), %{b: "b"}}, [], [:match]}])
788 Registry.select(registry, [{{:"$1", :_, :_}, [], [:"$1"]}]) |> Enum.sort()
791 test "select supports guard conditions", %{registry: registry} do
793 {:ok, _} = Registry.register(registry, "hello", value)
796 Registry.select(registry, [
802 Registry.select(registry, [
807 Registry.select(registry, [
812 test "select allows multiple specs", %{registry: registry} do
813 {:ok, _} = Registry.register(registry, "hello", :value)
814 {:ok, _} = Registry.register(registry, "world", :value)
817 Registry.select(registry, [
828 describe "clean up #{keys} registry on process crash" do
832 test "with 8 partitions", %{registry: registry} do
833 {_, task1} = register_task(registry, "hello", :value)
834 {_, task2} = register_task(registry, "world", :value)
842 [{_, _, {partition, _}}] = :ets.lookup(registry, i)
847 [{_, key, {_, pid}}] = :ets.lookup(registry, i)
854 test "with 1 partition", %{registry: registry} do
855 {_, task1} = register_task(registry, "hello", :value)
856 {_, task2} = register_task(registry, "world", :value)
861 [{-1, {_, _, key, {partition, pid}, _}}] = :ets.lookup(registry, -1)
892 test "unregistration on crash with {registry, key, value} via tuple", %{registry: registry} do
893 name = {:via, Registry, {registry, :name, :value}}
899 defp register_task(registry, key, value) do
904 send(parent, Registry.register(registry, key, value))
918 defp sum_pid_entries(registry, partitions) do
919 Enum.map(0..(partitions - 1), &Module.concat(registry, "PIDPartition#{&1}"))