amok_time 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fe053bae917824071c157ba458f05151a5bf5dda
4
+ data.tar.gz: 449d35331df3ed3c10c98ea642cdcdeed00e2646
5
+ SHA512:
6
+ metadata.gz: a59caa914958d36858f6ccd9935826cfe6d90e403be5846b43480129c2a2a32cafd9a65dc73d8f8535e59bcc13083887afe769fcd1899fe85e6cc4bacb5c29d7
7
+ data.tar.gz: f4dff2622181a9d8f77f9add27d646cc9866240b97a806a344c439984847916e6be7fadc10f1b4657ad5345f3d8262db3e21e0be5da56ecc973eb914a82503cf
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in amok_time.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Matt Larraz
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,103 @@
1
+ # Amok Time
2
+
3
+ Utility for mocking time across service boundaries on a per-request basis.
4
+ Also passes the mock along to any background jobs enqueued during the request.
5
+
6
+ Amok Time is designed for end-to-end testing distributed systems where certain behavior is time-based.
7
+ It works by allowing any client to send a request header that overrides Ruby's date and time.
8
+
9
+ What could **possibly** go wrong?
10
+
11
+ <img src=https://i.imgur.com/ZqAy9co.jpg height="75%" width="75%" title="Safety not guaranteed"/>
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'amok_time'
19
+ ```
20
+
21
+ And then execute:
22
+
23
+ $ bundle
24
+
25
+ Or install it yourself as:
26
+
27
+ $ gem install amok_time
28
+
29
+ ## Usage
30
+
31
+ ```ruby
32
+ # In a Rails initializer or equivalent file
33
+ AmokTime.enable!
34
+ ```
35
+
36
+ ### Rails
37
+
38
+ ```ruby
39
+ # In the same initializer as above
40
+ require 'amok_time/rack_middleware'
41
+
42
+ Rails.application.config.middleware.use AmokTime::RackMiddleware
43
+ ```
44
+
45
+ ### Sinatra
46
+
47
+ ```ruby
48
+ require 'amok_time/rack_middleware'
49
+
50
+ class MyApp < Sinatra::Base
51
+ use AmokTime::RackMiddleware
52
+ end
53
+ ```
54
+
55
+ ### Sidekiq
56
+
57
+ ```ruby
58
+ # In your sidekiq initializer
59
+
60
+ require 'amok_time/sidekiq_middlware'
61
+
62
+ Sidekiq.configure_client do |config|
63
+ config.client_middleware do |chain|
64
+ chain.add AmokTime::SidekiqMiddleware::Client
65
+ end
66
+ end
67
+
68
+ Sidekiq.configure_server do |config|
69
+ config.server_middleware do |chain|
70
+ chain.add AmokTime::SidekiqMiddleware::Server
71
+ end
72
+
73
+ # Need this again for any downstream jobs enqueued
74
+ config.client_middleware do |chain|
75
+ chain.add AmokTime::SidekiqMiddleware::Client
76
+ end
77
+ end
78
+ ```
79
+
80
+ ### API
81
+ For a given HTTP request to a service with Amok Time enabled, you can add the following header
82
+
83
+ ```ruby
84
+ req.headers['X-Amok-Time'] = timestamp.to_s # any Date or Time object will work
85
+ ```
86
+
87
+ The app will then behave as if the request was sent at that date.
88
+
89
+ ## Development
90
+
91
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
92
+
93
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
94
+
95
+ ## Contributing
96
+
97
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/amok_time.
98
+
99
+
100
+ ## License
101
+
102
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
103
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'amok_time/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "amok_time"
8
+ spec.version = AmokTime::VERSION
9
+ spec.authors = ["Matt Larraz"]
10
+ spec.email = ["matt.larraz@gmail.com"]
11
+
12
+ spec.summary = %q{Utility for mocking time across service boundaries on a per-request basis}
13
+ spec.description = %q{Amok Time is designed for end-to-end testing distributed systems where certain behavior is time-based.
14
+ It works by allowing any client to send a request header that overrides Ruby's date and time}
15
+ spec.homepage = "https://github.com/mlarraz/amok_time"
16
+ spec.license = "MIT"
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.11"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "amok_time"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,16 @@
1
+ require "amok_time/mixins"
2
+ require "amok_time/version"
3
+
4
+ module AmokTime
5
+ class << self
6
+ attr_accesor :enabled
7
+
8
+ def enable!
9
+ self.enabled = true
10
+
11
+ Date.singleton_class.prepend AmokTime::Mixins::Date
12
+ Time.singleton_class.prepend AmokTime::Mixins::Time
13
+ DateTime.singleton_class.prepend AmokTime::Mixins::DateTime
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,72 @@
1
+ module AmokTime
2
+ module Mixins
3
+ module Date
4
+ def today
5
+ if Thread.current[:amok_time].present?
6
+ Thread.current[:amok_time].to_date
7
+ else
8
+ super
9
+ end
10
+ end
11
+
12
+ def parse(*args)
13
+ str = args.first
14
+
15
+ if str && ::Date::DAYNAMES[str.capitalize]
16
+ offset = ::Date::DAYNAMES.index(str.capitalize) - ::Date.today.wday
17
+
18
+ ::Date.today + offset
19
+ else
20
+ super
21
+ end
22
+ end
23
+
24
+ # TODO: Figure out if this is necessary
25
+ # def strptime(str = '-4712-01-01', fmt = '%F', start = ::Date::ITALY)
26
+ # ::Time.strptime(str, fmt).to_date
27
+ # end
28
+ end
29
+
30
+ module DateTime
31
+ def now
32
+ if Thread.current[:amok_time].present?
33
+ Thread.current[:amok_time].to_datetime
34
+ else
35
+ super
36
+ end
37
+ end
38
+
39
+ def parse(*args)
40
+ str = args.first
41
+
42
+ if str && ::Date::DAYNAMES[str.capitalize]
43
+ offset = ::Date::DAYNAMES.index(str.capitalize) - ::DateTime.now.wday
44
+
45
+ parsed_weekday = (::DateTime.now + offset)
46
+
47
+ ::DateTime.new(parsed_weekday.year, parsed_weekday.month, parsed_weekday.day, 0, 0, 0, 0)
48
+ else
49
+ super
50
+ end
51
+ end
52
+ end
53
+
54
+ module Time
55
+ def now
56
+ if Thread.current[:amok_time].present?
57
+ Thread.current[:amok_time].to_time
58
+ else
59
+ super
60
+ end
61
+ end
62
+
63
+ def new(*args)
64
+ if args.size <= 0
65
+ ::Time.now
66
+ else
67
+ super
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,17 @@
1
+ module AmokTime
2
+ class RackMiddleware
3
+ def initialize(app)
4
+ @app = app
5
+ end
6
+
7
+ def call(env)
8
+ status, headers, response = @app.call(env)
9
+
10
+ if AmokTime.enabled
11
+ Thread.current[:amok_time] = headers['X-Amok-Time']
12
+ end
13
+
14
+ [status, headers, response]
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,9 @@
1
+ module AmokTime
2
+ module RailsFilter
3
+ def self.before(controller)
4
+ if AmokTime.enabled
5
+ Thread.current[:amok_time] = controller.request.headers['X-Amok-Time']
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,20 @@
1
+ module AmokTime
2
+ module SidekiqMiddleware
3
+ # Get the mocked time and store it in the message
4
+ # to be sent to Sidekiq.
5
+ class Client
6
+ def call(worker_class, msg, queue, redis_pool)
7
+ msg['amok_time'] ||= Thread.current[:amok_time]
8
+ yield
9
+ end
10
+ end
11
+
12
+ # Pull the mocked time out and set the current thread to use it.
13
+ class Server
14
+ def call(worker, msg, queue)
15
+ Thread.current[:amok_time] = msg['amok_time']
16
+ yield
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module AmokTime
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: amok_time
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Matt Larraz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-04-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: |-
56
+ Amok Time is designed for end-to-end testing distributed systems where certain behavior is time-based.
57
+ It works by allowing any client to send a request header that overrides Ruby's date and time
58
+ email:
59
+ - matt.larraz@gmail.com
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - ".rspec"
66
+ - ".travis.yml"
67
+ - Gemfile
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - amok_time.gemspec
72
+ - bin/console
73
+ - bin/setup
74
+ - lib/amok_time.rb
75
+ - lib/amok_time/mixins.rb
76
+ - lib/amok_time/rack_middleware.rb
77
+ - lib/amok_time/rails_filter.rb
78
+ - lib/amok_time/sidekiq_middleware.rb
79
+ - lib/amok_time/version.rb
80
+ homepage: https://github.com/mlarraz/amok_time
81
+ licenses:
82
+ - MIT
83
+ metadata: {}
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 2.5.1
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: Utility for mocking time across service boundaries on a per-request basis
104
+ test_files: []
105
+ has_rdoc: