Skip to content
This repository has been archived by the owner on Nov 21, 2024. It is now read-only.

Commit

Permalink
fix: test and CI
Browse files Browse the repository at this point in the history
  • Loading branch information
tdelabro committed Nov 6, 2024
1 parent 413a288 commit 9445777
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 22 deletions.
19 changes: 15 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,21 @@ jobs:

- name: Integration tests
run: |-
mix phx.server &
CASHUBREW_SERVER_PID=$!
cd integration-tests && cargo test
kill $CASHUBREW_SERVER_PID
cd integration-tests
cargo run &
BTCD_AND_LND_SERVERS_PID=$!
# Wait until the nodes are running by checking if the the env var are exported
while [[ -z "${LND_URL}" ]] do sleep 1 && source .env; done
source .env
cd ..
# mix doesn't behave when run in background, so we use `erlang -detached` instead
# but the `$!` thing won't work coz the app is run in another thread,
# so we write the actual pid in a file and later read it to kill it
elixir --erl "-detached" -e "File.write! 'pid', :os.getpid" -S mix phx.server
cd integration-tests
cargo test
cat ../pid | xargs kill
kill $BTCD_AND_LND_SERVERS_PID
Expand Down
2 changes: 2 additions & 0 deletions config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,5 @@ config :cashubrew, Cashubrew.Repo,
config :cashubrew, :repo, Cashubrew.Repo

config :cashubrew, ecto_repos: [Cashubrew.Repo]

config :cashubrew, :lnd_client, Cashubrew.LightingNetwork.Lnd
2 changes: 2 additions & 0 deletions config/prod.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ config :cashubrew, Cashubrew.Web.Endpoint, secret_key_base: System.get_env("SECR

# Do not print debug messages in production
config :logger, level: :info

config :cashubrew, :lnd_client, Cashubrew.LightingNetwork.Lnd
3 changes: 3 additions & 0 deletions config/runtime.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Config

config :cashubrew, :lnd_client, Cashubrew.LightingNetwork.MockLnd

if config_env() == :prod do
database_url =
System.get_env("DATABASE_URL") ||
Expand Down Expand Up @@ -28,3 +30,4 @@ if config_env() == :prod do
],
secret_key_base: secret_key_base
end

2 changes: 2 additions & 0 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ else
pool_size: 10

config :cashubrew, :repo, Cashubrew.Repo

config :cashubrew, :lnd_client, Cashubrew.LightingNetwork.MockLnd
end
6 changes: 2 additions & 4 deletions integration-tests/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ async fn main() {
);

println!("Downloading lnd...");
let mut lnd_conf = LndConf::default();
lnd_conf.view_stdout = true;
lnd_conf.view_stderr = true;
let lnd_conf = LndConf::default();
let mut lnd = Lnd::with_conf(
lnd::exe_path().unwrap(),
&lnd_conf,
Expand All @@ -41,7 +39,7 @@ async fn main() {
.is_ok());
println!("Done");
std::fs::write(
"../.env",
".env",
format!(
"export LND_URL={:?}\nexport LND_CERT={:?}\nexport LND_MACAROON={:?}",
lnd.grpc_url,
Expand Down
17 changes: 4 additions & 13 deletions lib/cashubrew/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,13 @@ defmodule Cashubrew.Application do
def start(_type, _args) do
children = [
Cashubrew.Web.Telemetry,
Cashubrew.LightingNetwork.Lnd,
Application.get_env(:cashubrew, :lnd_client),
{Phoenix.PubSub, name: Cashubrew.PubSub},
Endpoint
Endpoint,
Application.get_env(:cashubrew, :repo),
{Task, fn -> Cashubrew.Mint.init() end}
]

# Conditionally add the appropriate repo to the children list
children =
case Application.get_env(:cashubrew, :repo) do
Cashubrew.MockRepo -> [Cashubrew.MockRepo | children]
Cashubrew.Repo -> [Cashubrew.Repo | children]
_ -> children
end

# Always add Cashubrew.Mint after the repo
children = children ++ [{Task, fn -> Cashubrew.Mint.init() end}]

opts = [strategy: :one_for_one, name: Cashubrew.Supervisor]
Supervisor.start_link(children, opts)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ defmodule Cashubrew.LightingNetwork.Lnd do
require Logger

def start_link(arg) do
# Todo: validate args are valid url to ln server
GenServer.start_link(__MODULE__, arg, name: __MODULE__)
end

Expand Down
19 changes: 19 additions & 0 deletions lib/cashubrew/lightning/mock_lnd_client.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
defmodule Cashubrew.LightingNetwork.MockLnd do
@moduledoc """
Mock client to compile test without running an lnd node
"""
use GenServer
require Logger

def start_link(arg) do
GenServer.start_link(__MODULE__, arg, name: __MODULE__)
end

def init(_args) do
{:ok, %{}}
end

def create_invoice!(_amount, _unit, _description) do
%{}
end
end

0 comments on commit 9445777

Please sign in to comment.