File: emitter_spec.rb

package info (click to toggle)
ruby-http-2 1.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,360 kB
  • sloc: ruby: 6,033; makefile: 4
file content (63 lines) | stat: -rw-r--r-- 1,227 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
# frozen_string_literal: true

require "helper"

RSpec.describe HTTP2::Emitter do
  class Worker
    include Emitter

    def initialize
      @listeners = Hash.new { |hash, key| hash[key] = [] }
    end
  end

  let(:w) { Worker.new }
  before(:each) do
    @cnt = 0
  end

  it "should raise error on missing callback" do
    expect { w.on(:a) {} }.to_not raise_error
    expect { w.on(:a) }.to raise_error
  end

  it "should allow multiple callbacks on single event" do
    cnt = 0
    w.on(:a) { cnt += 1 }
    w.on(:a) { cnt += 1 }
    w.emit(:a)

    expect(cnt).to eq 2
  end

  it "should execute callback with optional args" do
    args = nil
    w.on(:a) { |a| args = a }
    w.emit(:a, 123)

    expect(args).to eq 123
  end

  it "should pass emitted callbacks to listeners" do
    cnt = 0
    w.on(:a)   { |&block| block.call }
    w.once(:a) { |&block| block.call }
    w.emit(:a) { cnt += 1 }

    expect(cnt).to eq 2
  end

  it "should allow events with no callbacks" do
    expect { w.emit(:missing) }.to_not raise_error
  end

  it "should execute callback exactly once" do
    cnt = 0
    w.on(:a)   { cnt += 1 }
    w.once(:a) { cnt += 1 }
    w.emit(:a)
    w.emit(:a)

    expect(cnt).to eq 3
  end
end