Commit f49625ca authored by qsr's avatar qsr Committed by Commit bot

mojo: Add RunLoop::Current() and refactor unit tests.

R=sdefresne@chromium.org

Review URL: https://codereview.chromium.org/644773002

Cr-Commit-Position: refs/heads/master@{#299100}
parent 1bae0dca
......@@ -41,6 +41,7 @@ cdef extern from "mojo/public/cpp/utility/run_loop.h" nogil:
void RunUntilIdle() except *
void Quit()
void PostDelayedTask(CClosure&, int64_t)
cdef CRunLoop CRunLoopCurrent "mojo::RunLoop::current"()
cdef extern from "mojo/public/cpp/environment/environment.h" nogil:
......
......@@ -17,6 +17,9 @@ from cpython.buffer cimport PyObject_GetBuffer
from cpython.mem cimport PyMem_Malloc, PyMem_Free
from libc.stdint cimport int32_t, int64_t, uint32_t, uint64_t, uintptr_t
import ctypes
import threading
def SetSystemThunks(system_thunks_as_object):
"""Bind the basic Mojo Core functions.
......@@ -711,10 +714,29 @@ class DuplicateSharedBufferOptions(object):
self.flags = DuplicateSharedBufferOptions.FLAG_NONE
# Keeps a thread local weak reference to the current run loop.
_RUN_LOOPS = threading.local()
cdef class RunLoop(object):
"""RunLoop to use when using asynchronous operations on handles."""
cdef c_environment.CRunLoop c_run_loop
cdef c_environment.CRunLoop* c_run_loop
def __init__(self):
assert not <uintptr_t>(c_environment.CRunLoopCurrent())
self.c_run_loop = new c_environment.CRunLoop()
_RUN_LOOPS.loop = id(self)
def __dealloc__(self):
del _RUN_LOOPS.loop
del self.c_run_loop
@staticmethod
def Current():
if hasattr(_RUN_LOOPS, 'loop'):
return ctypes.cast(_RUN_LOOPS.loop, ctypes.py_object).value
return None
def Run(self):
"""Run the runloop until Quit is called."""
......
......@@ -2,18 +2,16 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import unittest
import mojo_unittest
# pylint: disable=F0401
import mojo.embedder
from mojo import system
class AsyncWaitTest(unittest.TestCase):
class AsyncWaitTest(mojo_unittest.MojoTestCase):
def setUp(self):
mojo.embedder.Init()
self.loop = system.RunLoop()
super(AsyncWaitTest, self).setUp()
self.array = []
self.handles = system.MessagePipe()
self.cancel = self.handles.handle0.AsyncWait(system.HANDLE_SIGNAL_READABLE,
......@@ -24,7 +22,7 @@ class AsyncWaitTest(unittest.TestCase):
self.cancel()
self.handles = None
self.array = None
self.loop = None
super(AsyncWaitTest, self).tearDown()
def _OnResult(self, value):
self.array.append(value)
......
......@@ -4,8 +4,9 @@
import unittest
import mojo_unittest
# pylint: disable=F0401
import mojo.embedder
from mojo.bindings import messaging
from mojo import system
......@@ -19,11 +20,10 @@ class _ForwardingConnectionErrorHandler(messaging.ConnectionErrorHandler):
self._callback(result)
class ConnectorTest(unittest.TestCase):
class ConnectorTest(mojo_unittest.MojoTestCase):
def setUp(self):
mojo.embedder.Init()
self.loop = system.RunLoop()
super(ConnectorTest, self).setUp()
self.received_messages = []
self.received_errors = []
def _OnMessage(message):
......@@ -44,7 +44,7 @@ class ConnectorTest(unittest.TestCase):
def tearDown(self):
self.connector = None
self.handle = None
self.loop = None
super(ConnectorTest, self).tearDown()
def testConnectorRead(self):
self.handle.WriteMessage()
......@@ -134,11 +134,10 @@ class HeaderTest(unittest.TestCase):
self.assertEqual(other_header.request_id, 0xdeadbeafdeadbeaf)
class RouterTest(unittest.TestCase):
class RouterTest(mojo_unittest.MojoTestCase):
def setUp(self):
mojo.embedder.Init()
self.loop = system.RunLoop()
super(RouterTest, self).setUp()
self.received_messages = []
self.received_errors = []
def _OnMessage(message):
......@@ -158,7 +157,7 @@ class RouterTest(unittest.TestCase):
def tearDown(self):
self.router = None
self.handle = None
self.loop = None
super(RouterTest, self).tearDown()
def testSimpleMessage(self):
header_data = messaging.MessageHeader(0, messaging.NO_FLAG).Serialize()
......
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import unittest
# pylint: disable=F0401
import mojo.embedder
import mojo.system as system
class MojoTestCase(unittest.TestCase):
def setUp(self):
mojo.embedder.Init()
self.loop = system.RunLoop()
def tearDown(self):
self.loop = None
......@@ -2,10 +2,9 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import unittest
import mojo_unittest
# pylint: disable=F0401
import mojo.embedder
from mojo import system
......@@ -15,28 +14,28 @@ def _Increment(array):
return _Closure
class RunLoopTest(unittest.TestCase):
def setUp(self):
mojo.embedder.Init()
class RunLoopTest(mojo_unittest.MojoTestCase):
def testRunLoop(self):
loop = system.RunLoop()
array = []
for _ in xrange(10):
loop.PostDelayedTask(_Increment(array))
loop.RunUntilIdle()
self.loop.PostDelayedTask(_Increment(array))
self.loop.RunUntilIdle()
self.assertEquals(len(array), 10)
def testRunLoopWithException(self):
loop = system.RunLoop()
def Throw():
raise Exception("error")
array = []
loop.PostDelayedTask(Throw)
loop.PostDelayedTask(_Increment(array))
self.loop.PostDelayedTask(Throw)
self.loop.PostDelayedTask(_Increment(array))
with self.assertRaisesRegexp(Exception, '^error$'):
loop.Run()
self.loop.Run()
self.assertEquals(len(array), 0)
loop.RunUntilIdle()
self.loop.RunUntilIdle()
self.assertEquals(len(array), 1)
def testCurrent(self):
self.assertEquals(system.RunLoop.Current(), self.loop)
self.loop = None
self.assertIsNone(system.RunLoop.Current())
......@@ -3,12 +3,11 @@
# found in the LICENSE file.
import random
import sys
import time
import unittest
import mojo_unittest
# pylint: disable=F0401
import mojo.embedder
from mojo import system
DATA_SIZE = 1024
......@@ -19,13 +18,7 @@ def _GetRandomBuffer(size):
return bytearray(''.join(chr(random.randint(0, 255)) for i in xrange(size)))
class BaseMojoTest(unittest.TestCase):
def setUp(self):
mojo.embedder.Init()
class CoreTest(BaseMojoTest):
class CoreTest(mojo_unittest.MojoTestCase):
def testResults(self):
self.assertEquals(system.RESULT_OK, 0)
......@@ -300,11 +293,3 @@ class CoreTest(BaseMojoTest):
self.assertEquals(data, buf1.buffer)
self.assertEquals(data, buf2.buffer)
self.assertEquals(buf1.buffer, buf2.buffer)
if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(CoreTest)
test_results = unittest.TextTestRunner(verbosity=0).run(suite)
if not test_results.wasSuccessful():
sys.exit(1)
sys.exit(0)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment