Skip to content

Commit

Permalink
Overall fixes and enhancements including support for Elixir 1.13.x
Browse files Browse the repository at this point in the history
  • Loading branch information
cabol committed Mar 26, 2022
1 parent 2994dee commit ea68edb
Show file tree
Hide file tree
Showing 15 changed files with 104 additions and 73 deletions.
49 changes: 30 additions & 19 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ jobs:
strategy:
matrix:
include:
- elixir: 1.12.x
- elixir: 1.13.x
otp: 24.x
coverage: true
sobelow: true
dialyzer: true
- elixir: 1.11.x
- elixir: 1.10.x
otp: 23.x
inch-report: true
- elixir: 1.9.x
otp: 22.x

env:
GITHUB_TOKEN: '${{ secrets.GITHUB_TOKEN }}'
Expand All @@ -41,7 +40,8 @@ jobs:
experimental-otp: true

- name: Cache deps
uses: actions/cache@v1
uses: actions/cache@v3
id: mix-cache
with:
path: deps
key: >-
Expand All @@ -51,7 +51,7 @@ jobs:
${{ runner.os }}-${{ matrix.otp }}-${{ matrix.elixir }}-mix-
- name: Cache _build
uses: actions/cache@v1
uses: actions/cache@v3
with:
path: _build
key: >-
Expand All @@ -65,6 +65,7 @@ jobs:
mix local.hex --force
mix local.rebar --force
mix deps.get
if: ${{ steps.mix-cache.outputs.cache-hit != 'true' }}

- name: Run style and code consistency checks
run: |
Expand All @@ -76,28 +77,38 @@ jobs:
run: |
epmd -daemon
mix test --trace
if: ${{!matrix.coverage}}
if: ${{ !matrix.coverage }}

- name: Run tests with coverage
run: |
epmd -daemon
mix coveralls.github
if: ${{matrix.coverage}}
if: ${{ matrix.coverage }}

- name: Cache PLT
uses: actions/cache@v1
- name: Run sobelow
run: mix sobelow --exit --skip
if: ${{ matrix.sobelow }}

- name: Retrieve PLT Cache
uses: actions/cache@v3
id: plt-cache
with:
path: priv/plts
key: '${{ runner.os }}-${{ matrix.otp }}-${{ matrix.elixir }}-plt-v4'
restore-keys: |
${{ runner.os }}-${{ matrix.otp }}-${{ matrix.elixir }}-plt-v4
key: >-
${{ runner.os }}-${{ matrix.otp }}-${{ matrix.elixir }}-plts-${{
hashFiles(format('{0}{1}', github.workspace, '/mix.lock')) }}-v1
if: ${{ matrix.dialyzer }}

- name: Run static analysis checks
- name: Create PLTs
run: |
mix sobelow --exit --skip
mix dialyzer --format short
if: ${{matrix.dialyzer}}
mkdir -p priv/plts
mix dialyzer --plt
if: ${{ matrix.dialyzer && steps.plt-cache.outputs.cache-hit != 'true' }}

- name: Run dialyzer
run: mix dialyzer --no-check --halt-exit-status
if: ${{ matrix.dialyzer && steps.plt-cache.outputs.cache-hit != 'true' }}

- name: Doc Coverage Report
- name: Doc coverage report
run: MIX_ENV=docs mix inch.report
if: ${{matrix.inch-report}}
if: ${{ matrix.inch-report }}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,16 @@ Partitioned | [Nebulex.Adapters.Partitioned][pa] | Built-In
Replicated | [Nebulex.Adapters.Replicated][ra] | Built-In
Multilevel | [Nebulex.Adapters.Multilevel][ma] | Built-In
Nil (special adapter that disables the cache) | [Nebulex.Adapters.Nil][nil] | Built-In
Cachex | Nebulex.Adapters.Cachex | [nebulex_adapters_cachex][nbx_cachex]
Redis | NebulexRedisAdapter | [nebulex_redis_adapter][nbx_redis]
Cachex | Nebulex.Adapters.Cachex | [nebulex_adapters_cachex][nbx_cachex]

[la]: http://hexdocs.pm/nebulex/Nebulex.Adapters.Local.html
[pa]: http://hexdocs.pm/nebulex/Nebulex.Adapters.Partitioned.html
[ra]: http://hexdocs.pm/nebulex/Nebulex.Adapters.Replicated.html
[ma]: http://hexdocs.pm/nebulex/Nebulex.Adapters.Multilevel.html
[nil]: http://hexdocs.pm/nebulex/Nebulex.Adapters.Nil.html
[nbx_cachex]: https://github.com/cabol/nebulex_adapters_cachex
[nbx_redis]: https://github.com/cabol/nebulex_redis_adapter
[nbx_cachex]: https://github.com/cabol/nebulex_adapters_cachex

