-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from otobus/mustafaturan/monotonic_time
Enhancements
- Loading branch information
Showing
11 changed files
with
112 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
defmodule EventBus.Util.MonotonicTime do | ||
@moduledoc false | ||
|
||
@eb_app :event_bus | ||
@eb_time_unit Application.get_env(@eb_app, :time_unit, :microsecond) | ||
|
||
@doc """ | ||
Calculates monotonically increasing current time | ||
""" | ||
@spec now() :: integer() | ||
def now do | ||
init_time() + monotonic_time() | ||
end | ||
|
||
defp init_time do | ||
case Application.get_env(@eb_app, :init_time) do | ||
nil -> | ||
time = os_time() - monotonic_time() | ||
save_init_time(time) | ||
|
||
time -> | ||
time | ||
end | ||
end | ||
|
||
defp save_init_time(time) do | ||
Application.put_env(:event_bus, :init_time, time, persistent: true) | ||
time | ||
end | ||
|
||
defp os_time do | ||
System.os_time(@eb_time_unit) | ||
end | ||
|
||
defp monotonic_time do | ||
System.monotonic_time(@eb_time_unit) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,15 +15,17 @@ defmodule EventBus.EventSourceTest do | |
data = %{id: 1, name: "me", email: "[email protected]"} | ||
transaction_id = "t1" | ||
ttl = 100 | ||
params = %{ | ||
id: id, | ||
topic: topic, | ||
transaction_id: transaction_id, | ||
ttl: ttl, | ||
source: "me" | ||
} | ||
|
||
event = | ||
EventSource.build %{ | ||
id: id, | ||
topic: topic, | ||
transaction_id: transaction_id, | ||
ttl: ttl, | ||
source: "me" | ||
} do | ||
EventSource.build params do | ||
Process.sleep(1_000) | ||
data | ||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ defmodule EventBus.Model.EventTest do | |
use ExUnit.Case | ||
require EventBus.Model.Event | ||
alias EventBus.Model.Event | ||
alias EventBus.Util.MonotonicTime | ||
|
||
doctest Event | ||
|
||
|
@@ -11,16 +12,17 @@ defmodule EventBus.Model.EventTest do | |
end | ||
|
||
test "duration" do | ||
initialized_at = System.os_time(:microsecond) | ||
# do sth in this frame | ||
Process.sleep(1) | ||
initialized_at = MonotonicTime.now() | ||
# Do sth in this frame | ||
# For example; sleep 1 second | ||
Process.sleep(1_000) | ||
|
||
event = %Event{ | ||
id: 1, | ||
topic: "user_created", | ||
data: %{id: 1, name: "me", email: "[email protected]"}, | ||
initialized_at: initialized_at, | ||
occurred_at: System.os_time(:microsecond) | ||
occurred_at: MonotonicTime.now() | ||
} | ||
|
||
assert Event.duration(event) > 0 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
defmodule EventBus.Util.MonotonicTimeTest do | ||
use ExUnit.Case | ||
|
||
alias EventBus.Util.MonotonicTime | ||
|
||
test "now should return int" do | ||
assert is_integer(MonotonicTime.now()) | ||
end | ||
|
||
test "now should increase on every call" do | ||
assert MonotonicTime.now() <= MonotonicTime.now() | ||
end | ||
|
||
test "now does not change init_time configuration on multiple calls" do | ||
# First call | ||
MonotonicTime.now() | ||
init_time = Application.get_env(:event_bus, :init_time) # init_time | ||
|
||
# Second call | ||
MonotonicTime.now() | ||
assert init_time == Application.get_env(:event_bus, :init_time) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters