couchbase-jruby-client 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.jrubyrc +722 -0
  4. data/.ruby-version +1 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE.txt +203 -0
  7. data/README.md +349 -0
  8. data/Rakefile +10 -0
  9. data/couchbase-jruby-client.gemspec +31 -0
  10. data/lib/couchbase/async/callback.rb +19 -0
  11. data/lib/couchbase/async/queue.rb +26 -0
  12. data/lib/couchbase/async.rb +140 -0
  13. data/lib/couchbase/bucket.rb +556 -0
  14. data/lib/couchbase/cluster.rb +105 -0
  15. data/lib/couchbase/constants.rb +12 -0
  16. data/lib/couchbase/design_doc.rb +61 -0
  17. data/lib/couchbase/error.rb +43 -0
  18. data/lib/couchbase/jruby/couchbase_client.rb +22 -0
  19. data/lib/couchbase/jruby/future.rb +8 -0
  20. data/lib/couchbase/operations/arithmetic.rb +301 -0
  21. data/lib/couchbase/operations/delete.rb +104 -0
  22. data/lib/couchbase/operations/design_docs.rb +99 -0
  23. data/lib/couchbase/operations/get.rb +282 -0
  24. data/lib/couchbase/operations/stats.rb +26 -0
  25. data/lib/couchbase/operations/store.rb +461 -0
  26. data/lib/couchbase/operations/touch.rb +136 -0
  27. data/lib/couchbase/operations/unlock.rb +192 -0
  28. data/lib/couchbase/operations/utils.rb +44 -0
  29. data/lib/couchbase/operations.rb +27 -0
  30. data/lib/couchbase/query.rb +73 -0
  31. data/lib/couchbase/result.rb +43 -0
  32. data/lib/couchbase/transcoder.rb +77 -0
  33. data/lib/couchbase/utils.rb +62 -0
  34. data/lib/couchbase/version.rb +3 -0
  35. data/lib/couchbase/view.rb +367 -0
  36. data/lib/couchbase/view_row.rb +193 -0
  37. data/lib/couchbase.rb +157 -0
  38. data/lib/jars/commons-codec-1.5.jar +0 -0
  39. data/lib/jars/couchbase-client-1.2.0-javadoc.jar +0 -0
  40. data/lib/jars/couchbase-client-1.2.0-sources.jar +0 -0
  41. data/lib/jars/couchbase-client-1.2.0.jar +0 -0
  42. data/lib/jars/httpcore-4.1.1.jar +0 -0
  43. data/lib/jars/httpcore-nio-4.1.1.jar +0 -0
  44. data/lib/jars/jettison-1.1.jar +0 -0
  45. data/lib/jars/netty-3.5.5.Final.jar +0 -0
  46. data/lib/jars/spymemcached-2.10.0-javadoc.jar +0 -0
  47. data/lib/jars/spymemcached-2.10.0-sources.jar +0 -0
  48. data/lib/jars/spymemcached-2.10.0.jar +0 -0
  49. data/test/profile/.gitignore +1 -0
  50. data/test/profile/.jrubyrc +722 -0
  51. data/test/profile/Gemfile +6 -0
  52. data/test/profile/benchmark.rb +168 -0
  53. data/test/profile/profile.rb +59 -0
  54. data/test/setup.rb +203 -0
  55. data/test/test_arithmetic.rb +177 -0
  56. data/test/test_async.rb +324 -0
  57. data/test/test_bucket.rb +213 -0
  58. data/test/test_cas.rb +79 -0
  59. data/test/test_couchbase.rb +29 -0
  60. data/test/test_couchbase_rails_cache_store.rb +341 -0
  61. data/test/test_delete.rb +125 -0
  62. data/test/test_design_docs.rb +72 -0
  63. data/test/test_errors.rb +82 -0
  64. data/test/test_format.rb +161 -0
  65. data/test/test_get.rb +417 -0
  66. data/test/test_query.rb +23 -0
  67. data/test/test_stats.rb +57 -0
  68. data/test/test_store.rb +213 -0
  69. data/test/test_timer.rb +43 -0
  70. data/test/test_touch.rb +97 -0
  71. data/test/test_unlock.rb +121 -0
  72. data/test/test_utils.rb +58 -0
  73. data/test/test_version.rb +53 -0
  74. data/test/test_view.rb +94 -0
  75. metadata +255 -0
