couchbase 1.1.0-x86-mingw32

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.
@@ -0,0 +1,109 @@
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 TestArithmetic < MiniTest::Unit::TestCase
21
+
22
+ def setup
23
+ @mock = start_mock
24
+ end
25
+
26
+ def teardown
27
+ stop_mock(@mock)
28
+ end
29
+
30
+ def test_trivial_incr_decr
31
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
32
+
33
+ connection.set(uniq_id, 1)
34
+ val = connection.incr(uniq_id)
35
+ assert_equal 2, val
36
+ val = connection.get(uniq_id)
37
+ assert_equal 2, val
38
+
39
+ connection.set(uniq_id, 7)
40
+ val = connection.decr(uniq_id)
41
+ assert_equal 6, val
42
+ val = connection.get(uniq_id)
43
+ assert_equal 6, val
44
+ end
45
+
46
+ def test_it_fails_to_incr_decr_missing_key
47
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
48
+
49
+ assert_raises(Couchbase::Error::NotFound) do
50
+ connection.incr(uniq_id(:missing))
51
+ end
52
+ assert_raises(Couchbase::Error::NotFound) do
53
+ connection.decr(uniq_id(:missing))
54
+ end
55
+ end
56
+
57
+ def test_it_creates_missing_key_when_initial_value_specified
58
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
59
+
60
+ val = connection.incr(uniq_id(:missing), :initial => 5)
61
+ assert_equal 5, val
62
+ val = connection.incr(uniq_id(:missing), :initial => 5)
63
+ assert_equal 6, val
64
+ val = connection.get(uniq_id(:missing))
65
+ assert_equal 6, val
66
+ end
67
+
68
+ def test_it_uses_zero_as_default_value_for_missing_keys
69
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
70
+
71
+ val = connection.incr(uniq_id(:missing), :create => true)
72
+ assert_equal 0, val
73
+ val = connection.incr(uniq_id(:missing), :create => true)
74
+ assert_equal 1, val
75
+ val = connection.get(uniq_id(:missing))
76
+ assert_equal 1, val
77
+ end
78
+
79
+ def test_it_allows_custom_ttl
80
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
81
+
82
+ val = connection.incr(uniq_id(:missing), :create => true, :ttl => 1)
83
+ assert_equal 0, val
84
+ val = connection.incr(uniq_id(:missing), :create => true)
85
+ assert_equal 1, val
86
+ sleep(2)
87
+ refute connection.get(uniq_id(:missing))
88
+ end
89
+
90
+ def test_decrement_with_absolute_ttl
91
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
92
+ # absolute TTL: one second from now
93
+ exp = Time.now.to_i + 1
94
+ val = connection.decr(uniq_id, 12, :initial => 0, :ttl => exp)
95
+ assert_equal 0, val
96
+ assert_equal 0, connection.get(uniq_id)
97
+ sleep(2)
98
+ refute connection.get(uniq_id)
99
+ end
100
+
101
+ def test_it_allows_custom_delta
102
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
103
+
104
+ connection.set(uniq_id, 12)
105
+ val = connection.incr(uniq_id, 10)
106
+ assert_equal 22, val
107
+ end
108
+
109
+ end
@@ -0,0 +1,235 @@
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::Unit::TestCase
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
+ assert obj.respond_to?(:flags)
39
+ end
40
+
41
+ def test_it_requires_block_for_running_loop
42
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
43
+ refute connection.async?
44
+ assert_raises(LocalJumpError) do
45
+ connection.run
46
+ end
47
+ connection.run do |conn|
48
+ assert conn.async?
49
+ end
50
+ end
51
+
52
+ def test_it_resets_async_flag_when_raising_exception_from_callback
53
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
54
+
55
+ assert_raises(RuntimeError) do
56
+ connection.run do |conn|
57
+ conn.set(uniq_id, "foo") { raise }
58
+ end
59
+ end
60
+ refute connection.async?
61
+ end
62
+
63
+ def test_nested_async_get_set
64
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
65
+ connection.set(uniq_id, {"bar" => 1})
66
+ connection.set(uniq_id(:hit), 0)
67
+
68
+ connection.run do |conn|
69
+ conn.get(uniq_id) do
70
+ conn.get(uniq_id(:hit)) do |res|
71
+ conn.set(uniq_id(:hit), res.value + 1)
72
+ end
73
+ end
74
+ end
75
+
76
+ val = connection.get(uniq_id(:hit))
77
+ assert_equal 1, val
78
+ end
79
+
80
+ def test_nested_async_set_get
81
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
82
+ val = nil
83
+
84
+ connection.run do |conn|
85
+ conn.set(uniq_id, "foo") do
86
+ conn.get(uniq_id) do |res|
87
+ val = res.value
88
+ end
89
+ end
90
+ end
91
+
92
+ assert_equal "foo", val
93
+ end
94
+
95
+ def test_nested_async_touch_get
96
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
97
+ connection.set(uniq_id, "foo")
98
+ success = false
99
+ val = nil
100
+
101
+ connection.run do |conn|
102
+ conn.touch(uniq_id, :ttl => 1) do |res1|
103
+ success = res1.success?
104
+ conn.get(uniq_id) do |res2|
105
+ val = res2.value
106
+ end
107
+ end
108
+ end
109
+
110
+ assert success
111
+ assert_equal "foo", val
112
+ sleep(2)
113
+ refute connection.get(uniq_id)
114
+ end
115
+
116
+ def test_nested_async_delete_get
117
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
118
+ cas = connection.set(uniq_id, "foo")
119
+ success = false
120
+ val = :unknown
121
+
122
+ connection.run do |conn|
123
+ conn.delete(uniq_id, :cas => cas) do |res1|
124
+ success = res1.success?
125
+ conn.get(uniq_id) do |res2|
126
+ val = res2.value
127
+ end
128
+ end
129
+ end
130
+
131
+ assert success
132
+ refute val
133
+ end
134
+
135
+ def test_nested_async_stats_set
136
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
137
+ stats = {}
138
+
139
+ connection.run do |conn|
140
+ conn.stats do |res1|
141
+ id = uniq_id(res1.node, res1.key)
142
+ stats[id] = false
143
+ conn.set(id, res1.value) do |res2|
144
+ stats[id] = res2.cas
145
+ end
146
+ end
147
+ end
148
+
149
+ stats.keys.each do |key|
150
+ assert stats[key].is_a?(Numeric)
151
+ end
152
+ end
153
+
154
+ def test_nested_async_flush_set
155
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
156
+ cas = connection.set(uniq_id, "foo")
157
+ res = {}
158
+
159
+ connection.run do |conn|
160
+ conn.flush do |res1|
161
+ assert res1.success?
162
+ id = uniq_id(res1.node)
163
+ res[id] = false
164
+ conn.set(id, true) do |res2|
165
+ res[id] = res2.cas
166
+ end
167
+ end
168
+ end
169
+
170
+ refute connection.get(uniq_id)
171
+ res.keys.each do |key|
172
+ assert res[key].is_a?(Numeric)
173
+ assert connection.get(key)
174
+ end
175
+ end
176
+
177
+ def test_nested_async_incr_get
178
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
179
+ cas = connection.set(uniq_id, 1)
180
+ val = nil
181
+
182
+ connection.run do |conn|
183
+ conn.incr(uniq_id) do
184
+ conn.get(uniq_id) do |res|
185
+ val = res.value
186
+ end
187
+ end
188
+ end
189
+
190
+ assert_equal 2, val
191
+ end
192
+
193
+ def test_it_doesnt_accept_callbacks_in_synchronous_mode
194
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
195
+ refute connection.async?
196
+
197
+ assert_raises(ArgumentError) { connection.add(uniq_id, "foo") {} }
198
+ assert_raises(ArgumentError) { connection.set(uniq_id, "foo") {} }
199
+ assert_raises(ArgumentError) { connection.replace(uniq_id, "foo") {} }
200
+ assert_raises(ArgumentError) { connection.get(uniq_id) {} }
201
+ assert_raises(ArgumentError) { connection.touch(uniq_id) {} }
202
+ assert_raises(ArgumentError) { connection.incr(uniq_id) {} }
203
+ assert_raises(ArgumentError) { connection.decr(uniq_id) {} }
204
+ assert_raises(ArgumentError) { connection.delete(uniq_id) {} }
205
+ assert_raises(ArgumentError) { connection.append(uniq_id, "bar") {} }
206
+ assert_raises(ArgumentError) { connection.prepend(uniq_id, "bar") {} }
207
+ assert_raises(ArgumentError) { connection.flush {} }
208
+ assert_raises(ArgumentError) { connection.stats {} }
209
+ end
210
+
211
+ def test_it_disallow_nested_run
212
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
213
+ assert_raises(Couchbase::Error::Invalid) do
214
+ connection.run do
215
+ connection.run do
216
+ end
217
+ end
218
+ end
219
+ end
220
+
221
+ def test_it_extends_timeout_in_async_mode_if_needed
222
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
223
+ connection.set(uniq_id, "foo")
224
+
225
+ connection.timeout = 100 # 100 us
226
+ connection.run do
227
+ connection.get(uniq_id) do |ret|
228
+ assert ret.success?
229
+ assert_equal "foo", ret.value
230
+ end
231
+ sleep(1.5)
232
+ end
233
+ end
234
+
235
+ end
@@ -0,0 +1,227 @@
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::Unit::TestCase
21
+
22
+ def test_it_substitute_default_parts_to_url
23
+ # with_mock(:host => 'localhost', :port => 8091, :buckets_spec => 'default,foo') do |mock|
24
+ # connections = [
25
+ # Couchbase.new,
26
+ # Couchbase.new("http://#{mock.host}:8091"),
27
+ # Couchbase.new("http://#{mock.host}:8091/pools/default"),
28
+ # Couchbase.new(:hostname => mock.host),
29
+ # Couchbase.new(:hostname => mock.host, :port => 8091)
30
+ # ]
31
+ # connections.each do |connection|
32
+ # assert_equal mock.host, connection.hostname
33
+ # assert_equal 8091, connection.port
34
+ # assert_equal "#{mock.host}:8091", connection.authority
35
+ # assert_equal 'default', connection.bucket
36
+ # assert_equal "http://#{mock.host}:8091/pools/default/buckets/default/", connection.url
37
+ # end
38
+
39
+ # connections = [
40
+ # Couchbase.new("http://#{mock.host}:8091/pools/default/buckets/foo"),
41
+ # Couchbase.new(:bucket => 'foo'),
42
+ # Couchbase.new("http://#{mock.host}:8091/pools/default/buckets/default", :bucket => 'foo')
43
+ # ]
44
+ # connections.each do |connection|
45
+ # assert_equal 'foo', connection.bucket
46
+ # assert_equal "http://#{mock.host}:8091/pools/default/buckets/foo/", connection.url
47
+ # end
48
+ # end
49
+
50
+ with_mock(:host => 'localhost') do |mock| # pick first free port
51
+ connections = [
52
+ Couchbase.new("http://#{mock.host}:#{mock.port}"),
53
+ Couchbase.new(:port => mock.port),
54
+ Couchbase.new("http://#{mock.host}:8091", :port => mock.port)
55
+ ]
56
+ connections.each do |connection|
57
+ assert_equal mock.port, connection.port
58
+ assert_equal "#{mock.host}:#{mock.port}", connection.authority
59
+ assert_equal "http://#{mock.host}:#{mock.port}/pools/default/buckets/default/", connection.url
60
+ end
61
+ end
62
+
63
+ with_mock(:host => '127.0.0.1') do |mock|
64
+ connections = [
65
+ Couchbase.new("http://#{mock.host}:#{mock.port}"),
66
+ Couchbase.new(:hostname => mock.host, :port => mock.port),
67
+ Couchbase.new('http://example.com:8091', :hostname => mock.host, :port => mock.port)
68
+ ]
69
+ connections.each do |connection|
70
+ assert_equal mock.host, connection.hostname
71
+ assert_equal "#{mock.host}:#{mock.port}", connection.authority
72
+ assert_equal "http://#{mock.host}:#{mock.port}/pools/default/buckets/default/", connection.url
73
+ end
74
+ end
75
+ end
76
+
77
+ def test_it_raises_network_error_if_server_not_found
78
+ refute(`netstat -tnl` =~ /12345/)
79
+ assert_raises Couchbase::Error::Connect do
80
+ Couchbase.new(:port => 12345)
81
+ end
82
+ end
83
+
84
+ def test_it_raises_argument_error_for_illegal_url
85
+ illegal = [
86
+ "ftp://localhost:8091/",
87
+ "http:/localhost:8091/",
88
+ ""
89
+ ]
90
+ illegal.each do |url|
91
+ assert_raises ArgumentError do
92
+ Couchbase.new(url)
93
+ end
94
+ end
95
+ end
96
+
97
+ def test_it_able_to_connect_to_protected_buckets
98
+ with_mock(:buckets_spec => 'protected:secret') do |mock|
99
+ connection = Couchbase.new(:hostname => mock.host,
100
+ :port => mock.port,
101
+ :bucket => 'protected',
102
+ :username => 'protected',
103
+ :password => 'secret')
104
+ assert_equal "protected", connection.bucket
105
+ assert_equal "protected", connection.username
106
+ assert_equal "secret", connection.password
107
+ end
108
+ end
109
+
110
+ def test_it_allows_to_specify_credentials_in_url
111
+ with_mock(:buckets_spec => 'protected:secret') do |mock|
112
+ connection = Couchbase.new("http://protected:secret@#{mock.host}:#{mock.port}/pools/default/buckets/protected/")
113
+ assert_equal "protected", connection.bucket
114
+ assert_equal "protected", connection.username
115
+ assert_equal "secret", connection.password
116
+ end
117
+ end
118
+
119
+ def test_it_raises_error_with_wrong_credentials
120
+ with_mock do |mock|
121
+ assert_raises Couchbase::Error::Auth do
122
+ Couchbase.new(:hostname => mock.host,
123
+ :port => mock.port,
124
+ :bucket => 'default',
125
+ :username => 'wrong.username',
126
+ :password => 'wrong_password')
127
+ end
128
+ end
129
+ end
130
+
131
+ def test_it_unable_to_connect_to_protected_buckets_with_wrond_credentials
132
+ with_mock(:buckets_spec => 'protected:secret') do |mock|
133
+ assert_raises Couchbase::Error::Auth do
134
+ Couchbase.new(:hostname => mock.host,
135
+ :port => mock.port,
136
+ :bucket => 'protected',
137
+ :username => 'wrong',
138
+ :password => 'secret')
139
+ end
140
+ assert_raises Couchbase::Error::Auth do
141
+ Couchbase.new(:hostname => mock.host,
142
+ :port => mock.port,
143
+ :bucket => 'protected',
144
+ :username => 'protected',
145
+ :password => 'wrong')
146
+ end
147
+ end
148
+ end
149
+
150
+ def test_it_allows_change_quiet_flag
151
+ with_mock do |mock|
152
+ connection = Couchbase.new(:hostname => mock.host,
153
+ :port => mock.port)
154
+ assert connection.quiet?
155
+
156
+ connection = Couchbase.new(:hostname => mock.host,
157
+ :port => mock.port,
158
+ :quiet => true)
159
+ assert connection.quiet?
160
+
161
+ connection.quiet = nil
162
+ assert_equal false, connection.quiet?
163
+
164
+ connection.quiet = :foo
165
+ assert_equal true, connection.quiet?
166
+ end
167
+ end
168
+
169
+ def test_it_is_connected
170
+ with_mock do |mock|
171
+ connection = Couchbase.new(:hostname => mock.host,
172
+ :port => mock.port)
173
+ assert connection.connected?
174
+ end
175
+ end
176
+
177
+ def test_it_is_possible_to_disconnect_instance
178
+ with_mock do |mock|
179
+ connection = Couchbase.new(:hostname => mock.host,
180
+ :port => mock.port)
181
+ connection.disconnect
182
+ refute connection.connected?
183
+ end
184
+ end
185
+
186
+ def test_it_raises_error_on_double_disconnect
187
+ with_mock do |mock|
188
+ connection = Couchbase.new(:hostname => mock.host,
189
+ :port => mock.port)
190
+ connection.disconnect
191
+ assert_raises Couchbase::Error::Connect do
192
+ connection.disconnect
193
+ end
194
+ end
195
+ end
196
+
197
+ def test_it_allows_to_reconnect_the_instance
198
+ with_mock do |mock|
199
+ connection = Couchbase.new(:hostname => mock.host,
200
+ :port => mock.port)
201
+ connection.disconnect
202
+ refute connection.connected?
203
+ connection.reconnect
204
+ assert connection.connected?
205
+ assert connection.set(uniq_id, "foo")
206
+ end
207
+ end
208
+
209
+ def test_it_allows_to_change_configuration_during_reconnect
210
+ with_mock(:buckets_spec => 'protected:secret') do |mock|
211
+ connection = Couchbase.new(:hostname => mock.host,
212
+ :port => mock.port,
213
+ :bucket => 'protected',
214
+ :username => 'protected',
215
+ :password => 'secret')
216
+ connection.disconnect
217
+ assert_raises Couchbase::Error::Auth do
218
+ connection.reconnect(:password => 'incorrect')
219
+ end
220
+ refute connection.connected?
221
+
222
+ connection.reconnect(:password => 'secret')
223
+ assert connection.connected?
224
+ end
225
+ end
226
+
227
+ end