dalli 2.7.2 → 2.7.3

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of dalli might be problematic. Click here for more details.

@@ -4,7 +4,7 @@ require 'memcached_mock'
4
4
  describe 'Dalli::Cas::Client' do
5
5
  describe 'using a live server' do
6
6
  it 'supports get with CAS' do
7
- memcached_cas do |dc|
7
+ memcached_cas_persistent do |dc|
8
8
  dc.flush
9
9
 
10
10
  expected = { 'blah' => 'blerg!' }
@@ -29,7 +29,7 @@ describe 'Dalli::Cas::Client' do
29
29
  end
30
30
 
31
31
  it 'supports multi-get with CAS' do
32
- memcached_cas do |dc|
32
+ memcached_cas_persistent do |dc|
33
33
  dc.close
34
34
  dc.flush
35
35
 
@@ -41,14 +41,14 @@ describe 'Dalli::Cas::Client' do
41
41
  # Invocation without block
42
42
  resp = dc.get_multi_cas(%w(a b c d e f))
43
43
  resp.each_pair do |k, data|
44
- value, cas = [data.first, data.second]
44
+ value, cas = [data.first, data[1]]
45
45
  assert_equal expected_hash[k], value
46
46
  assert(cas && cas != 0)
47
47
  end
48
48
 
49
49
  # Invocation with block
50
50
  dc.get_multi_cas(%w(a b c d e f)) do |k, data|
51
- value, cas = [data.first, data.second]
51
+ value, cas = [data.first, data[1]]
52
52
  assert_equal expected_hash[k], value
53
53
  assert(cas && cas != 0)
54
54
  end
@@ -56,7 +56,7 @@ describe 'Dalli::Cas::Client' do
56
56
  end
57
57
 
58
58
  it 'supports replace-with-CAS operation' do
59
- memcached_cas do |dc|
59
+ memcached_cas_persistent do |dc|
60
60
  dc.flush
61
61
  cas = dc.set('key', 'value')
62
62
 
@@ -69,7 +69,7 @@ describe 'Dalli::Cas::Client' do
69
69
  end
70
70
 
71
71
  it 'supports delete with CAS' do
72
- memcached_cas do |dc|
72
+ memcached_cas_persistent do |dc|
73
73
  cas = dc.set('some_key', 'some_value')
74
74
  dc.delete_cas('some_key', cas)
75
75
  assert_nil dc.get('some_key')
@@ -77,7 +77,7 @@ describe 'Dalli::Cas::Client' do
77
77
  end
78
78
 
79
79
  it 'handles CAS round-trip operations' do
80
- memcached_cas do |dc|
80
+ memcached_cas_persistent do |dc|
81
81
  dc.flush
82
82
 
83
83
  expected = {'blah' => 'blerg!'}
@@ -16,15 +16,14 @@ end
16
16
  describe 'Compressor' do
17
17
 
18
18
  it 'default to Dalli::Compressor' do
19
- memcached_kill(29199) do |dc|
20
- memcache = Dalli::Client.new('127.0.0.1:29199')
21
- memcache.set 1,2
22
- assert_equal Dalli::Compressor, memcache.instance_variable_get('@ring').servers.first.compressor
19
+ memcached(29199) do |dc|
20
+ dc.set 1,2
21
+ assert_equal Dalli::Compressor, dc.instance_variable_get('@ring').servers.first.compressor
23
22
  end
24
23
  end
25
24
 
26
25
  it 'support a custom compressor' do
27
- memcached_kill(29199) do |dc|
26
+ memcached(29199) do |dc|
28
27
  memcache = Dalli::Client.new('127.0.0.1:29199', :compressor => NoopCompressor)
29
28
  memcache.set 1,2
30
29
  begin
@@ -37,7 +37,7 @@ describe 'Dalli' do
37
37
 
38
38
  describe 'key validation' do
39
39
  it 'not allow blanks' do
40
- memcached do |dc|
40
+ memcached_persistent do |dc|
41
41
  dc.set ' ', 1
42
42
  assert_equal 1, dc.get(' ')
43
43
  dc.set "\t", 1
@@ -54,7 +54,8 @@ describe 'Dalli' do
54
54
  end
