Skip to content

Commit

Permalink
feature: provide configuration for locked id response value
Browse files Browse the repository at this point in the history
  • Loading branch information
Ino Murko committed Oct 20, 2019
1 parent 6490752 commit b79c88e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
2 changes: 2 additions & 0 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ use Mix.Config
config :ethereumex, url: "http://localhost:8545"

config :ethereumex, ipc_path: "/.local/share/io.parity.ethereum/jsonrpc.ipc"

# config :ethereumex, id_lock: "0"
21 changes: 20 additions & 1 deletion lib/ethereumex/counter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,30 @@ defmodule Ethereumex.Counter do

@spec increment(atom()) :: integer()
def increment(key) do
:ets.update_counter(@tab, key, {2, 1}, {key, 0})
do_increment(Application.get_env(:ethereumex, :id_lock), key)
end

@spec increment(atom(), integer()) :: integer()
def increment(key, count) do
do_increment(Application.get_env(:ethereumex, :id_lock), key, count)
end

@spec do_increment(binary() | nil, atom()) :: integer()
defp do_increment(nil, key) do
:ets.update_counter(@tab, key, {2, 1}, {key, 0})
end

defp do_increment(id_lock, _key) do
id_lock
end

@spec do_increment(binary() | nil, atom(), integer()) :: integer()
defp do_increment(nil, key, count) do
:ets.update_counter(@tab, key, {2, count}, {key, 0})
end

@spec do_increment(binary() | nil, atom(), integer()) :: integer()
defp do_increment(id_lock, _key, _count) do
id_lock
end
end
28 changes: 28 additions & 0 deletions test/ethereumex/counter_lock_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
defmodule Ethereumex.CounterLockTest do
use ExUnit.Case
alias Ethereumex.Counter

setup do
Application.put_env(:ethereumex, :id_lock, "0")

on_exit(fn ->
Application.put_env(:ethereumex, :id_lock, nil)
end)
end

test "incrementing twice returns correct locked binary" do
"0" = Counter.increment(:test_1)
"0" = Counter.increment(:test_1)
end

test "incrementing twice and updating with a count returns correct locked binary" do
"0" = Counter.increment(:test_2)
"0" = Counter.increment(:test_2, 2)
end

test "incrementing twice, updating with a count and incrementing again returns correct locked binary" do
"0" = Counter.increment(:test_3)
"0" = Counter.increment(:test_3, 2)
"0" = Counter.increment(:test_3)
end
end

0 comments on commit b79c88e

Please sign in to comment.