Skip to content

Commit

Permalink
Merge pull request #48 from jechol/dev/matrix-tests
Browse files Browse the repository at this point in the history
Matrix tests for Elixir/OTP versions and small refactors.
  • Loading branch information
grempe authored Dec 2, 2020
2 parents cacfd31 + 2745615 commit 98db425
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 34 deletions.
106 changes: 94 additions & 12 deletions .github/workflows/elixir.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,105 @@ name: Elixir CI

on:
push:
branches: [ master ]
branches: [ master, dev/* ]
pull_request:
branches: [ master ]

jobs:
build:
# Refactoring duplicated yaml is currently not possible
# because Github does not support anchor syntax (& and *) now.
elixir_1_11:
runs-on: ubuntu-latest
name: Test on Elixir ${{matrix.elixir}} / OTP ${{matrix.otp}}
strategy:
matrix:
elixir: [1.11.0]
otp: [21.0, 22.0, 23.0]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-elixir@v1
with:
elixir-version: ${{matrix.elixir}}
otp-version: ${{matrix.otp}}
- run: mix deps.get
- run: mix test

elixir_1_10:
runs-on: ubuntu-latest
name: Test on Elixir ${{matrix.elixir}} / OTP ${{matrix.otp}}
strategy:
matrix:
elixir: [1.10.0]
otp: [21.0, 22.0]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-elixir@v1
with:
elixir-version: ${{matrix.elixir}}
otp-version: ${{matrix.otp}}
- run: mix deps.get
- run: mix test

elixir_1_9:
runs-on: ubuntu-latest
name: Test on Elixir ${{matrix.elixir}} / OTP ${{matrix.otp}}
strategy:
matrix:
elixir: [1.9.0]
otp: [20.0, 21.0, 22.0]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-elixir@v1
with:
elixir-version: ${{matrix.elixir}}
otp-version: ${{matrix.otp}}
- run: mix deps.get
- run: mix test

elixir_1_8:
runs-on: ubuntu-latest
name: Test on Elixir ${{matrix.elixir}} / OTP ${{matrix.otp}}
strategy:
matrix:
elixir: [1.8.0]
otp: [20.0, 21.0, 22.0]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-elixir@v1
with:
elixir-version: ${{matrix.elixir}}
otp-version: ${{matrix.otp}}
- run: mix deps.get
- run: mix test

elixir_1_7:
runs-on: ubuntu-latest
name: Test on Elixir ${{matrix.elixir}} / OTP ${{matrix.otp}}
strategy:
matrix:
elixir: [1.7.0]
otp: [19.0, 20.0, 21.0, 22.0]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-elixir@v1
with:
elixir-version: ${{matrix.elixir}}
otp-version: ${{matrix.otp}}
- run: mix deps.get
- run: mix test

elixir_1_6:
runs-on: ubuntu-latest
name: Test on Elixir ${{matrix.elixir}} / OTP ${{matrix.otp}}
strategy:
matrix:
elixir: [1.6.0]
otp: [19.0, 20.0]
steps:
- uses: actions/checkout@v2
- name: Setup elixir
uses: actions/setup-elixir@v1
with:
elixir-version: '1.9.4' # Define the elixir version [required]
otp-version: '22.2' # Define the OTP version [required]
- name: Install Dependencies
run: mix deps.get
- name: Run Tests
run: mix test
- uses: actions/checkout@v2
- uses: actions/setup-elixir@v1
with:
elixir-version: ${{matrix.elixir}}
otp-version: ${{matrix.otp}}
- run: mix deps.get
- run: mix test
16 changes: 10 additions & 6 deletions lib/ex_rated.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ defmodule ExRated do
@doc """
Starts the ExRated rate limit counter server.
"""
def start_link(args, opts \\ []) do
case args do
[] -> GenServer.start_link(__MODULE__, app_args_with_defaults(), opts)
_ -> GenServer.start_link(__MODULE__, Keyword.merge(app_args_with_defaults(), args), opts)
end
def start_link(args \\ [], opts \\ []) do
GenServer.start_link(__MODULE__, Keyword.merge(app_args_with_defaults(), args), opts)
end

@doc false
def child_spec(args_opts) do
%{id: __MODULE__, start: {__MODULE__, :start_link, args_opts}}
end


Expand Down Expand Up @@ -111,7 +113,9 @@ defmodule ExRated do

## Server Callbacks

@doc false
def init(args) do
Process.flag(:trap_exit, true)
[
{:timeout, timeout},
{:cleanup_rate, cleanup_rate},
Expand Down Expand Up @@ -144,7 +148,7 @@ defmodule ExRated do
end

def handle_cast(_msg, state) do
{:reply, state}
{:noreply, state}
end

def handle_info(:prune, state) do
Expand Down
5 changes: 1 addition & 4 deletions lib/ex_rated_app.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ defmodule ExRated.App do
# See http://elixir-lang.org/docs/stable/elixir/Application.html
# for more information on OTP Applications
def start(_type, _args) do
import Supervisor.Spec, warn: false

children = [
# Define workers and child supervisors to be supervised
# worker(ExRated.Worker, [arg1, arg2, arg3])
worker(ExRated, [[], [name: :ex_rated]])
{ExRated, [[], [name: :ex_rated]]}
]

# See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
Expand Down
20 changes: 8 additions & 12 deletions test/ex_rated_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -80,36 +80,35 @@ defmodule ExRatedServerTest do
@tag persistent: false
test "data is not persisted on server stop", context do
assert {:ok, 1} = ExRated.check_rate("my-bucket", 10_000, 10)
ExRated.stop(context[:exrated_server])
:ok = ExUnit.Callbacks.stop_supervised(ExRated)
assert File.exists?(context[:exrated_table] |> to_string) == false
end

@tag persistent: true
test "data is persisted on server stop", context do
assert {:ok, 1} = ExRated.check_rate("my-bucket", 10_000, 10)
ExRated.stop(context[:exrated_server])
:ok = ExUnit.Callbacks.stop_supervised(ExRated)
assert File.exists?(context[:exrated_table] |> to_string)
end

@tag persistent: true
test "in memory data and on disk data are the same when persisted", context do
assert {:ok, 1} = ExRated.check_rate("my-bucket", 10_000, 10)
data = ExRated.inspect_bucket("my-bucket", 10_000, 10)
ExRated.stop(context[:exrated_server])
:ok = ExUnit.Callbacks.stop_supervised(ExRated)

# assert process is not running
assert Process.alive?(context[:exrated_server]) == false
refute Process.alive?(context[:exrated_server])

# restart server in persistent mode
{:ok, pid} = start_server(context[:exrated_table], true)
{:ok, _pid} = start_server(context[:exrated_table], true)

# assert it reloads the data from disk
# remove key #2 in data before comparison: it is a timestamp and it's never the same
volatile = data |> Tuple.delete_at(2)
persistent = ExRated.inspect_bucket("my-bucket", 10_000, 10) |> Tuple.delete_at(2)

assert volatile == persistent
ExRated.stop(pid)
end

test "bucket names can be any()" do
Expand All @@ -119,11 +118,8 @@ defmodule ExRatedServerTest do
end

defp start_server(_table, persistent) do
GenServer.start_link(ExRated, [
{:timeout, 10_000},
{:cleanup_rate,10_000},
{:persistent, persistent},
], [name: :ex_rated])
args = [{:timeout, 10_000}, {:cleanup_rate,10_000}, {:persistent, persistent}]
opts = [name: :ex_rated]
{:ok, _child} = ExUnit.Callbacks.start_supervised({ExRated, [args, opts]})
end

end

0 comments on commit 98db425

Please sign in to comment.