mmyers

mmyers

Waiting after starting a C-Node

I’m exploring different ways of interfacing with C code, and in testing C-Nodes, my ExUnit tests were failing randomly. I start the C-Node process using Port.open. And I found that if I call Process.sleep(50) after opening the Port, then all the tests succeed.

Is this a good way to do it?
Is there a better way to wait for the C-Node to be ready to accept a connection?

Marked As Solved

ityonemo

ityonemo

Use net_kernel.monitor_nodes/2, then launch, the wait to have a nodeup message delivered to your test and block on a receive; then continue with your tests.

Also Liked

mmyers

mmyers

Thanks for your feedback. I have a solution.

The problem I was seeing in my tests occurred when trying to send a message before the C node was listening.

    Elixir                        C node        
-------------      ------------------------------------
  Port.open()  --->   OS process starts running C node
                                    |
  Send message ---> (X) not ready, Epmd connection fails
                                    |
                     (ei_listen, ei_publish, ei_accept)
  Send message --->    Epmd connection to C node (OK)

Comments from @benwilson512 and @ityonemo made me think, “What if the C node initiated the connection back to the Elixir node, instead of the other way around?” And maybe that’s how they were thinking it worked (or should have worked) in the first place. The initial code was just from sample C node code found on online, where the C node published itself to Epmd and listened, waiting for Elixir nodes to connect, as diagrammed above.

So I changed the C node to initiate the node connection, rather than just waiting for a connection:

    Elixir                     C node
--------------      --------------------------------
Port.open()    ---> OS process starts running C node
                                 |
connected (OK) <---        ei_connect()

And after I remembered that C nodes are hidden, and used :net_kernel.monitor_nodes(true, node_type: :hidden), then I did get a :nodeup message!

This also simplified the code on the C node side, I can just call ei_connect, and don’t have to use ei_listen, ei_publish and ei_accept.

Where Next?

Popular in Questions Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New

Other popular topics Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New

We're in Beta

About us Mission Statement