Пример #1
0
    def test_lib_net_link_tx(self):
        class DstLink(s_net.Link):
            def __init__(self):
                s_net.Link.__init__(self)
                self.callcount = 0
            def tx(self, mesg):
                self.callcount += 1

        dstlink = DstLink()
        link = s_net.Link(link=dstlink)
        self.none(link.txtime)
        self.eq(dstlink.callcount, 0)

        link.tx(('cool', {}))
        first_txtime = link.txtime
        self.ge(first_txtime, 0)
        self.eq(dstlink.callcount, 1)

        link.rxfini()
        link.txfini(data=('cool', {}))
        second_txtime = link.txtime
        self.ge(second_txtime, first_txtime)
        self.eq(dstlink.callcount, 2)

        link.tx(('cool', {}))
        self.eq(link.txtime, second_txtime)
        self.eq(dstlink.callcount, 2)
Пример #2
0
    def test_lib_net_link_rx_unknownmsgtype(self):
        link = s_net.Link()
        msg = 'unknown message type haha'
        with self.getLoggerStream('synapse.lib.net', msg) as stream:
            link.rx(None, ('haha', {}))
            self.true(stream.wait(10))

        stream.seek(0)
        self.isin(msg, stream.read())
Пример #3
0
    def test_lib_net_link_onrx(self):
        msg = 'woohoo'
        def fn(self, *args, **kwargs):
            logger.error('woohoo')

        link = s_net.Link()
        link.onrx(fn)
        self.eq(link.rxfunc, fn)

        with self.getLoggerStream('synapse.tests.test_lib_net', msg) as stream:
            link.rx(None, ('haha', {}))
            self.true(stream.wait(10))

        stream.seek(0)
        self.isin(msg, stream.read())
Пример #4
0
    def test_lib_net_chan_queue(self):
        chan = s_net.Chan(s_net.Link(), None)

        self.raises(AttributeError, chan.next)
        self.raises(AttributeError, chan.slice, 1337)
        with self.raises(AttributeError) as e:
            [logger.error(item) for item in chan.iter()]

        chan.setq()
        msgs = (
            ('woo1', {}),
            ('woo2', {}),
            ('woo3', {}),
            ('woo4', {}),
            ('woo5', {}),
        )

        [chan.rx(None, msg) for msg in msgs]
        self.eq(chan.next(timeout=1), msgs[0])
        self.eq(chan.next(timeout=1), msgs[1])
        self.eq(chan.next(timeout=1), msgs[2])
        self.eq(chan.next(timeout=1), msgs[3])
        self.eq(chan.next(timeout=1), msgs[4])
        self.raises(s_exc.TimeOut, chan.next, timeout=0.01)

        [chan.rx(None, msg) for msg in msgs]

        self.eq(chan.slice(4), [msgs[0], msgs[1], msgs[2], msgs[3]])
        self.eq(chan.next(timeout=1), msgs[4])

        self.raises(s_exc.TimeOut, chan.next, timeout=0.01)
        self.raises(s_exc.TimeOut, chan.slice, 100, timeout=0.01)

        #results = []
        #[chan.rx(None, msg) for msg in msgs]
        #[results.append(item) for item in chan.iter(timeout=1)]
        #self.eq(results, list(msgs))
        #[results.append(item) for item in chan.iter(timeout=1)]
        #self.eq(results, list(msgs))

        self.false(chan._chan_rxq._que_done)
        chan.rxfini()
        self.true(chan._chan_rxq._que_done)
        [chan.rx(None, msg) for msg in msgs]
        self.raises(s_exc.IsFini, chan.next, timeout=0.01)
Пример #5
0
 def test_lib_net_link_props(self):
     link = s_net.Link()
     self.none(link.getLinkProp('nope'))
     self.eq(link.getLinkProp('nope', defval=1337), 1337)
     self.none(link.setLinkProp('nope', 1337))
     self.eq(link.getLinkProp('nope'), 1337)
Пример #6
0
 def test_lib_net_link_repr(self):
     link = s_net.Link()
     rep = link.__repr__()
     self.len(28, rep)
     self.true(rep.startswith('Link: None at 0x'))