require'spec_helper'require'event_shipper'require'event_shipper/udp'describeEventShipper::UDPdoincludeEventShipper::Protocollet(:udp){described_class.new('127.0.0.1',5050)}describe"#send(hash)"dolet(:socket){UDPSocket.new}it"turns the hash into a bson string and sends it"dosocket.bind'127.0.0.1',5050msg,info=nil,nilready=falseThread.startdomsg,info=socket.recvfrom(10*1024)ready=trueendudp.send(a:1)sleep0.01untilreadytransmission=parse_transmission(msg)transmission.version.should==1transmission.events.first.tap{|event|JSON.parse(event.json).should=={'a'=>1}}endenddescribe"#handle_message(string)"dolet(:msg){udp.encode(key:'value')}it"yields a decoded message to the block"dobutton=flexmock('button').tap{|fm|fm.should_receive(:press).once}udp.handle_message(msg)do|queue,hash|hash['key'].should=='value'button.pressendendit"ignores messages that cannot be decoded"dobutton=flexmock('button').tap{|fm|fm.should_receive(:press).never}udp.handle_message('invalid message')dobutton.pressendendendclassSeqFilter@call_seq=[]class<<selfattr_accessor:call_seqenddefinitializename@name=nameenddefenobjobj.tap{self.class.call_seq<<@name}enddefdeobjobj.tap{self.class.call_seq<<@name}endenddescribe'#encode'dolet(:a){SeqFilter.new(:a)}let(:b){SeqFilter.new(:b)}it'calls filters in order'doSeqFilter.call_seq=[]udp.wrapaudp.wrapbudp.encode({})SeqFilter.call_seq.should==[:a,:b]endenddescribe'#decode'dolet(:c){SeqFilter.new(:c)}let(:d){SeqFilter.new(:d)}it'calls filters in reverse order'doSeqFilter.call_seq=[]udp.wrapcudp.wrapdudp.decode(udp.encode({}))SeqFilter.call_seq.should==[:c,:d,:d,:c]endit'returns nil if message is invalid'doudp.decode('invalid_message').shouldbe_nilendendend