File: concurrent_eager_loading_spec.rb

package info (click to toggle)
ruby-sequel 5.101.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,312 kB
  • sloc: ruby: 124,594; makefile: 3
file content (108 lines) | stat: -rw-r--r-- 3,217 bytes parent folder | download | duplicates (3)
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
require_relative 'spec_helper'

describe "concurrent_eager_loading plugin" do
  before do
    @db = Sequel.mock(:extensions=>'async_thread_pool', :fetch=>{:id=>1}, :keep_reference=>false, :num_async_threads=>2, :numrows=>1)
    @C = Class.new(Sequel::Model)
    @C.columns :id
    @C.set_dataset(@db[:cs])
    @C.plugin :concurrent_eager_loading
    @db.sqls
  end

  it 'should eager load concurrently if configured' do
    m2o_thread = nil
    o2m_thread = nil
    m2o_mutex = nil
    o2m_mutex = nil
    q1 = Queue.new
    q2 = Queue.new
    @C.many_to_one :c, :key=>:id, :class=>@C, :eager_loader=>(proc do |eo|
      m2o_thread = Thread.current
      if m2o_mutex = eo[:mutex]
        q2.push(nil)
        q1.pop
      end
    end)
    @C.one_to_many :cs, :key=>:id, :class=>@C, :eager_loader=>(proc do |eo|
      o2m_thread = Thread.current
      if o2m_mutex = eo[:mutex]
        q1.push(nil)
        q2.pop
      end
    end)

    @C.eager(:c, :cs).all
    m2o_thread.must_equal Thread.current
    o2m_thread.must_equal Thread.current
    m2o_mutex.must_be_nil
    o2m_mutex.must_be_nil

    @C.eager_load_serially.eager(:c, :cs).all
    m2o_thread.must_equal Thread.current
    o2m_thread.must_equal Thread.current
    m2o_mutex.must_be_nil
    o2m_mutex.must_be_nil

    @C.eager_load_concurrently.eager(:c, :cs).all
    m2o_thread.wont_equal Thread.current
    o2m_thread.wont_equal Thread.current
    o2m_thread.wont_equal m2o_thread
    m2o_mutex.wont_be_nil
    o2m_mutex.must_equal m2o_mutex

    @C.eager_load_concurrently.eager_load_serially.eager(:c, :cs).all
    m2o_thread.must_equal Thread.current
    o2m_thread.must_equal Thread.current
    m2o_mutex.must_be_nil
    o2m_mutex.must_be_nil

    @C.eager_load_serially.eager_load_concurrently.eager(:c, :cs).all
    m2o_thread.wont_equal Thread.current
    o2m_thread.wont_equal Thread.current
    o2m_thread.wont_equal m2o_thread
    m2o_mutex.wont_be_nil
    o2m_mutex.must_equal m2o_mutex

    @C.plugin :concurrent_eager_loading, :always=>true
    @C.eager(:c, :cs).all
    m2o_thread.wont_equal Thread.current
    o2m_thread.wont_equal Thread.current
    o2m_thread.wont_equal m2o_thread
    m2o_mutex.wont_be_nil
    o2m_mutex.must_equal m2o_mutex

    @C.eager_load_serially.eager(:c, :cs).all
    m2o_thread.must_equal Thread.current
    o2m_thread.must_equal Thread.current
    m2o_mutex.must_be_nil
    o2m_mutex.must_be_nil

    m2o_thread = nil
    @C.eager(:c).all
    m2o_thread.must_equal Thread.current
    m2o_mutex.must_be_nil

    o2m_thread = nil
    @C.eager(:cs).all
    o2m_thread.must_equal Thread.current
    o2m_mutex.must_be_nil

    vs = []
    @C.eager(:c, :cs).
      with_extend{define_method(:perform_eager_load) do |*a| vs << Struct.new(:wrapped).new(super(*a)) end}.all
    vs.map{|v| v.wrapped.__value}
    m2o_thread.wont_equal Thread.current
    o2m_thread.wont_equal Thread.current
    o2m_thread.wont_equal m2o_thread
    m2o_mutex.wont_be_nil
    o2m_mutex.must_equal m2o_mutex

    Class.new(@C).eager(:c, :cs).all
    m2o_thread.wont_equal Thread.current
    o2m_thread.wont_equal Thread.current
    o2m_thread.wont_equal m2o_thread
    m2o_mutex.wont_be_nil
    o2m_mutex.must_equal m2o_mutex
  end
end