55
55
 
56
56
  it 'allow namespace to be a symbol' do
57
- memcached(19122, '', :namespace => :wunderschoen) do |dc|
57
+ memcached_persistent do |dc, port|
58
+ dc = Dalli::Client.new("localhost:#{port}", :namespace => :wunderschoen)
58
59
  dc.set "x" * 251, 1
59
60
  assert 1, dc.get("#{'x' * 200}:md5:#{Digest::MD5.hexdigest('x' * 251)}")
60
61
  end
@@ -105,7 +106,7 @@ describe 'Dalli' do
105
106
  describe 'using a live server' do
106
107
 
107
108
  it "support get/set" do
108
- memcached do |dc|
109
+ memcached_persistent do |dc|
109
110
  dc.flush
110
111
 
111
112
  val1 = "1234567890"*105000
@@ -122,7 +123,7 @@ describe 'Dalli' do
122
123
  end
123
124
 
124
125
  it 'supports delete' do
125
- memcached do |dc|
126
+ memcached_persistent do |dc|
126
127
  dc.set('some_key', 'some_value')
127
128
  assert_equal 'some_value', dc.get('some_key')
128
129
 
@@ -132,21 +133,22 @@ describe 'Dalli' do
132
133
  end
133
134
 
134
135
  it 'returns nil for nonexist key' do
135
- memcached do |dc|
136
+ memcached_persistent do |dc|
136
137
  assert_equal nil, dc.get('notexist')
137
138
  end
138
139
  end
139
140
 
140
141
  it 'allows "Not found" as value' do
141
- memcached do |dc|
142
+ memcached_persistent do |dc|
142
143
  dc.set('key1', 'Not found')
143
144
  assert_equal 'Not found', dc.get('key1')
144
145
  end
145
146
  end
146
147
 
147
148
  it "support stats" do
148
- memcached do |dc|
149
+ memcached_persistent do |dc|
149
150
  # make sure that get_hits would not equal 0
151
+ dc.set(:a, "1234567890"*100000)
150
152
  dc.get(:a)
151
153
 
152
154
  stats = dc.stats
@@ -185,7 +187,7 @@ describe 'Dalli' do
185
187
  end
186
188
 
187
189
  it "support the fetch operation" do
188
- memcached do |dc|
190
+ memcached_persistent do |dc|
189
191
  dc.flush
190
192
 
191
193
  expected = { 'blah' => 'blerg!' }
@@ -208,7 +210,7 @@ describe 'Dalli' do
208
210
  end
209
211
 
210
212
  it "support the fetch operation with falsey values" do
211
- memcached do |dc|
213
+ memcached_persistent do |dc|
212
214
  dc.flush
213
215
 
214
216
  dc.set("fetch_key", false)
@@ -222,7 +224,7 @@ describe 'Dalli' do
222
224
  end
223
225
 
224
226
  it "support the cas operation" do
225
- memcached do |dc|
227
+ memcached_persistent do |dc|
226
228
  dc.flush
227
229
 
228
230
  expected = { 'blah' => 'blerg!' }
@@ -246,7 +248,7 @@ describe 'Dalli' do
246
248
  end
247
249
 
248
250
  it "support multi-get" do
249
- memcached do |dc|
251
+ memcached_persistent do |dc|
250
252
  dc.close
251
253
  dc.flush
252
254
  resp = dc.get_multi(%w(a b c d e f))
@@ -283,7 +285,7 @@ describe 'Dalli' do
283
285
  end
284
286
 
285
287
  it 'support raw incr/decr' do
286
- memcached do |client|
288
+ memcached_persistent do |client|
287
289
  client.flush
288
290
 
289
291
  assert op_addset_succeeds(client.set('fakecounter', 0, 0, :raw => true))
@@ -313,7 +315,7 @@ describe 'Dalli' do
313
315
  end
314
316
 
315
317
  it "support incr/decr operations" do
316
- memcached do |dc|
318
+ memcached_persistent do |dc|
317
319
  dc.flush
318
320
 
319
321
  resp = dc.decr('counter', 100, 5, 0)
@@ -361,7 +363,7 @@ describe 'Dalli' do
361
363
  end
