-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d969fb7
Showing
19 changed files
with
384 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Used by "mix format" | ||
[ | ||
inputs: ["mix.exs", "{config,lib,test}/**/*.{ex,exs}"] | ||
] |
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,24 @@ | ||
# The directory Mix will write compiled artifacts to. | ||
/_build/ | ||
|
||
# If you run "mix test --cover", coverage assets end up here. | ||
/cover/ | ||
|
||
# The directory Mix downloads your dependencies sources to. | ||
/deps/ | ||
|
||
# Where 3rd-party dependencies like ExDoc output generated docs. | ||
/doc/ | ||
|
||
# Ignore .fetch files in case you like to edit your project deps locally. | ||
/.fetch | ||
|
||
# If the VM crashes, it generates a dump, let's ignore it too. | ||
erl_crash.dump | ||
|
||
# Also ignore archive artifacts (built via "mix archive.build"). | ||
*.ez | ||
|
||
# Ignore package tarball (built via "mix hex.build"). | ||
time_machine-*.tar | ||
|
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,10 @@ | ||
language: elixir | ||
elixir: | ||
- 1.6.0 | ||
otp_release: | ||
- 20.0 | ||
script: mix analyze | ||
cache: | ||
directories: | ||
- _build | ||
- deps |
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,5 @@ | ||
# Changelog | ||
|
||
## 0.1.0 | ||
|
||
* Initial import |
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,22 @@ | ||
# MIT License | ||
|
||
Copyright (c) 2018 SQUARE ENIX LTD. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
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,75 @@ | ||
# TimeMachinex | ||
## Installation | ||
|
||
If [available in Hex](https://hex.pm/docs/publish), the package can be installed | ||
by adding `time_machine` to your list of dependencies in `mix.exs`: | ||
|
||
```elixir | ||
def deps do | ||
[ | ||
{:time_machinex, "~> 0.1.0"} | ||
] | ||
end | ||
``` | ||
|
||
## Introduction | ||
TimeMachinex is just a managed clock around the system clock which works with `DateTime.t()` types. | ||
|
||
### Configuration | ||
Configuring TimeMachine is very simple. What you generally want to do is to configure the *SystemClock* for `:prod` and `:dev` env: | ||
|
||
```elixir | ||
config :time_machinex, TimeMachinex, adapter: TimeMachinex.SystemClock | ||
``` | ||
|
||
and the *ManagedClock* in the `:test` env: | ||
```elixir | ||
config :time_machinex, TimeMachinex, adapter: TimeMachinex.ManagedClock | ||
``` | ||
|
||
### Usage | ||
Whenever you need the current system time just replace the standard `DateTime.utc_now/0` call with a `TimeMachinex.now/0` call. | ||
|
||
In `:prod` and `:dev` this will have no real side effects, since the `now/0` function is just an alias for the `DateTime.utc_now/0` thanks to the inline compilation attribute. | ||
|
||
The magic happens in the `:test` environment since the `TimeMachinex.ManagedClock` adapter will kick in (if configured properly). | ||
The only thing you need to do in your tests is to start the *ManagedClock*: | ||
|
||
ex(1)> TimeMachinex.ManagedClock.start() | ||
{:ok, #PID<0.190.0>} | ||
|
||
when it starts it is configured with the current time: | ||
|
||
iex(2)> TimeMachinex.ManagedClock.now() | ||
#DateTime<2018-01-30 15:51:33.689925Z> | ||
|
||
but now all the calls to `TimeMachinex.now/0` will read the time from the *ManagedClock* | ||
|
||
iex(3)> TimeMachinex.now() | ||
#DateTime<2018-01-30 15:51:33.689925Z> | ||
|
||
which means that you can manipulate, simulate the time passing and test the time used in your production code. | ||
|
||
And yes, you stopped the time! | ||
|
||
iex(4)> TimeMachinex.now() | ||
#DateTime<2018-01-30 15:51:33.689925Z> | ||
iex(5)> TimeMachinex.now() | ||
#DateTime<2018-01-30 15:51:33.689925Z> | ||
|
||
If you want to update the TimeMachinex with the current time again (to simulate time passing) you can just: | ||
|
||
iex(7)> TimeMachinex.ManagedClock.set() | ||
:ok | ||
iex(8)> TimeMachinex.now() | ||
#DateTime<2018-01-30 15:54:43.641350Z> | ||
|
||
or you may just want to set a specific time and wait for Marty McFly: | ||
|
||
iex(9)> TimeMachinex.ManagedClock.set(DateTime.from_naive!(~N[1985-10-26 09:00:00], "Etc/UTC")) | ||
:ok | ||
|
||
iex(10)> TimeMachinex.now() | ||
#DateTime<1985-10-26 09:00:00Z> | ||
|
||
Happy time travel! |
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 @@ | ||
use Mix.Config |
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,7 @@ | ||
{ | ||
"skip_files": [], | ||
"custom_stop_words": [ | ||
"\\\\\\\\", | ||
"defdelegate" | ||
] | ||
} |
Empty file.
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,16 @@ | ||
defmodule TimeMachinex do | ||
@moduledoc """ | ||
Define a generic clock api | ||
""" | ||
@spec now() :: DateTime.t() | ||
def now do | ||
adapter().now | ||
end | ||
|
||
defp adapter do | ||
case Application.fetch_env(:time_machinex, TimeMachinex) do | ||
{:ok, config} -> Keyword.get(config, :adapter, TimeMachinex.SystemClock) | ||
:error -> TimeMachinex.SystemClock | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
defmodule TimeMachinex.Adapter do | ||
@moduledoc """ | ||
Clock behaviour definition | ||
""" | ||
|
||
@callback now() :: DateTime.t() | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
defmodule TimeMachinex.ManagedClock do | ||
@moduledoc """ | ||
Programmable clock | ||
""" | ||
@behaviour TimeMachinex.Adapter | ||
|
||
@spec start() :: {:ok, pid} | ||
def start, do: Agent.start_link(&DateTime.utc_now/0, name: __MODULE__) | ||
|
||
@spec set(DateTime.t()) :: :ok | ||
def set(new_time \\ DateTime.utc_now()), do: Agent.update(__MODULE__, fn _ -> new_time end) | ||
|
||
@impl TimeMachinex.Adapter | ||
def now, do: Agent.get(__MODULE__, & &1) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
defmodule TimeMachinex.SystemClock do | ||
@moduledoc """ | ||
System clock implementation | ||
""" | ||
@compile {:inline, now: 0} | ||
@behaviour TimeMachinex.Adapter | ||
|
||
@impl TimeMachinex.Adapter | ||
def now, do: DateTime.utc_now() | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
defmodule TimeMachinex.MixProject do | ||
use Mix.Project | ||
|
||
@version "0.1.0" | ||
@description "Time machine clock to simplify time testing" | ||
|
||
def project do | ||
[ | ||
app: :time_machinex, | ||
version: @version, | ||
description: @description, | ||
elixir: "~> 1.6", | ||
start_permanent: Mix.env() == :prod, | ||
deps: deps(), | ||
package: package(), | ||
|
||
# Testing | ||
test_coverage: [tool: ExCoveralls], | ||
dialyzer: [ignore_warnings: "dialyzer.ignore-warnings", plt_add_deps: true], | ||
|
||
# Docs | ||
name: "TimeMachinex", | ||
docs: [extras: ["README.md", "CHANGELOG.md", "LICENSE.md"]] | ||
] | ||
end | ||
|
||
defp package do | ||
[ | ||
name: :time_machinex, | ||
files: [ | ||
# Project files | ||
"mix.exs", | ||
"README.md", | ||
"CHANGELOG.md", | ||
"LICENSE.md", | ||
# TimeMachinex | ||
"lib/time_machinex.ex", | ||
"lib/time_machinex" | ||
], | ||
maintainers: [ | ||
"Antonio Sagliocco", | ||
"Elliott Hilaire", | ||
"Francesco Grammatico", | ||
"Ian Luites", | ||
"Ricardo Perez", | ||
"Tatsuya Ono" | ||
], | ||
licenses: ["MIT"], | ||
links: %{"GitHub" => "https://github.com/shinyscorpion/time_machinex"} | ||
] | ||
end | ||
|
||
def application do | ||
[ | ||
extra_applications: [:logger] | ||
] | ||
end | ||
|
||
# Run "mix help deps" to learn about dependencies. | ||
defp deps do | ||
[ | ||
{:analyze, "~> 0.0.9", only: [:dev, :test]} | ||
] | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
%{ | ||
"analyze": {:hex, :analyze, "0.0.9", "0fa122220264e18d9f0c931d81440b030a02f9037bc8f13ea21bbc640a0f88ca", [], [{:credo, "~> 0.8", [hex: :credo, repo: "hexpm", optional: false]}, {:dialyxir, "~> 0.5", [hex: :dialyxir, repo: "hexpm", optional: false]}, {:ex_doc, "~> 0.18", [hex: :ex_doc, repo: "hexpm", optional: false]}, {:excoveralls, "~> 0.8", [hex: :excoveralls, repo: "hexpm", optional: false]}, {:hackney, "~> 1.10", [hex: :hackney, repo: "hexpm", optional: false]}, {:inch_ex, "~> 0.5", [hex: :inch_ex, repo: "hexpm", optional: false]}], "hexpm"}, | ||
"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [], [], "hexpm"}, | ||
"certifi": {:hex, :certifi, "2.0.0", "a0c0e475107135f76b8c1d5bc7efb33cd3815cb3cf3dea7aefdd174dabead064", [], [], "hexpm"}, | ||
"credo": {:hex, :credo, "0.8.10", "261862bb7363247762e1063713bb85df2bbd84af8d8610d1272cd9c1943bba63", [], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}], "hexpm"}, | ||
"dialyxir": {:hex, :dialyxir, "0.5.1", "b331b091720fd93e878137add264bac4f644e1ddae07a70bf7062c7862c4b952", [], [], "hexpm"}, | ||
"earmark": {:hex, :earmark, "1.2.4", "99b637c62a4d65a20a9fb674b8cffb8baa771c04605a80c911c4418c69b75439", [], [], "hexpm"}, | ||
"ex_doc": {:hex, :ex_doc, "0.18.2", "993e0a95e9fbb790ac54ea58e700b45b299bd48bc44b4ae0404f28161f37a83e", [], [{:earmark, "~> 1.1", [hex: :earmark, repo: "hexpm", optional: false]}], "hexpm"}, | ||
"excoveralls": {:hex, :excoveralls, "0.8.1", "0bbf67f22c7dbf7503981d21a5eef5db8bbc3cb86e70d3798e8c802c74fa5e27", [], [{:exjsx, ">= 3.0.0", [hex: :exjsx, repo: "hexpm", optional: false]}, {:hackney, ">= 0.12.0", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm"}, | ||
"exjsx": {:hex, :exjsx, "4.0.0", "60548841e0212df401e38e63c0078ec57b33e7ea49b032c796ccad8cde794b5c", [], [{:jsx, "~> 2.8.0", [hex: :jsx, repo: "hexpm", optional: false]}], "hexpm"}, | ||
"hackney": {:hex, :hackney, "1.11.0", "4951ee019df102492dabba66a09e305f61919a8a183a7860236c0fde586134b6", [], [{:certifi, "2.0.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "5.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm"}, | ||
"idna": {:hex, :idna, "5.1.0", "d72b4effeb324ad5da3cab1767cb16b17939004e789d8c0ad5b70f3cea20c89a", [], [{:unicode_util_compat, "0.3.1", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm"}, | ||
"inch_ex": {:hex, :inch_ex, "0.5.6", "418357418a553baa6d04eccd1b44171936817db61f4c0840112b420b8e378e67", [], [{:poison, "~> 1.5 or ~> 2.0 or ~> 3.0", [hex: :poison, repo: "hexpm", optional: false]}], "hexpm"}, | ||
"jsx": {:hex, :jsx, "2.8.3", "a05252d381885240744d955fbe3cf810504eb2567164824e19303ea59eef62cf", [], [], "hexpm"}, | ||
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [], [], "hexpm"}, | ||
"mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [], [], "hexpm"}, | ||
"poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [], [], "hexpm"}, | ||
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.1", "28a4d65b7f59893bc2c7de786dec1e1555bd742d336043fe644ae956c3497fbe", [], [], "hexpm"}, | ||
"unicode_util_compat": {:hex, :unicode_util_compat, "0.3.1", "a1f612a7b512638634a603c8f401892afbf99b8ce93a45041f8aaca99cadb85e", [], [], "hexpm"}, | ||
} |
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 @@ | ||
ExUnit.start() |
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,54 @@ | ||
defmodule TimeMachinex.ManagedClockTest do | ||
@moduledoc false | ||
use ExUnit.Case, async: false | ||
|
||
alias TimeMachinex.ManagedClock | ||
|
||
@past ~N[1970-01-01 10:10:10] | ||
|
||
describe "start/1" do | ||
test "starts the sigleton agent" do | ||
assert {:ok, _pid} = ManagedClock.start() | ||
assert {:error, {:already_started, _pid}} = ManagedClock.start() | ||
end | ||
end | ||
|
||
describe "now/0" do | ||
setup do | ||
ManagedClock.start() | ||
|
||
:ok | ||
end | ||
|
||
test "return the time set in the time machine" do | ||
now = ManagedClock.now() | ||
assert now < DateTime.utc_now() | ||
assert now == ManagedClock.now() | ||
|
||
ManagedClock.set(DateTime.from_naive!(@past, "Etc/UTC")) | ||
assert DateTime.from_naive!(@past, "Etc/UTC") == ManagedClock.now() | ||
end | ||
end | ||
|
||
describe "set/1" do | ||
setup do | ||
ManagedClock.start() | ||
|
||
:ok | ||
end | ||
|
||
test "set the time in the time machine to the new system time as default" do | ||
now = ManagedClock.now() | ||
|
||
ManagedClock.set() | ||
|
||
assert now < ManagedClock.now() | ||
end | ||
|
||
test "set the time in the time machine to a specific value" do | ||
ManagedClock.set(DateTime.from_naive!(@past, "Etc/UTC")) | ||
|
||
assert ManagedClock.now() < DateTime.utc_now() | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
defmodule TimeMachinex.SystemClockTest do | ||
@moduledoc false | ||
use ExUnit.Case, async: false | ||
|
||
alias TimeMachinex.SystemClock | ||
|
||
describe "now/0" do | ||
test "return the current system time" do | ||
now = SystemClock.now() | ||
|
||
assert now < SystemClock.now() | ||
end | ||
end | ||
end |
Oops, something went wrong.