You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_base.py 1.6 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. from threading import Thread
  2. from typing import Any, List
  3. from autogen.io import IOConsole, IOStream, IOWebsockets
  4. class TestIOStream:
  5. def test_initial_default_io_stream(self) -> None:
  6. assert isinstance(IOStream.get_default(), IOConsole)
  7. def test_set_default_io_stream(self) -> None:
  8. class MyIOStream(IOStream):
  9. def print(self, *objects: Any, sep: str = " ", end: str = "\n", flush: bool = False) -> None:
  10. pass
  11. def input(self, prompt: str = "", *, password: bool = False) -> str:
  12. return "Hello, World!"
  13. assert isinstance(IOStream.get_default(), IOConsole)
  14. with IOStream.set_default(MyIOStream()):
  15. assert isinstance(IOStream.get_default(), MyIOStream)
  16. with IOStream.set_default(IOConsole()):
  17. assert isinstance(IOStream.get_default(), IOConsole)
  18. assert isinstance(IOStream.get_default(), MyIOStream)
  19. assert isinstance(IOStream.get_default(), IOConsole)
  20. def test_get_default_on_new_thread(self) -> None:
  21. exceptions: List[Exception] = []
  22. def on_new_thread(exceptions: List[Exception] = exceptions) -> None:
  23. try:
  24. assert isinstance(IOStream.get_default(), IOConsole)
  25. except Exception as e:
  26. exceptions.append(e)
  27. # create a new thread and run the function
  28. thread = Thread(target=on_new_thread)
  29. thread.start()
  30. # get exception from the thread
  31. thread.join()
  32. if exceptions:
  33. raise exceptions[0]