couchbase-jruby-client 0.1.2 → 0.1.3
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.
- checksums.yaml +4 -4
- data/couchbase-jruby-client.gemspec +1 -1
- data/lib/couchbase.rb +21 -15
- data/lib/couchbase/bucket.rb +39 -41
- data/lib/couchbase/operations/store.rb +1 -1
- data/lib/couchbase/version.rb +1 -1
- data/test/setup.rb +8 -145
- data/test/test_arithmetic.rb +53 -76
- data/test/test_async.rb +74 -102
- data/test/test_bucket.rb +71 -63
- data/test/test_cas.rb +9 -23
- data/test/test_couchbase.rb +5 -4
- data/test/test_delete.rb +41 -43
- data/test/test_design_docs.rb +8 -18
- data/test/test_errors.rb +9 -18
- data/test/test_format.rb +21 -31
- data/test/test_get.rb +107 -150
- data/test/test_stats.rb +9 -24
- data/test/test_store.rb +53 -62
- data/test/test_timer.rb +3 -12
- data/test/test_touch.rb +26 -33
- data/test/test_unlock.rb +45 -78
- data/test/test_utils.rb +2 -11
- data/test/test_version.rb +4 -14
- data/test/test_view.rb +4 -11
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b70e454af837900912ab334c577374656157b1df
|
4
|
+
data.tar.gz: cd773d813f362ffd5eb695458800fab40ff2d3f0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cee4caac6a388131d089bac920d651240710b59caeac1dfa8595e301cad01ff0a89f49953b6657ce641831e1f36f25acf7d8678bb0f53d88311e2881b8b5b9b3
|
7
|
+
data.tar.gz: 8c8127b194702f2a58237f2f71f6064e2782cb89c07365adb5217804c3df2c7eec9ceb32bfdbed9ca2d1c625856740376b5914da2bdcb53d1dbc06c49bf0ef4a
|
@@ -20,7 +20,7 @@ Gem::Specification.new do |s|
|
|
20
20
|
s.require_paths = ['lib']
|
21
21
|
|
22
22
|
s.add_runtime_dependency 'multi_json', '~> 1.0'
|
23
|
-
s.add_runtime_dependency '
|
23
|
+
s.add_runtime_dependency 'thread_safe', '~> 0.1.2'
|
24
24
|
|
25
25
|
s.add_development_dependency 'bundler', '~> 1.3'
|
26
26
|
s.add_development_dependency 'rake'
|
data/lib/couchbase.rb
CHANGED
@@ -30,7 +30,7 @@ require 'jars/spymemcached-2.10.0.jar'
|
|
30
30
|
require 'jars/httpcore-nio-4.1.1.jar'
|
31
31
|
require 'couchbase/version'
|
32
32
|
require 'uri'
|
33
|
-
require '
|
33
|
+
require 'thread_safe'
|
34
34
|
require 'couchbase/transcoder'
|
35
35
|
require 'couchbase/async'
|
36
36
|
require 'couchbase/operations'
|
@@ -57,7 +57,7 @@ end
|
|
57
57
|
# Couchbase jruby client
|
58
58
|
module Couchbase
|
59
59
|
|
60
|
-
@@buckets =
|
60
|
+
@@buckets = ThreadSafe::Cache.new
|
61
61
|
|
62
62
|
class << self
|
63
63
|
|
@@ -85,9 +85,6 @@ module Couchbase
|
|
85
85
|
# @return [Bucket] connection instance
|
86
86
|
def connect(*options)
|
87
87
|
Bucket.new(*(options.flatten))
|
88
|
-
# disconnect
|
89
|
-
# @@bucket.update { |bucket| bucket ||= Bucket.new(*(options.flatten)) }
|
90
|
-
# @@bucket.value
|
91
88
|
end
|
92
89
|
alias :new :connect
|
93
90
|
|
@@ -100,7 +97,15 @@ module Couchbase
|
|
100
97
|
# Couchbase.bucket.name #=> "blog"
|
101
98
|
#
|
102
99
|
# @return [Hash, String]
|
103
|
-
|
100
|
+
attr_reader :connection_options
|
101
|
+
|
102
|
+
def connection_options=(options)
|
103
|
+
@connection_options = normalize_connection_options(options)
|
104
|
+
end
|
105
|
+
|
106
|
+
def normalize_connection_options(options)
|
107
|
+
Hash[ options.map { |k, v| [k.to_sym, v] } ]
|
108
|
+
end
|
104
109
|
|
105
110
|
# The connection instance for current thread
|
106
111
|
#
|
@@ -143,10 +148,10 @@ module Couchbase
|
|
143
148
|
path = URI.parse(@connection_options).path
|
144
149
|
path[%r(^(/pools/([A-Za-z0-9_.-]+)(/buckets/([A-Za-z0-9_.-]+))?)?), 3] || "default"
|
145
150
|
else
|
146
|
-
|
151
|
+
'default'
|
147
152
|
end
|
148
|
-
|
149
|
-
@@buckets
|
153
|
+
|
154
|
+
@@buckets[name] ||= connect(connection_options)
|
150
155
|
end
|
151
156
|
|
152
157
|
# Set a connection instance for current thread
|
@@ -155,18 +160,19 @@ module Couchbase
|
|
155
160
|
#
|
156
161
|
# @return [Bucket]
|
157
162
|
def bucket=(connection)
|
158
|
-
name
|
159
|
-
@@buckets
|
160
|
-
@@buckets.value[name]
|
163
|
+
name = @connection_options && @connection_options[:bucket] || "default"
|
164
|
+
@@buckets[name] = connection
|
161
165
|
end
|
162
166
|
|
163
167
|
def connected?
|
164
|
-
!!@@buckets.
|
168
|
+
!!@@buckets.empty?
|
165
169
|
end
|
166
170
|
|
167
171
|
def disconnect
|
168
|
-
@@buckets.
|
169
|
-
|
172
|
+
@@buckets.each_key do |name|
|
173
|
+
bucket = @@buckets.delete(name)
|
174
|
+
bucket.disconnect if connected?
|
175
|
+
end
|
170
176
|
end
|
171
177
|
end
|
172
178
|
end
|
data/lib/couchbase/bucket.rb
CHANGED
@@ -36,11 +36,39 @@ module Couchbase
|
|
36
36
|
include Couchbase::Async
|
37
37
|
|
38
38
|
attr_accessor :quiet, :hostname, :port, :pool, :bucket, :username,
|
39
|
-
:password, :default_ttl, :timeout,
|
39
|
+
:password, :default_ttl, :timeout, :default_format,
|
40
40
|
:default_arithmetic_init, :transcoder
|
41
41
|
|
42
42
|
attr_reader :client, :key_prefix, :default_format
|
43
43
|
|
44
|
+
DEFAULT_OPTIONS = {
|
45
|
+
type: nil,
|
46
|
+
quiet: false,
|
47
|
+
hostname: 'localhost',
|
48
|
+
port: 8091,
|
49
|
+
pool: 'default',
|
50
|
+
bucket: 'default',
|
51
|
+
password: '',
|
52
|
+
engine: nil,
|
53
|
+
default_ttl: 0,
|
54
|
+
async: false,
|
55
|
+
default_arithmetic_init: 0,
|
56
|
+
default_flags: 0,
|
57
|
+
default_format: :document,
|
58
|
+
default_observe_timeout: 2_500_000,
|
59
|
+
on_error: nil,
|
60
|
+
on_connect: nil,
|
61
|
+
timeout: 0,
|
62
|
+
environment: nil,
|
63
|
+
key_prefix: nil,
|
64
|
+
node_list: nil,
|
65
|
+
destroying: 0,
|
66
|
+
connected: 0,
|
67
|
+
on_connect_proc: nil,
|
68
|
+
async_disconnect_hook_set: 0,
|
69
|
+
connected: false
|
70
|
+
}.freeze
|
71
|
+
|
44
72
|
# Initialize new Bucket.
|
45
73
|
#
|
46
74
|
# @since 1.0.0
|
@@ -134,52 +162,20 @@ module Couchbase
|
|
134
162
|
# @return [Bucket]
|
135
163
|
#
|
136
164
|
def initialize(url = nil, options = {})
|
137
|
-
default_options = {
|
138
|
-
type: nil,
|
139
|
-
quiet: false,
|
140
|
-
hostname: 'localhost',
|
141
|
-
port: 8091,
|
142
|
-
pool: 'default',
|
143
|
-
bucket: 'default',
|
144
|
-
password: '',
|
145
|
-
engine: nil,
|
146
|
-
default_ttl: 0,
|
147
|
-
async: false,
|
148
|
-
default_arithmetic_init: 0,
|
149
|
-
default_flags: 0,
|
150
|
-
default_format: :document,
|
151
|
-
default_observe_timeout: 2500000,
|
152
|
-
on_error: nil,
|
153
|
-
on_connect: nil,
|
154
|
-
timeout: 0,
|
155
|
-
environment: nil,
|
156
|
-
key_prefix: nil,
|
157
|
-
node_list: nil,
|
158
|
-
destroying: 0,
|
159
|
-
connected: 0,
|
160
|
-
on_connect_proc: nil,
|
161
|
-
async_disconnect_hook_set: 0,
|
162
|
-
connected: false
|
163
|
-
}
|
164
|
-
|
165
165
|
url_options = if url.is_a? String
|
166
166
|
fail ArgumentError.new unless url =~ /^http:\/\//
|
167
|
-
|
168
167
|
uri = URI.new(url)
|
169
|
-
|
170
|
-
|
171
|
-
host: uri.host,
|
172
|
-
port: uri.port,
|
173
|
-
}.merge(path_to_pool_and_bucket(uri.path))
|
168
|
+
{ hostname: uri.host, port: uri.port }.
|
169
|
+
merge(path_to_pool_and_bucket(uri.path))
|
174
170
|
elsif url.nil?
|
175
171
|
{}
|
176
172
|
else
|
177
173
|
url
|
178
174
|
end
|
179
175
|
|
180
|
-
options =
|
176
|
+
options = Couchbase.normalize_connection_options(options)
|
181
177
|
|
182
|
-
connection_options =
|
178
|
+
connection_options = DEFAULT_OPTIONS.merge(url_options).merge(options)
|
183
179
|
|
184
180
|
connection_options.each_pair do |key, value|
|
185
181
|
instance_variable_set("@#{key}", value)
|
@@ -191,8 +187,6 @@ module Couchbase
|
|
191
187
|
plain: Transcoder::Plain.new
|
192
188
|
}
|
193
189
|
|
194
|
-
@transcoder = @transcoders[@default_format]
|
195
|
-
|
196
190
|
connect unless async?
|
197
191
|
end
|
198
192
|
|
@@ -213,12 +207,12 @@ module Couchbase
|
|
213
207
|
|
214
208
|
begin
|
215
209
|
builder = CouchbaseConnectionFactoryBuilder.new
|
216
|
-
builder.setTranscoder(
|
210
|
+
builder.setTranscoder(transcoder)
|
217
211
|
connection_factory = builder.buildCouchbaseConnection(uris, bucket.to_java_string, password.to_java_string)
|
218
212
|
@client = CouchbaseClient.new(connection_factory)
|
219
213
|
@connected = true
|
220
214
|
rescue Java::ComCouchbaseClientVbucket::ConfigurationException
|
221
|
-
fail Couchbase::Error::Auth
|
215
|
+
fail Couchbase::Error::Auth, 'Couchbase configurations are incorrect.'
|
222
216
|
rescue java.net.ConnectException => e
|
223
217
|
fail Couchbase::Error::Connect
|
224
218
|
end
|
@@ -254,6 +248,10 @@ module Couchbase
|
|
254
248
|
end
|
255
249
|
end
|
256
250
|
|
251
|
+
def transcoder
|
252
|
+
@transcoders[@default_format]
|
253
|
+
end
|
254
|
+
|
257
255
|
def on_connect(&block)
|
258
256
|
@on_connect = block
|
259
257
|
end
|
@@ -407,7 +407,7 @@ module Couchbase::Operations
|
|
407
407
|
def store_args_parser(key, value, options)
|
408
408
|
key = key.to_str if key.respond_to?(:to_str)
|
409
409
|
ttl = options.delete(:ttl) || default_ttl
|
410
|
-
transcoder = @transcoders[options.delete(:format)] ||
|
410
|
+
transcoder = @transcoders[options.delete(:format)] || self.transcoder
|
411
411
|
|
412
412
|
[key, value, ttl, transcoder]
|
413
413
|
end
|
data/lib/couchbase/version.rb
CHANGED
data/test/setup.rb
CHANGED
@@ -21,6 +21,7 @@ require 'couchbase'
|
|
21
21
|
|
22
22
|
require 'socket'
|
23
23
|
require 'open-uri'
|
24
|
+
require 'ostruct'
|
24
25
|
|
25
26
|
require 'pry'
|
26
27
|
|
@@ -41,153 +42,18 @@ require 'pry'
|
|
41
42
|
|
42
43
|
Minitest.after_run { Couchbase.disconnect }
|
43
44
|
|
44
|
-
class CouchbaseServer
|
45
|
-
attr_accessor :host, :port, :num_nodes, :buckets_spec
|
46
|
-
|
47
|
-
def real?
|
48
|
-
true
|
49
|
-
end
|
50
|
-
|
51
|
-
def initialize(params = {})
|
52
|
-
@host, @port = ['localhost', 8091] #ENV['COUCHBASE_SERVER'].split(':')
|
53
|
-
@port = @port.to_i
|
54
|
-
|
55
|
-
if @host.nil? || @host.empty? || @port == 0
|
56
|
-
raise ArgumentError, "Check COUCHBASE_SERVER variable. It should be hostname:port"
|
57
|
-
end
|
58
|
-
|
59
|
-
@config = MultiJson.load(open("http://#{@host}:#{@port}/pools/default"))
|
60
|
-
@num_nodes = @config["nodes"].size
|
61
|
-
@buckets_spec = params[:buckets_spec] || "default:" # "default:,protected:secret,cache::memcache"
|
62
|
-
end
|
63
|
-
|
64
|
-
def start
|
65
|
-
# flush all buckets
|
66
|
-
@buckets_spec.split(',') do |bucket|
|
67
|
-
name, password, _ = bucket.split(':')
|
68
|
-
connection = Couchbase.new(:hostname => @host,
|
69
|
-
:port => @port,
|
70
|
-
:username => name,
|
71
|
-
:bucket => name,
|
72
|
-
:password => password)
|
73
|
-
begin
|
74
|
-
connection.flush
|
75
|
-
rescue Couchbase::Error::NotSupported
|
76
|
-
# on recent server flush is disabled
|
77
|
-
ensure
|
78
|
-
connection.disconnect
|
79
|
-
end
|
80
|
-
end if ENV['COUCHBASE_FLUSH_BUCKETS']
|
81
|
-
end
|
82
|
-
|
83
|
-
def stop
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
class CouchbaseMock
|
88
|
-
Monitor = Struct.new(:pid, :client, :socket, :port)
|
89
|
-
|
90
|
-
attr_accessor :host, :port, :buckets_spec, :num_nodes, :num_vbuckets
|
91
|
-
|
92
|
-
def real?
|
93
|
-
false
|
94
|
-
end
|
95
|
-
|
96
|
-
def initialize(params = {})
|
97
|
-
@host = "127.0.0.1"
|
98
|
-
@port = 0
|
99
|
-
@num_nodes = 10
|
100
|
-
@num_vbuckets = 4096
|
101
|
-
@buckets_spec = "default:" # "default:,protected:secret,cache::memcache"
|
102
|
-
params.each do |key, value|
|
103
|
-
send("#{key}=", value)
|
104
|
-
end
|
105
|
-
yield self if block_given?
|
106
|
-
if @num_vbuckets < 1 || (@num_vbuckets & (@num_vbuckets - 1) != 0)
|
107
|
-
raise ArgumentError, "Number of vbuckets should be a power of two and greater than zero"
|
108
|
-
end
|
109
|
-
end
|
110
|
-
|
111
|
-
def start
|
112
|
-
@monitor = Monitor.new
|
113
|
-
@monitor.socket = TCPServer.new(nil, 0)
|
114
|
-
@monitor.socket.listen(10)
|
115
|
-
_, @monitor.port, _, _ = @monitor.socket.addr
|
116
|
-
trap("CLD") do
|
117
|
-
puts "CouchbaseMock.jar died unexpectedly during startup"
|
118
|
-
exit(1)
|
119
|
-
end
|
120
|
-
@monitor.pid = fork
|
121
|
-
if @monitor.pid.nil?
|
122
|
-
rc = exec(command_line("--harakiri-monitor=:#{@monitor.port}"))
|
123
|
-
else
|
124
|
-
trap("CLD", "SIG_DFL")
|
125
|
-
@monitor.client, _ = @monitor.socket.accept
|
126
|
-
@port = @monitor.client.recv(100).to_i
|
127
|
-
end
|
128
|
-
end
|
129
|
-
|
130
|
-
def stop
|
131
|
-
@monitor.client.close
|
132
|
-
@monitor.socket.close
|
133
|
-
Process.kill("TERM", @monitor.pid)
|
134
|
-
Process.wait(@monitor.pid)
|
135
|
-
end
|
136
|
-
|
137
|
-
def failover_node(index, bucket = "default")
|
138
|
-
@monitor.client.send("failover,#{index},#{bucket}", 0)
|
139
|
-
end
|
140
|
-
|
141
|
-
def respawn_node(index, bucket = "default")
|
142
|
-
@monitor.client.send("respawn,#{index},#{bucket}", 0)
|
143
|
-
end
|
144
|
-
|
145
|
-
protected
|
146
|
-
|
147
|
-
def command_line(extra = nil)
|
148
|
-
cmd = "java -jar #{File.dirname(__FILE__)}/CouchbaseMock.jar"
|
149
|
-
cmd << " --host #{@host}" if @host
|
150
|
-
cmd << " --port #{@port}" if @port
|
151
|
-
cmd << " --nodes #{@num_nodes}" if @num_nodes
|
152
|
-
cmd << " --vbuckets #{@num_vbuckets}" if @num_vbuckets
|
153
|
-
cmd << " --buckets #{@buckets_spec}" if @buckets_spec
|
154
|
-
cmd << " #{extra}"
|
155
|
-
cmd
|
156
|
-
end
|
157
|
-
end
|
158
|
-
|
159
45
|
class MiniTest::Test
|
160
46
|
|
161
|
-
def
|
162
|
-
|
163
|
-
if true # ENV['COUCHBASE_SERVER']
|
164
|
-
mock = CouchbaseServer.new(params)
|
165
|
-
if (params[:port] && mock.port != params[:port]) ||
|
166
|
-
(params[:host] && mock.host != params[:host]) ||
|
167
|
-
mock.buckets_spec != "default:"
|
168
|
-
skip("Unable to configure real cluster. Requested config is: #{params.inspect}")
|
169
|
-
end
|
170
|
-
else
|
171
|
-
mock = CouchbaseMock.new(params)
|
172
|
-
end
|
173
|
-
mock.start
|
174
|
-
mock
|
175
|
-
end
|
176
|
-
|
177
|
-
def stop_mock(mock)
|
178
|
-
assert(mock)
|
179
|
-
mock.stop
|
47
|
+
def cb
|
48
|
+
Couchbase.bucket
|
180
49
|
end
|
181
50
|
|
182
|
-
def
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
mock = start_mock(params)
|
187
|
-
yield mock
|
51
|
+
def with_configs(configs = {})
|
52
|
+
configs = Couchbase::Bucket::DEFAULT_OPTIONS.merge(configs)
|
53
|
+
if configs[:host].nil?
|
54
|
+
configs[:host] = configs[:hostname]
|
188
55
|
end
|
189
|
-
|
190
|
-
stop_mock(mock) if mock
|
56
|
+
yield OpenStruct.new(configs)
|
191
57
|
end
|
192
58
|
|
193
59
|
def uniq_id(*suffixes)
|
@@ -197,7 +63,4 @@ class MiniTest::Test
|
|
197
63
|
[test_id, @ids[test_id]].join("_")
|
198
64
|
end
|
199
65
|
|
200
|
-
def after_teardown
|
201
|
-
GC.start
|
202
|
-
end
|
203
66
|
end
|
data/test/test_arithmetic.rb
CHANGED
@@ -19,159 +19,136 @@ require File.join(File.dirname(__FILE__), 'setup')
|
|
19
19
|
|
20
20
|
class TestArithmetic < MiniTest::Test
|
21
21
|
|
22
|
-
def setup
|
23
|
-
@mock = start_mock
|
24
|
-
end
|
25
|
-
|
26
|
-
def teardown
|
27
|
-
stop_mock(@mock)
|
28
|
-
end
|
29
|
-
|
30
22
|
def test_trivial_incr_decr
|
31
|
-
|
32
|
-
|
33
|
-
connection.set(uniq_id, 1)
|
34
|
-
val = connection.incr(uniq_id)
|
23
|
+
cb.set(uniq_id, 1)
|
24
|
+
val = cb.incr(uniq_id)
|
35
25
|
assert_equal 2, val
|
36
|
-
val =
|
26
|
+
val = cb.get(uniq_id)
|
37
27
|
assert_equal 2, val
|
38
28
|
|
39
|
-
|
40
|
-
val =
|
29
|
+
cb.set(uniq_id, 7)
|
30
|
+
val = cb.decr(uniq_id)
|
41
31
|
assert_equal 6, val
|
42
|
-
val =
|
32
|
+
val = cb.get(uniq_id)
|
43
33
|
assert_equal 6, val
|
44
34
|
end
|
45
35
|
|
46
36
|
def test_it_fails_to_incr_decr_missing_key
|
47
|
-
connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
|
48
|
-
|
49
37
|
assert_raises(Couchbase::Error::NotFound) do
|
50
|
-
|
38
|
+
cb.incr(uniq_id(:missing))
|
51
39
|
end
|
52
40
|
assert_raises(Couchbase::Error::NotFound) do
|
53
|
-
|
41
|
+
cb.decr(uniq_id(:missing))
|
54
42
|
end
|
55
43
|
end
|
56
44
|
|
57
45
|
def test_it_allows_to_make_increments_less_verbose_by_forcing_create_by_default
|
58
|
-
|
59
|
-
:default_arithmetic_init => true)
|
46
|
+
cb.default_arithmetic_init = true
|
60
47
|
assert_raises(Couchbase::Error::NotFound) do
|
61
|
-
|
48
|
+
cb.get(uniq_id)
|
62
49
|
end
|
63
|
-
assert_equal 0,
|
64
|
-
assert_equal 0,
|
50
|
+
assert_equal 0, cb.incr(uniq_id), "return value"
|
51
|
+
assert_equal 0, cb.get(uniq_id), "via get command"
|
52
|
+
ensure
|
53
|
+
cb.default_arithmetic_init = 0
|
65
54
|
end
|
66
55
|
|
67
56
|
def test_it_allows_to_setup_initial_value_during_connection
|
68
|
-
|
69
|
-
:default_arithmetic_init => 10)
|
57
|
+
cb.default_arithmetic_init = 10
|
70
58
|
assert_raises(Couchbase::Error::NotFound) do
|
71
|
-
|
59
|
+
cb.get(uniq_id)
|
72
60
|
end
|
73
61
|
|
74
|
-
assert_equal 10,
|
75
|
-
assert_equal 10,
|
62
|
+
assert_equal 10, cb.incr(uniq_id), "return value"
|
63
|
+
assert_equal 10, cb.get(uniq_id), "via get command"
|
64
|
+
ensure
|
65
|
+
cb.default_arithmetic_init = 0
|
76
66
|
end
|
77
67
|
|
78
68
|
def test_it_allows_to_change_default_initial_value_after_connection
|
79
|
-
|
80
|
-
|
81
|
-
assert_equal 0, connection.default_arithmetic_init
|
69
|
+
assert_equal 0, cb.default_arithmetic_init
|
82
70
|
assert_raises(Couchbase::Error::NotFound) do
|
83
|
-
|
71
|
+
cb.incr(uniq_id)
|
84
72
|
end
|
85
73
|
|
86
|
-
|
87
|
-
assert_equal 10,
|
74
|
+
cb.default_arithmetic_init = 10
|
75
|
+
assert_equal 10, cb.default_arithmetic_init
|
88
76
|
assert_raises(Couchbase::Error::NotFound) do
|
89
|
-
|
77
|
+
cb.get(uniq_id)
|
90
78
|
end
|
91
|
-
assert_equal 10,
|
92
|
-
assert_equal 10,
|
79
|
+
assert_equal 10, cb.incr(uniq_id), "return value"
|
80
|
+
assert_equal 10, cb.get(uniq_id), "via get command"
|
81
|
+
ensure
|
82
|
+
cb.default_arithmetic_init = 0
|
93
83
|
end
|
94
84
|
|
95
85
|
def test_it_creates_missing_key_when_initial_value_specified
|
96
|
-
|
97
|
-
|
98
|
-
val = connection.incr(uniq_id(:missing), :initial => 5)
|
86
|
+
val = cb.incr(uniq_id(:missing), :initial => 5)
|
99
87
|
assert_equal 5, val
|
100
|
-
val =
|
88
|
+
val = cb.incr(uniq_id(:missing), :initial => 5)
|
101
89
|
assert_equal 6, val
|
102
|
-
val =
|
90
|
+
val = cb.get(uniq_id(:missing))
|
103
91
|
assert_equal 6, val
|
104
92
|
end
|
105
93
|
|
106
94
|
def test_it_uses_zero_as_default_value_for_missing_keys
|
107
|
-
|
108
|
-
|
109
|
-
val = connection.incr(uniq_id(:missing), :create => true)
|
95
|
+
val = cb.incr(uniq_id(:missing), :create => true)
|
110
96
|
assert_equal 0, val
|
111
|
-
val =
|
97
|
+
val = cb.incr(uniq_id(:missing), :create => true)
|
112
98
|
assert_equal 1, val
|
113
|
-
val =
|
99
|
+
val = cb.get(uniq_id(:missing))
|
114
100
|
assert_equal 1, val
|
115
101
|
end
|
116
102
|
|
117
103
|
def test_it_allows_custom_ttl
|
118
|
-
|
119
|
-
|
120
|
-
val = connection.incr(uniq_id(:missing), :create => true, :ttl => 1)
|
104
|
+
val = cb.incr(uniq_id(:missing), :create => true, :ttl => 1)
|
121
105
|
assert_equal 0, val
|
122
|
-
val =
|
106
|
+
val = cb.incr(uniq_id(:missing), :create => true)
|
123
107
|
assert_equal 1, val
|
124
108
|
sleep(2)
|
125
109
|
assert_raises(Couchbase::Error::NotFound) do
|
126
|
-
|
110
|
+
cb.get(uniq_id(:missing))
|
127
111
|
end
|
128
112
|
end
|
129
113
|
|
130
114
|
def test_decrement_with_absolute_ttl
|
131
|
-
connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
|
132
115
|
# absolute TTL: one second from now
|
133
116
|
exp = Time.now.to_i + 1
|
134
|
-
val =
|
117
|
+
val = cb.decr(uniq_id, 12, :initial => 0, :ttl => exp)
|
135
118
|
assert_equal 0, val
|
136
|
-
assert_equal 0,
|
119
|
+
assert_equal 0, cb.get(uniq_id)
|
137
120
|
sleep(3)
|
138
121
|
assert_raises(Couchbase::Error::NotFound) do
|
139
|
-
|
122
|
+
cb.get(uniq_id)
|
140
123
|
end
|
141
124
|
end
|
142
125
|
|
143
126
|
def test_it_allows_custom_delta
|
144
|
-
|
145
|
-
|
146
|
-
connection.set(uniq_id, 12)
|
147
|
-
val = connection.incr(uniq_id, 10)
|
127
|
+
cb.set(uniq_id, 12)
|
128
|
+
val = cb.incr(uniq_id, 10)
|
148
129
|
assert_equal 22, val
|
149
130
|
end
|
150
131
|
|
151
132
|
def test_it_allows_to_specify_delta_in_options
|
152
|
-
|
153
|
-
|
154
|
-
connection.set(uniq_id, 12)
|
133
|
+
cb.set(uniq_id, 12)
|
155
134
|
options = {:delta => 10}
|
156
|
-
val =
|
135
|
+
val = cb.incr(uniq_id, options)
|
157
136
|
assert_equal 22, val
|
158
137
|
end
|
159
138
|
|
160
139
|
def test_multi_incr
|
161
|
-
|
162
|
-
connection.set(uniq_id(:foo) => 1, uniq_id(:bar) => 1)
|
140
|
+
cb.set(uniq_id(:foo) => 1, uniq_id(:bar) => 1)
|
163
141
|
|
164
|
-
assert_equal [2, 2],
|
165
|
-
assert_equal [12, 12],
|
166
|
-
assert_equal [14, 15],
|
142
|
+
assert_equal [2, 2], cb.incr(uniq_id(:foo), uniq_id(:bar)).values.sort
|
143
|
+
assert_equal [12, 12], cb.incr(uniq_id(:foo), uniq_id(:bar), :delta => 10).values.sort
|
144
|
+
assert_equal [14, 15], cb.incr(uniq_id(:foo) => 2, uniq_id(:bar) => 3).values.sort
|
167
145
|
end
|
168
146
|
|
169
147
|
def test_multi_decr
|
170
|
-
|
171
|
-
connection.set(uniq_id(:foo) => 14, uniq_id(:bar) => 15)
|
148
|
+
cb.set(uniq_id(:foo) => 14, uniq_id(:bar) => 15)
|
172
149
|
|
173
|
-
assert_equal [12, 12],
|
174
|
-
assert_equal [2, 2],
|
175
|
-
assert_equal [1, 1],
|
150
|
+
assert_equal [12, 12], cb.decr(uniq_id(:foo) => 2, uniq_id(:bar) => 3).values.sort
|
151
|
+
assert_equal [2, 2], cb.decr(uniq_id(:foo), uniq_id(:bar), :delta => 10).values.sort
|
152
|
+
assert_equal [1, 1], cb.decr(uniq_id(:foo), uniq_id(:bar)).values.sort
|
176
153
|
end
|
177
154
|
end
|