Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a fake or dummy pid in elixir

In erlang it is possible to create a pid which does not correspond to a process. An example using this can be found here in Learn You Some Erlang.

pid(0,250,0).

Is there a way to do the same in elixir? The closes I have got at the moment is to create a process that immediately terminates and use that pid.

fake_pid = Process.spawn(fn -> end)

This seams like a bit of a hack, and I am unsure if there might be some slight differences between a never created pid and a dead pid.

like image 933
Peter Saxton Avatar asked Sep 28 '15 08:09

Peter Saxton


2 Answers

You can use the pid/3 Erlang function directly in Elixir:

:c.pid(0,250,0)

Also - just a not from the Erlang docs:

Converts X, Y, Z to the pid . This function should only be used when debugging.

like image 199
Gazler Avatar answered Oct 17 '22 16:10

Gazler


For your purposes, I would use self() from the test code. That would resolve to the process running the current test case.

If you for some reason need to test another pid, your way of using Process.spawn/1 is quite nice, because it ensures the pid is not taken (and doesn't point to something vital in the system) and that it is already dead (or alive, if you keep it alive for the duration of the test).

The Erlang :c.pid/3 or the new Elixir 1.1 IEx.Helpers.pid/3 are only meant to be convenience functions to be used in the shell.

like image 39
Adam Lindberg Avatar answered Oct 17 '22 18:10

Adam Lindberg