[zmq] add unit tests for ZMQ-based repository (server and dbapi)

authorDavid Douard <david.douard@logilab.fr>
changesetc1cc2f1cd177
branchdefault
phasepublic
hiddenno
parent revision#0e3b41118631 [dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
child revision#a9984ceebc26 [pyro] Refactor the pyrorql source
files modified by this revision
server/test/unittest_repository.py
# HG changeset patch
# User David Douard <david.douard@logilab.fr>
# Date 1333529823 -7200
# Wed Apr 04 10:57:03 2012 +0200
# Node ID c1cc2f1cd1776bd07f35c2a2d8ef4fae0197748a
# Parent 0e3b41118631d9788c4725595b80b5cd19572a81
[zmq] add unit tests for ZMQ-based repository (server and dbapi)

diff --git a/server/test/unittest_repository.py b/server/test/unittest_repository.py
@@ -34,11 +34,11 @@
1 
2  from cubicweb import (BadConnectionId, RepositoryError, ValidationError,
3                        UnknownEid, AuthenticationError, Unauthorized, QueryError)
4  from cubicweb.predicates import is_instance
5  from cubicweb.schema import CubicWebSchema, RQLConstraint
6 -from cubicweb.dbapi import connect, multiple_connections_unfix
7 +from cubicweb.dbapi import connect, multiple_connections_unfix, ConnectionProperties
8  from cubicweb.devtools.testlib import CubicWebTC
9  from cubicweb.devtools.repotest import tuplify
10  from cubicweb.server import repository, hook
11  from cubicweb.server.sqlutils import SQL_PREFIX
12  from cubicweb.server.hook import Hook
@@ -377,10 +377,67 @@
13              done.append(True)
14          finally:
15              # connect monkey patch some method by default, remove them
16              multiple_connections_unfix()
17 
18 +
19 +    def test_zmq(self):
20 +        try:
21 +            import zmq
22 +        except ImportError:
23 +            self.skipTest("zmq in not available")
24 +        done = []
25 +        from cubicweb.devtools import TestServerConfiguration as ServerConfiguration
26 +        from cubicweb.server.cwzmq import ZMQRepositoryServer
27 +        # the client part has to be in a thread due to sqlite limitations
28 +        t = threading.Thread(target=self._zmq_client, args=(done,))
29 +        t.start()
30 +
31 +        zmq_server = ZMQRepositoryServer(self.repo)
32 +        zmq_server.connect('tcp://127.0.0.1:41415')
33 +
34 +        t2 = threading.Thread(target=self._zmq_quit, args=(done, zmq_server,))
35 +        t2.start()
36 +
37 +        zmq_server.run()
38 +
39 +        t2.join(1)
40 +        t.join(1)
41 +
42 +        if t.isAlive():
43 +            self.fail('something went wrong, thread still alive')
44 +
45 +    def _zmq_quit(self, done, srv):
46 +        while not done:
47 +            time.sleep(0.1)
48 +        srv.quit()
49 +
50 +    def _zmq_client(self, done):
51 +        cnxprops = ConnectionProperties('zmq')
52 +        cnx = connect(self.repo.config.appid, u'admin', password=u'gingkow',
53 +                      host='tcp://127.0.0.1:41415',
54 +                      cnxprops=cnxprops,
55 +                      initlog=False) # don't reset logging configuration
56 +        try:
57 +            cnx.load_appobjects(subpath=('entities',))
58 +            # check we can get the schema
59 +            schema = cnx.get_schema()
60 +            self.assertTrue(cnx.vreg)
61 +            self.assertTrue('etypes'in cnx.vreg)
62 +            cu = cnx.cursor()
63 +            rset = cu.execute('Any U,G WHERE U in_group G')
64 +            user = iter(rset.entities()).next()
65 +            self.assertTrue(user._cw)
66 +            self.assertTrue(user._cw.vreg)
67 +            from cubicweb.entities import authobjs
68 +            self.assertIsInstance(user._cw.user, authobjs.CWUser)
69 +            cnx.close()
70 +            done.append(True)
71 +        finally:
72 +            # connect monkey patch some method by default, remove them
73 +            multiple_connections_unfix()
74 +
75      def test_internal_api(self):
76          repo = self.repo
77          cnxid = repo.connect(self.admlogin, password=self.admpassword)
78          session = repo._get_session(cnxid, setcnxset=True)
79          self.assertEqual(repo.type_and_source_from_eid(2, session),