File: connection_checkout_event_callback_spec.rb

package info (click to toggle)
ruby-sequel 5.102.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,328 kB
  • sloc: ruby: 124,743; makefile: 3
file content (74 lines) | stat: -rw-r--r-- 2,029 bytes parent folder | download
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
require_relative "spec_helper"

describe "connection_checkout_event_callback extension" do
  it "should error if using an unsupported connection pool" do
    db = Sequel.mock(:pool_class => :single)
    proc{db.extension(:connection_checkout_event_callback)}.must_raise Sequel::Error
  end
end 

[true, false].each do |sharded|
  describe "connection_checkout_event_callback extension with #{"sharded_" if sharded}timed_queue connection pool" do
    if sharded
      def wrap(x)
        [x, :default]
      end
    else
      def wrap(x)
        x
      end
    end

    it "should issue expected events" do
      opts = {max_connections: 1}
      opts[:servers] = {:a=>{}} if sharded
      db = Sequel.mock(opts)
      db.extension(:connection_checkout_event_callback)

      # Test that default callback does not break anything
      db.synchronize{}
      db.disconnect

      events = []
      if sharded
        db.pool.on_checkout_event = proc{|*event| events << event}
      else
        db.pool.on_checkout_event = proc{|event| events << event}
      end
    
      db.synchronize{}
      events.must_equal [wrap(:not_immediately_available), wrap(:new_connection)]
      events.clear

      q = Queue.new
      q2 = Queue.new
      db_thread = proc do
        db.synchronize do
          q2.push(true)
          _(q.pop(timeout: 1)).must_equal true
        end
        true
      end

      t = Thread.new(&db_thread)
      q2.pop(timeout: 1).must_equal true
      events.must_equal [wrap(:immediately_available)]
      events.clear

      t2 = Thread.new(&db_thread)
      sleep 0.001 until db.pool.num_waiting > 0
      q.push(true)
      q2.pop(timeout: 1).must_equal true
      q.push(true)
      events[0].must_equal wrap(:not_immediately_available)
      if sharded
        events[1][0].must_be :<, 1
        events[1][1].must_equal :default
      else
        events[1].must_be :<, 1
      end
      t.join(1).value.must_equal true
      t2.join(1).value.must_equal true
    end
  end
end if RUBY_VERSION >= "3.2"