Skip to content

Commit

Permalink
Delegate optional vars to optional configurations when building event…
Browse files Browse the repository at this point in the history
… with the block
  • Loading branch information
mustafaturan committed Jun 20, 2018
1 parent ac534e2 commit e7f48e6
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 32 deletions.
5 changes: 4 additions & 1 deletion config/test.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use Mix.Config

config :event_bus,
topics: [:metrics_received, :metrics_summed]
topics: [:metrics_received, :metrics_summed],
ttl: 30_000_000,
time_unit: :micro_seconds,
id_generator: EventBus.Util.String
24 changes: 14 additions & 10 deletions lib/event_bus/event_source.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ defmodule EventBus.EventSource do

alias EventBus.EventSource
alias EventBus.Model.Event
alias EventBus.Util.String, as: StringUtil

@eb_app :event_bus
@eb_id_gen Application.get_env(@eb_app, :id_generator, StringUtil)
@eb_source String.replace("#{__MODULE__}", "Elixir.", "")
@eb_time_unit Application.get_env(@eb_app, :time_unit, :micro_seconds)
@eb_ttl Application.get_env(@eb_app, :ttl)
end
end

Expand All @@ -21,13 +28,10 @@ defmodule EventBus.EventSource do
"""
defmacro build(params, do: yield) do
quote do
started_at = System.monotonic_time(:micro_seconds)
initialized_at = System.os_time(:micro_seconds)
started_at = System.monotonic_time(@eb_time_unit)
initialized_at = System.os_time(@eb_time_unit)
params = unquote(params)

source =
Map.get(params, :source, String.replace("#{__MODULE__}", "Elixir.", ""))

{topic, data} =
case unquote(yield) do
{:error, error} ->
Expand All @@ -37,17 +41,17 @@ defmodule EventBus.EventSource do
{params[:topic], result}
end

time_spent = System.monotonic_time(:micro_seconds) - started_at
time_spent = System.monotonic_time(@eb_time_unit) - started_at

%Event{
id: params[:id],
id: Map.get(params, :id, @eb_id_gen.unique_id()),
topic: topic,
transaction_id: params[:transaction_id],
transaction_id: Map.get(params, :transaction_id),
data: data,
initialized_at: initialized_at,
occurred_at: initialized_at + time_spent,
source: source,
ttl: params[:ttl]
source: Map.get(params, :source, @eb_source),
ttl: Map.get(params, :ttl, @eb_ttl)
}
end
end
Expand Down
46 changes: 25 additions & 21 deletions test/event_bus/event_source_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule EventBus.EventSourceTest do
:ok
end

test "build with source" do
test "build with all params" do
id = 1
topic = :user_created
data = %{id: 1, name: "me", email: "me@example.com"}
Expand All @@ -35,33 +35,37 @@ defmodule EventBus.EventSourceTest do
assert event.source == "me"
refute is_nil(event.initialized_at)
refute is_nil(event.occurred_at)
assert Event.duration(event) > 0
end

test "build without source" do
id = 1
test "build without passing source" do
topic = :user_created
data = %{id: 1, name: "me", email: "me@example.com"}
transaction_id = "t1"
ttl = 100

event =
EventSource.build %{
id: id,
topic: topic,
transaction_id: transaction_id,
ttl: ttl
} do
data
EventSource.build %{topic: topic} do
"some event data"
end

assert event.data == data
assert event.id == id
assert event.topic == topic
assert event.transaction_id == transaction_id
assert event.ttl == ttl
assert event.source == "EventBus.EventSourceTest"
refute is_nil(event.initialized_at)
refute is_nil(event.occurred_at)
end

test "build without passing ttl, sets the ttl from app configuration" do
topic = :user_created
event =
EventSource.build %{topic: topic} do
"some event data"
end

assert event.ttl == 30_000_000
end

test "build without passing id, sets the id with unique_id function" do
topic = :user_created
event =
EventSource.build %{topic: topic} do
"some event data"
end

refute is_nil(event.id)
end

test "build with error topic" do
Expand Down

0 comments on commit e7f48e6

Please sign in to comment.