File: mysql2_spec.rb

package info (click to toggle)
ruby-em-synchrony 1.0.5-3.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 572 kB
  • sloc: ruby: 3,458; sh: 37; sql: 7; makefile: 2
file content (123 lines) | stat: -rw-r--r-- 3,059 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
require "spec/helper/all"
require "em-synchrony/mysql2"

describe Mysql2::EM::Client do

  DELAY = 0.25
  QUERY = "SELECT sleep(#{DELAY}) as mysql2_query"

  it "should support queries" do
    res = []
    EventMachine.synchrony do
      db = Mysql2::EM::Client.new
      res = db.query QUERY

      EventMachine.stop
    end

    res.first.keys.should include("mysql2_query")
  end

  it "should fire sequential, synchronous requests" do
    EventMachine.synchrony do
      db = Mysql2::EM::Client.new

      start = now
      res = []

      res.push db.query QUERY
      res.push db.query QUERY
      (now - start.to_f).should be_within(DELAY * res.size * 0.15).of(DELAY * res.size)

      EventMachine.stop
    end
  end

  it "should have accept a callback, errback on async queries" do
    EventMachine.synchrony do
      db = Mysql2::EM::Client.new

      res = db.aquery(QUERY)
      res.errback {|r| fail }
      res.callback {|r|
        r.size.should == 1
        EventMachine.stop
      }
    end
  end

  it "should fire simultaneous requests via Multi interface" do
    EventMachine.synchrony do

      db = EventMachine::Synchrony::ConnectionPool.new(size: 2) do
        Mysql2::EM::Client.new
      end

      start = now

      multi = EventMachine::Synchrony::Multi.new
      multi.add :a, db.aquery(QUERY)
      multi.add :b, db.aquery(QUERY)
      res = multi.perform

      (now - start.to_f).should be_within(DELAY * 0.15).of(DELAY)
      res.responses[:callback].size.should == 2
      res.responses[:errback].size.should == 0

      EventMachine.stop
    end
  end

  it "should fire sequential and simultaneous MySQL requests" do
    EventMachine.synchrony do
      db = EventMachine::Synchrony::ConnectionPool.new(size: 3) do
        Mysql2::EM::Client.new
      end

      start = now
      res = []

      res.push db.query(QUERY)
      res.push db.query(QUERY)
      (now - start.to_f).should be_within(DELAY * res.size * 0.15).of(DELAY * res.size)

      start = now

      multi = EventMachine::Synchrony::Multi.new
      multi.add :a, db.aquery(QUERY)
      multi.add :b, db.aquery(QUERY)
      multi.add :c, db.aquery(QUERY)
      res = multi.perform

      (now - start.to_f).should be_within(DELAY * 0.15).of(DELAY)
      res.responses[:callback].size.should == 3
      res.responses[:errback].size.should == 0

      EventMachine.stop
    end
  end

  it "should raise Mysql::Error in case of error" do
    EventMachine.synchrony do
      db = Mysql2::EM::Client.new
      proc {
        db.query("SELECT * FROM i_hope_this_table_does_not_exist;")
      }.should raise_error(Mysql2::Error)
      EventMachine.stop
    end
  end

  it "errback should not catch exception thrown from callback" do
    class ErrbackShouldNotCatchThis < Exception; end
    proc {
      EM.synchrony do
        db = Mysql2::EM::Client.new
        res = db.query QUERY
        raise ErrbackShouldNotCatchThis.new("errback should not catch this")

        EventMachine.stop
      end
    }.should raise_error(ErrbackShouldNotCatchThis)
  end

end