For example, if you want to use a built-in cache, add to your `mix.exs` file:

Expand Down
1 change: 1 addition & 0 deletions coveralls.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
},

"skip_files": [
"lib/nebulex/cache/options.ex",
"test/support/*",
"test/dialyzer/*"
]
Expand Down
6 changes: 4 additions & 2 deletions lib/nebulex/adapter/persistence.ex
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ defmodule Nebulex.Adapter.Persistence do

import Nebulex.Helpers

# sobelow_skip ["Traversal.FileModule"]
@impl true
def dump(%{cache: cache}, path, opts) do
with_file(path, [:read, :write], fn io_dev ->
Expand All @@ -64,13 +63,13 @@ defmodule Nebulex.Adapter.Persistence do
|> Stream.chunk_every(Keyword.get(opts, :entries_per_line, 10))
|> Enum.each(fn entries ->
bin = Entry.encode(entries, get_compression(opts))

:ok = IO.puts(io_dev, bin)
end)
end
end)
end

# sobelow_skip ["Traversal.FileModule"]
@impl true
def load(%{cache: cache}, path, opts) do
with_file(path, [:read], fn io_dev ->
Expand All @@ -79,6 +78,7 @@ defmodule Nebulex.Adapter.Persistence do
|> Stream.map(&String.trim/1)
|> Enum.each(fn line ->
entries = Entry.decode(line, [:safe])

cache.put_all(entries, opts)
end)
end)
Expand All @@ -88,6 +88,7 @@ defmodule Nebulex.Adapter.Persistence do

## Helpers

# sobelow_skip ["Traversal.FileModule"]
defp with_file(path, modes, function) do
case File.open(path, modes) do
{:ok, io_device} ->
Expand All @@ -99,6 +100,7 @@ defmodule Nebulex.Adapter.Persistence do

{:error, reason} ->
reason = %File.Error{reason: reason, action: "open", path: path}

wrap_error Nebulex.Error, reason: reason
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/nebulex/adapter/transaction.ex
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,11 @@ defmodule Nebulex.Adapter.Transaction do
true ->
try do
_ = Process.put({pid, self()}, %{keys: keys, nodes: nodes})

{:ok, fun.()}
after
_ = Process.delete({pid, self()})

del_locks(ids, nodes)
end

Expand Down
1 change: 1 addition & 0 deletions lib/nebulex/cache/supervisor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ defmodule Nebulex.Cache.Supervisor do
"""
def start_link(cache, otp_app, adapter, opts) do
sup_opts = if name = Keyword.get(opts, :name, cache), do: [name: name], else: []

Supervisor.start_link(__MODULE__, {cache, otp_app, adapter, opts}, sup_opts)
end

Expand Down
12 changes: 9 additions & 3 deletions lib/nebulex/caching.ex
Original file line number Diff line number Diff line change
Expand Up @@ -728,17 +728,20 @@ if Code.ensure_loaded?(Decorator.Define) do
**NOTE:** Internal purposes only.
"""
@spec eval_cache_evict(boolean, boolean, module, keygen, [term], on_error, fun) :: term
@spec eval_cache_evict(boolean, boolean, module, keygen, [term] | nil, on_error, fun) :: term
def eval_cache_evict(before_invocation?, all_entries?, cache, keygen, keys, on_error, block_fun)

def eval_cache_evict(true, all_entries?, cache, keygen, keys, on_error, block_fun) do
_ = do_evict(all_entries?, cache, keygen, keys, on_error)

block_fun.()
end

def eval_cache_evict(false, all_entries?, cache, keygen, keys, on_error, block_fun) do
result = block_fun.()

_ = do_evict(all_entries?, cache, keygen, keys, on_error)

result
end

Expand All @@ -765,10 +768,12 @@ if Code.ensure_loaded?(Decorator.Define) do
case match.(result) do
{true, value} ->
_ = run_cmd(__MODULE__, :cache_put, [cache, key, value, opts], on_error)

result

true ->
_ = run_cmd(__MODULE__, :cache_put, [cache, key, result, opts], on_error)

result

false ->
Expand All @@ -785,8 +790,9 @@ if Code.ensure_loaded?(Decorator.Define) do
def cache_put(cache, key, value, opts)

