/
future_spec.rb
1356 lines (1181 loc) · 41.6 KB
/
future_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'
module Ione
describe Promise do
let :promise do
described_class.new
end
let :future do
promise.future
end
let :error do
StandardError.new('bork')
end
describe '#fulfill' do
it 'resolves its future' do
promise.fulfill
future.should be_resolved
end
it 'raises an error if fulfilled a second time' do
promise.fulfill
expect { promise.fulfill }.to raise_error(FutureError)
end
it 'raises an error if failed after being fulfilled' do
promise.fulfill
expect { promise.fail(error) }.to raise_error(FutureError)
end
it 'returns nil' do
promise.fulfill(:foo).should be_nil
end
end
describe '#fail' do
it 'fails its future' do
promise.fail(error)
future.should be_failed
end
it 'raises an error if failed a second time' do
promise.fail(error)
expect { promise.fail(error) }.to raise_error(FutureError)
end
it 'raises an error if fulfilled after being failed' do
promise.fail(error)
expect { promise.fulfill }.to raise_error(FutureError)
end
it 'returns nil' do
promise.fail(error).should be_nil
end
end
describe '#observe' do
it 'resolves its future when the specified future is resolved' do
p2 = Promise.new
promise.observe(p2.future)
p2.fulfill
promise.future.should be_resolved
end
it 'fails its future when the specified future fails' do
p2 = Promise.new
promise.observe(p2.future)
p2.fail(error)
promise.future.should be_failed
end
it 'silently ignores double fulfillment/failure' do
p2 = Promise.new
promise.observe(p2.future)
promise.fail(error)
p2.fulfill
end
it 'returns nil' do
promise.observe(Promise.new.future).should be_nil
end
end
describe '#try' do
it 'fulfills the promise with the result of the block' do
promise.try do
3 + 4
end
promise.future.value.should == 7
end
it 'fails the promise when the block raises an error' do
promise.try do
raise error
end
expect { promise.future.value }.to raise_error(/bork/)
end
it 'calls the block with the specified arguments' do
promise.try(:foo, 3) do |a, b|
a.length + b
end
promise.future.value.should == 6
end
it 'returns nil' do
promise.try { }.should be_nil
end
end
end
describe Future do
let :promise do
Promise.new
end
let :future do
promise.future
end
let :error do
StandardError.new('bork')
end
def async(*context, &listener)
Thread.start(*context, &listener)
end
def delayed(*context, &listener)
async(*context) do |*ctx|
sleep(0.1)
listener.call(*context)
end
end
describe '#completed?' do
it 'is true when the promise is fulfilled' do
promise.fulfill
future.should be_completed
end
it 'is true when the promise is failed' do
promise.fail(StandardError.new('bork'))
future.should be_completed
end
it 'is false before the future has been resolved or failed' do
future.should_not be_completed
end
end
describe '#resolved?' do
it 'is true when the promise is fulfilled' do
promise.fulfill('foo')
future.should be_resolved
end
it 'is true when the promise is fulfilled with something falsy' do
promise.fulfill(nil)
future.should be_resolved
end
it 'is false when the promise is failed' do
promise.fail(StandardError.new('bork'))
future.should_not be_resolved
end
it 'is false before the future has been resolved or failed' do
future.should_not be_resolved
end
end
describe '#failed?' do
it 'is true when the promise is failed' do
promise.fail(error)
future.should be_failed
end
it 'is false when the promise is fulfilled' do
promise.fulfill
future.should_not be_failed
end
it 'is false before the future has been resolved or failed' do
future.should_not be_failed
end
end
describe '#on_complete' do
context 'registers listeners and' do
it 'notifies all listeners when the promise is fulfilled' do
c1, c2 = false, false
future.on_complete { c1 = true }
future.on_complete { c2 = true }
promise.fulfill('bar')
c1.should be_true
c2.should be_true
end
it 'passes the future as the first parameter to the block' do
f1, f2 = nil, nil
future.on_complete { |f| f1 = f }
future.on_complete { |f| f2 = f }
promise.fulfill('bar')
f1.should equal(future)
f2.should equal(future)
end
it 'passes the value as the first parameter to the block when it expects two arguments' do
v1, v2 = nil, nil
future.on_complete { |v, _| v1 = v }
future.on_complete { |v, _| v2 = v }
promise.fulfill('bar')
v1.should == 'bar'
v2.should == 'bar'
end
it 'passes future as the third parameter to the block when it expects three arguments' do
f1, f2 = nil, nil
future.on_complete { |_, _, f| f1 = f }
future.on_complete { |_, _, f| f2 = f }
promise.fulfill('bar')
f1.should equal(future)
f2.should equal(future)
end
it 'passes the value, error and future to the block when it expects any number of arguments' do
value = 'bar'
error = StandardError.new('bork')
args1, args2 = nil, nil
p1 = Promise.new
p2 = Promise.new
p1.future.on_complete { |*a| args1 = a }
p2.future.on_complete { |*a| args2 = a }
p1.fulfill(value)
p2.fail(error)
args1[0].should equal(value)
args1[1].should be_nil
args1[2].should equal(p1.future)
args2[0].should be_nil
args2[1].should equal(error)
args2[2].should equal(p2.future)
end
it 'passes the value, error and future to the listener when the listener is a lambda' do
value = 'bar'
error = StandardError.new('bork')
args1, args2 = nil, nil
p1 = Promise.new
p2 = Promise.new
p1.future.on_complete(&lambda { |v, e, f| args1 = [v, e, f] })
p2.future.on_complete(&lambda { |v, e, f| args2 = [v, e, f] })
p1.fulfill(value)
p2.fail(error)
args1[0].should equal(value)
args1[1].should be_nil
args1[2].should equal(p1.future)
args2[0].should be_nil
args2[1].should equal(error)
args2[2].should equal(p2.future)
end
it 'ignores optional arguments for lambdas' do
value = 'bar'
error = StandardError.new('bork')
args1, args2 = nil, nil
p1 = Promise.new
p2 = Promise.new
p1.future.on_complete(&lambda { |v, e, f=nil| args1 = [v, e, f] })
p2.future.on_complete(&lambda { |v, e, f, x=1, y=2| args2 = [v, e, f, x, y] })
p1.fulfill(value)
p2.fail(error)
args1[0].should equal(value)
args1[1].should be_nil
args1[2].should be_nil
args2[0].should be_nil
args2[1].should equal(error)
args2[2].should equal(p2.future)
end
it 'does not handle listeners that are lambdas and have one optional argument' do
pending 'MRI 1.9.3 thinks these lambas have arity 1 and not -2' if RUBY_ENGINE == 'ruby' && RUBY_VERSION < '2.0.0'
called1, called2 = false, false
p1 = Promise.new
p2 = Promise.new
p1.future.on_complete(&lambda { |v, e=nil| called1 = true })
p2.future.on_complete(&lambda { |v, e=nil| called2 = true })
p1.fulfill('bar')
p2.fail(StandardError.new('bork'))
called1.should be_false
called2.should be_false
end
it 'notifies all listeners when the promise fails' do
c1, c2 = nil, nil
future.on_complete { c1 = true }
future.on_complete { c2 = true }
future.fail(error)
c1.should be_true
c2.should be_true
end
it 'passes the error as the second parameter to the block when it expects two arguments' do
e1, e2 = nil, nil
future.on_complete { |_, e| e1 = e }
future.on_complete { |_, e| e2 = e }
future.fail(error)
e1.should equal(error)
e2.should equal(error)
end
it 'notifies all listeners when the promise is fulfilled, even when one raises an error' do
value = nil
future.on_complete { |f| raise 'Blurgh' }
future.on_complete { |f| value = f.value }
promise.fulfill('bar')
value.should == 'bar'
end
it 'notifies all listeners when the promise fails, even when one raises an error' do
err = nil
future.on_complete { |f| raise 'Blurgh' }
future.on_complete { |f| begin; f.value; rescue => err; e = err; end }
promise.fail(error)
err.message.should == 'bork'
end
it 'notifies listeners registered after the promise was fulfilled' do
f, v, e = nil, nil, nil
promise.fulfill('bar')
future.on_complete { |vv, ee, ff| v = vv; e = ee; f = ff }
f.should equal(future)
v.should == 'bar'
e.should be_nil
end
it 'notifies listeners registered after the promise failed' do
f, v, e = nil, nil, nil
promise.fail(StandardError.new('bork'))
future.on_complete { |vv, ee, ff| v = vv; e = ee; f = ff }
f.should equal(future)
v.should be_nil
e.message.should == 'bork'
end
it 'notifies listeners registered after the promise failed' do
promise.fail(error)
expect { future.on_complete { raise 'blurgh' } }.to_not raise_error
end
it 'returns nil' do
future.on_complete { :foo }.should be_nil
end
it 'returns nil when the future is already resolved' do
promise.fulfill
future.on_complete { :foo }.should be_nil
end
it 'returns nil when the future already has failed' do
promise.fail(error)
future.on_complete { :foo }.should be_nil
end
end
end
describe '#on_value' do
context 'registers listeners and' do
it 'notifies all value listeners when the promise is fulfilled' do
v1, v2 = nil, nil
future.on_value { |v| v1 = v }
future.on_value { |v| v2 = v }
promise.fulfill('bar')
v1.should == 'bar'
v2.should == 'bar'
end
it 'notifies all listeners even when one raises an error' do
value = nil
future.on_value { |v| raise 'Blurgh' }
future.on_value { |v| value = v }
promise.fulfill('bar')
value.should == 'bar'
end
it 'notifies listeners registered after the promise was resolved' do
v1, v2 = nil, nil
promise.fulfill('bar')
future.on_value { |v| v1 = v }
future.on_value { |v| v2 = v }
v1.should == 'bar'
v2.should == 'bar'
end
it 'does not raise any error when the listener raises an error when already resolved' do
promise.fulfill('bar')
expect { future.on_value { |v| raise 'blurgh' } }.to_not raise_error
end
it 'returns nil' do
future.on_value { :foo }.should be_nil
end
it 'returns nil when the future is already resolved' do
promise.fulfill
future.on_failure { :foo }.should be_nil
end
end
end
describe '#on_failure' do
context 'registers listeners and' do
it 'notifies all failure listeners when the promise fails' do
e1, e2 = nil, nil
future.on_failure { |err| e1 = err }
future.on_failure { |err| e2 = err }
promise.fail(error)
e1.message.should eql(error.message)
e2.message.should eql(error.message)
end
it 'notifies all listeners even if one raises an error' do
e = nil
future.on_failure { |err| raise 'Blurgh' }
future.on_failure { |err| e = err }
promise.fail(error)
e.message.should eql(error.message)
end
it 'notifies new listeners even when already failed' do
e1, e2 = nil, nil
promise.fail(error)
future.on_failure { |e| e1 = e }
future.on_failure { |e| e2 = e }
e1.message.should eql(error.message)
e2.message.should eql(error.message)
end
it 'does not raise any error when the listener raises an error when already failed' do
promise.fail(error)
expect { future.on_failure { |e| raise 'Blurgh' } }.to_not raise_error
end
it 'returns nil' do
future.on_failure { :foo }.should be_nil
end
it 'returns nil when the future already has failed' do
promise.fail(error)
future.on_failure { :foo }.should be_nil
end
end
end
describe '#value' do
it 'is nil by default' do
promise.fulfill
future.value.should be_nil
end
it 'is the object passed to Promise#fulfill' do
obj = 'hello world'
promise.fulfill(obj)
future.value.should equal(obj)
end
it 'raises the error passed to Promise#fail' do
promise.fail(StandardError.new('bork'))
expect { future.value }.to raise_error(/bork/)
end
it 'blocks until the promise is completed' do
d = delayed(promise) do |p|
p.fulfill('bar')
end
d.value
future.value.should == 'bar'
end
it 'blocks on #value until completed, when value is nil' do
d = delayed(promise) do |p|
p.fulfill
end
d.value
future.value.should be_nil
end
it 'blocks on #value until failed' do
d = delayed(promise) do |p|
p.fail(StandardError.new('bork'))
end
d.value
expect { future.value }.to raise_error('bork')
end
it 'allows multiple threads to block on #value until completed' do
listeners = Array.new(10) do
async(future) do |f|
f.value
end
end
sleep 0.1
promise.fulfill(:hello)
listeners.map(&:value).should == Array.new(10, :hello)
end
it 'is aliased as #get' do
obj = 'hello world'
promise.fulfill(obj)
future.get.should equal(obj)
end
end
describe '#map' do
context 'returns a new future that' do
it 'will be resolved with the result of the given block' do
mapped_value = nil
p = Promise.new
f = p.future.map { |v| v * 2 }
f.on_value { |v| mapped_value = v }
p.fulfill(3)
mapped_value.should == 3 * 2
end
it 'will be resolved with the specified value' do
mapped_value = nil
p = Promise.new
f = p.future.map(7)
f.on_value { |v| mapped_value = v }
p.fulfill(3)
mapped_value.should == 7
end
it 'will be resolved with the result of the given block, even if a value is specified' do
mapped_value = nil
p = Promise.new
f = p.future.map(7) { |v| v * 2 }
f.on_value { |v| mapped_value = v }
p.fulfill(3)
mapped_value.should == 3 * 2
end
it 'will be resolved with nil when neither value nor block is specified' do
mapped_value = 3
p = Promise.new
f = p.future.map
f.on_value { |v| mapped_value = v }
p.fulfill(3)
mapped_value.should be_nil
end
it 'fails when the original future fails' do
failed = false
p = Promise.new
f = p.future.map { |v| v * 2 }
f.on_failure { failed = true }
p.fail(StandardError.new('Blurgh'))
failed.should be_true
end
it 'fails when the block raises an error' do
p = Promise.new
f = p.future.map { |v| raise 'blurgh' }
d = delayed do
p.fulfill
end
d.value
expect { f.value }.to raise_error('blurgh')
end
end
end
describe '#flat_map' do
context 'returns a future that' do
it 'passes the value of the source future to the block, and resolves to the value of the future returned by the block' do
p = Promise.new
f = p.future.flat_map { |v| Future.resolved(v * 2) }
p.fulfill(3)
f.value.should == 3 * 2
end
it 'fails when the block raises an error' do
p = Promise.new
f = p.future.flat_map { |v| raise 'Hurgh' }
p.fulfill(3)
expect { f.value }.to raise_error('Hurgh')
end
end
it 'accepts anything that implements #on_complete as a chained future' do
fake_future = double(:fake_future)
fake_future.stub(:on_complete) { |&listener| listener.call(:foobar, nil) }
p = Promise.new
f = p.future.flat_map { fake_future }
p.fulfill
f.value.should == :foobar
end
end
describe '#then' do
context 'when the block returns a future' do
it 'works like #flat_map' do
p = Promise.new
f = p.future.then { |v| Future.resolved(v * 2) }
p.fulfill(3)
f.value.should == 3 * 2
end
end
context 'when the block returns something that quacks like a future' do
context 'and yields a value from #on_complete' do
it 'works like #flat_map' do
fake_future = double(:fake_future)
fake_future.stub(:on_complete) { |&listener| listener.call(:foobar) }
p = Promise.new
f = p.future.then { |v| fake_future }
p.fulfill
f.value.should == :foobar
end
end
context 'and yields an error from #on_complete' do
it 'works like #flat_map' do
fake_future = double(:fake_future)
fake_future.stub(:on_complete) { |&listener| listener.call(nil, StandardError.new('bork')) }
p = Promise.new
f = p.future.then { |v| fake_future }
p.fulfill
expect { f.value }.to raise_error(StandardError, 'bork')
end
end
end
context 'when the block returns something that does not quack like a future' do
it 'works like #map' do
p = Promise.new
f = p.future.then { |v| v * 2 }
p.fulfill(3)
f.value.should == 3 * 2
end
end
it 'returns a failed future when the block raises an error' do
p = Promise.new
f = p.future.then { |v| raise 'blurgh' }
d = delayed do
p.fulfill
end
d.value
expect { f.value }.to raise_error('blurgh')
end
end
describe '#recover' do
context 'returns a new future that' do
it 'resolves to a value created by the block when the source future fails' do
p = Promise.new
f = p.future.recover { 'foo' }
p.fail(error)
f.value.should == 'foo'
end
it 'resolves to a specfied value when the source future fails' do
p = Promise.new
f = p.future.recover('bar')
p.fail(error)
f.value.should == 'bar'
end
it 'resovles to a value created by the block even when a value is specified when the source future fails' do
p = Promise.new
f = p.future.recover('bar') { 'foo' }
p.fail(error)
f.value.should == 'foo'
end
it 'resolves to nil value when no value nor block is specified and the source future fails' do
p = Promise.new
f = p.future.recover
p.fail(error)
f.value.should be_nil
end
it 'yields the error to the block' do
p = Promise.new
f = p.future.recover { |e| e.message }
p.fail(error)
f.value.should == error.message
end
it 'resolves to the value of the source future when the source future is resolved' do
p = Promise.new
f = p.future.recover { 'foo' }
p.fulfill('bar')
f.value.should == 'bar'
end
it 'fails with the error raised in the given block' do
p = Promise.new
f = p.future.recover { raise 'snork' }
p.fail(StandardError.new('bork'))
expect { f.value }.to raise_error('snork')
end
end
end
describe '#fallback' do
context 'returns a new future that' do
it 'is resolved with the value of the fallback future when the source future fails' do
p1 = Promise.new
p2 = Promise.new
f = p1.future.fallback { p2.future }
p1.fail(error)
p2.fulfill('foo')
f.value.should == 'foo'
end
it 'yields the error to the block' do
p1 = Promise.new
p2 = Promise.new
f = p1.future.fallback do |error|
Future.resolved(error.message)
end
p1.fail(error)
f.value.should == error.message
end
it 'is resolved with the value of the source future when the source future fullfills' do
p1 = Promise.new
p2 = Promise.new
f = p1.future.fallback { p2.future }
p2.fulfill('bar')
p1.fulfill('foo')
f.value.should == 'foo'
end
it 'fails when the block raises an error' do
p = Promise.new
f = p.future.fallback { raise 'bork' }
p.fail(StandardError.new('splork'))
expect { f.value }.to raise_error('bork')
end
it 'fails when the fallback future fails' do
p1 = Promise.new
p2 = Promise.new
f = p1.future.fallback { p2.future }
p2.fail(StandardError.new('bork'))
p1.fail(StandardError.new('fnork'))
expect { f.value }.to raise_error('bork')
end
it 'accepts anything that implements #on_complete as a fallback future' do
fake_future = double(:fake_future)
fake_future.stub(:on_complete) { |&listener| listener.call('foo', nil) }
p = Promise.new
f = p.future.fallback { fake_future }
p.fail(error)
f.value.should == 'foo'
end
end
end
describe '.traverse' do
it 'combines Array#map and Future.all' do
future = Future.traverse([1, 2, 3]) do |element|
Future.resolved(element * 2)
end
future.value.should == [2, 4, 6]
end
it 'fails if any of the source futures fail' do
future = Future.traverse([1, 2, 3]) do |element|
if element == 2
Future.failed(StandardError.new('BORK'))
else
Future.resolved(element * 2)
end
end
future.should be_failed
end
it 'fails if any of the block invocations fail' do
future = Future.traverse([1, 2, 3]) do |element|
if element == 2
raise 'BORK'
else
Future.resolved(element * 2)
end
end
future.should be_failed
end
it 'accepts anything that implements #on_complete as futures' do
fake_future = double(:fake_future)
fake_future.stub(:on_complete) { |&listener| listener.call(:foobar, nil) }
future = Future.traverse([1, 2, 3]) { fake_future }
future.value.should == [:foobar, :foobar, :foobar]
end
it 'accepts an enumerable of values' do
future = Future.traverse([1, 2, 3].to_enum) { |v| Future.resolved(v * 2) }
future.value.should == [2, 4, 6]
end
end
describe '.reduce' do
it 'returns a future which represents the value of reducing the values of the inputs' do
futures = [
Future.resolved({'foo' => 'bar'}),
Future.resolved({'qux' => 'baz'}),
Future.resolved({'hello' => 'world'})
]
future = Future.reduce(futures, {}) do |accumulator, value|
accumulator.merge(value)
end
future.value.should == {'foo' => 'bar', 'qux' => 'baz', 'hello' => 'world'}
end
it 'accepts boolean accumulators' do
futures = [
Future.resolved([:foo]),
Future.resolved([]),
]
future = Future.reduce(futures, false) do |accumulator, value|
accumulator || value.empty?
end
future.value.should == true
end
it 'calls the block with the values in the order of the source futures' do
promises = [Promise.new, Promise.new, Promise.new, Promise.new, Promise.new]
futures = promises.map(&:future)
future = Future.reduce(futures, []) do |accumulator, value|
accumulator.push(value)
end
promises[1].fulfill(1)
promises[0].fulfill(0)
promises[2].fulfill(2)
promises[4].fulfill(4)
promises[3].fulfill(3)
future.value.should == [0, 1, 2, 3, 4]
end
it 'uses the first value as initial value when no intial value is given' do
promises = [Promise.new, Promise.new, Promise.new]
futures = promises.map(&:future)
future = Future.reduce(futures) do |sum, n|
sum + n
end
promises[1].fulfill(2)
promises[0].fulfill(1)
promises[2].fulfill(3)
future.value.should == 6
end
it 'fails if any of the source futures fail' do
futures = [Future.resolved(0), Future.failed(StandardError.new('BORK')), Future.resolved(2)]
future = Future.reduce(futures, []) do |accumulator, value|
accumulator.push(value)
end
future.should be_failed
end
it 'fails if any of the block invocations fail' do
futures = [Future.resolved(0), Future.resolved(1), Future.resolved(2)]
future = Future.reduce(futures, []) do |accumulator, value|
if value == 2
raise 'BORK'
else
accumulator.push(value)
end
end
future.should be_failed
end
it 'allows invocations to return nil' do
futures = [Future.resolved(1), Future.resolved(2), Future.resolved(3)]
future = Future.reduce(futures, []) do |accumulator, value|
if value == 2
nil
else
value
end
end
future.value.should eq(3)
end
context 'when the list of futures is empty' do
it 'returns a future that resolves to the initial value' do
Future.reduce([], :foo).value.should == :foo
end
it 'returns a future that resolves to nil there is also no initial value' do
Future.reduce([]).value.should be_nil
end
end
it 'accepts anything that implements #on_complete as futures' do
ff1, ff2, ff3 = double, double, double
ff1.stub(:on_complete) { |&listener| listener.call(1, nil) }
ff2.stub(:on_complete) { |&listener| listener.call(2, nil) }
ff3.stub(:on_complete) { |&listener| listener.call(3, nil) }
future = Future.reduce([ff1, ff2, ff3], 0) { |sum, n| sum + n }
future.value.should == 6
end
it 'accepts an enumerable of futures' do
futures = [Future.resolved(1), Future.resolved(2), Future.resolved(3)].to_enum
future = Future.reduce(futures, 0) { |sum, n| sum + n }
future.value.should == 6
end
it 'handles a really long list of futures' do
futures = Array.new(10000, Future.resolved(1))
future = Future.reduce(futures, 0, &:+)
future.value.should eq(10000)
end
context 'when the :ordered option is false' do
it 'calls the block with the values in the order of completion, when the :ordered option is false' do
promises = [Promise.new, Promise.new, Promise.new]
futures = promises.map(&:future)
future = Future.reduce(futures, [], ordered: false) do |accumulator, value|
accumulator.push(value)
end
promises[1].fulfill(1)
promises[0].fulfill(0)
promises[2].fulfill(2)
future.value.should == [1, 0, 2]
end
it 'fails if any of the source futures fail' do
futures = [Future.resolved(0), Future.failed(StandardError.new('BORK')), Future.resolved(2)]
future = Future.reduce(futures, [], ordered: false) do |accumulator, value|
accumulator.push(value)
end
future.should be_failed
end
it 'fails if any of the block invocations fail' do
futures = [Future.resolved(0), Future.resolved(1), Future.resolved(2)]
future = Future.reduce(futures, [], ordered: false) do |accumulator, value|
if value == 1
raise 'BORK'
else
accumulator.push(value)
end
end
future.should be_failed
end
it 'handles a really long list of futures' do
futures = Array.new(10000, Future.resolved(1))
future = Future.reduce(futures, 0, ordered: false, &:+)
future.value.should eq(10000)
end
context 'when the list of futures is empty' do
it 'returns a future that resolves to the initial value' do
Future.reduce([], :foo, ordered: false).value.should == :foo
end
it 'returns a future that resolves to nil there is also no initial value' do
Future.reduce([], nil, ordered: false).value.should be_nil
end
end
end
end
describe '.after' do
context 'returns a new future which' do
it 'is resolved when the source futures are resolved' do
p1 = Promise.new
p2 = Promise.new
f = Future.after(p1.future, p2.future)
p1.fulfill
f.should_not be_resolved
p2.fulfill
f.should be_resolved
end
it 'returns no value' do
p1 = Promise.new
p2 = Promise.new
p3 = Promise.new
f = Future.after(p1.future, p2.future, p3.future)
p2.fulfill(2)
p1.fulfill(1)
p3.fulfill(3)
f.value.should be_nil
end
it 'fails if any of the source futures fail' do
p1 = Promise.new