Home Posts Post Search Tag Search

Agent w/ Supervisor
Published on: 2025-04-04 Tags: elixir, Agents, State, Testing

This was a fun project that had a few issues that came up as I was building it.

Agents are an “easier” version of GenServer. Its meant to deal with a State and pids.

Setting up the functions for each needed function was relatively easy once I new the style of the map I wanted to create. Where a player can be added to the map as more players are added.

%{

player: %{
  ping_pong: 0,
  rock_paper_scissors: 0,
  rubik_cube: 0
}
ping_pong: %{
  player: [...]
},
rock_paper_scissors: %{
  player: [...]
},
rubik_cube: %{
  player: [...]
}

}

Having learned about Kernel put_in and update_in I was able to update and add to any map with a cleaner code than using Map or Enum.

Once that was done I learned that to do tests for the Agent you need to be sure that you have a setup and pass the MODULE to the functions.

setup/0 will be run before every test and will be sure to exit out of the Agent before the next test so that you start with a clean state.

This required that I added a param for each function and the startlink/1 that will use the same MODULE for each test. This makes sure that each time that you start a new link that it will be passed to the function that needs to access the pid/_MODULE.

Once that was done it was easy to set all the tests that I wanted.