yapplabs-em-hiredis 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ $:.unshift File.expand_path(File.dirname(__FILE__) + "/../lib")
2
+ require 'em-hiredis'
3
+ require 'rspec'
4
+ require 'em-spec/rspec'
5
+
6
+ require 'support/connection_helper'
7
+ require 'support/redis_mock'
8
+ require 'stringio'
9
+
10
+ RSpec.configure do |config|
11
+ config.include ConnectionHelper
12
+ config.include EventMachine::SpecHelper
13
+ config.include RedisMock::Helper
14
+ end
15
+
16
+ # This speeds the tests up a bit
17
+ EM::Hiredis.reconnect_timeout = 0.01
@@ -0,0 +1,11 @@
1
+ module ConnectionHelper
2
+ # Use db 9 for tests to avoid flushing the main db
3
+ # It would be nice if there was a standard db number for testing...
4
+ def connect(timeout = 1, url = "redis://localhost:6379/9", &blk)
5
+ em(timeout) do
6
+ redis = EventMachine::Hiredis.connect(url)
7
+ redis.flushdb
8
+ blk.call(redis)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,83 @@
1
+ module IRedisMock
2
+ def self.start(replies = {})
3
+ @sig = EventMachine::start_server("127.0.0.1", 6381, Connection)
4
+ @received = []
5
+ @replies = replies
6
+ @paused = false
7
+ end
8
+
9
+ def self.stop
10
+ EventMachine::stop_server(@sig)
11
+ end
12
+
13
+ def self.received
14
+ @received ||= []
15
+ end
16
+
17
+ def self.pause
18
+ @paused = true
19
+ end
20
+ def self.unpause
21
+ @paused = false
22
+ end
23
+
24
+ def self.paused
25
+ @paused
26
+ end
27
+
28
+ def self.replies
29
+ @replies
30
+ end
31
+
32
+ class Connection < EventMachine::Connection
33
+ def initialize
34
+ @data = ""
35
+ @parts = []
36
+ end
37
+
38
+ def unbind
39
+ IRedisMock.received << 'disconnect'
40
+ end
41
+
42
+ def receive_data(data)
43
+ @data << data
44
+
45
+ while (idx = @data.index("\r\n"))
46
+ @parts << @data[0..idx-1]
47
+ @data = @data[idx+2..-1]
48
+ end
49
+
50
+ while @parts.length > 0
51
+ throw "commands out of sync" unless @parts[0][0] == '*'
52
+
53
+ num_parts = @parts[0][1..-1].to_i * 2 + 1
54
+ return if @parts.length < num_parts
55
+
56
+ command_parts = @parts[0..num_parts]
57
+ @parts = @parts[num_parts..-1]
58
+
59
+ # Discard length declarations
60
+ command_line =
61
+ command_parts
62
+ .reject { |p| p[0] == '*' || p[0] == '$' }
63
+ .join ' '
64
+
65
+ if IRedisMock.replies.member?(command_line)
66
+ reply = IRedisMock.replies[command_line]
67
+ else
68
+ reply = "+OK"
69
+ end
70
+
71
+ # p "[#{command_line}] => [#{reply}]"
72
+
73
+ IRedisMock.received << command_line
74
+
75
+ if IRedisMock.paused
76
+ # puts "Paused, therefore not sending [#{reply}]"
77
+ else
78
+ send_data "#{reply}\r\n"
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,65 @@
1
+ # nabbed from redis-rb, thanks!
2
+ require "socket"
3
+
4
+ module RedisMock
5
+ def self.start(port = 6380)
6
+ server = TCPServer.new("127.0.0.1", port)
7
+
8
+ loop do
9
+ session = server.accept
10
+
11
+ while line = session.gets
12
+ parts = Array.new(line[1..-3].to_i) do
13
+ bytes = session.gets[1..-3].to_i
14
+ argument = session.read(bytes)
15
+ session.read(2) # Discard \r\n
16
+ argument
17
+ end
18
+
19
+ response = yield(*parts)
20
+
21
+ if response.nil?
22
+ session.shutdown(Socket::SHUT_RDWR)
23
+ break
24
+ else
25
+ session.write(response)
26
+ session.write("\r\n")
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ module Helper
33
+ # Forks the current process and starts a new mock Redis server on
34
+ # port 6380.
35
+ #
36
+ # The server will reply with a `+OK` to all commands, but you can
37
+ # customize it by providing a hash. For example:
38
+ #
39
+ # redis_mock(:ping => lambda { "+PONG" }) do
40
+ # assert_equal "PONG", Redis.new(:port => 6380).ping
41
+ # end
42
+ #
43
+ def redis_mock(replies = {})
44
+ begin
45
+ pid = fork do
46
+ trap("TERM") { exit }
47
+
48
+ RedisMock.start do |command, *args|
49
+ (replies[command.to_sym] || lambda { |*_| "+OK" }).call(*args)
50
+ end
51
+ end
52
+
53
+ sleep 1 # Give time for the socket to start listening.
54
+
55
+ yield
56
+
57
+ ensure
58
+ if pid
59
+ Process.kill("TERM", pid)
60
+ Process.wait(pid)
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,43 @@
1
+ # adapted from redis-rb
2
+ require 'spec_helper'
3
+
4
+ describe EventMachine::Hiredis, "URL parsing" do
5
+ it "defaults URL defaults to 127.0.0.1:6379" do
6
+ redis = EventMachine::Hiredis.setup
7
+
8
+ redis.host.should == "127.0.0.1"
9
+ redis.port.should == 6379
10
+ redis.db.should == 0
11
+ redis.password.should == nil
12
+ end
13
+
14
+ it "allows to pass in a URL" do
15
+ redis = EventMachine::Hiredis.setup "redis://:secr3t@foo.com:999/2"
16
+
17
+ redis.host.should == "foo.com"
18
+ redis.port.should == 999
19
+ redis.db.should == 2
20
+ redis.password.should == "secr3t"
21
+ end
22
+
23
+ it "does not modify the passed options" do
24
+ options = "redis://:secr3t@foo.com:999/2"
25
+
26
+ redis = EventMachine::Hiredis.setup(options)
27
+
28
+ options.should == "redis://:secr3t@foo.com:999/2"
29
+ end
30
+
31
+ it "uses REDIS_URL over default if available" do
32
+ ENV["REDIS_URL"] = "redis://:secr3t@foo.com:999/2"
33
+
34
+ redis = EventMachine::Hiredis.setup
35
+
36
+ redis.host.should == "foo.com"
37
+ redis.port.should == 999
38
+ redis.db.should == 2
39
+ redis.password.should == "secr3t"
40
+
41
+ ENV.delete("REDIS_URL")
42
+ end
43
+ end
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "em-hiredis/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "yapplabs-em-hiredis"
7
+ s.version = EventMachine::Hiredis::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Martyn Loughran", "Luke Melia"]
10
+ s.email = ["me@mloughran.com", "luke@yapp.us"]
11
+ s.homepage = "http://github.com/yapplabs/em-hiredis"
12
+ s.summary = %q{Eventmachine redis client}
13
+ s.description = %q{Eventmachine redis client using hiredis native parser}
14
+
15
+ s.add_dependency 'eventmachine', '~> 1.2'
16
+ s.add_dependency 'hiredis', '~> 0.6.0'
17
+
18
+ s.add_development_dependency 'em-spec', '~> 0.2.5'
19
+ s.add_development_dependency 'rspec', '~> 2.6.0'
20
+ s.add_development_dependency 'rake', '< 11.0'
21
+
22
+ s.files = `git ls-files`.split("\n")
23
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
24
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
25
+ s.require_paths = ["lib"]
26
+ end
metadata ADDED
@@ -0,0 +1,163 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yapplabs-em-hiredis
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0
5
+ platform: ruby
6
+ authors:
7
+ - Martyn Loughran
8
+ - Luke Melia
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2021-10-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: eventmachine
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.2'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.2'
28
+ - !ruby/object:Gem::Dependency
29
+ name: hiredis
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: 0.6.0
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: 0.6.0
42
+ - !ruby/object:Gem::Dependency
43
+ name: em-spec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: 0.2.5
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: 0.2.5
56
+ - !ruby/object:Gem::Dependency
57
+ name: rspec
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: 2.6.0
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: 2.6.0
70
+ - !ruby/object:Gem::Dependency
71
+ name: rake
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "<"
75
+ - !ruby/object:Gem::Version
76
+ version: '11.0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "<"
82
+ - !ruby/object:Gem::Version
83
+ version: '11.0'
84
+ description: Eventmachine redis client using hiredis native parser
85
+ email:
86
+ - me@mloughran.com
87
+ - luke@yapp.us
88
+ executables: []
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - ".gitignore"
93
+ - ".rspec"
94
+ - CHANGELOG.md
95
+ - Gemfile
96
+ - LICENCE
97
+ - README.md
98
+ - Rakefile
99
+ - examples/getting_started.rb
100
+ - examples/lua/sum.lua
101
+ - examples/lua_example.rb
102
+ - examples/pubsub_basics.rb
103
+ - examples/pubsub_more.rb
104
+ - examples/pubsub_raw.rb
105
+ - lib/em-hiredis.rb
106
+ - lib/em-hiredis/base_client.rb
107
+ - lib/em-hiredis/client.rb
108
+ - lib/em-hiredis/connection.rb
109
+ - lib/em-hiredis/event_emitter.rb
110
+ - lib/em-hiredis/lock.rb
111
+ - lib/em-hiredis/lock_lua/lock_acquire.lua
112
+ - lib/em-hiredis/lock_lua/lock_release.lua
113
+ - lib/em-hiredis/persistent_lock.rb
114
+ - lib/em-hiredis/pubsub_client.rb
115
+ - lib/em-hiredis/version.rb
116
+ - spec/base_client_spec.rb
117
+ - spec/connection_spec.rb
118
+ - spec/inactivity_check_spec.rb
119
+ - spec/live_redis_protocol_spec.rb
120
+ - spec/lock_spec.rb
121
+ - spec/pubsub_spec.rb
122
+ - spec/redis_commands_spec.rb
123
+ - spec/spec_helper.rb
124
+ - spec/support/connection_helper.rb
125
+ - spec/support/inprocess_redis_mock.rb
126
+ - spec/support/redis_mock.rb
127
+ - spec/url_param_spec.rb
128
+ - yapplabs-em-hiredis.gemspec
129
+ homepage: http://github.com/yapplabs/em-hiredis
130
+ licenses: []
131
+ metadata: {}
132
+ post_install_message:
133
+ rdoc_options: []
134
+ require_paths:
135
+ - lib
136
+ required_ruby_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ required_rubygems_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ requirements: []
147
+ rubygems_version: 3.0.3
148
+ signing_key:
149
+ specification_version: 4
150
+ summary: Eventmachine redis client
151
+ test_files:
152
+ - spec/base_client_spec.rb
153
+ - spec/connection_spec.rb
154
+ - spec/inactivity_check_spec.rb
155
+ - spec/live_redis_protocol_spec.rb
156
+ - spec/lock_spec.rb
157
+ - spec/pubsub_spec.rb
158
+ - spec/redis_commands_spec.rb
159
+ - spec/spec_helper.rb
160
+ - spec/support/connection_helper.rb
161
+ - spec/support/inprocess_redis_mock.rb
162
+ - spec/support/redis_mock.rb
163
+ - spec/url_param_spec.rb