bloom_filter 0.6.3 → 0.6.4

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.6.3
1
+ 0.6.4
data/bloom_filter.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{bloom_filter}
8
- s.version = "0.6.3"
8
+ s.version = "0.6.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Arya Asemanfar"]
12
- s.date = %q{2010-01-27}
12
+ s.date = %q{2010-02-01}
13
13
  s.default_executable = %q{bloom_filter_server}
14
14
  s.description = %q{}
15
15
  s.email = %q{misterfunnyarsal@gmail.com}
@@ -16,6 +16,7 @@ class BloomFilter
16
16
  end
17
17
 
18
18
  def add(el)
19
+ self.connect
19
20
  el = el.to_s
20
21
  @socket.write("#{[el.size + 1].pack(PACK_N)}#{Protocol::ADD}#{el}")
21
22
  timeout_or_default(false) do
@@ -25,6 +26,7 @@ class BloomFilter
25
26
  alias_method :<<, :add
26
27
 
27
28
  def include?(el)
29
+ self.connect
28
30
  el = el.to_s
29
31
  @socket.write("#{[el.size + 1].pack(PACK_N)}#{Protocol::INCLUDE}#{el}")
30
32
  timeout_or_default(true) do
@@ -33,6 +35,7 @@ class BloomFilter
33
35
  end
34
36
 
35
37
  def &(els)
38
+ self.connect
36
39
  if els.size == 1
37
40
  el = els.first
38
41
  self.include?(el) ? [el] : []
@@ -52,6 +55,7 @@ class BloomFilter
52
55
  end
53
56
 
54
57
  def dump(path, timeout = nil)
58
+ self.connect
55
59
  @socket.write("#{[path.size + 1].pack(PACK_N)}#{Protocol::DUMP}#{path}")
56
60
  timeout_or_default(false, timeout) do
57
61
  @socket.read(@socket.read(4).unpack(PACK_N).first) == Protocol::TRUE
@@ -59,6 +63,7 @@ class BloomFilter
59
63
  end
60
64
 
61
65
  def load(path, timeout = nil)
66
+ self.connect
62
67
  @socket.write("#{[path.size + 1].pack(PACK_N)}#{Protocol::LOAD}#{path}")
63
68
  timeout_or_default(false, timeout) do
64
69
  @socket.read(@socket.read(4).unpack(PACK_N).first) == Protocol::TRUE
@@ -94,10 +99,12 @@ class BloomFilter
94
99
  if ready
95
100
  block.call
96
101
  elsif @raise_on_timeout
102
+ self.disconnect
97
103
  raise BloomFilter::Timeout
98
104
  else
105
+ self.disconnect
99
106
  default
100
107
  end
101
108
  end
102
109
  end
103
- end
110
+ end
@@ -5,6 +5,7 @@ class BloomFilterClientTest < Test::Unit::TestCase
5
5
  context "with a client object" do
6
6
  setup do
7
7
  @socket = mock()
8
+ @socket.stubs(:closed?).returns(false)
8
9
  TCPSocket.expects(:new).with("localhost", 4111).returns(@socket)
9
10
  IO.stubs(:select).returns(@socket)
10
11
  @client = BloomFilter::Client.new("localhost", 4111, :timeout => 1)
@@ -60,12 +61,14 @@ class BloomFilterClientTest < Test::Unit::TestCase
60
61
 
61
62
  should "timeout" do
62
63
  @socket.expects(:write).with("#{[6].pack("N")}#{BloomFilter::Protocol::ADD}hello")
64
+ @socket.stubs(:close)
63
65
  IO.expects(:select).with([@socket], nil, nil, 1)
64
66
  @client.add("hello")
65
67
  end
66
68
 
67
69
  should "return false for add if it times out" do
68
70
  @socket.expects(:write).with("#{[6].pack("N")}#{BloomFilter::Protocol::ADD}hello")
71
+ @socket.stubs(:close)
69
72
  IO.expects(:select).with([@socket], nil, nil, 1).returns(nil)
70
73
  assert !@client.add("hello")
71
74
  end
@@ -73,6 +76,8 @@ class BloomFilterClientTest < Test::Unit::TestCase
73
76
 
74
77
  should "raise on timeout if set to" do
75
78
  socket = mock()
79
+ socket.stubs(:closed?).returns(false)
80
+ socket.stubs(:close)
76
81
  TCPSocket.expects(:new).with("localhost", 4111).returns(socket)
77
82
  IO.stubs(:select).returns(socket)
78
83
  client = BloomFilter::Client.new("localhost", 4111, :timeout => 1, :raise_on_timeout => true)
@@ -84,4 +89,38 @@ class BloomFilterClientTest < Test::Unit::TestCase
84
89
  client.add("hello")
85
90
  end
86
91
  end
92
+
93
+ should "close socket on timeout" do
94
+ socket = mock()
95
+ socket.stubs(:closed?).returns(false)
96
+ socket.expects(:close)
97
+ TCPSocket.expects(:new).with("localhost", 4111).returns(socket)
98
+ IO.stubs(:select).returns(socket)
99
+ client = BloomFilter::Client.new("localhost", 4111, :timeout => 1, :raise_on_timeout => true)
100
+
101
+
102
+ socket.expects(:write).with("#{[6].pack("N")}#{BloomFilter::Protocol::ADD}hello")
103
+ IO.expects(:select).with([socket], nil, nil, 1).returns(nil)
104
+ begin
105
+ client.add("hello")
106
+ rescue BloomFilter::Timeout
107
+ end
108
+ end
109
+
110
+ should "reconnect if not connected before making a call" do
111
+ socket = mock()
112
+
113
+ socket.expects(:closed?).returns(true).once
114
+ TCPSocket.expects(:new).with("localhost", 4111).returns(socket).twice
115
+ IO.stubs(:select).returns(socket)
116
+ client = BloomFilter::Client.new("localhost", 4111, :timeout => 1, :raise_on_timeout => true)
117
+
118
+ socket.expects(:write).with("#{[6].pack("N")}#{BloomFilter::Protocol::ADD}hello")
119
+ socket.expects(:read).with(4).returns([1].pack("N"))
120
+ socket.expects(:read).with(1).returns(BloomFilter::Protocol::TRUE)
121
+
122
+
123
+ IO.expects(:select).with([socket], nil, nil, 1).returns(socket)
124
+ client.add("hello")
125
+ end
87
126
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bloom_filter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.3
4
+ version: 0.6.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arya Asemanfar
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-27 00:00:00 -08:00
12
+ date: 2010-02-01 00:00:00 -08:00
13
13
  default_executable: bloom_filter_server
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency