schlep 0.3.1 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Guardfile +4 -4
- data/LICENSE +20 -0
- data/Rakefile +4 -4
- data/lib/schlep.rb +30 -10
- data/lib/schlep/version.rb +1 -1
- data/schlep.gemspec +4 -4
- data/spec/lib/schlep_spec.rb +181 -0
- data/spec/redis_spec.rb +49 -0
- data/spec/spec_helper.rb +28 -0
- metadata +21 -17
- data/test/schlep_test.rb +0 -191
- data/test/test_helper.rb +0 -7
data/Guardfile
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
guard :
|
2
|
-
watch(%r{^
|
3
|
-
watch(%r{^
|
4
|
-
watch('
|
1
|
+
guard 'rspec', :version => 2, :cli => "--color" do
|
2
|
+
watch(%r{^spec/.+_spec\.rb$})
|
3
|
+
watch(%r{^lib/(.+)\.rb$}) { "spec" }
|
4
|
+
watch('spec/spec_helper.rb') { "spec" }
|
5
5
|
end
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Justin Campbell
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
CHANGED
@@ -4,9 +4,9 @@ rescue LoadError
|
|
4
4
|
puts "Ruby >= 1.9 required for build tasks"
|
5
5
|
end
|
6
6
|
|
7
|
-
require '
|
7
|
+
require 'rspec/core/rake_task'
|
8
8
|
|
9
|
-
task :default => :
|
9
|
+
task :default => :spec
|
10
10
|
|
11
|
-
desc "Run
|
12
|
-
|
11
|
+
desc "Run the test suite"
|
12
|
+
RSpec::Core::RakeTask.new
|
data/lib/schlep.rb
CHANGED
@@ -7,12 +7,14 @@ require "uri"
|
|
7
7
|
module Schlep
|
8
8
|
extend self
|
9
9
|
|
10
|
-
attr_writer :
|
10
|
+
attr_writer :redis_url
|
11
11
|
|
12
12
|
def app
|
13
13
|
@app ||= ""
|
14
|
+
end
|
14
15
|
|
15
|
-
|
16
|
+
def app=(string)
|
17
|
+
@app = sanitize string
|
16
18
|
end
|
17
19
|
|
18
20
|
def configure
|
@@ -30,23 +32,31 @@ module Schlep
|
|
30
32
|
end
|
31
33
|
|
32
34
|
def event(type, message)
|
33
|
-
|
35
|
+
events type, [message]
|
34
36
|
end
|
35
37
|
|
36
38
|
def events(type, messages)
|
37
|
-
messages.map! { |
|
39
|
+
messages.map! { |message| envelope type, message }
|
38
40
|
|
39
|
-
|
40
|
-
|
41
|
-
|
41
|
+
suppress_redis_errors do
|
42
|
+
redis.pipelined do
|
43
|
+
while messages.any?
|
44
|
+
redis.rpush key, messages.pop
|
45
|
+
end
|
42
46
|
end
|
43
47
|
end
|
44
48
|
end
|
45
49
|
|
46
50
|
def hostname
|
47
|
-
@hostname ||= `hostname`
|
51
|
+
@hostname ||= sanitize `hostname`
|
52
|
+
end
|
53
|
+
|
54
|
+
def hostname=(string)
|
55
|
+
@hostname = sanitize string
|
56
|
+
end
|
48
57
|
|
49
|
-
|
58
|
+
def key
|
59
|
+
@key ||= :schlep
|
50
60
|
end
|
51
61
|
|
52
62
|
def redis
|
@@ -76,7 +86,9 @@ module Schlep
|
|
76
86
|
end
|
77
87
|
|
78
88
|
def serialize_message(message)
|
79
|
-
return message unless
|
89
|
+
return message unless
|
90
|
+
message.is_a? String and
|
91
|
+
message.match /\{.+\}/
|
80
92
|
|
81
93
|
begin
|
82
94
|
JSON.parse message
|
@@ -98,4 +110,12 @@ module Schlep
|
|
98
110
|
|
99
111
|
string
|
100
112
|
end
|
113
|
+
|
114
|
+
def suppress_redis_errors
|
115
|
+
begin
|
116
|
+
yield
|
117
|
+
rescue Errno::ECONNREFUSED => e
|
118
|
+
puts e.inspect unless ENV['RUBY_ENV'] == 'test'
|
119
|
+
end
|
120
|
+
end
|
101
121
|
end
|
data/lib/schlep/version.rb
CHANGED
data/schlep.gemspec
CHANGED
@@ -5,6 +5,7 @@ require "schlep/version"
|
|
5
5
|
Gem::Specification.new do |s|
|
6
6
|
s.name = "schlep"
|
7
7
|
s.version = Schlep::VERSION
|
8
|
+
s.license = "MIT"
|
8
9
|
s.authors = ["Justin Campbell"]
|
9
10
|
s.email = ["justin@justincampbell.me"]
|
10
11
|
s.homepage = "http://github.com/Movitas/schlep-ruby"
|
@@ -14,8 +15,7 @@ Gem::Specification.new do |s|
|
|
14
15
|
s.rubyforge_project = "schlep"
|
15
16
|
|
16
17
|
s.files = `git ls-files`.split("\n")
|
17
|
-
s.test_files = `git ls-files --
|
18
|
-
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
|
+
s.test_files = `git ls-files -- spec/*`.split("\n")
|
19
19
|
s.require_paths = ["lib"]
|
20
20
|
|
21
21
|
s.add_runtime_dependency "redis"
|
@@ -25,7 +25,7 @@ Gem::Specification.new do |s|
|
|
25
25
|
s.add_runtime_dependency "system_timer"
|
26
26
|
end
|
27
27
|
|
28
|
-
s.add_development_dependency "guard-
|
28
|
+
s.add_development_dependency "guard-rspec"
|
29
29
|
s.add_development_dependency "rake"
|
30
|
-
s.add_development_dependency "
|
30
|
+
s.add_development_dependency "rspec"
|
31
31
|
end
|
@@ -0,0 +1,181 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Schlep do
|
4
|
+
before :each do
|
5
|
+
Schlep.reset
|
6
|
+
end
|
7
|
+
|
8
|
+
after :each do
|
9
|
+
%w[REDIS_URL REDISTOGO_URL].each do |e|
|
10
|
+
ENV[e] = nil
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should be defined as a module" do
|
15
|
+
Schlep.class.should be_a Module
|
16
|
+
end
|
17
|
+
|
18
|
+
context ".configure" do
|
19
|
+
it "should be configurable with setters" do
|
20
|
+
Schlep.app = "test_app_1"
|
21
|
+
Schlep.hostname = "test_hostname_1"
|
22
|
+
Schlep.redis_url = "redis://localhost:1234"
|
23
|
+
|
24
|
+
Schlep.app.should == "test_app_1"
|
25
|
+
Schlep.hostname.should == "test_hostname_1"
|
26
|
+
Schlep.redis_url.should == "redis://localhost:1234"
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should be configurable with a block" do
|
30
|
+
Schlep.configure do |config|
|
31
|
+
config.app = "test_app_2"
|
32
|
+
config.hostname = "test_hostname_2"
|
33
|
+
config.redis_url = "redis://localhost:4321"
|
34
|
+
end
|
35
|
+
|
36
|
+
Schlep.app.should == "test_app_2"
|
37
|
+
Schlep.hostname.should == "test_hostname_2"
|
38
|
+
Schlep.redis_url.should == "redis://localhost:4321"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context ".envelope" do
|
43
|
+
it "should return valid json" do
|
44
|
+
JSON.parse(Schlep.envelope "test_type", { :one => { :two => 3 }})
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should accept types as strings" do
|
48
|
+
Schlep.envelope "test", "test"
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should accept types as symbols" do
|
52
|
+
Schlep.envelope :test, "test"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
context ".hostname" do
|
57
|
+
it "should be a string" do
|
58
|
+
Schlep.hostname.should be_instance_of String
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should not include a newline from the hostname command" do
|
62
|
+
Schlep.hostname.should_not match /\s/
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should not remove dashes, underscores, or periods" do
|
66
|
+
Schlep.hostname = "this-is_a.hostname"
|
67
|
+
|
68
|
+
Schlep.hostname.should == "this-is_a.hostname"
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
context ".redis_url" do
|
73
|
+
it "should connect locally by default" do
|
74
|
+
Schlep.redis.client.host.should == "127.0.0.1"
|
75
|
+
Schlep.redis.client.port.should == 6379
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should connect to a basic url" do
|
79
|
+
Schlep.redis_url = "redis://1.2.3.4:1234"
|
80
|
+
|
81
|
+
Schlep.redis.client.host.should == "1.2.3.4"
|
82
|
+
Schlep.redis.client.port.should == 1234
|
83
|
+
Schlep.redis.client.password.should be_nil
|
84
|
+
end
|
85
|
+
|
86
|
+
it "should connect to a url with a username and password" do
|
87
|
+
Schlep.redis_url = "redis://redis:password@1.2.3.4:1234"
|
88
|
+
|
89
|
+
Schlep.redis.client.host.should == "1.2.3.4"
|
90
|
+
Schlep.redis.client.port.should == 1234
|
91
|
+
Schlep.redis.client.password.should == "password"
|
92
|
+
end
|
93
|
+
|
94
|
+
it "should detect the url from ENV[\"REDIS_URL\"]" do
|
95
|
+
ENV["REDIS_URL"] = "redis://redis:secret@4.3.2.1:4321"
|
96
|
+
|
97
|
+
Schlep.redis.client.host.should == "4.3.2.1"
|
98
|
+
Schlep.redis.client.port.should == 4321
|
99
|
+
Schlep.redis.client.password.should == "secret"
|
100
|
+
end
|
101
|
+
|
102
|
+
it "should detect the url from ENV[\"REDISTOGO_URL\"]" do
|
103
|
+
ENV["REDISTOGO_URL"] = "redis://redis:secret@4.3.2.1:4321"
|
104
|
+
|
105
|
+
Schlep.redis.client.host.should == "4.3.2.1"
|
106
|
+
Schlep.redis.client.port.should == 4321
|
107
|
+
Schlep.redis.client.password.should == "secret"
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
context ".reset" do
|
112
|
+
it "should reset instance variables to nil" do
|
113
|
+
Schlep.configure do |config|
|
114
|
+
config.app = "test_app"
|
115
|
+
config.hostname = "test_hostname"
|
116
|
+
end
|
117
|
+
|
118
|
+
Schlep.reset
|
119
|
+
|
120
|
+
%w[app hostname redis].each do |ivar|
|
121
|
+
Schlep.instance_variable_get("@#{ivar}").should be_nil
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
context ".serialize message" do
|
127
|
+
it "should convert json to a hash" do
|
128
|
+
Schlep.serialize_message("{\"one\":{\"two\":3}}").should == ({ "one" => { "two" => 3 }})
|
129
|
+
end
|
130
|
+
|
131
|
+
it "should leave strings alone" do
|
132
|
+
Schlep.serialize_message("test string").should == "test string"
|
133
|
+
end
|
134
|
+
|
135
|
+
it "should leave arrays alone" do
|
136
|
+
Schlep.serialize_message([1,2,[3,4]]).should == [1,2,[3,4]]
|
137
|
+
end
|
138
|
+
|
139
|
+
it "should leave hashes alone" do
|
140
|
+
Schlep.serialize_message({ :one => { :two => 3 }}).should == ({ :one => { :two => 3 }})
|
141
|
+
end
|
142
|
+
|
143
|
+
it "should leave integers alone" do
|
144
|
+
Schlep.serialize_message(123).should == 123
|
145
|
+
end
|
146
|
+
|
147
|
+
it "should leave floats alone" do
|
148
|
+
Schlep.serialize_message(1.23).should == 1.23
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
context ".timestamp" do
|
153
|
+
it "should be a float" do
|
154
|
+
Schlep.timestamp.should be_a Float
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
# private
|
159
|
+
|
160
|
+
context ".sanitize" do
|
161
|
+
it "should strip whitespace" do
|
162
|
+
Schlep.send(:sanitize, "test string").should == "teststring"
|
163
|
+
end
|
164
|
+
|
165
|
+
it "should strip newlines" do
|
166
|
+
Schlep.send(:sanitize, "test\n").should == "test"
|
167
|
+
end
|
168
|
+
|
169
|
+
it "should replace special characters with a colon" do
|
170
|
+
Schlep.send(:sanitize, "a:b/c^d$$e").should == "a:b:c:d:e"
|
171
|
+
end
|
172
|
+
|
173
|
+
it "should remove special characters at the beginning or end or a string" do
|
174
|
+
Schlep.send(:sanitize, "$test$string$").should == "test:string"
|
175
|
+
end
|
176
|
+
|
177
|
+
it "should not replace periods" do
|
178
|
+
Schlep.send(:sanitize, "a.b.c.d.e").should == "a.b.c.d.e"
|
179
|
+
end
|
180
|
+
end
|
181
|
+
end
|
data/spec/redis_spec.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
REDIS_ENABLED = `which redis-server`.lines.any? && RUBY_PLATFORM != "java"
|
4
|
+
|
5
|
+
describe Schlep do
|
6
|
+
before :all do
|
7
|
+
print "("
|
8
|
+
end
|
9
|
+
|
10
|
+
after :all do
|
11
|
+
stop_redis
|
12
|
+
print ")"
|
13
|
+
end
|
14
|
+
|
15
|
+
before :each do
|
16
|
+
start_redis
|
17
|
+
Schlep.reset
|
18
|
+
Schlep.redis_url = "redis://localhost:#{REDIS_OPTIONS[:port]}"
|
19
|
+
Schlep.redis.flushall
|
20
|
+
end
|
21
|
+
|
22
|
+
context ".event" do
|
23
|
+
it "should push an event to the schlep key" do
|
24
|
+
Schlep.event :test, "test"
|
25
|
+
|
26
|
+
Schlep.redis.llen(Schlep.key).should == 1
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should suppress connection errors" do
|
30
|
+
stop_redis
|
31
|
+
|
32
|
+
Schlep.event :test, "test"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
context ".events" do
|
37
|
+
it "should push multiple events to the schlep key" do
|
38
|
+
Schlep.events :test, [1,2,3]
|
39
|
+
|
40
|
+
Schlep.redis.llen(Schlep.key).should == 3
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should suppress connection errors" do
|
44
|
+
stop_redis
|
45
|
+
|
46
|
+
Schlep.events :test, [1,2,3]
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end if REDIS_ENABLED
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
ENV['RUBY_ENV'] = 'test'
|
2
|
+
|
3
|
+
require "schlep"
|
4
|
+
|
5
|
+
REDIS_OPTIONS = {
|
6
|
+
:daemonize => :yes,
|
7
|
+
:dbfilename => "dump.rdb",
|
8
|
+
:dir => ".",
|
9
|
+
:pidfile => "redis.pid",
|
10
|
+
:port => 9736
|
11
|
+
}
|
12
|
+
|
13
|
+
def start_redis
|
14
|
+
until FileTest.exists? REDIS_OPTIONS[:pidfile]
|
15
|
+
`echo '#{REDIS_OPTIONS.map { |k, v| "#{k} #{v}" }.join('\n')}' | redis-server -`
|
16
|
+
sleep 0.01
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def stop_redis
|
21
|
+
if FileTest.exists? REDIS_OPTIONS[:pidfile]
|
22
|
+
%x{
|
23
|
+
cat #{REDIS_OPTIONS[:pidfile]} | xargs kill -QUIT
|
24
|
+
rm -f #{REDIS_OPTIONS[:dir]}/#{REDIS_OPTIONS[:dbfilename]}
|
25
|
+
rm -f #{REDIS_OPTIONS[:dir]}/#{REDIS_OPTIONS[:pidfile]}
|
26
|
+
}
|
27
|
+
end
|
28
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: schlep
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-01-
|
12
|
+
date: 2012-01-11 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: redis
|
16
|
-
requirement: &
|
16
|
+
requirement: &70158924470240 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70158924470240
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
|
-
name: guard-
|
27
|
-
requirement: &
|
26
|
+
name: guard-rspec
|
27
|
+
requirement: &70158924469800 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70158924469800
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rake
|
38
|
-
requirement: &
|
38
|
+
requirement: &70158924469380 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70158924469380
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
|
-
name:
|
49
|
-
requirement: &
|
48
|
+
name: rspec
|
49
|
+
requirement: &70158924468960 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,7 +54,7 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70158924468960
|
58
58
|
description: Ruby client for schlep. Schlep provides a simple interface for logging
|
59
59
|
and broadcasting events.
|
60
60
|
email:
|
@@ -68,15 +68,18 @@ files:
|
|
68
68
|
- .travis.yml
|
69
69
|
- Gemfile
|
70
70
|
- Guardfile
|
71
|
+
- LICENSE
|
71
72
|
- Rakefile
|
72
73
|
- Readme.md
|
73
74
|
- lib/schlep.rb
|
74
75
|
- lib/schlep/version.rb
|
75
76
|
- schlep.gemspec
|
76
|
-
-
|
77
|
-
-
|
77
|
+
- spec/lib/schlep_spec.rb
|
78
|
+
- spec/redis_spec.rb
|
79
|
+
- spec/spec_helper.rb
|
78
80
|
homepage: http://github.com/Movitas/schlep-ruby
|
79
|
-
licenses:
|
81
|
+
licenses:
|
82
|
+
- MIT
|
80
83
|
post_install_message:
|
81
84
|
rdoc_options: []
|
82
85
|
require_paths:
|
@@ -100,5 +103,6 @@ signing_key:
|
|
100
103
|
specification_version: 3
|
101
104
|
summary: Ruby client for schlep http://github.com/Movitas/schlep
|
102
105
|
test_files:
|
103
|
-
-
|
104
|
-
-
|
106
|
+
- spec/lib/schlep_spec.rb
|
107
|
+
- spec/redis_spec.rb
|
108
|
+
- spec/spec_helper.rb
|
data/test/schlep_test.rb
DELETED
@@ -1,191 +0,0 @@
|
|
1
|
-
begin
|
2
|
-
require "test_helper"
|
3
|
-
rescue LoadError
|
4
|
-
require File.join(File.dirname(__FILE__), ".", "test_helper")
|
5
|
-
end
|
6
|
-
|
7
|
-
class SchlepTest < Test::Unit::TestCase
|
8
|
-
def setup
|
9
|
-
Schlep.reset
|
10
|
-
end
|
11
|
-
|
12
|
-
def teardown
|
13
|
-
%w[REDIS_URL REDISTOGO_URL].each do |e|
|
14
|
-
ENV[e] = nil
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
context "schlep" do
|
19
|
-
should "be defined as a module" do
|
20
|
-
assert_equal Module, Schlep.class
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
context "configure" do
|
25
|
-
should "be configurable with setters" do
|
26
|
-
Schlep.app = "test_app_1"
|
27
|
-
Schlep.hostname = "test_hostname_1"
|
28
|
-
Schlep.redis_url = "redis://localhost:1234"
|
29
|
-
|
30
|
-
assert_equal "test_app_1", Schlep.app, "app"
|
31
|
-
assert_equal "test_hostname_1", Schlep.hostname, "hostname"
|
32
|
-
assert_equal "redis://localhost:1234", Schlep.redis_url, "redis_url"
|
33
|
-
end
|
34
|
-
|
35
|
-
should "be configurable with a block" do
|
36
|
-
Schlep.configure do |config|
|
37
|
-
config.app = "test_app_2"
|
38
|
-
config.hostname = "test_hostname_2"
|
39
|
-
config.redis_url = "redis://localhost:4321"
|
40
|
-
end
|
41
|
-
|
42
|
-
assert_equal "test_app_2", Schlep.app, "app"
|
43
|
-
assert_equal "test_hostname_2", Schlep.hostname, "hostname"
|
44
|
-
assert_equal "redis://localhost:4321", Schlep.redis_url, "redis_url"
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
context "envelope" do
|
49
|
-
should "return valid json" do
|
50
|
-
assert_nothing_raised(Exception) {
|
51
|
-
JSON.parse(Schlep.envelope "test_type", { :one => { :two => 3 }})
|
52
|
-
}
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
context "hostname" do
|
57
|
-
should "be a string" do
|
58
|
-
assert Schlep.hostname.is_a? String
|
59
|
-
end
|
60
|
-
|
61
|
-
should "not include a newline from the hostname command" do
|
62
|
-
assert_nil Schlep.hostname =~ /\s/
|
63
|
-
end
|
64
|
-
|
65
|
-
should "not remove dashes, underscores, or periods" do
|
66
|
-
Schlep.hostname = "this-is_a.hostname"
|
67
|
-
assert_equal "this-is_a.hostname", Schlep.hostname
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
context "redis_url" do
|
72
|
-
should "connect locally by default" do
|
73
|
-
assert_equal "127.0.0.1", Schlep.redis.client.host
|
74
|
-
assert_equal 6379, Schlep.redis.client.port
|
75
|
-
end
|
76
|
-
|
77
|
-
should "connect to a basic url" do
|
78
|
-
Schlep.redis_url = "redis://1.2.3.4:1234"
|
79
|
-
|
80
|
-
assert_equal "1.2.3.4", Schlep.redis.client.host
|
81
|
-
assert_equal 1234, Schlep.redis.client.port
|
82
|
-
assert_nil Schlep.redis.client.password
|
83
|
-
end
|
84
|
-
|
85
|
-
should "connect to a url with a username and password" do
|
86
|
-
Schlep.redis_url = "redis://redis:password@1.2.3.4:1234"
|
87
|
-
|
88
|
-
assert_equal "1.2.3.4", Schlep.redis.client.host
|
89
|
-
assert_equal 1234, Schlep.redis.client.port
|
90
|
-
assert_equal "password", Schlep.redis.client.password
|
91
|
-
end
|
92
|
-
|
93
|
-
should "detect the url from ENV[\"REDIS_URL\"]" do
|
94
|
-
ENV["REDIS_URL"] = "redis://redis:secret@4.3.2.1:4321"
|
95
|
-
|
96
|
-
assert_equal "4.3.2.1", Schlep.redis.client.host
|
97
|
-
assert_equal 4321, Schlep.redis.client.port
|
98
|
-
assert_equal "secret", Schlep.redis.client.password
|
99
|
-
end
|
100
|
-
|
101
|
-
should "detect the url from ENV[\"REDISTOGO_URL\"]" do
|
102
|
-
ENV["REDISTOGO_URL"] = "redis://redis:secret@4.3.2.1:4321"
|
103
|
-
|
104
|
-
assert_equal "4.3.2.1", Schlep.redis.client.host
|
105
|
-
assert_equal 4321, Schlep.redis.client.port
|
106
|
-
assert_equal "secret", Schlep.redis.client.password
|
107
|
-
end
|
108
|
-
end
|
109
|
-
|
110
|
-
context "reset" do
|
111
|
-
should "reset instance variables to nil" do
|
112
|
-
Schlep.configure do |config|
|
113
|
-
config.app = "test_app"
|
114
|
-
config.hostname = "test_hostname"
|
115
|
-
end
|
116
|
-
|
117
|
-
Schlep.reset
|
118
|
-
|
119
|
-
%w[app hostname redis].each do |ivar|
|
120
|
-
assert_nil Schlep.instance_variable_get("@#{ivar}"), "@#{ivar}"
|
121
|
-
end
|
122
|
-
end
|
123
|
-
end
|
124
|
-
|
125
|
-
context "serialize message" do
|
126
|
-
should "convert json to a hash" do
|
127
|
-
assert_equal ({ "one" => { "two" => 3 }}),
|
128
|
-
Schlep.serialize_message("{\"one\":{\"two\":3}}")
|
129
|
-
end
|
130
|
-
|
131
|
-
should "leave strings alone" do
|
132
|
-
assert_equal "test string",
|
133
|
-
Schlep.serialize_message("test string")
|
134
|
-
end
|
135
|
-
|
136
|
-
should "leave arrays alone" do
|
137
|
-
assert_equal [1,2,[3,4]],
|
138
|
-
Schlep.serialize_message([1,2,[3,4]])
|
139
|
-
end
|
140
|
-
|
141
|
-
should "leave hashes alone" do
|
142
|
-
assert_equal ({ :one => { :two => 3 }}),
|
143
|
-
Schlep.serialize_message({ :one => { :two => 3 }})
|
144
|
-
end
|
145
|
-
|
146
|
-
should "leave integers alone" do
|
147
|
-
assert_equal 123,
|
148
|
-
Schlep.serialize_message(123)
|
149
|
-
end
|
150
|
-
|
151
|
-
should "leave floats alone" do
|
152
|
-
assert_equal 1.23,
|
153
|
-
Schlep.serialize_message(1.23)
|
154
|
-
end
|
155
|
-
end
|
156
|
-
|
157
|
-
context "timestamp" do
|
158
|
-
should "be a float" do
|
159
|
-
assert Schlep.timestamp.is_a? Float
|
160
|
-
end
|
161
|
-
end
|
162
|
-
|
163
|
-
# private
|
164
|
-
|
165
|
-
context "sanitize" do
|
166
|
-
should "strip whitespace" do
|
167
|
-
assert_equal "teststring",
|
168
|
-
Schlep.send(:sanitize, "test string")
|
169
|
-
end
|
170
|
-
|
171
|
-
should "strip newlines" do
|
172
|
-
assert_equal "test",
|
173
|
-
Schlep.send(:sanitize, "test\n")
|
174
|
-
end
|
175
|
-
|
176
|
-
should "replace special characters with a colon" do
|
177
|
-
assert_equal "a:b:c:d:e",
|
178
|
-
Schlep.send(:sanitize, "a:b/c^d$$e")
|
179
|
-
end
|
180
|
-
|
181
|
-
should "remove special characters at the beginning or end or a string" do
|
182
|
-
assert_equal "test:string",
|
183
|
-
Schlep.send(:sanitize, "$test$string$")
|
184
|
-
end
|
185
|
-
|
186
|
-
should "not replace periods" do
|
187
|
-
assert_equal "a.b.c.d.e",
|
188
|
-
Schlep.send(:sanitize, "a.b.c.d.e")
|
189
|
-
end
|
190
|
-
end
|
191
|
-
end
|