-
Notifications
You must be signed in to change notification settings - Fork 128
/
call_spec.rb
1742 lines (1421 loc) · 62.3 KB
/
call_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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# encoding: utf-8
require 'spec_helper'
class BrokenController < Adhearsion::CallController
def run
raise "Blat!"
end
end
module Adhearsion
describe Call do
let(:mock_client) { double('Client').as_null_object }
let(:call_id) { rand.to_s }
let(:domain) { 'rayo.net' }
let(:headers) { nil }
let(:to) { 'sip:you@there.com' }
let(:from) { 'sip:me@here.com' }
let(:transport) { 'footransport' }
let(:base_time) { Time.local(2008, 9, 1, 12, 0, 0) }
let :offer do
Punchblock::Event::Offer.new target_call_id: call_id,
domain: domain,
transport: transport,
to: to,
from: from,
headers: headers,
timestamp: base_time
end
subject { Adhearsion::Call.new offer }
before do
allow(offer).to receive(:client).and_return(mock_client)
end
after do
Adhearsion.active_calls.clear
end
def expect_message_waiting_for_response(message = nil, fail = false, &block)
expectation = expect(subject.wrapped_object).to receive(:write_and_await_response, &block).once
expectation = expectation.with message if message
if fail
expectation.and_raise fail
else
expectation.and_return message
end
end
it "should do recursion detection on inspect" do
subject[:foo] = subject
Timeout.timeout(0.2) do
expect(subject.inspect).to match('...')
end
end
it "should allow timers to be registered from outside" do
foo = :bar
subject.after(1) { foo = :baz }
sleep 1.1
expect(foo).to eq(:baz)
end
it { is_expected.to respond_to :<< }
describe '#end_reason' do
subject { super().end_reason }
it { is_expected.to eq(nil) }
end
it { is_expected.to be_active }
describe '#commands' do
subject { super().commands }
it { is_expected.to be_empty }
end
describe '#id' do
subject { super().id }
it { is_expected.to eq(call_id) }
end
describe '#domain' do
subject { super().domain }
it { is_expected.to eq(domain) }
end
describe '#uri' do
subject { super().uri }
it { is_expected.to eq("footransport:#{call_id}@#{domain}") }
end
describe '#to' do
subject { super().to }
it { is_expected.to eq(to) }
end
describe '#from' do
subject { super().from }
it { is_expected.to eq(from) }
end
describe '#auto_hangup' do
subject { super().auto_hangup }
it { is_expected.to be_truthy }
end
describe '#after_hangup_lifetime' do
subject { super().after_hangup_lifetime }
it { is_expected.to eq(nil) }
end
context "when the ID is nil" do
let(:call_id) { nil }
describe '#uri' do
subject { super().uri }
it { is_expected.to eq(nil) }
end
end
context "when the domain is nil" do
let(:domain) { nil }
describe '#uri' do
subject { super().uri }
it { is_expected.to eq("footransport:#{call_id}") }
end
end
context "when the transport is nil" do
let(:transport) { nil }
describe '#uri' do
subject { super().uri }
it { is_expected.to eq("#{call_id}@#{domain}") }
end
end
it "should mark its start time" do
expect(subject.start_time).to eq(base_time)
end
describe "#commands" do
it "should use a duplicating accessor for the command registry" do
expect(subject.commands).not_to be subject.commands
end
end
describe "its variables" do
context "with an offer" do
context "with headers" do
let(:headers) { {'X-foo' => 'bar'} }
describe '#variables' do
subject { super().variables }
it { is_expected.to eq({'x_foo' => 'bar'}) }
end
it "should be made available via []" do
expect(subject[:x_foo]).to eq('bar')
end
it "should be alterable using []=" do
subject[:x_foo] = 'baz'
expect(subject[:x_foo]).to eq('baz')
end
context "when receiving an event with headers" do
let(:event) { Punchblock::Event::End.new :headers => {'X-bar' => 'foo'} }
it "should merge later headers" do
subject << event
expect(subject.variables).to eq({'x_foo' => 'bar', 'x_bar' => 'foo'})
end
context "with have symbol names" do
let(:event) { Punchblock::Event::End.new :headers => {:x_bar => 'foo'} }
it "should merge later headers" do
subject << event
expect(subject.variables).to eq({'x_foo' => 'bar', 'x_bar' => 'foo'})
end
end
end
context "when sending a command with headers" do
let(:command) { Punchblock::Command::Accept.new :headers => {'X-bar' => 'foo'} }
it "should merge later headers" do
subject.write_command command
expect(subject.variables).to eq({'x_foo' => 'bar', 'x_bar' => 'foo'})
end
end
end
context "without headers" do
let(:headers) { nil }
describe '#variables' do
subject { super().variables }
it { is_expected.to eq({}) }
end
end
end
context "without an offer" do
let(:offer) { nil }
describe '#variables' do
subject { super().variables }
it { is_expected.to eq({}) }
end
end
end
describe 'without an offer' do
it 'should not raise an exception' do
expect { Adhearsion::Call.new }.not_to raise_error
end
end
context 'registered event handlers' do
let(:event) { double 'Event' }
let(:response) { double 'Response' }
it 'are called when messages are delivered' do
expect(event).to receive(:foo?).and_return true
expect(response).to receive(:call).once
subject.register_event_handler(:foo?) { response.call }
subject << event
end
context 'when a handler raises' do
it 'does not cause the call actor to crash' do
subject.register_event_handler { raise 'Boom' }
subject << event
expect(subject).to be_alive
end
it "triggers an exception event" do
e = StandardError.new('Boom')
expect(Events).to receive(:trigger).once.with(:exception, [e, subject.logger])
subject.register_event_handler { raise e }
subject << event
end
it 'executes all handlers for each event' do
expect(response).to receive(:call).once
subject.register_event_handler { raise 'Boom' }
subject.register_event_handler { response.call }
subject << event
end
end
end
describe "event handlers" do
let(:response) { double 'Response' }
describe "for joined events" do
context "joined to another call" do
let :event do
Punchblock::Event::Joined.new call_uri: 'footransport:foobar@rayo.net'
end
it "should trigger any on_joined callbacks set for the matching call ID" do
expect(response).to receive(:call).once.with(event)
subject.on_joined(:call_uri => 'footransport:foobar@rayo.net') { |event| response.call event }
subject << event
end
it "should trigger any on_joined callbacks set for the matching call ID as a string" do
expect(response).to receive(:call).once.with(event)
subject.on_joined('foobar') { |event| response.call event }
subject << event
end
it "should trigger any on_joined callbacks set for the matching call" do
expect(response).to receive(:call).once.with(event)
call = Call.new
allow(call.wrapped_object).to receive_messages id: 'foobar', domain: 'rayo.net', transport: 'footransport'
subject.on_joined(call) { |event| response.call event }
subject << event
end
it "should not trigger on_joined callbacks for other call IDs" do
expect(response).to receive(:call).never
subject.on_joined(:call_uri => 'barfoo') { |event| response.call event }
subject << event
end
it "should not trigger on_joined callbacks for mixers" do
expect(response).to receive(:call).never
subject.on_joined(:mixer_name => 'foobar') { |event| response.call event }
subject << event
end
end
context "joined to a mixer" do
let :event do
Punchblock::Event::Joined.new :mixer_name => 'foobar'
end
it "should trigger on_joined callbacks for the matching mixer name" do
expect(response).to receive(:call).once.with(event)
subject.on_joined(:mixer_name => 'foobar') { |event| response.call event }
subject << event
end
it "should not trigger on_joined callbacks for other mixer names" do
expect(response).to receive(:call).never
subject.on_joined(:mixer_name => 'barfoo') { |event| response.call event }
subject << event
end
it "should not trigger any on_joined callbacks set for calls" do
expect(response).to receive(:call).never
subject.on_joined(:call_uri => 'foobar') { |event| response.call event }
subject << event
end
it "should not trigger any on_joined callbacks set for the matching call ID as a string" do
expect(response).to receive(:call).never
subject.on_joined('foobar') { |event| response.call event }
subject << event
end
it "should not trigger any on_joined callbacks set for the matching call" do
expect(response).to receive(:call).never
call = Call.new
allow(call.wrapped_object).to receive_messages :id => 'foobar'
subject.on_joined(call) { |event| response.call event }
subject << event
end
end
end
describe "for unjoined events" do
context "unjoined from another call" do
let :event do
Punchblock::Event::Unjoined.new call_uri: 'footransport:foobar@rayo.net'
end
it "should trigger any on_unjoined callbacks set for the matching call ID" do
expect(response).to receive(:call).once.with(event)
subject.on_unjoined(:call_uri => 'footransport:foobar@rayo.net') { |event| response.call event }
subject << event
end
it "should trigger any on_unjoined callbacks set for the matching call ID as a string" do
expect(response).to receive(:call).once.with(event)
subject.on_unjoined('foobar') { |event| response.call event }
subject << event
end
it "should trigger any on_unjoined callbacks set for the matching call" do
expect(response).to receive(:call).once.with(event)
call = Call.new
allow(call.wrapped_object).to receive_messages id: 'foobar', domain: 'rayo.net', transport: 'footransport'
subject.on_unjoined(call) { |event| response.call event }
subject << event
end
it "should not trigger on_unjoined callbacks for other call IDs" do
expect(response).to receive(:call).never
subject.on_unjoined(:call_uri => 'barfoo') { |event| response.call event }
subject << event
end
it "should not trigger on_unjoined callbacks for mixers" do
expect(response).to receive(:call).never
subject.on_joined(:mixer_name => 'foobar') { |event| response.call event }
subject << event
end
end
context "unjoined from a mixer" do
let :event do
Punchblock::Event::Unjoined.new :mixer_name => 'foobar'
end
it "should trigger on_unjoined callbacks for the matching mixer name" do
expect(response).to receive(:call).once.with(event)
subject.on_unjoined(:mixer_name => 'foobar') { |event| response.call event }
subject << event
end
it "should not trigger on_unjoined callbacks for other mixer names" do
expect(response).to receive(:call).never
subject.on_unjoined(:mixer_name => 'barfoo') { |event| response.call event }
subject << event
end
it "should not trigger any on_unjoined callbacks set for calls" do
expect(response).to receive(:call).never
subject.on_unjoined(:call_uri => 'foobar') { |event| response.call event }
subject << event
end
it "should not trigger any on_unjoined callbacks set for the matching call ID as a string" do
expect(response).to receive(:call).never
subject.on_unjoined('foobar') { |event| response.call event }
subject << event
end
it "should not trigger any on_unjoined callbacks set for the matching call" do
expect(response).to receive(:call).never
call = Call.new
allow(call.wrapped_object).to receive_messages :id => 'foobar'
subject.on_unjoined(call) { |event| response.call event }
subject << event
end
end
end
describe "for end events" do
let :event do
Punchblock::Event::End.new :reason => :hangup
end
it "should trigger any on_end callbacks set" do
expect(response).to receive(:call).once.with(event)
subject.on_end { |event| response.call event }
subject << event
end
end
context "when raising an exception" do
it "does not kill the call actor" do
subject.register_event_handler { |e| raise 'foo' }
expect { subject << :foo }.not_to raise_error
sleep 1
expect(subject).to be_alive
end
it 'passes the exception through the Events system' do
latch = CountDownLatch.new 1
Adhearsion::Events.exception do |e, l|
expect(e).to be_a RuntimeError
expect(l).to be subject.logger
latch.countdown!
end
subject.register_event_handler { |e| raise 'foo' }
expect { subject << :foo }.not_to raise_error
expect(latch.wait(3)).to be true
Adhearsion::Events.clear_handlers :exception
end
end
end
context "peer registry" do
let(:other_call_uri) { 'xmpp:foobar@example.com' }
let(:other_call) { Call.new }
before { allow(other_call).to receive_messages uri: other_call_uri }
let :joined_event do
Punchblock::Event::Joined.new call_uri: other_call_uri
end
let :unjoined_event do
Punchblock::Event::Unjoined.new call_uri: other_call_uri
end
context "when we know about the joined call" do
before { Adhearsion.active_calls << other_call }
after { Adhearsion.active_calls.remove_inactive_call other_call }
it "should add the peer to its registry" do
subject << joined_event
expect(subject.peers).to eq({'xmpp:foobar@example.com' => other_call})
end
context "in a handler for the joined event" do
it "should have already populated the registry" do
peer = nil
subject.on_joined do |event|
peer = subject.peers.keys.first
end
subject << joined_event
expect(peer).to eq(other_call_uri)
end
end
context "when being unjoined from a previously joined call" do
before { subject << joined_event }
it "should remove the peer from its registry" do
expect(subject.peers).not_to eql({})
subject << unjoined_event
expect(subject.peers).to eql({})
end
context "in a handler for the unjoined event" do
it "should have already been removed the registry" do
peer_count = nil
subject.on_unjoined do |event|
peer_count = subject.peers.size
end
subject << unjoined_event
expect(peer_count).to eq(0)
end
end
end
end
context "when we don't know about the joined call" do
it "should add a nil entry to its registry" do
subject << joined_event
expect(subject.peers).to eq({'xmpp:foobar@example.com' => nil})
end
context "in a handler for the joined event" do
it "should have already populated the registry" do
peer = nil
subject.on_joined do |event|
peer = subject.peers.keys.first
end
subject << joined_event
expect(peer).to eq(other_call_uri)
end
end
context "when being unjoined from a previously joined call" do
before { subject << joined_event }
it "should remove the peer from its registry" do
expect(subject.peers).not_to eql({})
subject << unjoined_event
expect(subject.peers).to eql({})
end
context "in a handler for the unjoined event" do
it "should have already been removed the registry" do
peer_count = nil
subject.on_unjoined do |event|
peer_count = subject.peers.size
end
subject << unjoined_event
expect(peer_count).to eq(0)
end
end
end
end
it "should not return the same registry every call" do
expect(subject.peers).not_to be subject.peers
end
end
describe "#<<" do
describe "with a Punchblock End" do
let :end_event do
Punchblock::Event::End.new :reason => :hangup, :platform_code => 'arbitrary_code'
end
it "should mark the call as ended" do
subject << end_event
expect(subject).not_to be_active
end
it "should set the end reason" do
subject << end_event
expect(subject.end_reason).to eq(:hangup)
end
it "should set the end code" do
subject << end_event
expect(subject.end_code).to eq('arbitrary_code')
end
it "should set the end time" do
finish_time = Time.local(2008, 9, 1, 12, 1, 3)
Timecop.freeze finish_time
expect(subject.end_time).to eq(nil)
subject << end_event
expect(subject.end_time).to eq(finish_time)
end
it "should set the call duration" do
start_time = Time.local(2008, 9, 1, 12, 0, 0)
Timecop.freeze start_time
subject
mid_point_time = Time.local(2008, 9, 1, 12, 0, 37)
Timecop.freeze mid_point_time
expect(subject.duration).to eq(37.0)
finish_time = Time.local(2008, 9, 1, 12, 1, 3)
Timecop.freeze finish_time
subject << end_event
future_time = Time.local(2008, 9, 1, 12, 2, 3)
Timecop.freeze finish_time
expect(subject.duration).to eq(63.0)
end
it "should instruct the command registry to terminate" do
command = Punchblock::Command::Answer.new
command.request!
subject.future.write_and_await_response command
subject << end_event
expect(command.response(1)).to be_a Call::Hangup
end
it "removes itself from the active calls" do
size_before = Adhearsion.active_calls.size
Adhearsion.active_calls << subject
expect(Adhearsion.active_calls.size).to be > size_before
subject << end_event
expect(Adhearsion.active_calls.size).to eq(size_before)
end
context "with no custom lifetime" do
around do |example|
old_val = Adhearsion.config.platform.after_hangup_lifetime
begin
example.run
rescue
Adhearsion.config.platform.after_hangup_lifetime = old_val
end
end
it "shuts down the actor using platform config" do
Adhearsion.config.platform.after_hangup_lifetime = 2
subject << end_event
sleep 2.1
expect(subject.alive?).to be false
expect { subject.id }.to raise_error Call::ExpiredError, /expired and is no longer accessible/
end
end
context "with a custom lifetime" do
around do |example|
old_val = Adhearsion.config.platform.after_hangup_lifetime
begin
example.run
rescue
Adhearsion.config.platform.after_hangup_lifetime = old_val
end
end
it "shuts down the actor using the Call#after_hangup_lifetime" do
Adhearsion.config.platform.after_hangup_lifetime = 1
subject.after_hangup_lifetime = 2
subject << end_event
sleep 1.1
expect(subject.alive?).to be true
sleep 1
expect(subject.alive?).to be false
expect { subject.id }.to raise_error Call::ExpiredError, /expired and is no longer accessible/
end
end
end
end
describe "#wait_for_end" do
let :end_event do
Punchblock::Event::End.new reason: :hangup
end
context "when the call has already ended" do
before { subject << end_event }
it "should return the end reason" do
expect(subject.wait_for_end).to eq(:hangup)
end
end
context "when the call has not yet ended" do
it "should block until the call ends and return the end reason" do
fut = subject.future.wait_for_end
sleep 0.5
expect(fut).not_to be_ready
subject << end_event
expect(fut.value).to eq(:hangup)
end
end
end
describe "tagging a call" do
it 'with a single Symbol' do
expect {
subject.tag :moderator
}.not_to raise_error
end
it 'with multiple Symbols' do
expect {
subject.tag :moderator
subject.tag :female
}.not_to raise_error
end
it 'with a non-Symbol, non-String object' do
bad_objects = [123, Object.new, 888.88, nil, true, false, StringIO.new]
bad_objects.each do |bad_object|
expect {
subject.tag bad_object
}.to raise_error ArgumentError
end
end
end
it "#remove_tag" do
subject.tag :moderator
subject.tag :female
subject.remove_tag :female
subject.tag :male
expect(subject.tags).to eq([:moderator, :male])
end
describe "#tagged_with?" do
it 'with one tag' do
subject.tag :guest
expect(subject.tagged_with?(:guest)).to be true
expect(subject.tagged_with?(:authorized)).to be false
end
it 'with many tags' do
subject.tag :customer
subject.tag :authorized
expect(subject.tagged_with?(:customer)).to be true
expect(subject.tagged_with?(:authorized)).to be true
end
end
describe "#write_command" do
let(:command) { Punchblock::Command::Answer.new }
it "should write the command to the Punchblock connection" do
expect(subject.wrapped_object).to receive(:client).once.and_return mock_client
expect(mock_client).to receive(:execute_command).once.with(Punchblock::Command::Answer.new(target_call_id: call_id, domain: domain)).and_return true
subject.write_command command
end
describe "with a hungup call" do
before do
expect(subject.wrapped_object).to receive(:active?).and_return(false)
end
it "should raise a Hangup exception" do
expect { subject.write_command command }.to raise_error(Call::Hangup)
end
describe "if the command is a Hangup" do
let(:command) { Punchblock::Command::Hangup.new }
it "should not raise a Hangup exception" do
expect { subject.write_command command }.not_to raise_error
end
end
end
end
describe '#write_and_await_response' do
let(:message) { Punchblock::Command::Accept.new }
let(:response) { :foo }
before do
expect(message).to receive(:execute!).and_return true
message.response = response
end
it "writes a command to the call" do
expect(subject.wrapped_object).to receive(:write_command).once.with(message)
subject.write_and_await_response message
end
it "removes the command from the registry after execution" do
subject.write_and_await_response message
expect(subject.commands).to be_empty
end
it "blocks until a response is received" do
slow_command = Punchblock::Command::Dial.new
slow_command.request!
Thread.new do
sleep 0.5
slow_command.response = response
end
starting_time = Time.now
subject.write_and_await_response slow_command
expect(Time.now - starting_time).to be >= 0.5
end
context "while waiting for a response" do
let(:slow_command) { Punchblock::Command::Dial.new }
before { slow_command.request! }
it "does not block the whole actor while waiting for a response" do
fut = subject.future.write_and_await_response slow_command
expect(subject.id).to eq(call_id)
slow_command.response = response
fut.value
end
it "adds the command to the registry" do
subject.future.write_and_await_response slow_command
sleep 0.2
expect(subject.commands).not_to be_empty
expect(subject.commands.first).to be slow_command
end
end
describe "with a successful response" do
it "returns the executed command" do
expect(subject.write_and_await_response(message)).to be message
end
end
describe "with an error response" do
let(:new_exception) { Punchblock::ProtocolError }
let(:response) { new_exception.new }
it "raises the error" do
expect(Events).to receive(:trigger).never
expect { subject.write_and_await_response message }.to raise_error new_exception
end
context "where the name is :item_not_found" do
let(:response) { new_exception.new.setup :item_not_found }
it "should raise a Hangup exception" do
expect(Events).to receive(:trigger).never
expect { subject.write_and_await_response message }.to raise_error Call::Hangup
end
end
end
describe "when the response times out" do
before do
message.target_call_id = call_id
message.domain = domain
expect(message).to receive(:response).and_raise Timeout::Error
end
it "should raise the error in the caller but not crash the actor" do
expect { subject.write_and_await_response message }.to raise_error Call::CommandTimeout, message.to_s
sleep 0.5
expect(subject).to be_alive
end
end
end
describe "routing" do
before do
expect(Adhearsion::Process).to receive(:state_name).once.and_return process_state
end
after { subject.route }
context "when the Adhearsion::Process is :booting" do
let(:process_state) { :booting }
it 'should reject a call with cause :declined' do
expect_message_waiting_for_response Punchblock::Command::Reject.new(reason: :decline)
end
end
[ :running, :stopping ].each do |state|
context "when when Adhearsion::Process is in :#{state}" do
let(:process_state) { state }
it "should dispatch via the router" do
Adhearsion.router do
route 'foobar', Class.new
end
expect(Adhearsion.router).to receive(:handle).once.with subject
end
end
end
context "when when Adhearsion::Process is in :rejecting" do
let(:process_state) { :rejecting }
it 'should reject a call with cause :declined' do
expect_message_waiting_for_response Punchblock::Command::Reject.new(reason: :decline)
end
end
context "when when Adhearsion::Process is not :running, :stopping or :rejecting" do
let(:process_state) { :foobar }
it 'should reject a call with cause :error' do
expect_message_waiting_for_response Punchblock::Command::Reject.new(reason: :error)
end
end
end
describe "#send_message" do
it "should send a message through the Punchblock connection using the call ID and domain" do
expect(subject.wrapped_object).to receive(:client).once.and_return mock_client
expect(mock_client).to receive(:send_message).once.with(subject.id, subject.domain, "Hello World!", {})
subject.send_message "Hello World!"
end
it "should send a message with the given subject" do
expect(subject.wrapped_object).to receive(:client).once.and_return mock_client
expect(mock_client).to receive(:send_message).once.with(subject.id, subject.domain, nil, :subject => "Important Message")
subject.send_message nil, :subject => "Important Message"
end
end
describe "basic control commands" do
describe '#accept' do
describe "with no headers" do
it 'should send an Accept message' do
expect_message_waiting_for_response Punchblock::Command::Accept.new
subject.accept
end
end
describe "with headers set" do
it 'should send an Accept message with the correct headers' do
headers = {:foo => 'bar'}
expect_message_waiting_for_response Punchblock::Command::Accept.new(:headers => headers)
subject.accept headers
end
end
describe "a second time" do
it "should only send one Accept message" do
expect_message_waiting_for_response Punchblock::Command::Accept.new
subject.accept
subject.accept
end
end
context "with a failure response" do
it 'should raise the error but not crash the actor' do
error = Punchblock::ProtocolError.new.setup(:service_unavailable)
expect_message_waiting_for_response Punchblock::Command::Accept.new, error
expect { subject.accept }.to raise_error error
sleep 0.2
expect(subject.alive?).to be true
end
end
end
describe '#answer' do
describe "with no headers" do
it 'should send an Answer message' do
expect_message_waiting_for_response Punchblock::Command::Answer.new
subject.answer
end
end
describe "with headers set" do
it 'should send an Answer message with the correct headers' do
headers = {:foo => 'bar'}
expect_message_waiting_for_response Punchblock::Command::Answer.new(:headers => headers)
subject.answer headers
end
end
context "with a failure response" do
it 'should raise the error but not crash the actor' do
error = Punchblock::ProtocolError.new.setup(:service_unavailable)
expect_message_waiting_for_response Punchblock::Command::Answer.new, error
expect { subject.answer }.to raise_error error
sleep 0.2
expect(subject.alive?).to be true
end
end
end
describe '#reject' do
describe "with a reason given" do
it 'should send a Reject message with the correct reason' do
expect_message_waiting_for_response Punchblock::Command::Reject.new(:reason => :decline)
subject.reject :decline
end
end
describe "with no reason given" do
it 'should send a Reject message with the reason busy' do
expect_message_waiting_for_response Punchblock::Command::Reject.new(:reason => :busy)