mirror of
https://codeberg.org/JasterV/jaster.xyz.git
synced 2026-04-27 02:15:40 +00:00
wip
This commit is contained in:
parent
eda6c0a5e2
commit
94eeebfe02
1 changed files with 48 additions and 0 deletions
48
src/content/blog/DecouplingElixirGenServers.md
Normal file
48
src/content/blog/DecouplingElixirGenServers.md
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
---
|
||||
title: "Decoupling Elixir GenServers with Phoenix PubSub"
|
||||
description: "A way to make our Genservers more easy to test and maintain by using PubSub"
|
||||
pubDate: 2025-06-02
|
||||
image: "./assets/me.png"
|
||||
---
|
||||
|
||||
## TLDR
|
||||
|
||||
In this post I'd like to talk about a way to decouple a GenServer in your Elixir application in a way that makes it very easy to test and decouples it completely from the rest of the system.
|
||||
|
||||
We will do it by going through a real application I was working with recently (The code is not the same but the concept is).
|
||||
|
||||
I will first present a common Elixir application; including tests and we will discuss the problems with it. (TLDR; Coupling and messy tests).
|
||||
|
||||
Then I want to walk you through the process I went to solve these problems and we will discuss the final application, its benefits but also possible downsides.
|
||||
|
||||
## Automating a door
|
||||
|
||||
### Architecture
|
||||
|
||||
- Websocket client
|
||||
- Door GenServer
|
||||
|
||||
The Door GenServer can receive a lock or unlock messages, and it will call the websocket client back to emit a "locked" or "unlocked" events when done.
|
||||
|
||||
### Websocket client
|
||||
|
||||
### Door GenServer
|
||||
|
||||
## Let's write some tests
|
||||
|
||||
## What is going on here?
|
||||
|
||||
Possible problems:
|
||||
|
||||
- Duplication of test setup
|
||||
- The door tests having to call the websocket client to know if the event is sent via the websocket
|
||||
|
||||
## How can we improve it?
|
||||
|
||||
- Decouple the Door server by making it emit an event when the door is locked or unlocked
|
||||
|
||||
## Introducing Phoenix PubSub
|
||||
|
||||
## Rewriting our tests
|
||||
|
||||
## Conclusions
|
||||
Loading…
Add table
Add a link
Reference in a new issue