Skip to content
Permalink
Browse files

Added ZMQ project and wrote first tests

  • Loading branch information...
Dreat committed Mar 22, 2017
1 parent 019b2f9 commit 362fb27af3fe41da3f0d3da5c09eef184f256b56
Showing with 156 additions and 0 deletions.
  1. +17 −0 zmq_ex/.gitignore
  2. +24 −0 zmq_ex/README.md
  3. +30 −0 zmq_ex/config/config.exs
  4. +27 −0 zmq_ex/lib/zmq_ex.ex
  5. +32 −0 zmq_ex/mix.exs
  6. +1 −0 zmq_ex/test/test_helper.exs
  7. +25 −0 zmq_ex/test/zmq_ex_test.exs
@@ -0,0 +1,17 @@
# The directory Mix will write compiled artifacts to.
/_build

# If you run "mix test --cover", coverage assets end up here.
/cover

# The directory Mix downloads your dependencies sources to.
/deps

# Where 3rd-party dependencies like ExDoc output generated docs.
/doc

# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump

# Also ignore archive artifacts (built via "mix archive.build").
*.ez
@@ -0,0 +1,24 @@
# ZmqEx

**TODO: Add description**

## Installation

If [available in Hex](https://hex.pm/docs/publish), the package can be installed as:

1. Add `zmq_ex` to your list of dependencies in `mix.exs`:

```elixir
def deps do
[{:zmq_ex, "~> 0.1.0"}]
end
```

2. Ensure `zmq_ex` is started before your application:

```elixir
def application do
[applications: [:zmq_ex]]
end
```

@@ -0,0 +1,30 @@
# This file is responsible for configuring your application
# and its dependencies with the aid of the Mix.Config module.
use Mix.Config

# This configuration is loaded before any dependency and is restricted
# to this project. If another project depends on this project, this
# file won't be loaded nor affect the parent project. For this reason,
# if you want to provide default values for your application for
# 3rd-party users, it should be done in your "mix.exs" file.

# You can configure for your application as:
#
# config :zmq_ex, key: :value
#
# And access this configuration in your application as:
#
# Application.get_env(:zmq_ex, :key)
#
# Or configure a 3rd-party app:
#
# config :logger, level: :info
#

# It is also possible to import configuration files, relative to this
# directory. For example, you can emulate configuration per environment
# by uncommenting the line below and defining dev.exs, test.exs and such.
# Configuration from the imported file will override the ones defined
# here (which is why it is important to import them last).
#
# import_config "#{Mix.env}.exs"
@@ -0,0 +1,27 @@
defmodule ZmqEx do
use Bitwise

def encode(%{flags: flags, size: size, body: body}) do
<<encode_flags(flags), size, body>>
end

def decode(<<flags, size, body>>) do
case flags &&& 7 do
0 -> %{flags: %{reserved: 0, command: :message, long: :short, more: false}, size: size, body: body}
1 -> %{flags: %{reserved: 0, command: :message, long: :short, more: true}, size: size, body: body}
2 -> %{flags: %{reserved: 0, command: :message, long: :long, more: false}, size: size, body: body}
3 -> %{flags: %{reserved: 0, command: :message, long: :long, more: true}, size: size, body: body}
4 -> %{flags: %{reserved: 0, command: :command, long: :short, more: false}, size: size, body: body}
5 -> %{flags: %{reserved: 0, command: :command, long: :long, more: false}, size: size, body: body}
_ -> {:error}
end
end

defp encode_flags(%{reserved: 0, command: :message, long: :short, more: false}), do: 0
defp encode_flags(%{reserved: 0, command: :message, long: :short, more: true}), do: 1
defp encode_flags(%{reserved: 0, command: :message, long: :long, more: false}), do: 2
defp encode_flags(%{reserved: 0, command: :message, long: :long, more: true}), do: 3
defp encode_flags(%{reserved: 0, command: :command, long: :short, more: false}), do: 4
defp encode_flags(%{reserved: 0, command: :command, long: :long, more: false}), do: 5
defp encode_flags(_), do: {:error}
end
@@ -0,0 +1,32 @@
defmodule ZmqEx.Mixfile do
use Mix.Project

def project do
[app: :zmq_ex,
version: "0.1.0",
elixir: "~> 1.3",
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
deps: deps()]
end

# Configuration for the OTP application
#
# Type "mix help compile.app" for more information
def application do
[applications: [:logger]]
end

# Dependencies can be Hex packages:
#
# {:mydep, "~> 0.3.0"}
#
# Or git/path repositories:
#
# {:mydep, git: "https://github.com/elixir-lang/mydep.git", tag: "0.1.0"}
#
# Type "mix help deps" for more examples and options
defp deps do
[]
end
end
@@ -0,0 +1 @@
ExUnit.start()
@@ -0,0 +1,25 @@
defmodule ZmqExTest do
use ExUnit.Case
import ZmqEx
doctest ZmqEx

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

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

test "encode basic message frame" do
result = ZmqEx.encode(%{flags: %{reserved: 0, command: :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: %{reserved: 0, command: :command, long: :short, more: false}, size: 1, body: 3})
assert result == (<<4, 1, 3>>)
end
end

0 comments on commit 362fb27

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