mmyers

mmyers

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?

First 7 of 7 Posts Switch mode

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.

mmyers

mmyers OP

I’m not sure :net_kernel.monitor_nodes will give me what I’m looking for. I want to know when it is safe to connect to the C-Node. And according to the Erlang docs:

A nodeup message is delivered to all subscribing processes when a new node is connected

Maybe just a retry with a timeout or limit on the number of retries will work.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

How would the C node be connected, but not ready to be connected to?

mmyers

mmyers OP

How would the C node be connected, but not ready to be connected to?

Exactly. The C-Node is not connected initially, which is why net_kernel.monitor_nodes won’t work.
There is a delay between starting the C-Node process, and the C-Node having an open socket, ready to receive a connection from an Elixir/Erlang node. That delay is what I want to account for with something better than a fixed sleep time.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Which is what the messaging solution provides. You :net_kernel.monitor_nodes, and then receive do with a pattern to wait for the nodeup message.

mmyers

mmyers OP

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.

ityonemo

ityonemo

shoot! I should have been more specific and reminded you that C nodes are hidden.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement