dynport_tools 0.2.15 → 0.2.16

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.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.15
1
+ 0.2.16
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{dynport_tools}
8
- s.version = "0.2.15"
8
+ s.version = "0.2.16"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Tobias Schwab"]
@@ -46,6 +46,7 @@ Gem::Specification.new do |s|
46
46
  "spec/dynport_tools/ascii_table_spec.rb",
47
47
  "spec/dynport_tools/deep_merger_spec.rb",
48
48
  "spec/dynport_tools/differ_spec.rb",
49
+ "spec/dynport_tools/embedded_redis_spec.rb",
49
50
  "spec/dynport_tools/eta_spec.rb",
50
51
  "spec/dynport_tools/have_attributes_spec.rb",
51
52
  "spec/dynport_tools/jenkins_spec.rb",
@@ -1,11 +1,12 @@
1
1
  require "redis"
2
2
  require "singleton"
3
3
  require "logger"
4
+ require "fileutils"
4
5
 
5
6
  class EmbeddedRedis
6
7
  include Singleton
7
8
 
8
- attr_accessor :started, :base_path
9
+ attr_accessor :started, :base_path, :killed
9
10
  attr_writer :logger
10
11
 
11
12
  def initialize(options = {})
@@ -22,27 +23,34 @@ class EmbeddedRedis
22
23
  end
23
24
 
24
25
  def pid
25
- File.read(pid_path).strip.presence if File.exists?(pid_path)
26
+ if File.exists?(pid_path)
27
+ pid = File.read(pid_path).strip
28
+ pid.length > 0 ? pid : nil
29
+ end
26
30
  end
27
31
 
28
32
  def running?
29
- !!(pid && `ps -p #{pid} | tail -n +2`.present?)
33
+ !!(pid && IO.popen("ps -p #{pid} | grep redis-server").count > 0)
30
34
  end
31
35
 
32
36
  def start
33
37
  if !running?
34
- [socket_path, pid_path].each { |path| FileUtils.mkdir_p(File.dirname(path)) }
35
- system(%(echo "#{config}" | redis-server -))
36
- sleep 0.1
37
- self.started = true
38
- log "started redis with pid #{pid}"
39
- at_exit do
40
- kill
41
- end
42
- connection
38
+ do_start!
43
39
  else
44
40
  log "already running with pid #{pid}"
45
41
  end
42
+ connection
43
+ end
44
+
45
+ def do_start!
46
+ [socket_path, pid_path].each { |path| FileUtils.mkdir_p(File.dirname(path)) }
47
+ system(%(echo "#{config}" | redis-server -))
48
+ sleep 0.1
49
+ self.started = true
50
+ log "started redis with pid #{pid}"
51
+ at_exit do
52
+ kill
53
+ end
46
54
  end
47
55
 
48
56
  def started?
@@ -50,7 +58,9 @@ class EmbeddedRedis
50
58
  end
51
59
 
52
60
  def connection
53
- start if !started?
61
+ if !started?
62
+ start
63
+ end
54
64
  @connection ||= Redis.new(:path => socket_path)
55
65
  end
56
66
 
@@ -62,11 +72,17 @@ class EmbeddedRedis
62
72
  @logger ||= Logger.new($stdout)
63
73
  end
64
74
 
75
+ def killed?
76
+ !!killed
77
+ end
78
+
65
79
  def kill
66
80
  log "killing redis"
