forked from braverhealth/phoenix-socket-dart
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tests around connectivity issues (braverhealth#74)
* add tests around connectivity issues * ensure backend always is always started * expose control side-channel * add build container workflow * tag image as latest * properly set tags
- Loading branch information
Showing
15 changed files
with
222 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
name: ci-build-container | ||
|
||
on: | ||
pull_request: | ||
paths: | ||
- "example/backend/**" | ||
- ".github/workflows/build_container.yaml" | ||
|
||
jobs: | ||
build: | ||
timeout-minutes: 20 | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Check out the repo | ||
uses: actions/checkout@v4 | ||
|
||
- name: Log in to Docker Hub | ||
uses: docker/login-action@f4ef78c080cd8ba55a85445d5b36e214a81df20a | ||
with: | ||
username: ${{ secrets.DOCKER_USERNAME }} | ||
password: ${{ secrets.DOCKER_PASSWORD }} | ||
|
||
- name: Build and push Docker image | ||
uses: docker/build-push-action@3b5e8027fcad23fda98b2e3ac259d8d67585f671 | ||
with: | ||
context: ./example/backend/ | ||
file: ./example/backend/Dockerfile | ||
push: true | ||
tags: braverhq/phoenix-dart-server:latest | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
defmodule BackendWeb.ControlEndpoint do | ||
use Phoenix.Endpoint, otp_app: :backend | ||
|
||
plug(BackendWeb.ControlRouter) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
defmodule BackendWeb.ControlRouter do | ||
use Phoenix.Router | ||
|
||
get "/stop", BackendWeb.Control, :stop | ||
get "/start", BackendWeb.Control, :start | ||
end | ||
|
||
defmodule BackendWeb.Control do | ||
@moduledoc """ | ||
Control endpoint | ||
""" | ||
|
||
use Phoenix.Controller, namespace: BackendWeb | ||
|
||
import Plug.Conn | ||
|
||
def stop(conn, _args) do | ||
Supervisor.terminate_child(Backend.Supervisor, BackendWeb.Supervisor) | ||
send_resp(conn, 200, "OK") | ||
end | ||
|
||
def start(conn, _args) do | ||
Supervisor.which_children(Backend.Supervisor) | ||
|> Enum.find(&(elem(&1, 0) == BackendWeb.Supervisor)) | ||
|> case do | ||
{BackendWeb.Supervisor, :undefined, :supervisor, _rest} -> | ||
Supervisor.restart_child(Backend.Supervisor, BackendWeb.Supervisor) | ||
|
||
_ -> | ||
:ok | ||
end | ||
|
||
send_resp(conn, 200, "OK") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
defmodule BackendWeb.Supervisor do | ||
use Supervisor | ||
|
||
def start_link(opts) do | ||
Supervisor.start_link(__MODULE__, opts ++ [name: Backend.Supervisor]) | ||
end | ||
|
||
def init(_) do | ||
children = [ | ||
{Phoenix.PubSub, [name: Backend.PubSub, adapter: Phoenix.PubSub.PG2]}, | ||
BackendWeb.Endpoint, | ||
BackendWeb.Presence | ||
] | ||
|
||
Supervisor.init(children, strategy: :one_for_one) | ||
end | ||
|
||
def child_spec(init_arg) do | ||
%{ | ||
id: BackendWeb.Supervisor, | ||
start: {BackendWeb.Supervisor, :start_link, [init_arg]}, | ||
type: :supervisor | ||
} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,3 +22,4 @@ dev_dependencies: | |
test: ^1.25.2 | ||
async: ^2.11.0 | ||
stream_channel: ^2.1.2 | ||
http: any |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import 'package:http/http.dart'; | ||
|
||
Future<void> stopBackend() { | ||
return get(Uri.parse('http://localhost:4002/stop')).then((response) { | ||
if (response.statusCode != 200) { | ||
throw Exception('Failed to stop backend'); | ||
} | ||
}); | ||
} | ||
|
||
Future<void> restartBackend() { | ||
return get(Uri.parse('http://localhost:4002/start')).then((response) { | ||
if (response.statusCode != 200) { | ||
throw Exception('Failed to start backend'); | ||
} | ||
}); | ||
} | ||
|
||
Future<void> stopThenRestartBackend( | ||
[Duration delay = const Duration(milliseconds: 200)]) { | ||
return get(Uri.parse('http://localhost:4002/stop')).then((response) { | ||
if (response.statusCode != 200) { | ||
throw Exception('Failed to stop backend'); | ||
} | ||
Future.delayed(delay).then((_) => restartBackend()); | ||
}); | ||
} |