code: def test_cancel(self):
      errors = []
  
      def neverending(conn):
          cur = conn.cursor()
          try:
              self.assertRaises(
                  psycopg2.extensions.QueryCanceledError,
                  cur.execute,
                  "select pg_sleep(60)",
              )
              # make sure the connection still works
              conn.rollback()
              cur.execute("select 1")
              self.assertEqual(cur.fetchall(), [(1,)])
          except Exception as e:
              errors.append(e)
              raise
  
      def canceller(conn):
          cur = conn.cursor()
          try:
              conn.cancel()
          except Exception as e:
              errors.append(e)
              raise
          del cur
  
      thread1 = threading.Thread(target=neverending, args=(self.conn,))
      # wait a bit to make sure that the other thread is already in
      # pg_sleep -- ugly and racy, but the chances are ridiculously low
      thread2 = threading.Timer(0.3, canceller, args=(self.conn,))
      thread1.start()
      thread2.start()
      thread1.join()
      thread2.join()
  
      self.assertEqual(errors, [])
  
file_path: psycopg2/tests/test_cancel.py
imports: import time
  import threading
  import psycopg2
  import psycopg2.extensions
  from psycopg2 import extras
  from .testconfig import dsn
  import unittest
  from .testutils import ConnectingTestCase, skip_before_postgres, slow
  from .testutils import skip_if_crdb
label_desc: Use image node_id 3 for calling the CancelTests obj's underlying member method code with example usage: obj.test_cancel() without return types