def cache_put(cache, {:"$keys", keys}, value, opts) do
entries = for k <- keys, do: {k, value}
cache.put_all(entries, opts)
keys
|> Enum.map(&{&1, value})
|> cache.put_all(opts)
end

def cache_put(cache, key, value, opts) do
Expand Down
26 changes: 22 additions & 4 deletions lib/nebulex/entry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ defmodule Nebulex.Entry do
## Example
iex> Nebulex.Entry.encode(%Nebulex.Entry{})
_encoded_entry
iex> "hello"
...> |> Nebulex.Entry.encode()
...> |> Nebulex.Entry.decode()
"hello"
"""
@spec encode(term, [term]) :: binary
def encode(data, opts \\ []) do
Expand All @@ -48,10 +51,11 @@ defmodule Nebulex.Entry do
## Example
iex> %Nebulex.Entry{}
iex> "hello"
...> |> Nebulex.Entry.encode()
...> |> Nebulex.Entry.decode()
_decoded_entry
"hello"
"""
# sobelow_skip ["Misc.BinToTerm"]
@spec decode(binary, [term]) :: term
Expand All @@ -68,6 +72,12 @@ defmodule Nebulex.Entry do
iex> Nebulex.Entry.expired?(%Nebulex.Entry{})
false
iex> Nebulex.Entry.expired?(
...> %Nebulex.Entry{touched: Nebulex.Time.now() - 10, ttl: 1}
...> )
true
"""
@spec expired?(t) :: boolean
def expired?(%__MODULE__{ttl: :infinity}), do: false
Expand All @@ -83,6 +93,14 @@ defmodule Nebulex.Entry do
iex> Nebulex.Entry.ttl(%Nebulex.Entry{})
:infinity
iex> ttl =
...> Nebulex.Entry.ttl(
...> %Nebulex.Entry{touched: Nebulex.Time.now(), ttl: 100}
...> )
iex> ttl > 0
true
"""
@spec ttl(t) :: timeout
def ttl(%__MODULE__{ttl: :infinity}), do: :infinity
Expand Down
21 changes: 5 additions & 16 deletions lib/nebulex/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,6 @@ defmodule Nebulex.Helpers do
end
end

@spec get_boolean_option(keyword, atom, boolean) :: term
def get_boolean_option(opts, key, default \\ false)
when is_list(opts) and is_atom(key) and is_boolean(default) do
value = Keyword.get(opts, key, default)

if is_boolean(value) do
value
else
raise ArgumentError, "expected #{key}: to be boolean, got: #{inspect(value)}"
end
end

@spec assert_behaviour(module, module, binary) :: module
def assert_behaviour(module, behaviour, msg \\ "module") do
if behaviour in module_behaviours(module, msg) do
Expand Down Expand Up @@ -70,15 +58,14 @@ defmodule Nebulex.Helpers do
end
end

# FIXME: this is because coveralls does not mark this as covered
# coveralls-ignore-start

@doc false
defmacro wrap_ok(call) do
# FIXME: this is because coveralls does not mark this as covered
# coveralls-ignore-start
quote do
{:ok, unquote(call)}
end

# coveralls-ignore-stop
end

@doc false
Expand All @@ -87,4 +74,6 @@ defmodule Nebulex.Helpers do
{:error, unquote(exception).exception(unquote(opts))}
end
end

# coveralls-ignore-stop
end
4 changes: 4 additions & 0 deletions lib/nebulex/telemetry/stats_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ defmodule Nebulex.Telemetry.StatsHandler do
update_stats(metadata)
end

# coveralls-ignore-start

def handle_event(_event, _measurements, _metadata, _ref) do
:ok
end

# coveralls-ignore-stop

defp update_stats(%{
function_name: action,
result: {:error, %Nebulex.KeyError{reason: :expired}},
Expand Down
10 changes: 8 additions & 2 deletions lib/nebulex/time.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ defmodule Nebulex.Time do
## Examples
iex> Nebulex.Time.now()
_milliseconds
iex> now = Nebulex.Time.now()
iex> is_integer(now) and now > 0
true
"""
@spec now(System.time_unit()) :: integer()
defdelegate now(unit \\ :millisecond), to: System, as: :system_time
Expand All @@ -37,6 +39,10 @@ defmodule Nebulex.Time do
iex> Nebulex.Time.timeout?(-1)
false
iex> Nebulex.Time.timeout?(1.1)
false
"""
@spec timeout?(term) :: boolean
def timeout?(timeout) do
Expand Down
Loading

0 comments on commit ea68edb

Please sign in to comment.