Skip to content

Commit e6a8849

Browse files
committed
[#2] Implement Nebulex.Adapter.Queryable behaviour
[#4] Support distributed adapters with Cachex
1 parent da9dbcc commit e6a8849

14 files changed

Lines changed: 331 additions & 30 deletions

File tree

.credo.exs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
%{
2+
configs: [
3+
%{
4+
name: "default",
5+
files: %{
6+
included: ["lib/", "src/", "test/", "benchmarks/"],
7+
excluded: [~r"/_build/", ~r"/deps/"]
8+
},
9+
color: true,
10+
checks: [
11+
# Design Checks
12+
{Credo.Check.Design.AliasUsage, priority: :low},
13+
14+
# Deactivate due to they're not compatible with current Elixir version
15+
{Credo.Check.Refactor.MapInto, false},
16+
{Credo.Check.Warning.LazyLogging, false},
17+
18+
# Readability Checks
19+
{Credo.Check.Readability.MaxLineLength, priority: :low, max_length: 100},
20+
21+
# Refactoring Opportunities
22+
{Credo.Check.Refactor.LongQuoteBlocks, false},
23+
{Credo.Check.Refactor.CyclomaticComplexity, max_complexity: 15},
24+
25+
# TODO and FIXME do not cause the build to fail
26+
{Credo.Check.Design.TagTODO, exit_status: 0},
27+
{Credo.Check.Design.TagFIXME, exit_status: 0}
28+
]
29+
}
30+
]
31+
}

.github/workflows/ci.yml

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ jobs:
1616
strategy:
1717
matrix:
1818
include:
19+
- elixir: 1.11.x
20+
otp: 23.x
21+
coverage: true
1922
- elixir: 1.10.x
2023
otp: 23.x
2124
- elixir: 1.10.x
@@ -26,17 +29,19 @@ jobs:
2629
env:
2730
GITHUB_TOKEN: '${{ secrets.GITHUB_TOKEN }}'
2831
MIX_ENV: test
29-
NBX_TEST: true
32+
NEBULEX_PATH: nebulex
3033

3134
steps:
3235
- uses: actions/checkout@v2
3336

34-
- uses: actions/setup-elixir@v1
37+
- name: Install OTP and Elixir
38+
uses: actions/setup-elixir@v1
3539
with:
3640
otp-version: '${{ matrix.otp }}'
3741
elixir-version: '${{ matrix.elixir }}'
3842

39-
- uses: actions/cache@v1
43+
- name: Cache deps
44+
uses: actions/cache@v1
4045
with:
4146
path: deps
4247
key: >-
@@ -45,7 +50,8 @@ jobs:
4550
restore-keys: |
4651
${{ runner.os }}-${{ matrix.otp }}-${{ matrix.elixir }}-mix-
4752
48-
- uses: actions/cache@v1
53+
- name: Cache _build
54+
uses: actions/cache@v1
4955
with:
5056
path: _build
5157
key: >-
@@ -59,6 +65,7 @@ jobs:
5965
mix local.hex --force
6066
mix local.rebar --force
6167
mix deps.get
68+
git clone --depth 1 --branch master https://github.com/cabol/nebulex.git
6269
6370
- name: Run style and code consistency checks
6471
run: |
@@ -67,11 +74,19 @@ jobs:
6774
mix credo --strict
6875
6976
- name: Run tests
77+
run: |
78+
epmd -daemon
79+
mix test --trace
80+
if: ${{!matrix.coverage}}
81+
82+
- name: Run tests with coverage
7083
run: |
7184
epmd -daemon
7285
mix coveralls.github
86+
if: ${{matrix.coverage}}
7387

74-
- uses: actions/cache@v1
88+
- name: Cache PLT
89+
uses: actions/cache@v1
7590
with:
7691
path: priv/plts
7792
key: '${{ runner.os }}-${{ matrix.otp }}-${{ matrix.elixir }}-plt-v1'

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ erl_crash.dump
3636
.vs*
3737
/priv
3838
.sobelow*
39-
mix.lock
39+
/nebulex

lib/nebulex_cachex_adapter.ex

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ defmodule NebulexCachexAdapter do
55

66
# Provide Cache Implementation
77
@behaviour Nebulex.Adapter
8+
@behaviour Nebulex.Adapter.Queryable
89

910
import Nebulex.Helpers
1011

12+
alias Cachex.Query
13+
1114
@compile {:inline, to_ttl: 1}
1215

1316
## Adapter
@@ -57,6 +60,8 @@ defmodule NebulexCachexAdapter do
5760
end
5861

5962
def put(%{name: name}, key, value, ttl, :put_new, _opts) do
63+
# FIXME: This is a workaround since Cachex does not support a direct action
64+
# for put_new. Fix it if a better solution comes up.
6065
if Cachex.get!(name, key) do
6166
false
6267
else
@@ -76,6 +81,8 @@ defmodule NebulexCachexAdapter do
7681
def put_all(%{name: name}, entries, ttl, :put_new, _opts) when is_list(entries) do
7782
{keys, _} = Enum.unzip(entries)
7883

84+
# FIXME: This is a workaround since Cachex does not support a direct action
85+
# for put_new. Fix it if a better solution comes up.
7986
Cachex.transaction!(name, keys, fn worker ->
8087
if Enum.any?(keys, &(worker |> Cachex.exists?(&1) |> elem(1))) do
8188
false
@@ -105,12 +112,15 @@ defmodule NebulexCachexAdapter do
105112
@impl true
106113
def ttl(%{name: name}, key) do
107114
cond do
115+
# Key does exist and has a TTL associated with it
108116
ttl = Cachex.ttl!(name, key) ->
109117
ttl
110118

119+
# Key does exist and hasn't a TTL associated with it
111120
Cachex.get!(name, key) ->
112121
:infinity
113122

123+
# Key does not exist
114124
true ->
115125
nil
116126
end
@@ -132,6 +142,8 @@ defmodule NebulexCachexAdapter do
132142
end
133143

134144
def incr(%{name: name}, key, incr, ttl, opts) do
145+
# FIXME: This is a workaround since Cachex does not support `:ttl` here.
146+
# Fix it if a better solution comes up.
135147
Cachex.transaction!(name, [key], fn worker ->
136148
counter = Cachex.incr!(worker, key, incr, initial: opts[:default] || 0)
137149
if ttl = to_ttl(ttl), do: Cachex.expire!(worker, key, ttl)
@@ -151,6 +163,27 @@ defmodule NebulexCachexAdapter do
151163
size
152164
end
153165

166+
## Queryable
167+
168+
@impl true
169+
def all(adapter_meta, query, opts) do
170+
adapter_meta
171+
|> stream(query, opts)
172+
|> Enum.to_list()
173+
end
174+
175+
@impl true
176+
def stream(adapter_meta, nil, opts) do
177+
stream(adapter_meta, Query.create(true, :key), opts)
178+
end
179+
180+
def stream(%{name: name}, query, opts) do
181+
Cachex.stream!(name, query, batch_size: opts[:page_size] || 100)
182+
rescue
183+
e in Cachex.ExecutionError ->
184+
reraise Nebulex.QueryError, [message: e.message, query: query], __STACKTRACE__
185+
end
186+
154187
## Private Functions
155188

156189
defp to_ttl(:infinity), do: nil

mix.exs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ defmodule NebulexCachexAdapter.MixProject do
5252
{:credo, "~> 1.5", only: [:dev, :test], runtime: false},
5353
{:dialyxir, "~> 1.0", only: [:dev, :test], runtime: false},
5454
{:sobelow, "~> 0.10", only: [:dev, :test], runtime: false},
55+
{:stream_data, "~> 0.5", only: [:dev, :test]},
5556

5657
# Benchmark Test
5758
{:benchee, "~> 1.0", only: :test},
@@ -63,12 +64,8 @@ defmodule NebulexCachexAdapter.MixProject do
6364
end
6465

6566
defp nebulex_dep do
66-
if System.get_env("NBX_TEST") do
67-
# This is because the adapter tests need some support modules and shared
68-
# tests from nebulex dependency, and the hex dependency doesn't include
69-
# the test folder. Hence, to run the tests it is necessary to fetch
70-
# nebulex dependency directly from GH.
71-
{:nebulex, github: "cabol/nebulex", branch: "master"}
67+
if path = System.get_env("NEBULEX_PATH") do
68+
{:nebulex, "~> 2.0.0-rc.1", path: path}
7269
else
7370
{:nebulex, "~> 2.0.0-rc.1"}
7471
end

mix.lock

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
%{
2+
"benchee": {:hex, :benchee, "1.0.1", "66b211f9bfd84bd97e6d1beaddf8fc2312aaabe192f776e8931cb0c16f53a521", [:mix], [{:deep_merge, "~> 1.0", [hex: :deep_merge, repo: "hexpm", optional: false]}], "hexpm", "3ad58ae787e9c7c94dd7ceda3b587ec2c64604563e049b2a0e8baafae832addb"},
3+
"benchee_html": {:hex, :benchee_html, "1.0.0", "5b4d24effebd060f466fb460ec06576e7b34a00fc26b234fe4f12c4f05c95947", [:mix], [{:benchee, ">= 0.99.0 and < 2.0.0", [hex: :benchee, repo: "hexpm", optional: false]}, {:benchee_json, "~> 1.0", [hex: :benchee_json, repo: "hexpm", optional: false]}], "hexpm", "5280af9aac432ff5ca4216d03e8a93f32209510e925b60e7f27c33796f69e699"},
4+
"benchee_json": {:hex, :benchee_json, "1.0.0", "cc661f4454d5995c08fe10dd1f2f72f229c8f0fb1c96f6b327a8c8fc96a91fe5", [:mix], [{:benchee, ">= 0.99.0 and < 2.0.0", [hex: :benchee, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "da05d813f9123505f870344d68fb7c86a4f0f9074df7d7b7e2bb011a63ec231c"},
5+
"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm", "7af5c7e09fe1d40f76c8e4f9dd2be7cebd83909f31fee7cd0e9eadc567da8353"},
6+
"cachex": {:hex, :cachex, "3.3.0", "6f2ebb8f27491fe39121bd207c78badc499214d76c695658b19d6079beeca5c2", [:mix], [{:eternal, "~> 1.2", [hex: :eternal, repo: "hexpm", optional: false]}, {:jumper, "~> 1.0", [hex: :jumper, repo: "hexpm", optional: false]}, {:sleeplocks, "~> 1.1", [hex: :sleeplocks, repo: "hexpm", optional: false]}, {:unsafe, "~> 1.0", [hex: :unsafe, repo: "hexpm", optional: false]}], "hexpm", "d90e5ee1dde14cef33f6b187af4335b88748b72b30c038969176cd4e6ccc31a1"},
7+
"certifi": {:hex, :certifi, "2.5.3", "70bdd7e7188c804f3a30ee0e7c99655bc35d8ac41c23e12325f36ab449b70651", [:rebar3], [{:parse_trans, "~>3.3", [hex: :parse_trans, repo: "hexpm", optional: false]}], "hexpm", "ed516acb3929b101208a9d700062d520f3953da3b6b918d866106ffa980e1c10"},
8+
"credo": {:hex, :credo, "1.5.4", "9914180105b438e378e94a844ec3a5088ae5875626fc945b7c1462b41afc3198", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2.8", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "cf51af45eadc0a3f39ba13b56fdac415c91b34f7b7533a13dc13550277141bc4"},
9+
"deep_merge": {:hex, :deep_merge, "1.0.0", "b4aa1a0d1acac393bdf38b2291af38cb1d4a52806cf7a4906f718e1feb5ee961", [:mix], [], "hexpm", "ce708e5f094b9cd4e8f2be4f00d2f4250c4095be93f8cd6d018c753894885430"},
10+
"dialyxir": {:hex, :dialyxir, "1.0.0", "6a1fa629f7881a9f5aaf3a78f094b2a51a0357c843871b8bc98824e7342d00a5", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "aeb06588145fac14ca08d8061a142d52753dbc2cf7f0d00fc1013f53f8654654"},
11+
"earmark_parser": {:hex, :earmark_parser, "1.4.12", "b245e875ec0a311a342320da0551da407d9d2b65d98f7a9597ae078615af3449", [:mix], [], "hexpm", "711e2cc4d64abb7d566d43f54b78f7dc129308a63bc103fbd88550d2174b3160"},
12+
"erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"},
13+
"eternal": {:hex, :eternal, "1.2.2", "d1641c86368de99375b98d183042dd6c2b234262b8d08dfd72b9eeaafc2a1abd", [:mix], [], "hexpm", "2c9fe32b9c3726703ba5e1d43a1d255a4f3f2d8f8f9bc19f094c7cb1a7a9e782"},
14+
"ex_doc": {:hex, :ex_doc, "0.23.0", "a069bc9b0bf8efe323ecde8c0d62afc13d308b1fa3d228b65bca5cf8703a529d", [:mix], [{:earmark_parser, "~> 1.4.0", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm", "f5e2c4702468b2fd11b10d39416ddadd2fcdd173ba2a0285ebd92c39827a5a16"},
15+
"excoveralls": {:hex, :excoveralls, "0.13.4", "7b0baee01fe150ef81153e6ffc0fc68214737f54570dc257b3ca4da8e419b812", [:mix], [{:hackney, "~> 1.16", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "faae00b3eee35cdf0342c10b669a7c91f942728217d2a7c7f644b24d391e6190"},
16+
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},
17+
"hackney": {:hex, :hackney, "1.17.0", "717ea195fd2f898d9fe9f1ce0afcc2621a41ecfe137fae57e7fe6e9484b9aa99", [:rebar3], [{:certifi, "~>2.5", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~>6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~>1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "~>3.3", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~>1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "64c22225f1ea8855f584720c0e5b3cd14095703af1c9fbc845ba042811dc671c"},
18+
"idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"},
19+
"jason": {:hex, :jason, "1.2.2", "ba43e3f2709fd1aa1dce90aaabfd039d000469c05c56f0b8e31978e03fa39052", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "18a228f5f0058ee183f29f9eae0805c6e59d61c3b006760668d8d18ff0d12179"},
20+
"jumper": {:hex, :jumper, "1.0.1", "3c00542ef1a83532b72269fab9f0f0c82bf23a35e27d278bfd9ed0865cecabff", [:mix], [], "hexpm", "318c59078ac220e966d27af3646026db9b5a5e6703cb2aa3e26bcfaba65b7433"},
21+
"makeup": {:hex, :makeup, "1.0.5", "d5a830bc42c9800ce07dd97fa94669dfb93d3bf5fcf6ea7a0c67b2e0e4a7f26c", [:mix], [{:nimble_parsec, "~> 0.5 or ~> 1.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "cfa158c02d3f5c0c665d0af11512fed3fba0144cf1aadee0f2ce17747fba2ca9"},
22+
"makeup_elixir": {:hex, :makeup_elixir, "0.15.0", "98312c9f0d3730fde4049985a1105da5155bfe5c11e47bdc7406d88e01e4219b", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.1", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "75ffa34ab1056b7e24844c90bfc62aaf6f3a37a15faa76b07bc5eba27e4a8b4a"},
23+
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"},
24+
"mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"},
25+
"nimble_parsec": {:hex, :nimble_parsec, "1.1.0", "3a6fca1550363552e54c216debb6a9e95bd8d32348938e13de5eda962c0d7f89", [:mix], [], "hexpm", "08eb32d66b706e913ff748f11694b17981c0b04a33ef470e33e11b3d3ac8f54b"},
26+
"parse_trans": {:hex, :parse_trans, "3.3.0", "09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1", [:rebar3], [], "hexpm", "17ef63abde837ad30680ea7f857dd9e7ced9476cdd7b0394432af4bfc241b960"},
27+
"sleeplocks": {:hex, :sleeplocks, "1.1.1", "3d462a0639a6ef36cc75d6038b7393ae537ab394641beb59830a1b8271faeed3", [:rebar3], [], "hexpm", "84ee37aeff4d0d92b290fff986d6a95ac5eedf9b383fadfd1d88e9b84a1c02e1"},
28+
"sobelow": {:hex, :sobelow, "0.10.6", "ac9ebd742035b119eb00a4b1416098b88a4d54d2f262a42602e1e9e3ed4c2afd", [:mix], [], "hexpm", "06e426b8dc0bc80ab1333ce89b904c720474824825b9ceb3dc424eb0f731b6e9"},
29+
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.6", "cf344f5692c82d2cd7554f5ec8fd961548d4fd09e7d22f5b62482e5aeaebd4b0", [:make, :mix, :rebar3], [], "hexpm", "bdb0d2471f453c88ff3908e7686f86f9be327d065cc1ec16fa4540197ea04680"},
30+
"stream_data": {:hex, :stream_data, "0.5.0", "b27641e58941685c75b353577dc602c9d2c12292dd84babf506c2033cd97893e", [:mix], [], "hexpm", "012bd2eec069ada4db3411f9115ccafa38540a3c78c4c0349f151fc761b9e271"},
31+
"unicode_util_compat": {:hex, :unicode_util_compat, "0.7.0", "bc84380c9ab48177092f43ac89e4dfa2c6d62b40b8bd132b1059ecc7232f9a78", [:rebar3], [], "hexpm", "25eee6d67df61960cf6a794239566599b09e17e668d3700247bc498638152521"},
32+
"unsafe": {:hex, :unsafe, "1.0.1", "a27e1874f72ee49312e0a9ec2e0b27924214a05e3ddac90e91727bc76f8613d8", [:mix], [], "hexpm", "6c7729a2d214806450d29766abc2afaa7a2cbecf415be64f36a6691afebb50e5"},
33+
}

test/nebulex_cachex_adapter/local_test.exs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,9 @@ defmodule NebulexCachexAdapter.LocalTest do
22
use ExUnit.Case, async: true
33
use NebulexCachexAdapter.CacheTest
44

5-
alias NebulexCachexAdapter.TestCache.Local, as: Cache
6-
7-
setup do
8-
{:ok, pid} = Cache.start_link()
9-
_ = Cache.flush()
10-
:ok
5+
import Nebulex.CacheCase
116

12-
on_exit(fn ->
13-
:ok = Process.sleep(100)
14-
if Process.alive?(pid), do: Cache.stop(pid)
15-
end)
7+
alias NebulexCachexAdapter.TestCache.Local, as: Cache
168

17-
{:ok, cache: Cache, name: Cache}
18-
end
9+
setup_with_dynamic_cache(Cache, :local_with_cachex)
1910
end
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
defmodule NebulexCachexAdapter.MultilevelTest do
2+
use ExUnit.Case, async: true
3+
use Nebulex.NodeCase
4+
use Nebulex.MultilevelTest
5+
# use Nebulex.Cache.QueryableTest
6+
# use Nebulex.Cache.TransactionTest
7+
8+
import Nebulex.CacheCase
9+
10+
alias Nebulex.Time
11+
alias NebulexCachexAdapter.TestCache.Multilevel
12+
alias NebulexCachexAdapter.TestCache.Multilevel.{L1, L2, L3}
13+
14+
@gc_interval Time.expiry_time(1, :hour)
15+
16+
@levels [
17+
{
18+
L1,
19+
name: :multilevel_inclusive_l1, gc_interval: @gc_interval
20+
},
21+
{
22+
L2,
23+
name: :multilevel_inclusive_l2, primary: [gc_interval: @gc_interval]
24+
},
25+
{
26+
L3,
27+
name: :multilevel_inclusive_l3, primary: [gc_interval: @gc_interval]
28+
}
29+
]
30+
31+
setup_with_dynamic_cache(Multilevel, :multilevel_inclusive,
32+
model: :inclusive,
33+
levels: @levels
34+
)
35+
end
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
defmodule NebulexCachexAdapter.PartitionedTest do
2+
use Nebulex.NodeCase
3+
use NebulexCachexAdapter.CacheTest
4+
5+
alias NebulexCachexAdapter.TestCache.Partitioned
6+
7+
@primary :"primary@127.0.0.1"
8+
9+
setup do
10+
cluster =
11+
:lists.usort([
12+
@primary
13+
| Application.get_env(:nebulex_cachex_adapter, :nodes, [])
14+
])
15+
16+
node_pid_list =
17+
start_caches(
18+
[node() | Node.list()],
19+
[{Partitioned, []}]
20+
)
21+
22+
on_exit(fn ->
23+
:ok = Process.sleep(100)
24+
stop_caches(node_pid_list)
25+
end)
26+
27+
{:ok, cache: Partitioned, name: Partitioned, cluster: cluster}
28+
end
29+
30+
describe "partitioned cache" do
31+
test "get_and_update" do
32+
assert Partitioned.get_and_update(1, &Partitioned.get_and_update_fun/1) == {nil, 1}
33+
assert Partitioned.get_and_update(1, &Partitioned.get_and_update_fun/1) == {1, 2}
34+
assert Partitioned.get_and_update(1, &Partitioned.get_and_update_fun/1) == {2, 4}
35+
36+
assert_raise ArgumentError, fn ->
37+
Partitioned.get_and_update(1, &Partitioned.get_and_update_bad_fun/1)
38+
end
39+
end
40+
end
41+
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
defmodule NebulexCachexAdapter.ReplicatedTest do
2+
use Nebulex.NodeCase
3+
use NebulexCachexAdapter.CacheTest
4+
5+
alias NebulexCachexAdapter.TestCache.Replicated
6+
7+
setup do
8+
node_pid_list = start_caches([node() | Node.list()], [{Replicated, []}])
9+
10+
on_exit(fn ->
11+
:ok = Process.sleep(100)
12+
stop_caches(node_pid_list)
13+
end)
14+
15+
{:ok, cache: Replicated, name: Replicated}
16+
end
17+
end

0 commit comments

Comments
 (0)