def prog(): try: yield tb.starttls(client_sslctx) yield tb.connect(addr) yield tb.write(A_DATA) data = yield tb.read(2) self.assertEqual(data, b'OK') yield tb.write(B_DATA) data = yield tb.read(4) self.assertEqual(data, b'SPAM') yield tb.close() except Exception as ex: self.loop.call_soon_threadsafe(fut.set_exception, ex) else: self.loop.call_soon_threadsafe(fut.set_result, None)