(self, client, *ops)
| 118 | super().tearDown() |
| 119 | |
| 120 | def _test_ops(self, client, *ops): |
| 121 | listener = client.options.event_listeners[0] |
| 122 | |
| 123 | for f, args, kw in ops: |
| 124 | with client.start_session() as s: |
| 125 | listener.reset() |
| 126 | s._materialize() |
| 127 | last_use = s._server_session.last_use |
| 128 | start = time.monotonic() |
| 129 | self.assertLessEqual(last_use, start) |
| 130 | # In case "f" modifies its inputs. |
| 131 | args = copy.copy(args) |
| 132 | kw = copy.copy(kw) |
| 133 | kw["session"] = s |
| 134 | f(*args, **kw) |
| 135 | self.assertGreaterEqual(len(listener.started_events), 1) |
| 136 | for event in listener.started_events: |
| 137 | self.assertIn( |
| 138 | "lsid", |
| 139 | event.command, |
| 140 | f"{f.__name__} sent no lsid with {event.command_name}", |
| 141 | ) |
| 142 | |
| 143 | self.assertEqual( |
| 144 | s.session_id, |
| 145 | event.command["lsid"], |
| 146 | f"{f.__name__} sent wrong lsid with {event.command_name}", |
| 147 | ) |
| 148 | |
| 149 | self.assertFalse(s.has_ended) |
| 150 | |
| 151 | self.assertTrue(s.has_ended) |
| 152 | with self.assertRaisesRegex(InvalidOperation, "ended session"): |
| 153 | f(*args, **kw) |
| 154 | |
| 155 | # Test a session cannot be used on another client. |
| 156 | with self.client2.start_session() as s: |
| 157 | # In case "f" modifies its inputs. |
| 158 | args = copy.copy(args) |
| 159 | kw = copy.copy(kw) |
| 160 | kw["session"] = s |
| 161 | with self.assertRaisesRegex( |
| 162 | InvalidOperation, |
| 163 | "Can only use session with the MongoClient that started it", |
| 164 | ): |
| 165 | f(*args, **kw) |
| 166 | |
| 167 | # No explicit session. |
| 168 | for f, args, kw in ops: |
| 169 | listener.reset() |
| 170 | f(*args, **kw) |
| 171 | self.assertGreaterEqual(len(listener.started_events), 1) |
| 172 | lsids = [] |
| 173 | for event in listener.started_events: |
| 174 | self.assertIn( |
| 175 | "lsid", |
| 176 | event.command, |
| 177 | f"{f.__name__} sent no lsid with {event.command_name}", |
no test coverage detected