Skip to content

Commit

Permalink
Merge pull request #12 from Local-Connectivity-Lab/ticket-495
Browse files Browse the repository at this point in the history
Adding exception handling and a related test.
  • Loading branch information
philion authored Feb 27, 2024
2 parents 499eb82 + 7c24699 commit 6e03d4e
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 12 deletions.
23 changes: 14 additions & 9 deletions cog_scn.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,20 @@ async def sync_all_threads(self):
# get all threads
for guild in self.bot.guilds:
for thread in guild.threads:
#log.debug(f"THREAD: guild:{guild}, thread:{thread}")
# sync each thread,
ticket = await self.sync_thread(thread)
if ticket:
# successful sync
log.debug(f"SYNC complete for ticket #{ticket.id} to {thread.name}")
else:
log.debug(f"no ticket found for {thread.name}")

try:
# try syncing each thread. if there's no ticket found, there's no thread to sync.
ticket = await self.sync_thread(thread)
if ticket:
# successful sync
log.debug(f"SYNC complete for ticket #{ticket.id} to {thread.name}")
else:
log.debug(f"no ticket found for {thread.name}")
except NetbotException as ex:
# ticket is locked.
# skip gracefully
log.debug(f"Ticket locked, sync in progress: {thread}: {ex}")
except Exception:
log.exception(f"Error syncing {thread}")

@scn.command()
async def sync(self, ctx:discord.ApplicationContext):
Expand Down
55 changes: 52 additions & 3 deletions test_cog_scn.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/env python3
"""testing the SCN cog"""

import asyncio
import unittest
import logging
import discord
Expand All @@ -14,8 +13,6 @@
import test_utils


logging.basicConfig(level=logging.FATAL)

log = logging.getLogger(__name__)

test_username: str = "acmerocket"
Expand Down Expand Up @@ -122,6 +119,58 @@ async def test_block_user(self):
self.redmine.remove_ticket(ticket.id)


async def test_locked_during_sync_ticket(self):
"""
Unhandled exception in internal background task 'sync_all_threads'.
Traceback (most recent call last):
File "/usr/local/lib/python3.11/site-packages/discord/ext/tasks/__init__.py", line 169, in _loop
await self.coro(*args, **kwargs)
File "/cog_scn.py", line 124, in sync_all_threads
ticket = await self.sync_thread(thread)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/cog_scn.py", line 106, in sync_thread
raise NetbotException(f"Ticket {ticket.id} is locked for syncronization.")
netbot.NetbotException: Ticket 195 is locked for syncronization.
--
This test intends to try to reproduce by locking a ticket before starting the sync.
"""
# create a new ticket, identified by the tag, with a note
ticket = self.create_test_ticket()
message = f"Message for {self.tag}"
self.redmine.append_message(ticket.id, self.user.login, message)

# create mock message and thread
message = unittest.mock.AsyncMock(discord.Message)
message.content = f"This is a new note about ticket #{ticket.id} for test {self.tag}"
message.author = unittest.mock.AsyncMock(discord.Member)
message.author.name = self.discord_user

thread = unittest.mock.AsyncMock(discord.Thread)
thread.name = f"Ticket #{ticket.id}: {ticket.subject}"

# Lock the ticket...
# TODO - this could be encapsulated in Ticket or Netbot
log.debug("locking ticket")
async with self.bot.lock:
if ticket.id in self.bot.ticket_locks:
self.fail(f"New ticket {ticket.id} is already locked?")
else:
# create lock flag
self.bot.ticket_locks[ticket.id] = True

try:
# synchronize thread
await self.cog.sync_thread(thread)
self.fail("No exception when one was expected")
except Exception as ex:
self.assertIn(f"Ticket {ticket.id}", str(ex))
# pass: exception contains "Ticket id"

# clean up
del self.bot.ticket_locks[ticket.id]
self.redmine.remove_ticket(ticket.id)


if __name__ == '__main__':
# when running this main, turn on DEBUG
logging.basicConfig(level=logging.DEBUG, format="{asctime} {levelname:<8s} {name:<16} {message}", style='{')
Expand Down

0 comments on commit 6e03d4e

Please sign in to comment.