/
helper.rb
132 lines (113 loc) · 3.08 KB
/
helper.rb
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
# -- coding: utf-8
=begin
!!!!!!!!!!!!!
!! CAUTION !!
!!!!!!!!!!!!!
This script access http://0.0.0.0:19999/ and destroy all records.
Be carefully for run, and run `ktserver -port 19999 '*'` before testing.
=end
$LOAD_PATH.unshift(File.dirname(__FILE__) + "/../lib")
require "rubygems"
require "kyototycoon.rb"
describe do
before(:all) do
@kt = KyotoTycoon.new('0.0.0.0', 19999)
@kt.serializer=:default # or :msgpack
@kt.logger=nil
end
before(:each) do
@kt.clear
end
it 'should handle multi servers' do
kt = KyotoTycoon.new('www.example.com', 11111)
kt.connect_timeout = 0.1
kt.servers << ['example.net', 1978]
kt.servers << ['0.0.0.0', 19999]
kt['foo'] = 'bar'
kt[:foo].should == 'bar'
end
it 'should provide simple kvs feature' do
@kt.set('a', 'b')
@kt.get('a').should == 'b'
@kt['foo'] = 'bar'
@kt['foo'].should == 'bar'
@kt.delete('foo')
@kt['foo'].should be_nil
@kt.clear
@kt.report['db_total_count'].to_i.should == 0
@kt.status['count'].to_i.should == 0
@kt['foo'] = 'oldbaz'
@kt.cas('foo', 'oldbaz', 'newbaz').should be_true
@kt.cas('foo', 'oldbaz', 'newbaz').should be_false
@kt.get('foo').should == 'newbaz'
@kt.clear
@kt['foo'] ||= 'aaa'
@kt['foo'] ||= 'bbb'
@kt['foo'].should == 'aaa'
@kt.clear
@kt[:a] = 1
@kt[:b] = 1
@kt.keys.sort.should == %w!a b!.sort
end
it 'should provide bulk' do
data = {}
receive = {}
10.times{|n|
data[n.to_s] = n.to_s
receive["_#{n}"] = n.to_s
}
receive['num'] = "10"
@kt.set_bulk(data)
@kt.get_bulk(data.keys).sort.should == receive.sort
@kt.remove_bulk(data.keys)
@kt.get_bulk(data.keys).should == {'num' => '0'}
end
it 'should can handle strange key/value' do
# '+' is known ambiguity key on URL encode/decode processing
%w!a\tb a\nb a\r\nb a*-b a^@b!.each{|outlaw|
@kt[outlaw] = outlaw
@kt[outlaw].should == outlaw
}
end
it 'should provide delete variation' do
build = lambda {|kt|
kt.clear
kt.set_bulk({
:a => 1,
:b => 1,
:c => 1,
})
}
build.call(@kt)
@kt.delete('a','b')
@kt.keys.should == ['c']
build.call(@kt)
@kt.delete(['a', 'b'])
@kt.keys.should == ['c']
end
it 'should increment' do
@kt.increment('foo').should == 1
@kt.increment('foo').should == 2
@kt.increment('foo').should == 3
@kt.increment('foo', 10).should == 13
@kt.increment('foo', -10).should == 3
end
it 'should provide status/report' do
@kt[:a] = 1
@kt[:b] = 2
@kt.report['db_total_count'].to_i.should == 2
@kt.status['count'].to_i.should == 2
end
it 'should match prefixes' do
@kt['123'] = 1
@kt['124'] = 1
@kt['125'] = 1
@kt['999'] = 1
@kt['9999'] = 1
@kt.match_prefix("12").sort.should == %w!123 124 125!.sort
@kt.match_prefix("9").sort.should == %w!999 9999!.sort
@kt.match_prefix("9999").sort.should == %w!9999!
@kt.match_regex(/^12/).sort.should == %w!123 124 125!.sort
@kt.match_regex(/^9+$/).sort.should == %w!999 9999!.sort
end
end