Esempio n. 1
0
    async def _test_ops(self, client, *ops):
        listener = client.options.event_listeners[0]

        for f, args, kw in ops:
            s = await client.start_session()
            # Simulate "async with" on Python 3.4.
            try:
                listener.results.clear()
                # In case "f" modifies its inputs.
                args2 = copy.copy(args)
                kw2 = copy.copy(kw)
                kw2["session"] = s
                await f(*args2, **kw2)
                for event in listener.results["started"]:
                    self.assertTrue(
                        "lsid" in event.command,
                        "%s sent no lsid with %s" %
                        (f.__name__, event.command_name),
                    )

                    self.assertEqual(
                        s.session_id,
                        event.command["lsid"],
                        "%s sent wrong lsid with %s" %
                        (f.__name__, event.command_name),
                    )

                self.assertFalse(s.has_ended)
            finally:
                await s.end_session()

            with self.assertRaisesRegex(InvalidOperation, "ended session"):
                await f(*args2, **kw2)

        # No explicit session.
        for f, args, kw in ops:
            listener.results.clear()
            await f(*args, **kw)
            self.assertGreaterEqual(len(listener.results["started"]), 1)
            lsids = []
            for event in listener.results["started"]:
                self.assertTrue(
                    "lsid" in event.command,
                    "%s sent no lsid with %s" %
                    (f.__name__, event.command_name),
                )

                lsids.append(event.command["lsid"])

            if "PyPy" not in sys.version:
                # Server session was returned to pool. Ignore interpreters with
                # non-deterministic GC.
                for lsid in lsids:
                    self.assertIn(
                        lsid,
                        session_ids(client),
                        "%s did not return implicit session to pool" %
                        (f.__name__, ),
                    )
    def _test_ops(self, client, *ops):
        listener = client.event_listeners()[0][0]

        for f, args, kw in ops:
            # Simulate "async with" on all Pythons.
            s = yield client.start_session()
            try:
                listener.results.clear()
                # In case "f" modifies its inputs.
                args2 = copy.copy(args)
                kw2 = copy.copy(kw)
                kw2['session'] = s
                yield f(*args2, **kw2)
                for event in listener.results['started']:
                    self.assertTrue(
                        'lsid' in event.command,
                        "%s sent no lsid with %s" % (
                            f.__name__, event.command_name))

                    self.assertEqual(
                        s.session_id,
                        event.command['lsid'],
                        "%s sent wrong lsid with %s" % (
                            f.__name__, event.command_name))

                self.assertFalse(s.has_ended)
            finally:
                yield s.end_session()

            with self.assertRaises(InvalidOperation) as ctx:
                yield f(*args2, **kw2)

            self.assertIn("ended session", str(ctx.exception))

        # No explicit session.
        for f, args, kw in ops:
            listener.results.clear()
            yield f(*args, **kw)
            self.assertGreaterEqual(len(listener.results['started']), 1)
            lsids = []
            for event in listener.results['started']:
                self.assertTrue(
                    'lsid' in event.command,
                    "%s sent no lsid with %s" % (
                        f.__name__, event.command_name))

                lsids.append(event.command['lsid'])

            if 'PyPy' not in sys.version:
                # Server session was returned to pool. Ignore interpreters with
                # non-deterministic GC.
                for lsid in lsids:
                    self.assertIn(
                        lsid, session_ids(client),
                        "%s did not return implicit session to pool" % (
                            f.__name__,))