Skip to content
Permalink
Browse files

Added encoding long frames

  • Loading branch information...
Dreat committed Apr 5, 2017
1 parent 0863026 commit 1d645945893f63fe8e6c0f29ef9f35c6e645130e
Showing with 18 additions and 4 deletions.
  1. +6 −2 zmq_ex/lib/zmq_ex.ex
  2. +12 −2 zmq_ex/test/zmq_ex_test.exs
@@ -1,8 +1,12 @@
defmodule ZmqEx do
use Bitwise

def encode(%{flags: flags, size: size, body: body}) do
<<encode_flags(flags), size, body>>
def encode(%{flags: flags = %{type: _, long: :short, more: _}, size: size, body: <<body>>}) do
<<encode_flags(flags), size :: size(8), body>>
end

def encode(%{flags: flags = %{type: _, long: :long, more: _}, size: size, body: <<body>>}) do
<<encode_flags(flags), size :: size(64), body>>
end

def decode(<<flags :: size(8), rest :: binary>>) do
@@ -24,12 +24,22 @@ defmodule ZmqExTest do
end

test "encode basic message frame" do
result = ZmqEx.encode(%{flags: %{type: :message, long: :short, more: false}, size: 1, body: 1})
result = ZmqEx.encode(%{flags: %{type: :message, long: :short, more: false}, size: 1, body: <<1>>})
assert result == (<<0, 1, 1>>)
end

test "encode basic command frame" do
result = ZmqEx.encode(%{flags: %{type: :command, long: :short, more: false}, size: 1, body: 3})
result = ZmqEx.encode(%{flags: %{type: :command, long: :short, more: false}, size: 1, body: <<3>>})
assert result == (<<4, 1, 3>>)
end

test "encode long message frame" do
result = ZmqEx.encode(%{flags: %{type: :message, long: :short, more: false}, size: 1, body: <<1>>})
assert result == (<<0, 1, 1>>)
end

test "encode long command frame" do
result = ZmqEx.encode(%{flags: %{type: :command, long: :short, more: false}, size: 1, body: <<3>>})
assert result == (<<4, 1, 3>>)
end
end

0 comments on commit 1d64594

Please sign in to comment.
You can’t perform that action at this time.