gpeng-throttler 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.1.0
data/bin/throttler CHANGED
@@ -12,14 +12,17 @@ require 'throttler'
12
12
 
13
13
  def help_text(exit_code = 0)
14
14
  #TODO add help text
15
- "Need to add help text"
15
+ puts "Need to add help text"
16
16
  end
17
17
 
18
- if ARGV.size.zero? || ['-h', '--help', 'help']
18
+ if ARGV.size.zero? || ['-h', '--help', 'help'].include?(ARGV.first)
19
19
  help_text
20
20
  else
21
21
  case ARGV[0]
22
- when 'test'
23
- puts Throttler.test
22
+ when 'throttle'
23
+ ARGV.shift
24
+ Throttler.throttle(*ARGV)
25
+ when 'restore'
26
+ Throttler.restore
24
27
  end
25
28
  end
data/lib/throttler.rb CHANGED
@@ -2,8 +2,28 @@ $: << File.dirname(__FILE__)
2
2
 
3
3
  class Throttler
4
4
  class << self
5
- def test
6
- "The test is working..."
5
+ def throttle(kb_speed)
6
+ #TODO check the argument is a positive number
7
+ #TODO check that there are no exisiting ipfw rules that would be deleted by the restore command
8
+ #TODO delete pipe 1 in case this is called more than once without restoring
9
+
10
+ exec_command "sudo ipfw add 1 pipe 1 ip from any to any out src-port 80"
11
+ exec_command "sudo ipfw add 2 pipe 1 ip from any to any in src-port 80"
12
+ exec_command "sudo ipfw pipe 1 config bw #{kb_speed}Kbit/s"
13
+ exec_command "sudo ipfw pipe 2 config bw #{kb_speed}Kbit/s"
14
+ puts "Connection speed set to #{kb_speed}Kbit/s on port 80"
15
+ end
16
+
17
+ def restore
18
+ #TODO add error message if this is called when no throttling is in place
19
+ exec_command "sudo ipfw delete 1"
20
+ exec_command "sudo ipfw delete 2"
21
+ puts "Connection speed restored"
22
+ end
23
+
24
+ private
25
+ def exec_command(command)
26
+ `#{command}`
7
27
  end
8
28
  end
9
29
  end
@@ -1,7 +1,47 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  describe "Throttler" do
4
- it "fails" do
5
- fail "hey buddy, you should probably rename this file and start specing for real"
4
+ before(:each) do
5
+ Throttler.stub!(:exec_command)
6
+ Throttler.stub!(:puts)
6
7
  end
8
+
9
+ describe "throttle" do
10
+
11
+ it "should add the pipes" do
12
+ Throttler.should_receive(:exec_command).with("sudo ipfw add 1 pipe 1 ip from any to any out src-port 80")
13
+ Throttler.should_receive(:exec_command).with("sudo ipfw add 2 pipe 1 ip from any to any in src-port 80")
14
+ Throttler.throttle 10
15
+ end
16
+
17
+ it "should set the ipfw speed to 10Kbit/s" do
18
+ Throttler.should_receive(:exec_command).with("sudo ipfw pipe 1 config bw 10Kbit/s")
19
+ Throttler.should_receive(:exec_command).with("sudo ipfw pipe 2 config bw 10Kbit/s")
20
+ Throttler.throttle 10
21
+ end
22
+
23
+
24
+ it "should return the message 'Connection speed set to *Kbit/s on port 80'" do
25
+ Throttler.should_receive(:puts).with("Connection speed set to 10Kbit/s on port 80")
26
+ Throttler.throttle 10
27
+ end
28
+ end
29
+
30
+ describe "restore" do
31
+ it "should delete the ipfw with id 1" do
32
+ Throttler.should_receive(:exec_command).with("sudo ipfw delete 1")
33
+ Throttler.restore
34
+ end
35
+
36
+ it "should delete the ipfw with id 2" do
37
+ Throttler.should_receive(:exec_command).with("sudo ipfw delete 2")
38
+ Throttler.restore
39
+ end
40
+
41
+ it "should return the message 'Connection speed restored" do
42
+ Throttler.should_receive(:puts).with("Connection speed restored")
43
+ Throttler.restore
44
+ end
45
+ end
46
+
7
47
  end
Binary file
data/throttler.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{throttler}
8
- s.version = "0.0.3"
8
+ s.version = "0.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Graham Pengelly"]
@@ -29,6 +29,7 @@ Gem::Specification.new do |s|
29
29
  "lib/throttler.rb",
30
30
  "spec/spec_helper.rb",
31
31
  "spec/throttler_spec.rb",
32
+ "throttler-0.0.3.gem",
32
33
  "throttler.gemspec"
33
34
  ]
34
35
  s.homepage = %q{http://github.com/gpeng/throttler}
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gpeng-throttler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Graham Pengelly
@@ -42,6 +42,7 @@ files:
42
42
  - lib/throttler.rb
43
43
  - spec/spec_helper.rb
44
44
  - spec/throttler_spec.rb
45
+ - throttler-0.0.3.gem
45
46
  - throttler.gemspec
46
47
  has_rdoc: false
47
48
  homepage: http://github.com/gpeng/throttler