Fl4m3Ph03n1x

Fl4m3Ph03n1x

Background

I have an app called :my_app that has a dependency called :my_dep. :my_dep connects to the internet and downloads a bunch of files. Every time I run :my_app, it executes :my_dep because :my_dep is a runtime dependency. So, when I run tests, launch the app in my DEV environment or simply run it, :my_dep always connects to the internet to download files.

If :my_dep cannot connect, it fails, and because it is a runtime dependency, :my_app fails to launch.

My Approach

The current system means I can’t run local tests nor do anything without a connection to the internet. So my attempt at fixing this was to follow the approach suggested by mocks and explicit contracts and to create an interface (add a boundary between :my_app and :my_dep) and to create a mock that implements the interface.

Following is an example of how I am implementing this technique:

Geolocation Module of MyApp:

defmodule MyApp.Geolocation do

  @geo Application.get_env(:my_app, :my_dep_api)

  require Logger

  @spec geolocate(String.t) :: String.t
  def geolocate(ip) do
    ip
    |> @geo.find()
    |> get_country(ip)
  end

  @spec get_country({:ok, [String.t]}, String.t) :: String.t
  defp get_country({:ok, [country | _tail]}, _ip), do: String.upcase(country)
end

DEV config file of MyApp:

config :my_app,
  my_dep_api: MyDep.StaticMock

The StaticMock file:

defmodule MyDep.StaticMock do
  @behaviour MyApp.IMyDep

  @behaviour MyApp.IMyDep
  @spec find(String.t) :: {:ok, [String.t]}
  def find(_ip), do: {:ok, ["ES", "cat", "b"]}
end

Problem

The issue here, is that when I launch my app in DEV, it still runs the original my_dep app and it still tries to connect and download the files.

Question

How can I fix this?

Showing Posts 1 to 5

LostKobrakai

LostKobrakai

Application.get_env(:my_app, :my_dep_api) vs
config :bidtor, my_dep_api: MyDep.StaticMock

Is this just a copy/paste error?

Fl4m3Ph03n1x

Fl4m3Ph03n1x OP

Yes. Fixed ! (thanks)

al2o3cr

al2o3cr

Have you tried forcing recompilation? IIRC the module attribute is read from the application env at compile-time and stored in the BEAM file.

Fl4m3Ph03n1x

Fl4m3Ph03n1x OP

I have deleted the _build and deps folders and forced recompilation. The issue still persists.

al2o3cr

al2o3cr

Apologies for the confusion, I totally skipped this part of your question:

The right place to fix this isn’t in MyApp.Geolocation, but rather where :my_dep is being started: might be in :my_app’s application.exs or in mix.exs.

— 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
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
widianto
I think I’ve found a small improvement I could contribute to <%= web_namespace %>.CoreComponents (installer/templates/phx_web/compo...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & 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
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
aseigo
ICal is a library for interacting with iCalendar data. It parses iCalendars into typed Elixir structs via ICal.from_ics, and can prepare ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews