Exemplo n.º 1
0
def _getIoLoop():
    '''
    Get a RemCycle module local IOLoop.

    Returns:
        t_ioloop.IoLoop: A Tornado IOLoop.
    '''
    global ioloop
    if ioloop is None:
        ioloop = t_ioloop.IOLoop()
        s_threads.worker(ioloop.start)
    return ioloop
Exemplo n.º 2
0
    def test_eventbus_waitfini(self):

        ebus = s_eventbus.EventBus()

        self.false(ebus.waitfini(timeout=0.1))

        def callfini():
            time.sleep(0.1)
            ebus.fini()

        thr = s_threads.worker(callfini)
        # actually wait...
        self.true(ebus.waitfini(timeout=0.3))

        # bounce off the isfini block
        self.true(ebus.waitfini(timeout=0.3))
Exemplo n.º 3
0
    def __init__(self, conf=None):

        s_config.Config.__init__(self, conf)

        self.epoll = select.epoll()

        self.socks = {}
        self.links = {}

        self.thrd = s_threads.worker(self._runPollLoop)

        self.onfini(self._onPlexFini)

        pmax = self.getConfOpt('pool:max')
        self.pool = s_threads.Pool(maxsize=pmax)

        self.onfini(self.pool.fini)

        self.polls = {}
Exemplo n.º 4
0
    def test_threads_cancelable(self):
        sock1, sock2 = s_socket.socketpair()

        data = []
        def echoloop():
            with s_threads.cancelable(sock1.fini):
                byts = sock1.recv(1024)
                data.append(byts)
                sock1.sendall(byts)

        thr = s_threads.worker(echoloop)

        sock2.sendall(b'hi')
        self.assertEqual( sock2.recv(1024), b'hi')

        thr.fini()
        thr.join()

        sock1.fini()
        sock2.fini()
Exemplo n.º 5
0
    def test_threads_cancelable(self):
        sock1, sock2 = s_socket.socketpair()

        data = []
        def echoloop():
            with s_threads.cancelable(sock1.fini):
                byts = sock1.recv(1024)
                data.append(byts)
                sock1.sendall(byts)

        thr = s_threads.worker(echoloop)

        sock2.sendall(b'hi')
        self.eq(sock2.recv(1024), b'hi')

        thr.fini()
        thr.join()

        sock1.fini()
        sock2.fini()
Exemplo n.º 6
0
    async def test_asyncqueue(self):

        # The axon tests test most of the asyncqueue functionality.  We just need to test the
        # draining part

        async with await s_queue.AsyncQueue.anit(5, drain_level=3) as q:
            [await q.put(i) for i in range(5)]
            got_to_end = False
            waiter = asyncio.Event()
            last_msg = 0

            def sync_worker():

                nonlocal got_to_end
                nonlocal last_msg

                time.sleep(0.1)

                last_msg = q.get()  # got 0
                last_msg = q.get()  # got 1
                q.schedCallSafe(waiter.set)
                time.sleep(0.1)
                last_msg = q.get()  # got 2
                got_to_end = True

            t = s_threads.worker(sync_worker)
            before = time.time()

            await waiter.wait()

            await q.put(6)

            self.lt(0.1, time.time() - before)
            await asyncio.sleep(0.1)
            self.eq(last_msg, 2)

            await asyncio.sleep(0.1)

            self.true(got_to_end)

            t.join()
Exemplo n.º 7
0
    def test_threads_retnwait(self):
        with s_threads.RetnWait() as retn:

            def work():
                retn.retn(True)

            thrd = s_threads.worker(work)
            self.eq(retn.wait(timeout=1), (True, True))
            thrd.join()

        self.eq(retn.wait(timeout=1), (True, True))

        # no timeout
        with s_threads.RetnWait() as retn:

            def work():
                retn.retn(True)

            thrd = s_threads.worker(work)
            self.eq(retn.wait(), (True, True))
            thrd.join()

        self.eq(retn.wait(timeout=1), (True, True))

        # Let a wait() timeout
        with s_threads.RetnWait() as retn:

            def work():
                time.sleep(0.5)
                retn.retn(True)

            thrd = s_threads.worker(work)
            ok, retn = retn.wait(timeout=0.01)

            self.false(ok)
            self.eq(retn[0], 'TimeOut')

            thrd.join()

        with s_threads.RetnWait() as retn:

            def work():
                try:
                    1 / 0
                except ZeroDivisionError as e:
                    retn.errx(e)

            thrd = s_threads.worker(work)
            ret = retn.wait(timeout=1)
            thrd.join()
            self.false(ret[0])
            excfo = ret[1]
            self.istufo(excfo)
            self.eq(excfo[0], 'ZeroDivisionError')
            self.eq(excfo[1].get('msg'), 'division by zero')
            self.eq(excfo[1].get('name'), 'work')
            self.isin('test_lib_threads.py', excfo[1].get('file'))
            self.isin('line', excfo[1])  # Line may change
            self.isin(
                'src',
                excfo[1])  # source for a inner function may not be available.

        # Test capture
        with s_threads.RetnWait() as retn:

            def work(a, b, c, callback):
                sum = a + b
                multiple = sum * c
                callback(sum, multiple, a=a, b=b, c=c)

            thrd = s_threads.worker(work, 1, 2, 3, retn.capture)
            ret = retn.wait(timeout=1)
            thrd.join()
            self.true(ret[0])
            self.eq(ret[1], ((3, 9), {'a': 1, 'b': 2, 'c': 3}))
Exemplo n.º 8
0
    def test_sqlite_pool(self):

        names = ['t1:write:0', 't0:read:0', 't1:done']
        steps = self.getTestSteps(names)

        with self.getTestDir() as dirn:

            path = os.path.join(dirn, 'db0.db')

            with s_sqlite.pool(3, path) as pool:

                self.eq(pool.avail(), 3)
                with pool.xact() as xact:
                    self.eq(pool.avail(), 2)
                    xact.execute('CREATE TABLE test ( foo VARCHAR, bar INT )')
                self.eq(pool.avail(), 3)

                ###############################################################
                def t1():

                    with pool.xact() as xact1:
                        xact1.execute(
                            'INSERT INTO test (foo,bar) VALUES ("lol", 20)')
                        xact1.executemany(
                            'INSERT INTO test (foo,bar) VALUES (?, ?)',
                            [('lol', 30), ('lol', 40)])
                        steps.step('t1:write:0', 't0:read:0', timeout=8)

                    steps.done('t1:done')

                ###############################################################

                thrd = s_threads.worker(t1)
                with pool.xact() as xact0:

                    # wait for t1 to hold a write xact open...
                    steps.wait('t1:write:0')

                    # we should be able to see his un-commited write due to shared cache
                    # ( using a select query to specifically avoid the write lock )
                    rows = list(xact0.select('SELECT * FROM test'))
                    self.isin(('lol', 20), rows)
                    self.isin(('lol', 30), rows)
                    self.isin(('lol', 40), rows)

                    # allow t1 to close the write xact
                    steps.step('t0:read:0', 't1:done', timeout=8)

                    # now we can write...
                    xact0.execute('INSERT INTO test (foo, bar) VALUES (?, ?)',
                                  ('hah', 30))

                self.none(thrd.join(timeout=8))

                with pool.xact() as xact2:
                    # we should see our previously commited write
                    rows = list(
                        xact2.select('SELECT * FROM test WHERE foo = ?',
                                     ('hah', )))
                    self.len(1, rows)

                    self.eq(
                        1,
                        xact2.update('UPDATE test SET foo = ? WHERE foo = ?',
                                     ('heh', 'hah')))

            # ensure that the pool commit/fini wrote all data...
            with s_sqlite.pool(2, path) as pool:
                with pool.xact() as xact:
                    # we should see our previously commited write
                    rows = list(xact.select('SELECT * FROM test'))
                    self.len(4, rows)

            with s_sqlite.pool(2, path) as pool:
                with pool.xact() as xact:
                    pool.fini()
                    # the xact should be fini because the pool is fini
                    self.raises(IsFini, xact.select, 'SELECT * FROM test')
                    self.raises(IsFini, xact.wrlock)