pg_faker 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ OTE1YzcwZTJkNjllY2M3OTEyNjg1ZWRjNWRlYzYxYTQ5NDYwMWJlYQ==
5
+ data.tar.gz: !binary |-
6
+ ZGYxNTJmNDVhMGQwMzFlMzQyNjBhODY3ZDA5ZjlkMWM4MmVlYjJkNQ==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ NjIwNjZmYjQ1M2Q3NTgwOTA1MDMxNzdiZmQ0NzVhOTk0ODhhMTlhN2Y0YmEx
10
+ OTkxYjhkNTdjMWQ3YmVmNWE1NWIyM2FlMjI3MTBkZjQ5YjEwZmM1MTRhMzlm
11
+ MmQ1ZThmYTQxMjVhZTE3MjVjZThhYWRiMThjOWM4ODMwODBkNTg=
12
+ data.tar.gz: !binary |-
13
+ ZjljMjNiZmZjZmZmNGU5ZWFjNDYyZTlmNGQyY2U4MGNmZTExZTgwNTdhZGEw
14
+ YWIyNmNmNGI0ODdiZDBhOGE5MDI5ZGQ1ODhlNTA2NThhYWZkZWNkZjY3YzM1
15
+ MWYzZGJlN2ZkZGJlY2RjN2MzY2VjNjZmY2M2ZTc2MGRjNmM1Y2M=
data/README.md ADDED
@@ -0,0 +1,61 @@
1
+ ## pg_faker
2
+
3
+ Used to simulate PostgreSQL database delays and errors. Works with the 'pg' gem.
4
+
5
+ Wraps `PG::Connection` for one, more, or all class methods.
6
+
7
+ Do not use this gem in production or with data, etc. that you cannot restore. Use at your own risk.
8
+
9
+ ### Setup
10
+
11
+ Either:
12
+
13
+ gem install 'pg_faker'
14
+
15
+ Or, if using Bundler, put this in your Gemfile:
16
+
17
+ gem 'pg_faker'
18
+
19
+ Then run:
20
+
21
+ bundle install
22
+
23
+ ### Usage
24
+
25
+ The following is an example of its use in Rails console with an existing model we'll call MyModel, but this gem is not dependent on Rails or ActiveRecord:
26
+
27
+ rails c
28
+
29
+ Then:
30
+
31
+ PG::Connection.fake_delays = true
32
+ PG::Connection.alter_methods
33
+ MyModel.last # => waits 5 seconds for each PG::Connection method call
34
+ PG::Connection.fake_delays = false
35
+ PG::Connection.fake_errors = true
36
+ MyModel.last # => raises error
37
+ PG::Connection.fake_delays = true
38
+ PG::Connection.fake_errors = true
39
+ PG::Connection.fake_delay_seconds = 2
40
+ MyModel.last # => delays 2 seconds and raises error
41
+ PG::Connection.unalter_methods
42
+ MyModel.last => pg behaves normally
43
+ PG::Connection.alter_method :async_exec # => only affect this method
44
+ PG::Connection.start_intermittent_delay_and_error # => sets fake_strategy which is called to randomize settings
45
+ MyModel.last # => may fake delay or raise error
46
+ PG::Connection.stop_intermittent_delay_and_error # => now it will behave consistently (sets fake_strategy to nil)
47
+ # this is equivalent
48
+ PG::Connection.fake_strategy = ->(m, *args) do
49
+ PG::Connection.fake_delays = [true, false].sample
50
+ PG::Connection.fake_errors = [true, false].sample
51
+ puts "#{m}(#{args.inspect}) fake_delays=#{PG::Connection.fake_delays} fake_errors=#{PG::Connection.fake_errors}"
52
+ end
53
+ MyModel.last # => may still fake delay or raise error
54
+ PG::Connection.fake_strategy = nil # => now it will behave consistently, without the puts
55
+ PG::Connection.unalter_methods => pg behaves normally
56
+
57
+ ### License
58
+
59
+ Copyright (c) 2013 Gary S. Weaver, released under the [MIT license][lic].
60
+
61
+ [lic]: http://github.com/garysweaver/pg_faker/blob/master/LICENSE
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new(:test) do |t|
4
+ t.libs << 'test'
5
+ t.test_files = FileList['test/**/*_test.rb']
6
+ t.verbose = true
7
+ end
8
+
9
+ desc "Run tests"
10
+ task :default => :test
@@ -0,0 +1,3 @@
1
+ module PgFaker
2
+ VERSION = '0.0.1'
3
+ end
data/lib/pg_faker.rb ADDED
@@ -0,0 +1,49 @@
1
+ require 'pg_faker/version'
2
+
3
+ module PG
4
+ class Connection
5
+ class << self
6
+ attr_accessor :fake_delays, :fake_errors, :fake_strategy, :fake_delay_seconds
7
+
8
+ def start_intermittent_delay_and_error
9
+ @fake_strategy = lambda {|m, *args| PG::Connection.fake_delays = [true, false].sample; PG::Connection.fake_errors = [true, false].sample; puts "#{m}(#{args.inspect}) fake_delays=#{PG::Connection.fake_delays} fake_errors=#{PG::Connection.fake_errors}" }
10
+ end
11
+
12
+ def stop_intermittent_delay_and_error
13
+ @fake_strategy = nil
14
+ end
15
+
16
+ def alter_methods(*ms)
17
+ PG::Connection.class_eval do
18
+ ((ms if ms.size > 0) || instance_methods(false)).each do |m|
19
+ alias_method "orig_#{m}".to_sym, m
20
+ eval <<-eos
21
+ def #{m}(*args)
22
+ PG::Connection.fake_strategy.call(#{m.to_sym.inspect}, *args) if PG::Connection.fake_strategy.is_a?(Proc)
23
+ sleep(PG::Connection.fake_delay_seconds) if PG::Connection.fake_delays
24
+ puts "intercepted: PG::Connection.#{m} \#{args.inspect}"
25
+ raise PG::Error.new("fake error raised by pg_faker") if PG::Connection.fake_errors
26
+ orig_#{m} *args
27
+ end
28
+ eos
29
+ end
30
+ end
31
+ end
32
+ alias_method :alter_method, :alter_methods
33
+
34
+ def unalter_methods(*ms)
35
+ PG::Connection.class_eval do
36
+ meths = instance_methods(false)
37
+ ((ms if ms.size > 0) || meths).each do |m|
38
+ orig_m = "orig_#{m}".to_sym
39
+ alias_method m, orig_m if meths.include?(orig_m)
40
+ remove_method orig_m
41
+ end
42
+ end
43
+ end
44
+ alias_method :unalter_method, :unalter_methods
45
+ end
46
+ end
47
+ end
48
+
49
+ PG::Connection.fake_delay_seconds = 5
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pg_faker
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Gary S. Weaver
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-06-11 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Allows you to specify one or all PG::Connection class methods to cause
14
+ configurable delays and raise errors consistently or intermittently.
15
+ email:
16
+ - garysweaver@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/pg_faker/version.rb
22
+ - lib/pg_faker.rb
23
+ - Rakefile
24
+ - README.md
25
+ homepage: https://github.com/garysweaver/pg_faker
26
+ licenses:
27
+ - MIT
28
+ metadata: {}
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 2.0.3
46
+ signing_key:
47
+ specification_version: 4
48
+ summary: Fake postgres delays and errors for testing
49
+ test_files: []