@@ -0,0 +1,324 @@
1
+ # Author:: Couchbase <info@couchbase.com>
2
+ # Copyright:: 2011, 2012 Couchbase, Inc.
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+ require File.join(File.dirname(__FILE__), 'setup')
19
+
20
+ class TestAsync < MiniTest::Test
21
+
22
+ def setup
23
+ @mock = start_mock
24
+ end
25
+
26
+ def teardown
27
+ stop_mock(@mock)
28
+ end
29
+
30
+ def test_result_object_provides_enough_info
31
+ obj = Couchbase::Result.new
32
+ assert obj.respond_to?(:success?)
33
+ assert obj.respond_to?(:error)
34
+ assert obj.respond_to?(:key)
35
+ assert obj.respond_to?(:value)
36
+ assert obj.respond_to?(:node)
37
+ assert obj.respond_to?(:cas)
38
+ end
39
+
40
+ def test_it_requires_block_for_running_loop
41
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
42
+ refute connection.async?
43
+ assert_raises(LocalJumpError) do
44
+ connection.run
45
+ end
46
+ connection.run do |conn|
47
+ assert conn.async?
48
+ end
49
+ end
50
+
51
+ def test_it_resets_async_flag_when_raising_exception_from_callback
52
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
53
+
54
+ assert_raises(RuntimeError) do
55
+ connection.run do |conn|
56
+ conn.set(uniq_id, "foo") { raise }
57
+ end
58
+ end
59
+ refute connection.async?
60
+ end
61
+
62
+ def test_nested_async_get_set
63
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
64
+ connection.set(uniq_id, {"bar" => 1})
65
+ connection.set(uniq_id(:hit), 0)
66
+
67
+ connection.run do |conn|
68
+ conn.get(uniq_id) do
69
+ conn.get(uniq_id(:hit)) do |res|
70
+ conn.set(uniq_id(:hit), res.value + 1)
71
+ end
72
+ end
73
+ end
74
+
75
+ val = connection.get(uniq_id(:hit))
76
+ assert_equal 1, val
77
+ end
78
+
79
+ def test_nested_async_set_get
80
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
81
+ val = nil
82
+
83
+ connection.run do |conn|
84
+ conn.set(uniq_id, "foo") do
85
+ conn.get(uniq_id) do |res|
86
+ val = res.value
87
+ end
88
+ end
89
+ end
90
+
91
+ assert_equal "foo", val
92
+ end
93
+
94
+ def test_nested_async_touch_get
95
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
96
+ connection.set(uniq_id, "foo")
97
+ success = false
98
+ val = nil
99
+
100
+ connection.run do |conn|
101
+ conn.touch(uniq_id, :ttl => 1) do |res1|
102
+ success = res1.success?
103
+ conn.get(uniq_id) do |res2|
104
+ val = res2.value
105
+ end
106
+ end
107
+ end
108
+
109
+ assert success
110
+ assert_equal "foo", val
111
+ sleep(2)
112
+ assert_raises(Couchbase::Error::NotFound) do
113
+ connection.get(uniq_id)
114
+ end
115
+ end
116
+
117
+ def test_nested_async_delete_get
118
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
119
+ cas = connection.set(uniq_id, "foo")
120
+ success = false
121
+ val = :unknown
122
+
123
+ connection.run do |conn|
124
+ conn.delete(uniq_id, :cas => cas) do |res1|
125
+ success = res1.success?
126
+ conn.get(uniq_id, :quiet => true) do |res2|
127
+ val = res2.value
128
+ end
129
+ end
130
+ end
131
+
132
+ assert success
133
+ refute val
134
+ end
135
+
136
+ def test_nested_async_stats_set
137
+ skip
138
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
139
+ stats = {}
140
+
141
+ connection.run do |conn|
142
+ conn.stats do |res1|
143
+ id = uniq_id(res1.node, res1.key)
144
+ stats[id] = false
145
+ conn.set(id, res1.value) do |res2|
146
+ stats[id] = res2.cas
147
+ end
148
+ end
149
+ end
150
+
151
+ stats.keys.each do |key|
152
+ assert stats[key].is_a?(Numeric)
153
+ end
154
+ end
155
+
156
+ def test_nested_async_flush_set
157
+ skip
158
+ if @mock.real?
159
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
160
+ cas = connection.set(uniq_id, "foo")
161
+ res = {}
162
+
163
+ connection.run do |conn|
164
+ conn.flush do |res1|
165
+ assert res1.success?, "Expected: successful status code.\nActual: #{res1.error.inspect}"
166
+ id = uniq_id(res1.node)
167
+ res[id] = false
168
+ conn.set(id, true) do |res2|
169
+ res[id] = res2.cas
170
+ end
171
+ end
172
+ end
173
+
174
+ assert_raises(Couchbase::Error::NotFound) do
175
+ connection.get(uniq_id)
176
+ end
177
+ res.keys.each do |key|
178
+ assert res[key].is_a?(Numeric)
179
+ assert connection.get(key)
180
+ end
181
+ else
182
+ skip("REST FLUSH isn't implemented in CouchbaseMock.jar yet")
183
+ end
184
+ end
185
+
186
+ def test_nested_async_incr_get
187
+ skip
188
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
189
+ cas = connection.set(uniq_id, 1)
190
+ val = nil
191
+
192
+ connection.run do |conn|
193
+ conn.incr(uniq_id) do
194
+ conn.get(uniq_id) do |res|
195
+ val = res.value
196
+ end
197
+ end
198
+ end
199
+
200
+ assert_equal 2, val
201
+ end
202
+
203
+ def test_it_doesnt_accept_callbacks_in_synchronous_mode
204
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
205
+ refute connection.async?
206
+
207
+ assert_raises(ArgumentError) { connection.add(uniq_id, "foo") {} }
208
+ assert_raises(ArgumentError) { connection.set(uniq_id, "foo") {} }
209
+ assert_raises(ArgumentError) { connection.replace(uniq_id, "foo") {} }
210
+ assert_raises(ArgumentError) { connection.get(uniq_id) {} }
211
+ assert_raises(ArgumentError) { connection.touch(uniq_id) {} }
212
+ assert_raises(ArgumentError) { connection.incr(uniq_id) {} }
213
+ assert_raises(ArgumentError) { connection.decr(uniq_id) {} }
214
+ assert_raises(ArgumentError) { connection.delete(uniq_id) {} }
215
+ assert_raises(ArgumentError) { connection.append(uniq_id, "bar") {} }
216
+ assert_raises(ArgumentError) { connection.prepend(uniq_id, "bar") {} }
217
+ assert_raises(ArgumentError) { connection.flush {} }
218
+ assert_raises(ArgumentError) { connection.stats {} }
219
+ end
220
+
221
+ def test_it_disallow_nested_run
222
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
223
+ assert_raises(Couchbase::Error::Invalid) do
224
+ connection.run do
225
+ connection.run do
226
+ end
227
+ end
228
+ end
229
+ end
230
+
231
+ def test_it_extends_timeout_in_async_mode_if_needed
232
+ skip
233
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
234
+ connection.set(uniq_id, "foo")
235
+
236
+ connection.timeout = 100_000 # 100_000 us
237
+ connection.run do
238
+ connection.get(uniq_id) do |ret|
239
+ assert ret.success?
240
+ assert_equal "foo", ret.value
241
+ end
242
+ sleep(1.5) # 1_500_000 us
243
+ end
244
+ end
245
+
246
+ def test_send_threshold
247
+ skip
248
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
249
+
250
+ sent = false
251
+ connection.run(:send_threshold => 100) do # 100 bytes
252
+ connection.set(uniq_id, "foo" * 100) {|r| sent = true}
253
+ assert sent
254
+ end
255
+ end
256
+
257
+ def test_asynchronous_connection
258
+ skip
259
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port, :async => true)
260
+ refute connection.connected?, "new asynchronous connection must be disconnected"
261
+ connection.on_connect do |res|
262
+ assert res.success?, "on_connect called with error #{res.error.inspect}"
263
+ assert_same connection, res.bucket
264
+ end
265
+ connection.run {}
266
+ assert connection.connected?, "it should be connected after first run"
267
+ end
268
+
269
+ def test_it_calls_callback_immediately_if_connected_sync
270
+ skip
271
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
272
+ assert connection.connected?, "connection wasn't established in sync mode"
273
+ called = false
274
+ connection.on_connect do |res|
275
+ assert res.success?, "on_connect called with error #{res.error.inspect}"
276
+ called = true
277
+ end
278
+ assert called, "the callback hasn't been called on set"
279
+ called = false
280
+ connection.on_connect do |res|
281
+ assert res.success?, "on_connect called with error #{res.error.inspect}"
282
+ called = true
283
+ end
284
+ refute called, "the callback must not be called on subsequent sets"
285
+ end
286
+
287
+ def test_it_calls_callback_immediately_if_connected_async
288
+ skip
289
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port, :async => true)
290
+ refute connection.connected?, "new asynchronous connection must be disconnected"
291
+ called = false
292
+ connection.run {}
293
+ assert connection.connected?, "the connection must be established"
294
+ connection.run do
295
+ connection.on_connect do |res|
296
+ assert res.success?, "on_connect called with error #{res.error.inspect}"
297
+ called = true
298
+ end
299
+ end
300
+ assert called, "the callback hasn't been called on set"
301
+ called = false
302
+ connection.run do
303
+ connection.on_connect do |res|
304
+ assert res.success?, "on_connect called with error #{res.error.inspect}"
305
+ called = true
306
+ end
307
+ end
308
+ refute called, "the callback must not be called on subsequent sets"
309
+ end
310
+
311
+ def test_it_returns_error_if_user_start_work_on_disconnected_instance_outside_on_connect_callback
312
+ skip
313
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port, :async => true)
314
+ refute connection.connected?, "new asynchronous connection must be disconnected"
315
+ error = nil
316
+ connection.on_error do |ex|
317
+ error = ex
318
+ end
319
+ connection.run do |c|
320
+ c.set("foo", "bar")
321
+ end
322
+ assert_instance_of(Couchbase::Error::Connect, error)
323
+ end
324
+ end
@@ -0,0 +1,213 @@
1
+ # Author:: Couchbase <info@couchbase.com>
2
+ # Copyright:: 2011, 2012 Couchbase, Inc.
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+ require File.join(File.dirname(__FILE__), 'setup')
19
+
20
+ class TestBucket < MiniTest::Test
21
+
22
+ def test_it_substitute_default_parts_to_url
23
+
24
+ with_mock(:host => 'localhost') do |mock| # pick first free port
25
+ connections = [
26
+ Couchbase.new("http://#{mock.host}:#{mock.port}"),
27
+ Couchbase.new(:port => mock.port),
28
+ Couchbase.new("http://#{mock.host}:8091", :port => mock.port)
29
+ ]
30
+ connections.each do |connection|
31
+ assert_equal mock.port, connection.port
32
+ assert_equal "#{mock.host}:#{mock.port}", connection.authority
33
+ assert_equal "http://#{mock.host}:#{mock.port}/pools/default/buckets/default/", connection.url
34
+ end
35
+ end
36
+
37
+ with_mock(:host => '127.0.0.1') do |mock|
38
+ connections = [
39
+ Couchbase.new("http://#{mock.host}:#{mock.port}"),
40
+ Couchbase.new(:hostname => mock.host, :port => mock.port),
41
+ Couchbase.new('http://example.com:8091', :hostname => mock.host, :port => mock.port)
42
+ ]
43
+ connections.each do |connection|
44
+ assert_equal mock.host, connection.hostname
45
+ assert_equal "#{mock.host}:#{mock.port}", connection.authority
46
+ assert_equal "http://#{mock.host}:#{mock.port}/pools/default/buckets/default/", connection.url
47
+ end
48
+ end
49
+ end
50
+
51
+ def test_it_raises_network_error_if_server_not_found
52
+ skip 'Exception not being trapped correctly'
53
+ refute(`netstat -tnl` =~ /12345/)
54
+ assert_raises Couchbase::Error::Connect do
55
+ Couchbase.new(:port => 12345)
56
+ end
57
+ end
58
+
59
+ def test_it_raises_argument_error_for_illegal_url
60
+ illegal = [
61
+ "ftp://localhost:8091/",
62
+ "http:/localhost:8091/",
63
+ ""
64
+ ]
65
+ illegal.each do |url|
66
+ assert_raises ArgumentError do
67
+ Couchbase.new(url)
68
+ end
69
+ end
70
+ end
71
+
72
+ def test_it_able_to_connect_to_protected_buckets
73
+ with_mock(:buckets_spec => 'protected:secret') do |mock|
74
+ connection = Couchbase.new(:hostname => mock.host,
75
+ :port => mock.port,
76
+ :bucket => 'protected',
77
+ :username => 'protected',
78
+ :password => 'secret')
79
+ assert_equal "protected", connection.bucket
80
+ assert_equal "protected", connection.username
81
+ assert_equal "secret", connection.password
82
+ end
83
+ end
84
+
85
+ def test_it_allows_to_specify_credentials_in_url
86
+ with_mock(:buckets_spec => 'protected:secret') do |mock|
87
+ connection = Couchbase.new("http://protected:secret@#{mock.host}:#{mock.port}/pools/default/buckets/protected/")
88
+ assert_equal "protected", connection.bucket
89
+ assert_equal "protected", connection.username
90
+ assert_equal "secret", connection.password
91
+ end
92
+ end
93
+
94
+ def test_it_raises_error_with_wrong_credentials
95
+ with_mock do |mock|
96
+ assert_raises Couchbase::Error::Auth do
97
+ Couchbase.new(:hostname => mock.host,
98
+ :port => mock.port,
99
+ :bucket => 'default',
100
+ :username => 'wrong.username',
101
+ :password => 'wrong_password')
102
+ end
103
+ end
104
+ end
105
+
106
+ def test_it_unable_to_connect_to_protected_buckets_with_wrong_credentials
107
+ with_mock(:buckets_spec => 'protected:secret') do |mock|
108
+ assert_raises Couchbase::Error::Auth do
109
+ Couchbase.new(:hostname => mock.host,
110
+ :port => mock.port,
111
+ :bucket => 'protected',
112
+ :username => 'wrong',
113
+ :password => 'secret')
114
+ end
115
+ assert_raises Couchbase::Error::Auth do
116
+ Couchbase.new(:hostname => mock.host,
117
+ :port => mock.port,
118
+ :bucket => 'protected',
119
+ :username => 'protected',
120
+ :password => 'wrong')
121
+ end
122
+ end
123
+ end
124
+
125
+ def test_it_allows_change_quiet_flag
126
+ with_mock do |mock|
127
+ connection = Couchbase.new(:hostname => mock.host,
128
+ :port => mock.port)
129
+
130
+ refute connection.quiet?
131
+
132
+ connection = Couchbase.new(:hostname => mock.host,
133
+ :port => mock.port,
134
+ :quiet => true)
135
+ assert connection.quiet?
136
+
137
+ connection.quiet = nil
138
+ assert_equal false, connection.quiet?
139
+
140
+ connection.quiet = :foo
141
+ assert_equal true, connection.quiet?
142
+ end
143
+ end
144
+
145
+ def test_it_is_connected
146
+ with_mock do |mock|
147
+ connection = Couchbase.new(:hostname => mock.host,
148
+ :port => mock.port)
149
+ assert connection.connected?
150
+ end
151
+ end
152
+
153
+ def test_it_is_possible_to_disconnect_instance
154
+ with_mock do |mock|
155
+ connection = Couchbase.new(:hostname => mock.host,
156
+ :port => mock.port)
157
+ connection.disconnect
158
+ refute connection.connected?
159
+ end
160
+ end
161
+
162
+ def test_it_raises_error_on_double_disconnect
163
+ with_mock do |mock|
164
+ connection = Couchbase.new(:hostname => mock.host,
165
+ :port => mock.port)
166
+ connection.disconnect
167
+ assert_raises Couchbase::Error::Connect do
168
+ connection.disconnect
169
+ end
170
+ end
171
+ end
172
+
173
+ def test_it_allows_to_reconnect_the_instance
174
+ with_mock do |mock|
175
+ connection = Couchbase.new(:hostname => mock.host,
176
+ :port => mock.port)
177
+ connection.disconnect
178
+ refute connection.connected?
179
+ connection.reconnect
180
+ assert connection.connected?
181
+ assert connection.set(uniq_id, "foo")
182
+ end
183
+ end
184
+
185
+ def test_it_allows_to_change_configuration_during_reconnect
186
+ with_mock(:buckets_spec => 'protected:secret') do |mock|
187
+ connection = Couchbase.new(:hostname => mock.host,
188
+ :port => mock.port,
189
+ :bucket => 'protected',
190
+ :username => 'protected',
191
+ :password => 'secret')
192
+ connection.disconnect
193
+ assert_raises Couchbase::Error::Auth do
194
+ connection.reconnect(:password => 'incorrect')
195
+ end
196
+ refute connection.connected?
197
+
198
+ connection.reconnect(:password => 'secret')
199
+ assert connection.connected?
200
+ end
201
+ end
202
+
203
+ def test_it_uses_bucket_name_as_username_if_username_is_empty
204
+ with_mock(:buckets_spec => 'protected:secret') do |mock|
205
+ connection = Couchbase.new(:hostname => mock.host,
206
+ :port => mock.port,
207
+ :bucket => 'protected',
208
+ :password => 'secret')
209
+ assert connection.connected?
210
+ end
211
+ end
212
+
213
+ end
data/test/test_cas.rb ADDED
@@ -0,0 +1,79 @@
1
+ # Author:: Couchbase <info@couchbase.com>
2
+ # Copyright:: 2011, 2012 Couchbase, Inc.
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+ require File.join(File.dirname(__FILE__), 'setup')
19
+
20
+ class TestCas < MiniTest::Test
21
+
22
+ def setup
23
+ @mock = start_mock
24
+ end
25
+
26
+ def teardown
27
+ stop_mock(@mock)
28
+ end
29
+
30
+ def test_compare_and_swap
31
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port,
32
+ :default_format => :document)
33
+ connection.set(uniq_id, {"bar" => 1})
34
+ connection.cas(uniq_id) do |val|
35
+ val["baz"] = 2
36
+ val
37
+ end
38
+ val = connection.get(uniq_id)
39
+ expected = {"bar" => 1, "baz" => 2}
40
+ assert_equal expected, val
41
+ end
42
+
43
+ def test_compare_and_swap_async
44
+ skip
45
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port,
46
+ :default_format => :document)
47
+ connection.set(uniq_id, {"bar" => 1})
48
+ calls = 0
49
+ connection.run do |conn|
50
+ conn.cas(uniq_id) do |ret|
51
+ calls += 1
52
+ case ret.operation
53
+ when :get
54
+ new_val = ret.value
55
+ new_val["baz"] = 2
56
+ new_val
57
+ when :set
58
+ assert ret.success?
59
+ else
60
+ flunk "Unexpected operation: #{ret.operation.inspect}"
61
+ end
62
+ end
63
+ end
64
+ assert_equal 2, calls
65
+ val = connection.get(uniq_id)
66
+ expected = {"bar" => 1, "baz" => 2}
67
+ assert_equal expected, val
68
+ end
69
+
70
+ def test_flags_replication
71
+ skip
72
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port,
73
+ :default_format => :document)
74
+ connection.set(uniq_id, "bar", :flags => 0x100)
75
+ connection.cas(uniq_id) { "baz" }
76
+ _, flags, _ = connection.get(uniq_id, :extended => true)
77
+ assert_equal 0x100, flags
78
+ end
79
+ end
@@ -0,0 +1,29 @@
1
+ # Author:: Couchbase <info@couchbase.com>
2
+ # Copyright:: 2011, 2012 Couchbase, Inc.
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+ require File.join(File.dirname(__FILE__), 'setup')
19
+ require 'minitest/mock'
20
+
21
+ class TestCouchbase < MiniTest::Test
22
+
23
+ def test_that_it_create_instance_of_bucket
24
+ with_mock do |mock|
25
+ assert_instance_of Couchbase::Bucket, Couchbase.new("http://#{mock.host}:#{mock.port}")
26
+ end
27
+ end
28
+
29
+ end