-
-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathabstract_handle_spec.rb
117 lines (94 loc) · 2.79 KB
/
abstract_handle_spec.rb
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
# frozen_string_literal: true
describe Rdkafka::AbstractHandle do
let(:response) { 0 }
let(:result) { -1 }
context "A subclass that does not implement the required methods" do
class BadTestHandle < Rdkafka::AbstractHandle
layout :pending, :bool,
:response, :int
end
it "raises an exception if operation_name is called" do
expect {
BadTestHandle.new.operation_name
}.to raise_exception(RuntimeError, /Must be implemented by subclass!/)
end
it "raises an exception if create_result is called" do
expect {
BadTestHandle.new.create_result
}.to raise_exception(RuntimeError, /Must be implemented by subclass!/)
end
end
class TestHandle < Rdkafka::AbstractHandle
layout :pending, :bool,
:response, :int,
:result, :int
def operation_name
"test_operation"
end
def create_result
self[:result]
end
end
subject do
TestHandle.new.tap do |handle|
handle[:pending] = pending_handle
handle[:response] = response
handle[:result] = result
end
end
describe ".register and .remove" do
let(:pending_handle) { true }
it "should register and remove a delivery handle" do
Rdkafka::AbstractHandle.register(subject)
removed = Rdkafka::AbstractHandle.remove(subject.to_ptr.address)
expect(removed).to eq subject
expect(Rdkafka::AbstractHandle::REGISTRY).to be_empty
end
end
describe "#pending?" do
context "when true" do
let(:pending_handle) { true }
it "should be true" do
expect(subject.pending?).to be true
end
end
context "when not true" do
let(:pending_handle) { false }
it "should be false" do
expect(subject.pending?).to be false
end
end
end
describe "#wait" do
context 'when pending_handle true' do
let(:pending_handle) { true }
it "should wait until the timeout and then raise an error" do
expect {
subject.wait(max_wait_timeout: 0.1)
}.to raise_error Rdkafka::AbstractHandle::WaitTimeoutError, /test_operation/
end
end
context 'when pending_handle false' do
let(:pending_handle) { false }
context "without error" do
let(:result) { 1 }
it "should return a result" do
wait_result = subject.wait
expect(wait_result).to eq(result)
end
it "should wait without a timeout" do
wait_result = subject.wait(max_wait_timeout: nil)
expect(wait_result).to eq(result)
end
end
context "with error" do
let(:response) { 20 }
it "should raise an rdkafka error" do
expect {
subject.wait
}.to raise_error Rdkafka::RdkafkaError
end
end
end
end
end