forked from ably/ably-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
presence_spec.rb
2854 lines (2474 loc) · 116 KB
/
presence_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'
describe Ably::Realtime::Presence, :event_machine do
include Ably::Modules::Conversions
vary_by_protocol do
let(:default_options) { { key: api_key, environment: environment, protocol: protocol } }
let(:client_options) { default_options }
let(:anonymous_client) { auto_close Ably::Realtime::Client.new(client_options) }
let(:client_one_id) { random_str }
let(:client_one) { auto_close Ably::Realtime::Client.new(client_options.merge(client_id: client_one_id)) }
let(:client_two_id) { random_str }
let(:client_two) { auto_close Ably::Realtime::Client.new(client_options.merge(client_id: client_two_id)) }
let(:wildcard_token) { lambda { |token_params| Ably::Rest::Client.new(client_options).auth.request_token(client_id: '*') } }
let(:channel_name) { "presence-#{random_str(4)}" }
let(:channel_anonymous_client) { anonymous_client.channel(channel_name) }
let(:presence_anonymous_client) { channel_anonymous_client.presence }
let(:channel_client_one) { client_one.channel(channel_name) }
let(:channel_rest_client_one) { client_one.rest_client.channel(channel_name) }
let(:presence_client_one) { channel_client_one.presence }
let(:channel_client_two) { client_two.channel(channel_name) }
let(:presence_client_two) { channel_client_two.presence }
let(:data_payload) { random_str }
def force_connection_failure(client)
# Prevent any further SYNC messages coming in on this connection
client.connection.transport.send(:driver).remove_all_listeners('message')
client.connection.transport.unbind
end
shared_examples_for 'a public presence method' do |method_name, expected_state, args, options = {}|
let(:client_id) do
if args.empty?
random_str
else
args
end
end
def setup_test(method_name, args, options)
if options[:enter_first]
acked = false
received = false
presence_client_one.public_send(method_name.to_s.gsub(/leave|update/, 'enter'), args) do
acked = true
yield if acked & received
end
presence_client_one.subscribe do |presence_message|
expect(presence_message.action).to eq(:enter)
presence_client_one.unsubscribe
received = true
yield if acked & received
end
else
yield
end
end
unless expected_state == :left
it 'raise an exception if the channel is detached' do
setup_test(method_name, args, options) do
channel_client_one.attach do
channel_client_one.transition_state_machine :detaching
channel_client_one.once(:detached) do
presence_client_one.public_send(method_name, args).tap do |deferrable|
deferrable.callback { raise 'Get should not succeed' }
deferrable.errback do |error|
expect(error).to be_a(Ably::Exceptions::InvalidState)
expect(error.message).to match(/Operation is not allowed when channel is in STATE.Detached/)
stop_reactor
end
end
end
end
end
end
it 'raise an exception if the channel becomes detached' do
setup_test(method_name, args, options) do
channel_client_one.attach do
channel_client_one.transition_state_machine :detaching
presence_client_one.public_send(method_name, args).tap do |deferrable|
deferrable.callback { raise 'Get should not succeed' }
deferrable.errback do |error|
expect(error).to be_a(Ably::Exceptions::InvalidState)
expect(error.message).to match(/Operation failed as channel transitioned to STATE.Detached/)
stop_reactor
end
end
end
end
end
it 'raise an exception if the channel is failed' do
setup_test(method_name, args, options) do
channel_client_one.attach do
channel_client_one.transition_state_machine :failed
expect(channel_client_one.state).to eq(:failed)
presence_client_one.public_send(method_name, args).tap do |deferrable|
deferrable.callback { raise 'Get should not succeed' }
deferrable.errback do |error|
expect(error).to be_a(Ably::Exceptions::InvalidState)
expect(error.message).to match(/Operation is not allowed when channel is in STATE.Failed/)
stop_reactor
end
end
end
end
end
it 'raise an exception if the channel becomes failed' do
setup_test(method_name, args, options) do
channel_client_one.attach do
presence_client_one.public_send(method_name, args).tap do |deferrable|
deferrable.callback { raise 'Get should not succeed' }
deferrable.errback do |error|
expect(error).to be_a(Ably::Exceptions::MessageDeliveryFailed)
stop_reactor
end
end
channel_client_one.transition_state_machine :failed
expect(channel_client_one.state).to eq(:failed)
end
end
end
it 'implicitly attaches the channel' do
expect(channel_client_one).to_not be_attached
presence_client_one.public_send(method_name, args) do
expect(channel_client_one).to be_attached
stop_reactor
end
end
context 'when :queue_messages client option is false' do
let(:client_one) { auto_close Ably::Realtime::Client.new(default_options.merge(queue_messages: false, client_id: client_id)) }
context 'and connection state initialized' do
it 'fails the deferrable' do
presence_client_one.public_send(method_name, args).errback do |error|
expect(error).to be_a(Ably::Exceptions::MessageQueueingDisabled)
stop_reactor
end
expect(client_one.connection).to be_initialized
end
end
context 'and connection state connecting' do
it 'fails the deferrable' do
client_one.connect
EventMachine.next_tick do
presence_client_one.public_send(method_name, args).errback do |error|
expect(error).to be_a(Ably::Exceptions::MessageQueueingDisabled)
stop_reactor
end
expect(client_one.connection).to be_connecting
end
end
end
context 'and connection state disconnected' do
let(:client_one) { auto_close Ably::Realtime::Client.new(default_options.merge(queue_messages: false, client_id: client_id, :log_level => :error)) }
it 'fails the deferrable' do
client_one.connection.once(:connected) do
client_one.connection.once(:disconnected) do
presence_client_one.public_send(method_name, args).errback do |error|
expect(error).to be_a(Ably::Exceptions::MessageQueueingDisabled)
stop_reactor
end
expect(client_one.connection).to be_disconnected
end
client_one.connection.transition_state_machine :disconnected
end
end
end
context 'and connection state connected' do
it 'publishes the message' do
client_one.connection.once(:connected) do
presence_client_one.public_send(method_name, args)
stop_reactor
end
end
end
end
end
context 'with supported data payload content type' do
def register_presence_and_check_data(method_name, data)
if method_name.to_s.match(/_client/)
presence_client_one.public_send(method_name, client_id, data)
else
presence_client_one.public_send(method_name, data)
end
presence_client_one.subscribe do |presence_message|
expect(presence_message.data).to eql(data)
stop_reactor
end
end
context 'JSON Object (Hash)' do
let(:data) { { 'Hash' => 'true' } }
it 'is encoded and decoded to the same hash' do
setup_test(method_name, args, options) do
register_presence_and_check_data method_name, data
end
end
end
context 'JSON Array' do
let(:data) { [ nil, true, false, 55, 'string', { 'Hash' => true }, ['array'] ] }
it 'is encoded and decoded to the same Array' do
setup_test(method_name, args, options) do
register_presence_and_check_data method_name, data
end
end
end
context 'String' do
let(:data) { random_str }
it 'is encoded and decoded to the same Array' do
setup_test(method_name, args, options) do
register_presence_and_check_data method_name, data
end
end
end
context 'Binary' do
let(:data) { Base64.encode64(random_str) }
it 'is encoded and decoded to the same Array' do
setup_test(method_name, args, options) do
register_presence_and_check_data method_name, data
end
end
end
end
context 'with unsupported data payload content type' do
def presence_action(method_name, data)
if method_name.to_s.match(/_client/)
presence_client_one.public_send(method_name, client_id, data)
else
presence_client_one.public_send(method_name, data)
end
end
context 'Integer' do
let(:data) { 1 }
it 'raises an UnsupportedDataType 40013 exception' do
expect { presence_action(method_name, data) }.to raise_error(Ably::Exceptions::UnsupportedDataType)
stop_reactor
end
end
context 'Float' do
let(:data) { 1.1 }
it 'raises an UnsupportedDataType 40013 exception' do
expect { presence_action(method_name, data) }.to raise_error(Ably::Exceptions::UnsupportedDataType)
stop_reactor
end
end
context 'Boolean' do
let(:data) { true }
it 'raises an UnsupportedDataType 40013 exception' do
expect { presence_action(method_name, data) }.to raise_error(Ably::Exceptions::UnsupportedDataType)
stop_reactor
end
end
context 'False' do
let(:data) { false }
it 'raises an UnsupportedDataType 40013 exception' do
expect { presence_action(method_name, data) }.to raise_error(Ably::Exceptions::UnsupportedDataType)
stop_reactor
end
end
end
it 'returns a SafeDeferrable that catches exceptions in callbacks and logs them' do
setup_test(method_name, args, options) do
expect(presence_client_one.public_send(method_name, args)).to be_a(Ably::Util::SafeDeferrable)
stop_reactor
end
end
it 'allows a block to be passed in that is executed upon success' do
setup_test(method_name, args, options) do
presence_client_one.public_send(method_name, args) do
stop_reactor
end
end
end
it 'calls the Deferrable callback on success' do
setup_test(method_name, args, options) do
presence_client_one.public_send(method_name, args).callback do |presence|
expect(presence).to eql(presence_client_one)
expect(presence_client_one.state).to eq(expected_state) if expected_state
stop_reactor
end
end
end
it 'catches exceptions in the provided method block and logs them to the logger' do
setup_test(method_name, args, options) do
expect(presence_client_one.logger).to receive(:error) do |*args, &block|
expect(args.concat([block ? block.call : nil]).join(',')).to match(/Intentional exception/)
stop_reactor
end
presence_client_one.public_send(method_name, args) { raise 'Intentional exception' }
end
end
context 'if connection fails before success' do
let(:client_options) { default_options.merge(log_level: :none) }
it 'calls the Deferrable errback if channel is detached' do
setup_test(method_name, args, options) do
channel_client_one.attach do
client_one.connection.__outgoing_protocol_msgbus__.subscribe(:protocol_message) do |protocol_message|
# Don't allow any messages to reach the server
client_one.connection.__outgoing_protocol_msgbus__.unsubscribe
error_message = Ably::Models::ProtocolMessage.new(action: 9, error: { message: 'force failure' })
client_one.connection.__incoming_protocol_msgbus__.publish :protocol_message, error_message
end
presence_client_one.public_send(method_name, args).tap do |deferrable|
deferrable.callback { raise 'Should not succeed' }
deferrable.errback do |error|
expect(error).to be_kind_of(Ably::Exceptions::BaseAblyException)
stop_reactor
end
end
end
end
end
end
end
shared_examples_for 'a presence on behalf of another client method' do |method_name|
context ":#{method_name} when authenticated with a wildcard client_id" do
let(:token) { Ably::Rest::Client.new(default_options).auth.request_token(client_id: '*').token }
let(:client_options) { default_options.merge(key: nil, token: token) }
let(:client) { auto_close Ably::Realtime::Client.new(client_options) }
let(:presence_channel) { client.channels.get(channel_name).presence }
context 'and a valid client_id' do
it 'succeeds' do
presence_channel.public_send(method_name, 'clientId') do
EM.add_timer(0.5) { stop_reactor }
end.tap do |deferrable|
deferrable.errback { raise 'Should have succeeded' }
end
end
end
context 'and a wildcard client_id' do
it 'throws an exception' do
expect { presence_channel.public_send(method_name, '*') }.to raise_error Ably::Exceptions::IncompatibleClientId
stop_reactor
end
end
context 'and an empty client_id' do
it 'throws an exception' do
expect { presence_channel.public_send(method_name, nil) }.to raise_error Ably::Exceptions::IncompatibleClientId
stop_reactor
end
end
context 'and a client_id that is not a string type' do
it 'throws an exception' do
expect { presence_channel.public_send(method_name, 1) }.to raise_error Ably::Exceptions::IncompatibleClientId
stop_reactor
end
end
end
context ":#{method_name} when authenticated with a valid client_id" do
let(:token) { Ably::Rest::Client.new(default_options).auth.request_token(client_id: 'valid').token }
let(:client_options) { default_options.merge(key: nil, token: token) }
let(:client) { auto_close Ably::Realtime::Client.new(client_options.merge(log_level: :error)) }
let(:channel) { client.channels.get(channel_name) }
let(:presence_channel) { channel.presence }
context 'and another invalid client_id' do
context 'before authentication' do
it 'allows the operation and then Ably rejects the operation' do
presence_channel.public_send(method_name, 'invalid').errback do |error|
expect(error.code).to eql(40012)
stop_reactor
end
end
end
context 'after authentication' do
it 'throws an exception' do
channel.attach do
expect { presence_channel.public_send(method_name, 'invalid') }.to raise_error Ably::Exceptions::IncompatibleClientId
stop_reactor
end
end
end
end
context 'and a wildcard client_id' do
it 'throws an exception' do
expect { presence_channel.public_send(method_name, '*') }.to raise_error Ably::Exceptions::IncompatibleClientId
stop_reactor
end
end
context 'and an empty client_id' do
it 'throws an exception' do
expect { presence_channel.public_send(method_name, nil) }.to raise_error Ably::Exceptions::IncompatibleClientId
stop_reactor
end
end
end
context ":#{method_name} when anonymous and no client_id" do
let(:token) { Ably::Rest::Client.new(default_options).auth.request_token(client_id: nil).token }
let(:client_options) { default_options.merge(key: nil, token: token) }
let(:client) { auto_close Ably::Realtime::Client.new(client_options.merge(log_level: :error)) }
let(:channel) { client.channels.get(channel_name) }
let(:presence_channel) { channel.presence }
context 'and another invalid client_id' do
context 'before authentication' do
it 'allows the operation and then Ably rejects the operation' do
presence_channel.public_send(method_name, 'invalid').errback do |error|
expect(error.code).to eql(40012)
stop_reactor
end
end
end
context 'after authentication' do
it 'throws an exception' do
channel.attach do
expect { presence_channel.public_send(method_name, 'invalid') }.to raise_error Ably::Exceptions::IncompatibleClientId
stop_reactor
end
end
end
end
context 'and a wildcard client_id' do
it 'throws an exception' do
expect { presence_channel.public_send(method_name, '*') }.to raise_error Ably::Exceptions::IncompatibleClientId
stop_reactor
end
end
context 'and an empty client_id' do
it 'throws an exception' do
expect { presence_channel.public_send(method_name, nil) }.to raise_error Ably::Exceptions::IncompatibleClientId
stop_reactor
end
end
end
end
context 'when attached (but not present) on a presence channel with an anonymous client (no client ID)' do
it 'maintains state as other clients enter and leave the channel (#RTP2e)' do
channel_anonymous_client.attach do
presence_anonymous_client.subscribe(:enter) do |presence_message|
expect(presence_message.client_id).to eql(client_one.client_id)
presence_anonymous_client.get do |members|
expect(members.first.client_id).to eql(client_one.client_id)
expect(members.first.action).to eq(:present)
presence_anonymous_client.subscribe(:leave) do |leave_presence_message|
expect(leave_presence_message.client_id).to eql(client_one.client_id)
presence_anonymous_client.get do |members_once_left|
expect(members_once_left.count).to eql(0)
stop_reactor
end
end
end
end
end
presence_client_one.enter do
presence_client_one.leave
end
end
end
context '#members map / PresenceMap (#RTP2)', api_private: true do
it 'is available once the channel is created' do
expect(presence_anonymous_client.members).to_not be_nil
stop_reactor
end
it 'is not synchronised when initially created' do
expect(presence_anonymous_client.members).to_not be_sync_complete
stop_reactor
end
it 'will emit an :in_sync event when synchronisation is complete' do
presence_client_one.enter
presence_client_two.enter
presence_anonymous_client.members.once(:in_sync) do
stop_reactor
end
channel_anonymous_client.attach
end
context 'before server sync complete' do
it 'behaves like an Enumerable allowing direct access to current members' do
expect(presence_anonymous_client.members.count).to eql(0)
expect(presence_anonymous_client.members.map(&:member_key)).to eql([])
stop_reactor
end
end
context 'once server sync is complete' do
it 'behaves like an Enumerable allowing direct access to current members' do
presence_client_one.enter
presence_client_two.enter
entered = 0
presence_client_one.subscribe(:enter) do
entered += 1
next unless entered == 2
presence_anonymous_client.members.once(:in_sync) do
expect(presence_anonymous_client.members.count).to eql(2)
member_ids = presence_anonymous_client.members.map(&:member_key)
expect(member_ids.count).to eql(2)
expect(member_ids.uniq.count).to eql(2)
stop_reactor
end
channel_anonymous_client.attach
end
end
end
context 'the map is based on the member_key (connection_id & client_id)' do
# 2 unqiue client_id with client_id "b" being on two connections
let(:enter_action) { 2 }
let(:presence_data) do
[
{ client_id: 'a', connection_id: 'one', id: 'one:0:0', action: enter_action },
{ client_id: 'b', connection_id: 'one', id: 'one:0:1', action: enter_action },
{ client_id: 'a', connection_id: 'one', id: 'one:0:2', action: enter_action },
{ client_id: 'b', connection_id: 'one', id: 'one:0:3', action: enter_action },
{ client_id: 'b', connection_id: 'two', id: 'two:0:4', action: enter_action }
]
end
it 'ensures uniqueness from this member_key (#RTP2a)' do
channel_anonymous_client.attach do
presence_anonymous_client.get do |members|
expect(members.length).to eql(0)
## Fabricate members
action = Ably::Models::ProtocolMessage::ACTION.Presence
presence_msg = Ably::Models::ProtocolMessage.new(
action: action,
connection_serial: 20,
channel: channel_name,
presence: presence_data,
timestamp: Time.now.to_i * 1000
)
anonymous_client.connection.__incoming_protocol_msgbus__.publish :protocol_message, presence_msg
EventMachine.add_timer(0.5) do
presence_anonymous_client.get do |members|
expect(members.length).to eql(3)
expect(members.map { |member| member.client_id }.uniq).to contain_exactly('a', 'b')
stop_reactor
end
end
end
end
end
end
context 'newness is compared based on PresenceMessage#id unless synthesized' do
let(:page_size) { 100 }
let(:enter_expected_count) { page_size + 1 } # 100 per page, this ensures we have more than one page so that we can test newness during sync
let(:enter_action) { 2 }
let(:leave_action) { 3 }
let(:now) { Time.now.to_i * 1000 }
let(:entered) { [] }
let(:client_one) { auto_close Ably::Realtime::Client.new(default_options.merge(auth_callback: wildcard_token)) }
def setup_members_on(presence)
enter_expected_count.times do |indx|
# 10 messages per second max rate on simulation accounts
rate = indx.to_f / 10
EventMachine.add_timer(rate) do
presence.enter_client("client:#{indx}") do |message|
entered << message
next unless entered.count == enter_expected_count
yield
end
end
end
end
def allow_sync_fabricate_data_final_sync_and_assert_members
setup_members_on(presence_client_one) do
sync_pages_received = []
anonymous_client.connection.once(:connected) do
anonymous_client.connection.transport.__incoming_protocol_msgbus__.subscribe(:protocol_message) do |protocol_message|
if protocol_message.action == :sync
sync_pages_received << protocol_message
if sync_pages_received.count == 1
action = Ably::Models::ProtocolMessage::ACTION.Presence
presence_msg = Ably::Models::ProtocolMessage.new(
action: action,
connection_serial: anonymous_client.connection.serial + 1,
channel: channel_name,
presence: presence_data,
timestamp: Time.now.to_i * 1000
)
anonymous_client.connection.__incoming_protocol_msgbus__.publish :protocol_message, presence_msg
# Now simulate an end to the sync
action = Ably::Models::ProtocolMessage::ACTION.Sync
sync_msg = Ably::Models::ProtocolMessage.new(
action: action,
connection_serial: anonymous_client.connection.serial + 2,
channel: channel_name,
channel_serial: 'validserialprefix:', # with no part after the `:` this indicates the end to the SYNC
presence: [],
timestamp: Time.now.to_i * 1000
)
anonymous_client.connection.__incoming_protocol_msgbus__.publish :protocol_message, sync_msg
# Stop the next SYNC arriving
anonymous_client.connection.__incoming_protocol_msgbus__.unsubscribe
end
end
end
presence_anonymous_client.get do |members|
expect(members.length).to eql(page_size + 2)
expect(members.find { |member| member.client_id == 'a' }).to be_nil
expect(members.find { |member| member.client_id == 'b' }.timestamp.to_i).to eql(now / 1000)
expect(members.find { |member| member.client_id == 'c' }.timestamp.to_i).to eql(now / 1000)
stop_reactor
end
end
end
end
context 'when presence messages are synthesized' do
let(:presence_data) do
[
{ client_id: 'a', connection_id: 'one', id: 'one:0:0', action: enter_action, timestamp: now }, # first messages from client, second fabricated
{ client_id: 'a', connection_id: 'one', id: 'fabricated:0:1', action: leave_action, timestamp: now + 1 }, # leave after enter based on timestamp
{ client_id: 'b', connection_id: 'one', id: 'one:0:2', action: enter_action, timestamp: now }, # first messages from client, second fabricated
{ client_id: 'b', connection_id: 'one', id: 'fabricated:0:3', action: leave_action, timestamp: now - 1 }, # leave before enter based on timestamp
{ client_id: 'c', connection_id: 'one', id: 'fabricated:0:4', action: enter_action, timestamp: now }, # both messages fabricated
{ client_id: 'c', connection_id: 'one', id: 'fabricated:0:5', action: leave_action, timestamp: now - 1 } # leave before enter based on timestamp
]
end
it 'compares based on timestamp if either has a connectionId not part of the presence message id (#RTP2b1)' do
allow_sync_fabricate_data_final_sync_and_assert_members
end
end
context 'when presence messages are not synthesized (events sent from clients)' do
let(:presence_data) do
[
{ client_id: 'a', connection_id: 'one', id: 'one:0:0', action: enter_action, timestamp: now }, # first messages from client
{ client_id: 'a', connection_id: 'one', id: 'one:1:0', action: leave_action, timestamp: now - 1 }, # leave after enter based on msgSerial part of ID
{ client_id: 'b', connection_id: 'one', id: 'one:2:2', action: enter_action, timestamp: now }, # first messages from client
{ client_id: 'b', connection_id: 'one', id: 'one:2:1', action: leave_action, timestamp: now + 1 }, # leave before enter based on index part of ID
{ client_id: 'c', connection_id: 'one', id: 'one:4:4', action: enter_action, timestamp: now }, # first messages from client
{ client_id: 'c', connection_id: 'one', id: 'one:3:5', action: leave_action, timestamp: now + 1 } # leave before enter based on msgSerial part of ID
]
end
it 'compares based on timestamp if either has a connectionId not part of the presence message id (#RTP2b2)' do
allow_sync_fabricate_data_final_sync_and_assert_members
end
end
end
end
context '#sync_complete? and SYNC flags (#RTP1)' do
context 'when attaching to a channel without any members present' do
it 'sync_complete? is true, there is no presence flag, and the presence channel is considered synced immediately (#RTP1)' do
flag_checked = false
anonymous_client.connection.__incoming_protocol_msgbus__.subscribe(:protocol_message) do |protocol_message|
if protocol_message.action == :attached
flag_checked = true
expect(protocol_message.has_presence_flag?).to eql(false)
end
end
channel_anonymous_client.attach do
expect(channel_anonymous_client.presence).to be_sync_complete
EventMachine.next_tick do
expect(flag_checked).to eql(true)
stop_reactor
end
end
end
end
context 'when attaching to a channel with members present' do
it 'sync_complete? is false, there is a presence flag, and the presence channel is subsequently synced (#RTP1)' do
flag_checked = false
anonymous_client.connection.__incoming_protocol_msgbus__.subscribe(:protocol_message) do |protocol_message|
if protocol_message.action == :attached
flag_checked = true
expect(protocol_message.has_presence_flag?).to eql(true)
end
end
presence_client_one.enter
presence_client_one.subscribe(:enter) do
presence_client_one.unsubscribe :enter
channel_anonymous_client.attach do
expect(channel_anonymous_client.presence).to_not be_sync_complete
channel_anonymous_client.presence.get do
expect(channel_anonymous_client.presence).to be_sync_complete
EventMachine.next_tick do
expect(flag_checked).to eql(true)
stop_reactor
end
end
end
end
end
end
end
context '101 existing (present) members on a channel (2 SYNC pages)' do
context 'requiring at least 2 SYNC ProtocolMessages', em_timeout: 40 do
let(:enter_expected_count) { 101 }
let(:present) { [] }
let(:entered) { [] }
let(:sync_pages_received) { [] }
let(:client_one) { auto_close Ably::Realtime::Client.new(client_options.merge(auth_callback: wildcard_token)) }
def setup_members_on(presence)
enter_expected_count.times do |indx|
# 10 messages per second max rate on simulation accounts
rate = indx.to_f / 10
EventMachine.add_timer(rate) do
presence.enter_client("client:#{indx}") do |message|
entered << message
next unless entered.count == enter_expected_count
yield
end
end
end
end
context 'when a client attaches to the presence channel' do
it 'emits :present for each member' do
setup_members_on(presence_client_one) do
presence_anonymous_client.subscribe(:present) do |present_message|
expect(present_message.action).to eq(:present)
present << present_message
next unless present.count == enter_expected_count
expect(present.map(&:client_id).uniq.count).to eql(enter_expected_count)
stop_reactor
end
end
end
context 'and a member enters before the SYNC operation is complete' do
let(:enter_client_id) { random_str }
it 'emits a :enter immediately and the member is :present once the sync is complete (#RTP2g)' do
setup_members_on(presence_client_one) do
member_entered = false
anonymous_client.connect do
presence_anonymous_client.subscribe(:enter) do |member|
expect(member.client_id).to eql(enter_client_id)
member_entered = true
end
presence_anonymous_client.get do |members|
expect(members.find { |member| member.client_id == enter_client_id }.action).to eq(:present)
stop_reactor
end
anonymous_client.connection.transport.__incoming_protocol_msgbus__.subscribe(:protocol_message) do |protocol_message|
if protocol_message.action == :sync
sync_pages_received << protocol_message
if sync_pages_received.count == 1
enter_action = Ably::Models::PresenceMessage::ACTION.Enter
enter_member = Ably::Models::PresenceMessage.new(
'id' => "#{client_one.connection.id}:#{random_str}:0",
'clientId' => enter_client_id,
'connectionId' => client_one.connection.id,
'timestamp' => as_since_epoch(Time.now),
'action' => enter_action
)
presence_anonymous_client.__incoming_msgbus__.publish :presence, enter_member
end
end
end
end
end
end
end
context 'and a member leaves before the SYNC operation is complete' do
it 'emits :leave immediately as the member leaves and cleans up the ABSENT member after (#RTP2f, #RTP2g)' do
all_client_ids = enter_expected_count.times.map { |id| "client:#{id}" }
setup_members_on(presence_client_one) do
leave_member = nil
presence_anonymous_client.subscribe(:present) do |present_message|
present << present_message
all_client_ids.delete(present_message.client_id)
end
presence_anonymous_client.subscribe(:leave) do |leave_message|
expect(leave_message.client_id).to eql(leave_member.client_id)
expect(present.count).to be < enter_expected_count
# Hacky accessing a private method, but absent members are intentionally not exposed to any public APIs
expect(presence_anonymous_client.members.send(:absent_members).length).to eql(1)
presence_anonymous_client.members.once(:in_sync) do
# Check that members count is exact indicating the members with LEAVE action after sync are removed
expect(presence_anonymous_client).to be_sync_complete
expect(presence_anonymous_client.members.length).to eql(enter_expected_count - 1)
presence_anonymous_client.unsubscribe
stop_reactor
end
end
anonymous_client.connect do
anonymous_client.connection.transport.__incoming_protocol_msgbus__.subscribe(:protocol_message) do |protocol_message|
if protocol_message.action == :sync
sync_pages_received << protocol_message
if sync_pages_received.count == 1
leave_action = Ably::Models::PresenceMessage::ACTION.Leave
leave_member = Ably::Models::PresenceMessage.new(
'id' => "#{client_one.connection.id}:#{all_client_ids.first}:0",
'clientId' => all_client_ids.first,
'connectionId' => client_one.connection.id,
'timestamp' => as_since_epoch(Time.now),
'action' => leave_action
)
presence_anonymous_client.__incoming_msgbus__.publish :presence, leave_member
end
end
end
end
end
end
it 'ignores presence events with timestamps / identifiers prior to the current :present event in the MembersMap (#RTP2c)' do
started_at = Time.now
setup_members_on(presence_client_one) do
leave_member = nil
presence_anonymous_client.subscribe(:present) do |present_message|
present << present_message
if present.count == enter_expected_count
presence_anonymous_client.get do |members|
member = members.find { |member| member.client_id == leave_member.client_id}
expect(member).to_not be_nil
expect(member.action).to eq(:present)
EventMachine.add_timer(1) do
presence_anonymous_client.unsubscribe
stop_reactor
end
end
end
end
presence_anonymous_client.subscribe(:leave) do |leave_message|
raise "Leave event for #{leave_message} should not have been fired because it is out of date"
end
anonymous_client.connect do
anonymous_client.connection.transport.__incoming_protocol_msgbus__.subscribe(:protocol_message) do |protocol_message|
if protocol_message.action == :sync
sync_pages_received << protocol_message
if sync_pages_received.count == 1
first_member = protocol_message.presence[0] # get the first member in the SYNC set
leave_action = Ably::Models::PresenceMessage::ACTION.Leave
leave_member = Ably::Models::PresenceMessage.new(
first_member.as_json.merge('action' => leave_action, 'timestamp' => as_since_epoch(started_at))
)
# After the SYNC has started, no inject that member has having left with a timestamp before the sync
presence_anonymous_client.__incoming_msgbus__.publish :presence, leave_member
end
end
end
end
end
end
it 'does not emit :present after the :leave event has been emitted, and that member is not included in the list of members via #get (#RTP2f)' do
left_client = 10
left_client_id = "client:#{left_client}"
setup_members_on(presence_client_one) do
member_left_emitted = false
presence_anonymous_client.subscribe(:present) do |present_message|
if present_message.client_id == left_client_id
raise "Member #{present_message.client_id} should not have been emitted as present"
end
present << present_message.client_id
end
presence_anonymous_client.subscribe(:leave) do |leave_message|
if present.include?(leave_message.client_id)
raise "Member #{leave_message.client_id} should not have been emitted as present previously"
end
expect(leave_message.client_id).to eql(left_client_id)
member_left_emitted = true
end
presence_anonymous_client.get do |members|
expect(members.count).to eql(enter_expected_count - 1)
expect(member_left_emitted).to eql(true)
expect(members.map(&:client_id)).to_not include(left_client_id)
EventMachine.add_timer(1) do
presence_anonymous_client.unsubscribe
stop_reactor
end
end
channel_anonymous_client.attach do
leave_action = Ably::Models::PresenceMessage::ACTION.Leave
fake_leave_presence_message = Ably::Models::PresenceMessage.new(
'id' => "#{client_one.connection.id}:#{left_client_id}:0",
'clientId' => left_client_id,
'connectionId' => client_one.connection.id,
'timestamp' => as_since_epoch(Time.now),
'action' => leave_action
)
# Push out a LEAVE event directly to the Presence object before it's received the :present action via the SYNC ProtocolMessage
presence_anonymous_client.__incoming_msgbus__.publish :presence, fake_leave_presence_message
end
end
end
end
context '#get' do
context 'by default' do
it 'waits until sync is complete (#RTP11c1)', em_timeout: 30 do # allow for slow connections and lots of messages
enter_expected_count.times do |indx|
EventMachine.add_timer(indx / 10) do
presence_client_one.enter_client "client:#{indx}"
end
end
presence_client_one.subscribe(:enter) do |message|
entered << message
next unless entered.count == enter_expected_count
presence_anonymous_client.get do |members|
expect(members.map(&:client_id).uniq.count).to eql(enter_expected_count)
expect(members.count).to eql(enter_expected_count)
stop_reactor
end
end
end
end
context 'with :wait_for_sync option set to false (#RTP11c1)' do
it 'it does not wait for sync', em_timeout: 30 do # allow for slow connections and lots of messages
enter_expected_count.times do |indx|
EventMachine.add_timer(indx / 10) do