362
364
 
363
365
  it 'support the append and prepend operations' do
364
- memcached do |dc|
366
+ memcached_persistent do |dc|
365
367
  dc.flush
366
368
  assert op_addset_succeeds(dc.set('456', 'xyz', 0, :raw => true))
367
369
  assert_equal true, dc.prepend('456', '0')
@@ -375,7 +377,7 @@ describe 'Dalli' do
375
377
  end
376
378
 
377
379
  it 'supports replace operation' do
378
- memcached do |dc|
380
+ memcached_persistent do |dc|
379
381
  dc.flush
380
382
  dc.set('key', 'value')
381
383
  assert op_replace_succeeds(dc.replace('key', 'value2'))
@@ -385,7 +387,7 @@ describe 'Dalli' do
385
387
  end
386
388
 
387
389
  it 'support touch operation' do
388
- memcached do |dc|
390
+ memcached_persistent do |dc|
389
391
  begin
390
392
  dc.flush
391
393
  dc.set 'key', 'value'
@@ -401,7 +403,7 @@ describe 'Dalli' do
401
403
  end
402
404
 
403
405
  it 'support version operation' do
404
- memcached do |dc|
406
+ memcached_persistent do |dc|
405
407
  v = dc.version
406
408
  servers = v.keys
407
409
  assert(servers.any? do |s|
@@ -411,7 +413,8 @@ describe 'Dalli' do
411
413
  end
412
414
 
413
415
  it 'allow TCP connections to be configured for keepalive' do
414
- memcached(19122, '', :keepalive => true) do |dc|
416
+ memcached_persistent do |dc, port|
417
+ dc = Dalli::Client.new("localhost:#{port}", :keepalive => true)
415
418
  dc.set(:a, 1)
416
419
  ring = dc.send(:ring)
417
420
  server = ring.servers.first
@@ -425,7 +428,7 @@ describe 'Dalli' do
425
428
  end
426
429
 
427
430
  it "pass a simple smoke test" do
428
- memcached do |dc|
431
+ memcached_persistent do |dc, port|
429
432
  resp = dc.flush
430
433
  refute_nil resp
431
434
  assert_equal [true, true], resp
@@ -453,7 +456,61 @@ describe 'Dalli' do
453
456
  dc.close
454
457
  dc = nil
455
458
 
456
- dc = Dalli::Client.new('localhost:19122')
459
+ dc = Dalli::Client.new("localhost:#{port}")
460
+
461
+ assert op_addset_succeeds(dc.set('456', 'xyz', 0, :raw => true))
462
+
463
+ resp = dc.prepend '456', '0'
464
+ assert_equal true, resp
465
+
466
+ resp = dc.append '456', '9'
467
+ assert_equal true, resp
468
+
469
+ resp = dc.get('456', :raw => true)
470
+ assert_equal '0xyz9', resp
471
+
472
+ assert op_addset_succeeds(dc.set('456', false))
473
+
474
+ resp = dc.get('456')
475
+ assert_equal false, resp
476
+
477
+ resp = dc.stats
478
+ assert_equal Hash, resp.class
479
+
480
+ dc.close
481
+ end
482
+ end
483
+
484
+ it "pass a simple smoke test on unix socket" do
485
+ memcached_persistent(MemcachedMock::UNIX_SOCKET_PATH) do |dc, path|
486
+ resp = dc.flush
487
+ refute_nil resp
488
+ assert_equal [true], resp
489
+
490
+ assert op_addset_succeeds(dc.set(:foo, 'bar'))
491
+ assert_equal 'bar', dc.get(:foo)
492
+
493
+ resp = dc.get('123')
494
+ assert_equal nil, resp
495
+
496
+ assert op_addset_succeeds(dc.set('123', 'xyz'))
497
+
498
+ resp = dc.get('123')
499
+ assert_equal 'xyz', resp
500
+
501
+ assert op_addset_succeeds(dc.set('123', 'abc'))
502
+
503
+ dc.prepend('123', '0')
504
+ dc.append('123', '0')
505
+
506
+ assert_raises Dalli::UnmarshalError do
507
+ resp = dc.get('123')
508
+ end
509
+
510
+ dc.close
511
+ dc = nil
512
+
513
+ dc = Dalli::Client.new(path)
457
514
 
458
515
  assert op_addset_succeeds(dc.set('456', 'xyz', 0, :raw => true))
459
516
 
@@ -479,7 +536,7 @@ describe 'Dalli' do
479
536
  end
480
537
 
481
538
  it "support multithreaded access" do
482
- memcached do |cache|
539
+ memcached_persistent do |cache|
483
540
  cache.flush
484
541
  workers = []
485
542
 
@@ -522,10 +579,10 @@ describe 'Dalli' do
522
579
  end
523
580
 
524
581
  it "handle namespaced keys" do
525
- memcached do |dc|
526
- dc = Dalli::Client.new('localhost:19122', :namespace => 'a')
582
+ memcached_persistent do |dc, port|
583
+ dc = Dalli::Client.new("localhost:#{port}", :namespace => 'a')
527
584
  dc.set('namespaced', 1)
528
- dc2 = Dalli::Client.new('localhost:19122', :namespace => 'b')
585
+ dc2 = Dalli::Client.new("localhost:#{port}", :namespace => 'b')
529
586
  dc2.set('namespaced', 2)
530
587
  assert_equal 1, dc.get('namespaced')
531
588
  assert_equal 2, dc2.get('namespaced')
@@ -533,15 +590,15 @@ describe 'Dalli' do
533
590
  end
534
591
 
535
592
  it "handle nil namespace" do
536
- memcached do |dc|
537
- dc = Dalli::Client.new('localhost:19122', :namespace => nil)
593
+ memcached_persistent do |dc, port|
594
+ dc = Dalli::Client.new("localhost:#{port}", :namespace => nil)
538
595
  assert_equal 'key', dc.send(:validate_key, 'key')
539
596
  end
540
597
  end
541
598
 
542
599
  it 'truncate cache keys that are too long' do
543
- memcached do
544
- dc = Dalli::Client.new('localhost:19122', :namespace => 'some:namspace')
600
+ memcached_persistent do |dc, port|
601
+ dc = Dalli::Client.new("localhost:#{port}", :namespace => 'some:namspace')
545
602
  key = "this cache key is far too long so it must be hashed and truncated and stuff" * 10
546
603
  value = "some value"
547
604
  assert op_addset_succeeds(dc.set(key, value))
@@ -550,8 +607,8 @@ describe 'Dalli' do
550
607
  end
551
608
 
552
609
  it "handle namespaced keys in multi_get" do
553
- memcached do |dc|
554
- dc = Dalli::Client.new('localhost:19122', :namespace => 'a')
610
+ memcached_persistent do |dc, port|
611
+ dc = Dalli::Client.new("localhost:#{port}", :namespace => 'a')
555
612
  dc.set('a', 1)
556
613
  dc.set('b', 2)
557
614
  assert_equal({'a' => 1, 'b' => 2}, dc.get_multi('a', 'b'))
@@ -559,7 +616,7 @@ describe 'Dalli' do
559
616
  end
560
617
 
561
618
  it "handle application marshalling issues" do
562
- memcached do |dc|
619
+ memcached_persistent do |dc|
563
620
  old = Dalli.logger
564
621
  Dalli.logger = Logger.new(nil)
565
622
  begin
@@ -572,7 +629,7 @@ describe 'Dalli' do
572
629
 
573
630
  describe 'with compression' do
574
631
  it 'allow large values' do
575
- memcached do |dc|
632
+ memcached_persistent do |dc|
576
633
  dalli = Dalli::Client.new(dc.instance_variable_get(:@servers), :compress => true)
577
634
 
578
635
  value = "0"*1024*1024
@@ -585,7 +642,7 @@ describe 'Dalli' do
585
642
  describe 'in low memory conditions' do
586
643
 
587
644
  it 'handle error response correctly' do
588
- memcached(19125, '-m 1 -M') do |dc|
645
+ memcached_low_mem_persistent do |dc|
589
646
  failed = false
590
647
  value = "1234567890"*100
591
648
  1_000.times do |idx|
@@ -602,8 +659,8 @@ describe 'Dalli' do
602
659
  end
603
660
 
604
661
  it 'fit more values with compression' do
605
- memcached(19126, '-m 1 -M') do |dc|
606
- dalli = Dalli::Client.new('localhost:19126', :compress => true)
662
+ memcached_low_mem_persistent do |dc, port|
663
+ dalli = Dalli::Client.new("localhost:#{port}", :compress => true)
607
664
  failed = false
608
665
  value = "1234567890"*1000
609
666
  10_000.times do |idx|
@@ -6,7 +6,7 @@ describe 'Encoding' do
6
6
 
7
7
  describe 'using a live server' do
8
8
  it 'support i18n content' do
9
- memcached do |dc|
9
+ memcached_persistent do |dc|
10
10
  key = 'foo'
11
11
  utf_key = utf8 = 'ƒ©åÍÎ'
12
12
 
@@ -19,7 +19,7 @@ describe 'Encoding' do
19
19
  end
20
20
 
21
21
  it 'support content expiry' do
22
- memcached do |dc|
22
+ memcached_persistent do |dc|
23
23
  key = 'foo'
24
24
  assert dc.set(key, 'bar', 1)
25
25
  assert_equal 'bar', dc.get(key)
@@ -4,19 +4,20 @@ describe 'failover' do
4
4
 
5
5
  describe 'timeouts' do
6
6
  it 'not lead to corrupt sockets' do
7
- memcached(29125) do
8
- dc = Dalli::Client.new ['localhost:29125']
7
+ memcached_persistent do |dc|
8
+ value = {:test => "123"}
9
9
  begin
10
10
  Timeout.timeout 0.01 do
11
- 1_000.times do
12
- dc.set("test_123", {:test => "123"})
11
+ start_time = Time.now
12
+ 10_000.times do
13
+ dc.set("test_123", value)
13
14
  end
14
- flunk("Did not timeout")
15
+ flunk("Did not timeout in #{Time.now - start_time}")
15
16
  end
16
17
  rescue Timeout::Error
17
18
  end
18
19
 
19
- assert_equal({:test => '123'}, dc.get("test_123"))
20
+ assert_equal(value, dc.get("test_123"))
20
21
  end
21
22
  end
22
23
  end
@@ -25,38 +26,40 @@ describe 'failover' do
25
26
  describe 'assuming some bad servers' do
26
27
 
27
28
  it 'silently reconnect if server hiccups' do
28
- memcached(29125) do
29
- dc = Dalli::Client.new ['localhost:29125']
29
+ server_port = 30124
30
+ memcached_persistent(server_port) do |dc, port|
30
31
  dc.set 'foo', 'bar'
31
32
  foo = dc.get 'foo'
32
33
  assert_equal foo, 'bar'
33
34
 
34
- memcached_kill(29125)
35
- memcached(29125) do
35
+ memcached_kill(port)
36
+ memcached_persistent(port) do
36
37
 
37
38
  foo = dc.get 'foo'
38
39
  assert_nil foo
39
40
 
40
- memcached_kill(29125)
41
+ memcached_kill(port)
41
42
  end
42
43
  end
43
44
  end
44
45
 
45
46
  it 'handle graceful failover' do
46
- memcached(29125) do
47
- memcached(29126) do
48
- dc = Dalli::Client.new ['localhost:29125', 'localhost:29126']
47
+ port_1 = 31777
48
+ port_2 = 32113
49
+ memcached_persistent(port_1) do |first_dc, first_port|
50
+ memcached_persistent(port_2) do |second_dc, second_port|
51
+ dc = Dalli::Client.new ["localhost:#{first_port}", "localhost:#{second_port}"]
49
52
  dc.set 'foo', 'bar'
50
53
  foo = dc.get 'foo'
51
54
  assert_equal foo, 'bar'
52
55
 
53
- memcached_kill(29125)
56
+ memcached_kill(first_port)
54
57
 
55
58
  dc.set 'foo', 'bar'
56
59
  foo = dc.get 'foo'
57
60
  assert_equal foo, 'bar'
58
61
 
59
- memcached_kill(29126)
62
+ memcached_kill(second_port)
60
63
 
61
64
  assert_raises Dalli::RingError, :message => "No server available" do
62
65
  dc.set 'foo', 'bar'
@@ -66,14 +69,16 @@ describe 'failover' do
66
69
  end
67
70
 
68
71
  it 'handle them gracefully in get_multi' do
69
- memcached(29125) do
70
- memcached(29126) do
71
- dc = Dalli::Client.new ['localhost:29125', 'localhost:29126']
72
+ port_1 = 32971
73
+ port_2 = 34312
74
+ memcached_persistent(port_1) do |first_dc, first_port|
75
+ memcached(port_2) do |second_dc, second_port|
76
+ dc = Dalli::Client.new ["localhost:#{first_port}", "localhost:#{second_port}"]
72
77
  dc.set 'a', 'a1'
73
78
  result = dc.get_multi ['a']
74
79
  assert_equal result, {'a' => 'a1'}
75
80
 
76
- memcached_kill(29125)
81
+ memcached_kill(first_port)
77
82
 
78
83
  result = dc.get_multi ['a']
79
84
  assert_equal result, {'a' => 'a1'}
@@ -82,22 +87,24 @@ describe 'failover' do
82
87
  end
83
88
 
84
89
  it 'handle graceful failover in get_multi' do
85
- memcached(29125) do
86
- memcached(29126) do
87
- dc = Dalli::Client.new ['localhost:29125', 'localhost:29126']
90
+ port_1 = 34541
91
+ port_2 = 33044
92
+ memcached_persistent(port_1) do |first_dc, first_port|
93
+ memcached_persistent(port_2) do |second_dc, second_port|
94
+ dc = Dalli::Client.new ["localhost:#{first_port}", "localhost:#{second_port}"]
88
95
  dc.set 'foo', 'foo1'
89
96
  dc.set 'bar', 'bar1'
90
97
  result = dc.get_multi ['foo', 'bar']
91
98
  assert_equal result, {'foo' => 'foo1', 'bar' => 'bar1'}
92
99
 
93
- memcached_kill(29125)
100
+ memcached_kill(first_port)
94
101
 
95
102
  dc.set 'foo', 'foo1'
96
103
  dc.set 'bar', 'bar1'
97
104
  result = dc.get_multi ['foo', 'bar']
98
105
  assert_equal result, {'foo' => 'foo1', 'bar' => 'bar1'}
99
106
 
100
- memcached_kill(29126)
107
+ memcached_kill(second_port)
101
108
 
102
109
  result = dc.get_multi ['foo', 'bar']
103
110
  assert_equal result, {}
@@ -106,21 +113,23 @@ describe 'failover' do
106
113
  end
107
114
 
108
115
  it 'stats it still properly report' do
109
- memcached(29125) do
110
- memcached(29126) do
111
- dc = Dalli::Client.new ['localhost:29125', 'localhost:29126']
116
+ port_1 = 34547
117
+ port_2 = 33219
118
+ memcached_persistent(port_1) do |first_dc, first_port|
119
+ memcached_persistent(port_2) do |second_dc, second_port|
120
+ dc = Dalli::Client.new ["localhost:#{first_port}", "localhost:#{second_port}"]
112
121
  result = dc.stats
113
- assert_instance_of Hash, result['localhost:29125']
114
- assert_instance_of Hash, result['localhost:29126']
122
+ assert_instance_of Hash, result["localhost:#{first_port}"]
123
+ assert_instance_of Hash, result["localhost:#{second_port}"]
115
124
 
116
- memcached_kill(29125)
125
+ memcached_kill(first_port)
117
126
 
118
- dc = Dalli::Client.new ['localhost:29125', 'localhost:29126']
127
+ dc = Dalli::Client.new ["localhost:#{first_port}", "localhost:#{second_port}"]
119
128
  result = dc.stats
120
- assert_instance_of NilClass, result['localhost:29125']
121
- assert_instance_of Hash, result['localhost:29126']
129
+ assert_instance_of NilClass, result["localhost:#{first_port}"]
130
+ assert_instance_of Hash, result["localhost:#{second_port}"]
122
131
 
123
- memcached_kill(29126)
132
+ memcached_kill(second_port)
124
133
  end
125
134
  end
126
135
  end