-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from zvkemp/specs
specs with phoenix app (via docker-compose)
- Loading branch information
Showing
32 changed files
with
804 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,5 @@ | ||
phoenix: | ||
build: spec_example | ||
ports: | ||
- "4000:4000" | ||
command: mix phx.server |
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
module Rb | ||
module Phoenix | ||
module Socket | ||
VERSION = "0.1.1" | ||
VERSION = "0.2.0" | ||
end | ||
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,69 @@ | ||
require 'pry-byebug' | ||
require "spec_helper" | ||
|
||
RSpec.describe Phoenix::Socket do | ||
it "has a version number" do | ||
expect(Rb::Phoenix::Socket::VERSION).not_to be nil | ||
end | ||
|
||
let(:socket_handler) do | ||
Phoenix::Socket.new("rspec:default", path: "ws://#{`docker-machine ip`.strip}:4000/socket/websocket") | ||
end | ||
|
||
it 'echoes back the requested payload' do | ||
response = socket_handler.request_reply(event: :echo, payload: { foo: :bar }) | ||
expect(response['event']).to eq('phx_reply') | ||
expect(response['topic']).to eq('rspec:default') | ||
expect(response['payload']).to eq({ 'status' => 'ok', 'response' => { 'foo' => 'bar' }}) | ||
end | ||
|
||
it 'handles concurrent threads' do | ||
# NOTE: This is a proof of concept, and is WAY more than anyone would ever want/need | ||
# to spawn in a runtime process. I.e. don't do this. If one at a time isn't enough, | ||
# do it in Elixir. Although you should probably also ask yourself why you need 500 processes | ||
# to share a single websocket. | ||
responses = (0..500).map do |n| | ||
Thread.new do | ||
Thread.current[:id] = n | ||
socket_handler.request_reply(event: :echo, payload: { n: n }, timeout: nil) | ||
end | ||
end.map(&:value) | ||
|
||
responses.each_with_index do |response, index| | ||
expect(response['payload']).to eq({ 'status' => 'ok', 'response' => { 'n' => index }}) | ||
end | ||
end | ||
|
||
describe 're-spawn' do | ||
20.times do |n| | ||
# NOTE: Running this multiple times because there was some unexpected thread scheduling | ||
# behavior that came up during development. Generally came up at least 1 / 5 times; | ||
# running 20 for safety. | ||
it "handles termination but respawns the connection handler" do | ||
expect { socket_handler.request_reply(event: :unsupported) }.to raise_error(RuntimeError, /reply .* not found/) | ||
expect(socket_handler.request_reply(event: :echo)['payload']['status']).to eq('ok') | ||
|
||
# Ensure dead handler threads have been cleaned up; we should have at most | ||
# the live main thread and a live respawned handler | ||
expect(Thread.list.count).to be < 3 | ||
end | ||
end | ||
end | ||
|
||
describe 'timeout handling' do | ||
specify 'small sleep' do | ||
response = socket_handler.request_reply(event: :sleep, payload: { ms: 50 }) | ||
expect(response.dig('payload', 'status')).to eq('ok') | ||
end | ||
|
||
specify 'long sleep' do | ||
response = socket_handler.request_reply(event: :sleep, payload: { ms: 1000 }) | ||
expect(response.dig('payload', 'status')).to eq('ok') | ||
end | ||
|
||
specify 'sleep exceeding timeout' do | ||
expect { socket_handler.request_reply(timeout: 0.5, event: :sleep, payload: { ms: 1000 }) }.to raise_error(RuntimeError, /timeout/) | ||
expect { socket_handler.request_reply(timeout: 0.5, event: :sleep, payload: { ms: 10 }) }.not_to raise_error(RuntimeError, /timeout/) | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
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 @@ | ||
# App artifacts | ||
/_build | ||
/db | ||
/deps | ||
/*.ez |
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,16 @@ | ||
# App artifacts | ||
/_build | ||
/db | ||
/deps | ||
/*.ez | ||
|
||
# Generated on crash by the VM | ||
erl_crash.dump | ||
|
||
# Files matching config/*.secret.exs pattern contain sensitive | ||
# data and you should not commit them into version control. | ||
# | ||
# Alternatively, you may comment the line below and commit the | ||
# secrets files as long as you replace their contents by environment | ||
# variables. | ||
/config/*.secret.exs |
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,15 @@ | ||
FROM elixir:1.5.1-alpine | ||
|
||
ENV LANG=C.UTF-8 | ||
WORKDIR /usr/src/app | ||
|
||
ENV MIX_ENV=dev | ||
RUN mix local.hex --force && mix local.rebar --force | ||
|
||
COPY mix* ./ | ||
RUN mix deps.get && mix compile | ||
|
||
COPY . ./ | ||
RUN mix deps.get && mix compile | ||
|
||
CMD ["mix", "phx.server"] |
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,18 @@ | ||
# SpecExample | ||
|
||
To start your Phoenix server: | ||
|
||
* Install dependencies with `mix deps.get` | ||
* Start Phoenix endpoint with `mix phx.server` | ||
|
||
Now you can visit [`localhost:4000`](http://localhost:4000) from your browser. | ||
|
||
Ready to run in production? Please [check our deployment guides](http://www.phoenixframework.org/docs/deployment). | ||
|
||
## Learn more | ||
|
||
* Official website: http://www.phoenixframework.org/ | ||
* Guides: http://phoenixframework.org/docs/overview | ||
* Docs: https://hexdocs.pm/phoenix | ||
* Mailing list: http://groups.google.com/group/phoenix-talk | ||
* Source: https://github.com/phoenixframework/phoenix |
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,23 @@ | ||
# This file is responsible for configuring your application | ||
# and its dependencies with the aid of the Mix.Config module. | ||
# | ||
# This configuration file is loaded before any dependency and | ||
# is restricted to this project. | ||
use Mix.Config | ||
|
||
# Configures the endpoint | ||
config :spec_example, SpecExampleWeb.Endpoint, | ||
url: [host: "localhost"], | ||
secret_key_base: "7Y7mZ/GEwoVarXDx6yNFzs6nXyuh3r93hggda16yzFtarWlLCillTwenNrNjjyra", | ||
render_errors: [view: SpecExampleWeb.ErrorView, accepts: ~w(json)], | ||
pubsub: [name: SpecExample.PubSub, | ||
adapter: Phoenix.PubSub.PG2] | ||
|
||
# Configures Elixir's Logger | ||
config :logger, :console, | ||
format: "$time $metadata[$level] $message\n", | ||
metadata: [:request_id] | ||
|
||
# Import environment specific config. This must remain at the bottom | ||
# of this file so it overrides the configuration defined above. | ||
import_config "#{Mix.env}.exs" |
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,37 @@ | ||
use Mix.Config | ||
|
||
# For development, we disable any cache and enable | ||
# debugging and code reloading. | ||
# | ||
# The watchers configuration can be used to run external | ||
# watchers to your application. For example, we use it | ||
# with brunch.io to recompile .js and .css sources. | ||
config :spec_example, SpecExampleWeb.Endpoint, | ||
http: [port: 4000], | ||
debug_errors: true, | ||
code_reloader: true, | ||
check_origin: false, | ||
watchers: [] | ||
|
||
# ## SSL Support | ||
# | ||
# In order to use HTTPS in development, a self-signed | ||
# certificate can be generated by running the following | ||
# command from your terminal: | ||
# | ||
# openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 -subj "/C=US/ST=Denial/L=Springfield/O=Dis/CN=www.example.com" -keyout priv/server.key -out priv/server.pem | ||
# | ||
# The `http:` config above can be replaced with: | ||
# | ||
# https: [port: 4000, keyfile: "priv/server.key", certfile: "priv/server.pem"], | ||
# | ||
# If desired, both `http:` and `https:` keys can be | ||
# configured to run both http and https servers on | ||
# different ports. | ||
|
||
# Do not include metadata nor timestamps in development logs | ||
config :logger, :console, format: "[$level] $message\n" | ||
|
||
# Set a higher stacktrace during development. Avoid configuring such | ||
# in production as building large stacktraces may be expensive. | ||
config :phoenix, :stacktrace_depth, 20 |
Oops, something went wrong.