Skip to content

Commit

Permalink
Adding Ameba workflow (#62)
Browse files Browse the repository at this point in the history
* Adding in Ameba workflow. Updated code fixes per Ameba suggestions and formatted. Fixes #59

* Only support Crystal 1.6 or later due to conflicts with latest Ameba
  • Loading branch information
jwoertink authored Apr 8, 2023
1 parent cf01ba0 commit e725d9e
Show file tree
Hide file tree
Showing 15 changed files with 165 additions and 149 deletions.
13 changes: 11 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@ jobs:
shard_file:
- shard.yml
crystal_version:
- 1.4.0
- 1.5.0
- 1.6.0
- 1.7.0
- latest
experimental:
- false
include:
- crystal_version: nightly
experimental: true
runs-on: ubuntu-latest
container: crystallang/crystal:${{ matrix.crystal_version }}-alpine
continue-on-error: ${{ matrix.experimental }}
services:
redis:
image: redis
Expand All @@ -42,5 +47,9 @@ jobs:
key: ${{ runner.os }}-crystal
- name: Install shards
run: shards install
- name: Format
run: crystal tool format --check
- name: Lint
run: ./bin/ameba
- name: Run tests
run: crystal spec
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
deploy:
runs-on: ubuntu-latest
container:
image: crystallang/crystal:1.6.1
image: crystallang/crystal:1.7.3
steps:
- uses: actions/checkout@v2
with:
Expand Down
6 changes: 5 additions & 1 deletion shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ version: 0.2.2
authors:
- Celso Fernandes <celso.fernandes@gmail.com>

crystal: ">= 1.4.0"
crystal: ">= 1.6.0"

dependencies:
tasker:
github: spider-gazelle/tasker
version: ~> 2.0
habitat:
github: luckyframework/habitat
version: ~> 0.4

development_dependencies:
redis:
Expand All @@ -23,5 +24,8 @@ development_dependencies:
# redis:
# github: stefanwille/crystal-redis
# version: ~> 2.8.0
ameba:
github: crystal-ameba/ameba
version: ~> 1.4.3

license: MIT
20 changes: 10 additions & 10 deletions spec/cable/connection_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,21 @@ describe Cable::Connection do

describe ".identified_by" do
it "uses the right identifier name for it" do
connect do |connection, socket|
connect do |connection, _socket|
connection.identifier.should eq("98")
end

connect(connection_class: ConnectionWithDifferentIndentifierTest) do |connection, socket|
connect(connection_class: ConnectionWithDifferentIndentifierTest) do |connection, _socket|
connection.identifier.should eq("98")
end
end
end

describe ".owned_by" do
it "uses the right identifier name for it" do
connect do |connection, socket|
connection.current_user.not_nil!.email.should eq("user98@mail.com")
connection.organization.not_nil!.name.should eq("Acme Inc.")
connect do |connection, _socket|
connection.current_user.as(User).email.should eq("user98@mail.com")
connection.organization.as(Organization).name.should eq("Acme Inc.")
end
end
end
Expand Down Expand Up @@ -470,26 +470,26 @@ describe Cable::Connection do
end
end

def builds_request(token : String)
def builds_request(token : String) : HTTP::Request
headers = HTTP::Headers{
"Upgrade" => "websocket",
"Connection" => "Upgrade",
"Sec-WebSocket-Key" => "OqColdEJm3i9e/EqMxnxZw==",
"Sec-WebSocket-Protocol" => "actioncable-v1-json, actioncable-unsupported",
"Sec-WebSocket-Version" => "13",
}
request = HTTP::Request.new("GET", "#{Cable.settings.route}?test_token=#{token}", headers)
HTTP::Request.new("GET", "#{Cable.settings.route}?test_token=#{token}", headers)
end

def builds_request(token : Nil)
def builds_request(token : Nil) : HTTP::Request
headers = HTTP::Headers{
"Upgrade" => "websocket",
"Connection" => "Upgrade",
"Sec-WebSocket-Key" => "OqColdEJm3i9e/EqMxnxZw==",
"Sec-WebSocket-Protocol" => "actioncable-v1-json, actioncable-unsupported",
"Sec-WebSocket-Version" => "13",
}
request = HTTP::Request.new("GET", "#{Cable.settings.route}", headers)
HTTP::Request.new("GET", Cable.settings.route, headers)
end

private class DummySocket < HTTP::WebSocket
Expand Down Expand Up @@ -566,7 +566,7 @@ private class UnauthorizedConnectionTest < Cable::Connection
end
end

def connect(connection_class : Cable::Connection.class = ConnectionTest, token : String? = "98", &block)
def connect(connection_class : Cable::Connection.class = ConnectionTest, token : String? = "98", &)
socket = DummySocket.new(IO::Memory.new)
connection = connection_class.new(builds_request(token: token), socket)

Expand Down
17 changes: 8 additions & 9 deletions spec/cable/handler_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -297,14 +297,14 @@ describe Cable::Handler do
Cable.server.connections.size.should eq(2)
ws4 = HTTP::WebSocket.new("ws://#{listen_address}/updates?test_token=ws4")
Cable.server.connections.size.should eq(3)
ws5 = HTTP::WebSocket.new("ws://#{listen_address}/updates?test_token=ws5")
_ws5 = HTTP::WebSocket.new("ws://#{listen_address}/updates?test_token=ws5")
Cable.server.connections.size.should eq(4)

connections = Cable.server.connections.keys
connections.any? { |c| c.starts_with?("ws2") }.should eq(true)
connections.any? { |c| c.starts_with?("ws3") }.should eq(true)
connections.any? { |c| c.starts_with?("ws4") }.should eq(true)
connections.any? { |c| c.starts_with?("ws5") }.should eq(true)
connections.any?(&.starts_with?("ws2")).should eq(true)
connections.any?(&.starts_with?("ws3")).should eq(true)
connections.any?(&.starts_with?("ws4")).should eq(true)
connections.any?(&.starts_with?("ws5")).should eq(true)

messages = [
{type: "welcome"}.to_json,
Expand Down Expand Up @@ -340,7 +340,7 @@ describe Cable::Handler do
# connection 1 will be disconnected due to error
Cable.server.connections.size.should eq(3)
connections = Cable.server.connections.keys
connections.any? { |c| c.starts_with?("ws2") }.should eq(false)
connections.any?(&.starts_with?("ws2")).should eq(false)

messages = [
{type: "welcome"}.to_json,
Expand Down Expand Up @@ -376,7 +376,7 @@ describe Cable::Handler do
# connection 1 will be disconnected due to error
Cable.server.connections.size.should eq(2)
connections = Cable.server.connections.keys
connections.any? { |c| c.starts_with?("ws3") }.should eq(false)
connections.any?(&.starts_with?("ws3")).should eq(false)

messages = [
{type: "welcome"}.to_json,
Expand Down Expand Up @@ -437,8 +437,7 @@ private def start_server

spawn do
# Make pinger real fast so we don't need to wait
http_ref = nil
http_server = http_ref = HTTP::Server.new([Cable::Handler(ApplicationCable::Connection).new])
http_server = HTTP::Server.new([Cable::Handler(ApplicationCable::Connection).new])
address = http_server.bind_unused_port
address_chan.send(address)
http_server.listen
Expand Down
12 changes: 6 additions & 6 deletions src/backend/redis/backend.cr
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ module Cable
return if redis_subscribe.nil?

redis_subscribe.subscribe(channel) do |subscription|
subscription.on_message do |channel, message|
if channel == "_internal" && message == "ping"
subscription.on_message do |sub_channel, message|
if sub_channel == Cable::INTERNAL[:channel] && message == "ping"
Cable::Logger.debug { "Cable::Server#subscribe -> PONG" }
elsif channel == "_internal" && message == "debug"
elsif sub_channel == Cable::INTERNAL[:channel] && message == "debug"
Cable.server.debug
else
Cable.server.fiber_channel.send({channel, message})
Cable::Logger.debug { "Cable::Server#subscribe channel:#{channel} message:#{message}" }
Cable.server.fiber_channel.send({sub_channel, message})
Cable::Logger.debug { "Cable::Server#subscribe channel:#{sub_channel} message:#{message}" }
end
end
end
Expand Down Expand Up @@ -74,7 +74,7 @@ module Cable
# the @server.redis_subscribe picks up this special combination
# and calls ping on the block loop for us
def ping_redis_subscribe
Cable.server.publish("_internal", "ping")
Cable.server.publish(Cable::INTERNAL[:channel], "ping")
end

def ping_redis_publish
Expand Down
Loading

0 comments on commit e725d9e

Please sign in to comment.