You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Note `Process.monitor(pid)` returns a unique reference that allows us to match upcoming messages to that monitoring reference. After we stop the agent, we can `flush()` all messages and notice a `:DOWN` message arrived, with the exact reference returned by monitor, notifying that the bucket process exited with reason `:normal`.
193
+
Note `Process.monitor(pid)` returns a unique reference that allows us to match upcoming messages to that monitoring reference. After we stop the agent, we can `flush/0` all messages and notice a `:DOWN` message arrived, with the exact reference returned by monitor, notifying that the bucket process exited with reason `:normal`.
194
194
195
195
Let's reimplement the server callbacks to fix the bug and make the test pass. First, we will modify the GenServer state to two dictionaries: one that contains `name -> pid` and another that holds `ref -> name`. Then we need to monitor the buckets on `handle_cast/2` as well as implement a `handle_info/2` callback to handle the monitoring messages. The full server callbacks implementation is shown below:
196
196
@@ -211,7 +211,7 @@ Let's reimplement the server callbacks to fix the bug and make the test pass. Fi
211
211
ifMap.has_key?(names, name) do
212
212
{:noreply, {names, refs}}
213
213
else
214
-
{:ok, pid} =KV.Bucket.start_link()
214
+
{:ok, pid} =KV.Bucket.start_link
215
215
ref =Process.monitor(pid)
216
216
refs =Map.put(refs, ref, name)
217
217
names =Map.put(names, name, pid)
@@ -257,7 +257,7 @@ Links are bi-directional. If you link two process and one of them crashes, the o
257
257
Returning to our `handle_cast/2` implementation, you can see the registry is both linking and monitoring the buckets:
0 commit comments