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

68 lines
2.0 KiB

  1. import pytest
  2. from tests.lib import * # NOQA - fixtures
  3. from time import sleep, time
  4. from pyircbot import IRCCore
  5. import logging
  6. logging.getLogger().setLevel(logging.DEBUG)
  7. def wait_until(server, channel, nick, func, timeout):
  8. start = time()
  9. while time() < start + timeout:
  10. v = func()
  11. if v:
  12. return v
  13. sleep(0.02)
  14. raise Exception("Function {} did not settle after {}s".format(func, timeout))
  15. def wait_until_joined(server, channel, nick, timeout=5.0):
  16. def in_ch():
  17. if channel in server.channels:
  18. members = [u.nickname for u in server.channels[channel].members]
  19. if nick in members:
  20. return members
  21. return wait_until(server, channel, nick, in_ch, timeout=timeout)
  22. def wait_until_absent(server, channel, nick, timeout=5.0):
  23. def not_in_ch():
  24. if channel not in server.channels:
  25. return True
  26. members = [u.nickname for u in server.channels[channel].members]
  27. if nick not in members:
  28. return True
  29. return wait_until(server, channel, nick, not_in_ch, timeout=timeout)
  30. def test_connect_and_join(livebot):
  31. port, server, bot, bot_t, channel, nick = livebot
  32. bot_t.start()
  33. assert nick in wait_until_joined(server, channel, nick)
  34. @pytest.mark.slow
  35. def test_hop_to_next_server(livebot):
  36. port, server, bot, bot_t, channel, nick = livebot
  37. bot.irc.servers = [["localhost", bot.irc.servers[0][1] + 1], bot.irc.servers[0]] # bad port, con refused on 1st srv
  38. bot_t.start()
  39. assert nick in wait_until_joined(server, channel, nick)
  40. @pytest.mark.slow
  41. def test_quit_reconnect(livebot):
  42. port, server, bot, bot_t, channel, nick = livebot
  43. bot.irc.reconnect_delay = 0.1
  44. bot_t.start()
  45. wait_until_joined(server, channel, nick)
  46. bot.act_QUIT("quitting")
  47. wait_until_absent(server, channel, nick)
  48. assert nick in wait_until_joined(server, channel, nick)
  49. def test_bs():
  50. IRCCore.fulltrace()
  51. IRCCore.trace()