|
| 1 | +require 'spec_helper' |
| 2 | + |
| 3 | +def shared_actor_test_class |
| 4 | + Class.new do |
| 5 | + include Concurrent::ActorContext |
| 6 | + def receive(*msg) |
| 7 | + case msg.first |
| 8 | + when :poison |
| 9 | + raise StandardError |
| 10 | + when :terminate |
| 11 | + Thread.current.kill |
| 12 | + when :sleep |
| 13 | + sleep(msg.last) |
| 14 | + when :check |
| 15 | + msg[1].set(msg.last) |
| 16 | + else |
| 17 | + msg.first |
| 18 | + end |
| 19 | + end |
| 20 | + end |
| 21 | +end |
| 22 | + |
| 23 | +share_examples_for :actor_ref do |
| 24 | + |
| 25 | + it 'includes ActorRef' do |
| 26 | + subject.should be_a Concurrent::ActorRef |
| 27 | + end |
| 28 | + |
| 29 | + context 'running and shutdown' do |
| 30 | + |
| 31 | + specify { subject.should respond_to :shutdown } |
| 32 | + |
| 33 | + specify { subject.should be_running } |
| 34 | + |
| 35 | + specify { subject.should_not be_shutdown } |
| 36 | + |
| 37 | + specify do |
| 38 | + subject.shutdown |
| 39 | + sleep(0.1) |
| 40 | + subject.should be_shutdown |
| 41 | + end |
| 42 | + end |
| 43 | + |
| 44 | + context '#post' do |
| 45 | + |
| 46 | + it 'raises an exception when the message is empty' do |
| 47 | + expect { |
| 48 | + subject.post |
| 49 | + }.to raise_error(ArgumentError) |
| 50 | + end |
| 51 | + |
| 52 | + it 'returns an IVar' do |
| 53 | + subject.post(:foo).should be_a Concurrent::IVar |
| 54 | + end |
| 55 | + |
| 56 | + it 'fulfills the IVar when message is processed' do |
| 57 | + ivar = subject.post(:foo) |
| 58 | + sleep(0.1) |
| 59 | + ivar.should be_fulfilled |
| 60 | + ivar.value.should eq :foo |
| 61 | + end |
| 62 | + |
| 63 | + it 'rejects the IVar when message processing fails' do |
| 64 | + ivar = subject.post(:poison) |
| 65 | + sleep(0.1) |
| 66 | + ivar.should be_rejected |
| 67 | + ivar.reason.should be_a StandardError |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + context '#<<' do |
| 72 | + |
| 73 | + it 'posts the message' do |
| 74 | + ivar = Concurrent::IVar.new |
| 75 | + subject << [:check, ivar, :foo] |
| 76 | + ivar.value(0.1).should eq :foo |
| 77 | + end |
| 78 | + |
| 79 | + it 'returns self' do |
| 80 | + (subject << [1,2,3,4]).should eq subject |
| 81 | + end |
| 82 | + end |
| 83 | + |
| 84 | + context '#post with callback' do |
| 85 | + |
| 86 | + specify 'on success calls the callback with time and value' do |
| 87 | + expected_value = expected_reason = nil |
| 88 | + subject.post(:foo) do |time, value, reason| |
| 89 | + expected_value = value |
| 90 | + expected_reason = reason |
| 91 | + end |
| 92 | + sleep(0.1) |
| 93 | + |
| 94 | + expected_value.should eq :foo |
| 95 | + expected_reason.should be_nil |
| 96 | + end |
| 97 | + |
| 98 | + specify 'on failure calls the callback with time and reason' do |
| 99 | + expected_value = expected_reason = nil |
| 100 | + subject.post(:poison) do |time, value, reason| |
| 101 | + expected_value = value |
| 102 | + expected_reason = reason |
| 103 | + end |
| 104 | + sleep(0.1) |
| 105 | + |
| 106 | + expected_value.should be_nil |
| 107 | + expected_reason.should be_a StandardError |
| 108 | + end |
| 109 | + end |
| 110 | + |
| 111 | + context '#post!' do |
| 112 | + |
| 113 | + it 'raises an exception when the message is empty' do |
| 114 | + expect { |
| 115 | + subject.post!(1) |
| 116 | + }.to raise_error(ArgumentError) |
| 117 | + end |
| 118 | + |
| 119 | + it 'blocks for up to the given number of seconds' do |
| 120 | + start = Time.now.to_f |
| 121 | + begin |
| 122 | + subject.post!(1, :sleep, 2) |
| 123 | + rescue |
| 124 | + end |
| 125 | + delta = Time.now.to_f - start |
| 126 | + delta.should >= 1 |
| 127 | + delta.should <= 2 |
| 128 | + end |
| 129 | + |
| 130 | + it 'blocks forever when the timeout is nil' do |
| 131 | + start = Time.now.to_f |
| 132 | + begin |
| 133 | + subject.post!(nil, :sleep, 1) |
| 134 | + rescue |
| 135 | + end |
| 136 | + delta = Time.now.to_f - start |
| 137 | + delta.should > 1 |
| 138 | + end |
| 139 | + |
| 140 | + it 'raises a TimeoutError when timeout is zero' do |
| 141 | + expect { |
| 142 | + subject.post!(0, :foo) |
| 143 | + }.to raise_error(Concurrent::TimeoutError) |
| 144 | + end |
| 145 | + |
| 146 | + it 'raises a TimeoutError when the timeout is reached' do |
| 147 | + expect { |
| 148 | + subject.post!(1, :sleep, 10) |
| 149 | + }.to raise_error(Concurrent::TimeoutError) |
| 150 | + end |
| 151 | + |
| 152 | + it 'returns the result of success processing' do |
| 153 | + subject.post!(1, :foo).should eq :foo |
| 154 | + end |
| 155 | + |
| 156 | + it 'bubbles exceptions thrown during processing' do |
| 157 | + expect { |
| 158 | + subject.post!(1, :poison) |
| 159 | + }.to raise_error(StandardError) |
| 160 | + end |
| 161 | + end |
| 162 | +end |
0 commit comments