Skip to content

Commit

Permalink
Merge pull request #3 from zvkemp/readme-update
Browse files Browse the repository at this point in the history
Readme update
  • Loading branch information
zvkemp authored Sep 23, 2017
2 parents 39c9cd0 + 6bde042 commit 8f887e7
Show file tree
Hide file tree
Showing 36 changed files with 81 additions and 121 deletions.
9 changes: 7 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
sudo: false
language: ruby
services: docker
rvm:
- 2.3.3
before_install: gem install bundler -v 1.14.6
- 2.4.1
before_install:
- gem install bundler
- docker-compose up --build -d
env:
- PHOENIX_HOST=localhost
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
### 0.2.0

- Added specs, backed by Phoenix app via docker-compose
- Support timeout on requests

### 0.1.1

Provide better threadsafety. This now works correctly:
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[![Build Status](https://travis-ci.org/zvkemp/rb-phoenix-socket.svg?branch=master)](https://travis-ci.org/zvkemp/rb-phoenix-socket)

# Phoenix::Socket

Phoenix Channels websocket client wrapper for synchronous Ruby applications.
Expand Down Expand Up @@ -28,7 +30,7 @@ The `join_options` keyword in the initializer can be used to declare the payload

## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
After checking out the repo, run `bin/setup` to install dependencies, and `docker-compose up --build -d` to run the phoenix application that supports the specs. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).

Expand Down
1 change: 1 addition & 0 deletions bin/setup
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ set -vx
bundle install

# Do any other automated setup that you need to do here
docker-compose build
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
phoenix:
build: spec_example
build: phoenix_example
ports:
- "4000:4000"
command: mix phx.server
45 changes: 45 additions & 0 deletions lib/phoenix/inbox.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require 'mutex_m'
module Phoenix
class Inbox
# Read it or forget it

include Mutex_m
attr_reader :ttl, :data
def initialize(ttl:)
@ttl = ttl
@bucket = Time.now.to_i / ttl
@data = Hash.new { |h, k| h[k] = {} }
super()
end

def push(key, val)
synchronize do
ts = current_timestamp
(data[ts][key] = val).tap do
if data.keys.size >= 3
data.delete_if { |key, _| key < (ts - 1) }
end
end
end
end

alias_method :[]=, :push

def pop(key)
synchronize do
ts = current_timestamp
data[ts - 1].delete(key) { data[ts].delete(key) { yield }}
end
end

alias_method :delete, :pop

def key?(key)
data.values.any? { |v| v.key?(key) }
end

def current_timestamp
Time.now.to_i / ttl
end
end
end
3 changes: 2 additions & 1 deletion lib/phoenix/socket.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "phoenix/socket/version"
require 'faye/websocket'
require 'eventmachine'
require 'phoenix/inbox'
require 'json'

module Phoenix
Expand All @@ -13,7 +14,7 @@ def initialize(topic, join_options: {}, path: 'ws://localhost:4000/socket/websoc
@path = path
@topic = topic
@join_options = join_options
@inbox = {}
@inbox = Phoenix::Inbox.new(ttl: 15)
super() # MonitorMixin
@inbox_cond = new_cond
@thread_ready = new_cond
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions phoenix_example/lib/spec_example_web/endpoint.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
defmodule SpecExampleWeb.Endpoint do
use Phoenix.Endpoint, otp_app: :spec_example

socket "/socket", SpecExampleWeb.UserSocket
end
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
9 changes: 5 additions & 4 deletions spec/phoenix/socket_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
end

let(:socket_handler) do
Phoenix::Socket.new("rspec:default", path: "ws://#{`docker-machine ip`.strip}:4000/socket/websocket")
Phoenix::Socket.new("rspec:default", path: "ws://#{ENV.fetch('PHOENIX_HOST')}:4000/socket/websocket")
end

it 'echoes back the requested payload' do
Expand All @@ -20,9 +20,10 @@
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
# do it in Elixir. Although you should probably also ask yourself why you need 100 processes
# to share a single websocket.
responses = (0..500).map do |n|
# ALSO NOTE: This used to be 500 but travis was choking on it
responses = (0..100).map do |n|
Thread.new do
Thread.current[:id] = n
socket_handler.request_reply(event: :echo, payload: { n: n }, timeout: nil)
Expand Down Expand Up @@ -63,7 +64,7 @@

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/)
expect { socket_handler.request_reply(timeout: 0.5, event: :sleep, payload: { ms: 10 }) }.not_to raise_error
end
end
end
7 changes: 7 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "bundler/setup"
require "phoenix/socket"
require 'pry-byebug'

RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
Expand All @@ -9,3 +10,9 @@
c.syntax = :expect
end
end

ENV['PHOENIX_HOST'] ||= begin
`docker-machine ip`.strip
rescue Errno::ENOENT
'localhost'
end
55 changes: 0 additions & 55 deletions spec_example/lib/spec_example_web/endpoint.ex

This file was deleted.

11 changes: 0 additions & 11 deletions spec_example/lib/spec_example_web/router.ex

This file was deleted.

29 changes: 0 additions & 29 deletions spec_example/lib/spec_example_web/views/error_helpers.ex

This file was deleted.

17 changes: 0 additions & 17 deletions spec_example/lib/spec_example_web/views/error_view.ex

This file was deleted.

0 comments on commit 8f887e7

Please sign in to comment.