File: client_spec.rb

package info (click to toggle)
ruby-riddle 2.3.1-2~deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 10,752 kB
  • sloc: sql: 25,022; php: 5,992; ruby: 4,757; sh: 59; makefile: 5
file content (292 lines) | stat: -rw-r--r-- 9,228 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# frozen_string_literal: true

require 'spec_helper'

describe Riddle::Client do
  it "should have the same keys for both commands and versions, except persist" do
    
    (Riddle::Client::Commands.keys - [:persist]).should == Riddle::Client::Versions.keys
  end
  
  it "should default to localhost as the server" do
    Riddle::Client.new.server.should == "localhost"
  end
  
  it "should default to port 9312" do
    Riddle::Client.new.port.should == 9312
  end

  it "should accept an array of servers" do
    servers = ["1.1.1.1", "2.2.2.2", "3.3.3.3"]
    client = Riddle::Client.new(servers)
    client.servers.should == servers
  end
  
  it "should translate anchor arguments correctly" do
    client = Riddle::Client.new
    client.set_anchor "latitude", 10.0, "longitude", 95.0
    client.anchor.should == {
      :latitude_attribute   => "latitude",
      :latitude             => 10.0,
      :longitude_attribute  => "longitude",
      :longitude            => 95.0
    }
  end
  
  it "should add queries to the queue" do
    client = Riddle::Client.new
    client.queue.should be_empty
    client.append_query "spec"
    client.queue.should_not be_empty
  end
  
  describe 'query contents' do
    it "should build a basic search message correctly" do
      client = Riddle::Client.new
      client.append_query "test "
      client.queue.first.should == query_contents(:simple)
    end
  
    it "should build a message with a specified index correctly" do
      client = Riddle::Client.new
      client.append_query "test ", "edition"
      client.queue.first.should == query_contents(:index)
    end
  
    it "should build a message using match mode :any correctly" do
      client = Riddle::Client.new
      client.match_mode = :any
      client.append_query "test this "
      client.queue.first.should == query_contents(:any)
    end
  
    it "should build a message using sort by correctly" do
      client = Riddle::Client.new
      client.sort_by = 'id'
      client.sort_mode = :extended
      client.append_query "testing "
      client.queue.first.should == query_contents(:sort)
    end
  
    it "should build a message using match mode :boolean correctly" do
      client = Riddle::Client.new
      client.match_mode = :boolean
      client.append_query "test "
      client.queue.first.should == query_contents(:boolean)
    end
  
    it "should build a message using match mode :phrase correctly" do
      client = Riddle::Client.new
      client.match_mode = :phrase
      client.append_query "testing this "
      client.queue.first.should == query_contents(:phrase)
    end
  
    it "should build a message with a filter correctly" do
      client = Riddle::Client.new
      client.filters << Riddle::Client::Filter.new("id", [10, 100, 1000])
      client.append_query "test "
      client.queue.first.should == query_contents(:filter)
    end
  
    it "should build a message with group values correctly" do
      client = Riddle::Client.new
      client.group_by       = "id"
      client.group_function = :attr
      client.group_clause   = "id"
      client.append_query "test "
      client.queue.first.should == query_contents(:group)
    end
  
    it "should build a message with group distinct value correctly" do
      client = Riddle::Client.new
      client.group_distinct = "id"
      client.append_query "test "
      client.queue.first.should == query_contents(:distinct)
    end
  
    it "should build a message with weights correctly" do
      client = Riddle::Client.new
      client.weights = [100, 1]
      client.append_query "test "
      client.queue.first.should == query_contents(:weights)
    end
  
    it "should build a message with an anchor correctly" do
      client = Riddle::Client.new
      client.set_anchor "latitude", 10.0, "longitude", 95.0
      client.append_query "test "
      client.queue.first.should == query_contents(:anchor)
    end
  
    it "should build a message with index weights correctly" do
      client = Riddle::Client.new
      client.index_weights = {"people" => 101}
      client.append_query "test "
      client.queue.first.should == query_contents(:index_weights)
    end
  
    it "should build a message with field weights correctly" do
      client = Riddle::Client.new
      client.field_weights = {"city" => 101}
      client.append_query "test "
      client.queue.first.should == query_contents(:field_weights)
    end
  
    it "should build a message with a comment correctly" do
      client = Riddle::Client.new
      client.append_query "test ", "*", "commenting"
      client.queue.first.should == query_contents(:comment)
    end
  
    if Riddle.loaded_version == '0.9.9' || Riddle.loaded_version == '1.10'
      it "should build a message with overrides correctly" do
        client = Riddle::Client.new
        client.add_override("rating", :float, {1 => 10.0})
        client.append_query "test "
        client.queue.first.should == query_contents(:overrides)
      end
  
      it "should build a message with selects correctly" do
        client = Riddle::Client.new
        client.select = "selecting"
        client.append_query "test "
        client.queue.first.should == query_contents(:select)
      end
    end
  
    it "should keep multiple messages in the queue" do
      client = Riddle::Client.new
      client.weights = [100, 1]
      client.append_query "test "
      client.append_query "test "
      client.queue.length.should == 2
      client.queue.each { |item| item.should == query_contents(:weights) }
    end
  
    it "should keep multiple messages in the queue with different params" do
      client = Riddle::Client.new
      client.weights = [100, 1]
      client.append_query "test "
      client.weights = []
      client.append_query "test ", "edition"
      client.queue.first.should == query_contents(:weights)
      client.queue.last.should  == query_contents(:index)
    end
  
    it "should build a basic update message correctly" do
      client = Riddle::Client.new
      client.send(
        :update_message,
        "people",
        ["birthday"],
        {1 => [191163600]}
      ).should == query_contents(:update_simple)
    end
  
    it "should build a keywords request without hits correctly" do
      client = Riddle::Client.new
      client.send(
        :keywords_message,
        "pat",
        "people",
        false
      ).should == query_contents(:keywords_without_hits)
    end
  
    it "should build a keywords request with hits correctly" do
      client = Riddle::Client.new
      client.send(
        :keywords_message,
        "pat",
        "people",
        true
      ).should == query_contents(:keywords_with_hits)
    end
  end
  
  it "should timeout after a specified time" do
    client = Riddle::Client.new
    client.port     = 9314
    client.timeout  = 1
    
    server = TCPServer.new "localhost", 9314
    
    lambda {
      client.send(:connect) { |socket| }
    }.should raise_error(Riddle::ConnectionError)
    
    server.close
  end unless RUBY_PLATFORM == 'java' # JRuby doesn't like Timeout

  context "connection retrying" do
    it "should try fives time when connection refused" do
      client = Riddle::Client.new
      client.port    = 3314

      TCPSocket.should_receive(:new).with('localhost', 3314).exactly(5).times.
        and_raise(Errno::ECONNREFUSED)

      lambda {
        client.send(:connect) { |socket| }
      }.should raise_error(Riddle::ConnectionError)
    end
  end

  context "connection fail over" do
    it "should try each of several server addresses after timeouts" do
      client = Riddle::Client.new
      client.port    = 3314
      client.servers = %w[localhost 127.0.0.1 0.0.0.0]
      client.timeout  = 1

      TCPSocket.should_receive(:new).with(
        an_instance_of(String), 3314
      ).exactly(3).and_raise Timeout::Error

      lambda {
        client.send(:connect) { |socket| }
      }.should raise_error(Riddle::ConnectionError)
    end unless RUBY_PLATFORM == 'java' # JRuby doesn't like Timeout

    it "should try each of several server addresses after a connection refused" do
      client = Riddle::Client.new
      client.port    = 3314
      client.servers = %w[localhost 127.0.0.1 0.0.0.0]
      client.timeout  = 1

      # initialise_socket will retry 5 times before failing,
      # these combined with the multiple server failover should result in 15
      # calls to TCPSocket.new
      TCPSocket.should_receive(:new).with(
        an_instance_of(String), 3314
      ).exactly(3 * 5).and_raise Errno::ECONNREFUSED

      lambda {
        client.send(:connect) { |socket| }
      }.should raise_error(Riddle::ConnectionError)
    end unless RUBY_PLATFORM == 'java' # JRuby doesn't like Timeout
  end

  it "should fail if the server has the wrong version" do
    client = Riddle::Client.new
    client.port     = 9314
    client.timeout  = 1

    server = TCPServer.new "localhost", 9314

    thread = Thread.new do
      client = server.accept
      client.send [0].pack("N"), 0
      client.close
    end

    lambda {
      client.send(:connect) { |socket| }
    }.should raise_error(Riddle::VersionError)

    thread.exit
    server.close
  end

end