67
- if pid
81
+ if !killed? && pid
82
+ log "killing #{pid}"
68
83
  system(%(kill #{pid}))
69
84
  FileUtils.rm_f(socket_path)
85
+ self.killed = true
70
86
  end
71
87
  end
72
88
 
data/lib/dynport_tools.rb CHANGED
@@ -8,4 +8,4 @@ require "cgi"
8
8
  require "term/ansicolor"
9
9
  require "diff/lcs"
10
10
 
11
- %w(deep_merger differ jenkins redis_dumper xml_file have_attributes redis_q eta ascii_table).map { |m| require "dynport_tools/#{m}" }
11
+ %w(deep_merger differ jenkins redis_dumper xml_file have_attributes redis_q eta ascii_table embedded_redis).map { |m| require "dynport_tools/#{m}" }
@@ -0,0 +1,136 @@
1
+ require 'spec_helper'
2
+
3
+ describe EmbeddedRedis do
4
+ let(:er) { EmbeddedRedis.instance }
5
+
6
+ before(:each) do
7
+ er.logger = Logger.new("/dev/null")
8
+ er.stub(:system)
9
+ er.stub!(:sleep)
10
+ er.stub!(:kill)
11
+ FileUtils.stub!(:mkdir_p)
12
+ end
13
+
14
+ after(:each) do
15
+ EmbeddedRedis.instance.instance_variable_set("@connection", nil)
16
+ end
17
+
18
+ describe "#pid" do
19
+ it "returns nil when file not found" do
20
+ pid_path = root.join("tmp/some_weird_pid_path.pid")
21
+ er.stub!(:pid_path).and_return pid_path
22
+ er.pid.should be_nil
23
+ end
24
+
25
+ it "returns nil when blank" do
26
+ pid_path = root.join("tmp/redis_test.pid")
27
+ File.open(pid_path, "w") { |f| f.puts " " }
28
+ er.stub!(:pid_path).and_return pid_path
29
+ er.pid.should be_nil
30
+ end
31
+
32
+ it "returns the correct pid when present" do
33
+ pid_path = root.join("tmp/redis_test.pid")
34
+ File.open(pid_path, "w") { |f| f.puts "123" }
35
+ er.stub!(:pid_path).and_return pid_path
36
+ er.pid.should == "123"
37
+ end
38
+ end
39
+
40
+ describe "#running?" do
41
+ it "returns false when pid is nil" do
42
+ er.should_receive(:pid).and_return nil
43
+ er.running?.should be_false
44
+ end
45
+
46
+ it "returns false when not in pid list" do
47
+ er.stub!(:pid).and_return "1212"
48
+ IO.should_receive(:popen).with("ps -p 1212 | grep redis-server").and_return([])
49
+ er.running?.should be_false
50
+ end
51
+
52
+ it "returns true lines > 0" do
53
+ er.stub!(:pid).and_return "1212"
54
+ IO.stub(:popen).with("ps -p 1212 | grep redis-server").and_return(["line"])
55
+ er.running?.should be_true
56
+ end
57
+ end
58
+
59
+ describe "#start" do
60
+ before(:each) do
61
+ er.stub!(:do_start!)
62
+ er.stub!(:connection).and_return double("connection")
63
+ end
64
+
65
+ it "calls do_start when running? is false" do
66
+ er.should_receive(:running?).and_return false
67
+ er.should_receive(:do_start!).and_return true
68
+ er.start
69
+ end
70
+
71
+ it "does not call do_start! of running" do
72
+ er.should_receive(:running?).and_return true
73
+ er.should_not_receive(:do_start!).and_return true
74
+ er.start
75
+ end
76
+ end
77
+
78
+ describe "do_start" do
79
+ it "sets started to true" do
80
+ er.do_start!
81
+ er.started.should be_true
82
+ end
83
+
84
+ it "creates dir of pid and socket paths" do
85
+ er.stub(:base_path).and_return "/custom_base"
86
+ FileUtils.should_receive(:mkdir_p).with("/custom_base/pids")
87
+ FileUtils.should_receive(:mkdir_p).with("/custom_base/sockets")
88
+ er.do_start!
89
+ end
90
+
91
+ it "starts redis" do
92
+ er.stub(:config).and_return "some config"
93
+ er.should_receive(:system).with(/echo "some config" \| redis-server -/)
94
+ er.do_start!
95
+ end
96
+
97
+ it "registers an on_exit hook" do
98
+ er.should_receive(:at_exit)
99
+ er.do_start!
100
+ end
101
+ end
102
+
103
+ describe "#started?" do
104
+ it "returns true when started is true" do
105
+ er.started = true
106
+ er.started?.should == true
107
+ end
108
+
109
+ it "returns false when started is nil" do
110
+ er.started = nil
111
+ er.started?.should == false
112
+ end
113
+ end
114
+
115
+ describe "#connection" do
116
+ it "calls start when not started" do
117
+ er.should_receive(:started?).and_return false
118
+ er.should_receive(:start)
119
+ er.connection
120
+ end
121
+
122
+ it "does not call start when already started" do
123
+ er.should_receive(:started?).and_return true
124
+ er.should_not_receive(:start)
125
+ er.connection
126
+ end
127
+
128
+ it "creates a new redis connection" do
129
+ er.instance_variable_set("@connection", nil)
130
+ er.stub(:socket_path).and_return("/some/socket/path")
131
+ er.stub(:started?).and_return true
132
+ redis = er.connection
133
+ redis.should be_kind_of(Redis)
134
+ end
135
+ end
136
+ end
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  describe DynportTools::RedisQ do
4
4
  let(:key) { "test/redis_queue" }
5
- let(:redis) { Redis.new(:path => root.join("tmp/redis.socket")) }
5
+ let(:redis) { EmbeddedRedis.instance.connection }
6
6
  let(:queue) do
7
7
  q = DynportTools::RedisQ.new(key)
8
8
  q.redis = redis
data/spec/spec_helper.rb CHANGED
@@ -21,31 +21,8 @@ RSpec.configure do |config|
21
21
  end
22
22
  end
23
23
 
24
+ EmbeddedRedis.instance.logger = Logger.new("/dev/null")
25
+
24
26
  def root
25
27
  Pathname.new(File.expand_path("../../", __FILE__))
26
- end
27
-
28
- def redis_pidfile
29
- root.join("tmp/redis.pid")
30
- end
31
-
32
- def redis_socket
33
- root.join("tmp/redis.socket")
34
- end
35
-
36
- redis_config = [
37
- "port 0",
38
- "unixsocket #{redis_socket}",
39
- "pidfile #{redis_pidfile}",
40
- "daemonize yes"
41
- ].join("\n")
42
-
43
- FileUtils.mkdir_p(File.dirname(redis_pidfile))
44
-
45
- system("echo '#{redis_config}' | redis-server -")
46
-
47
- at_exit do
48
- pid = File.read(redis_pidfile).strip
49
- system("kill #{pid}")
50
- FileUtils.rm_f(redis_socket)
51
28
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dynport_tools
3
3
  version: !ruby/object:Gem::Version
4
- hash: 9
4
+ hash: 55
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 15
10
- version: 0.2.15
9
+ - 16
10
+ version: 0.2.16
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tobias Schwab
@@ -261,6 +261,7 @@ files:
261
261
  - spec/dynport_tools/ascii_table_spec.rb
262
262
  - spec/dynport_tools/deep_merger_spec.rb
263
263
  - spec/dynport_tools/differ_spec.rb
264
+ - spec/dynport_tools/embedded_redis_spec.rb
264
265
  - spec/dynport_tools/eta_spec.rb
265
266
  - spec/dynport_tools/have_attributes_spec.rb
266
267
  - spec/dynport_tools/jenkins_spec.rb