测试从另一个线程

时间:2018-06-06 11:54:56

标签: django multithreading email django-testing

我正在尝试测试发送电子邮件的代码。或多或少我的单元测试看起来像:

class EmailTest(TestCase):
    def test_send_email(self):
        my_send_mail_method()
        self.assertEqual(len(mail.outbox), 1)

只要my_send_mail_method在同一个帖子中发送电子邮件,此代码就可以正常运行。但是,当我以某种方式更改代码时,my_send_mail_method将从另一个线程len(mail.outbox)发送邮件等于0. my_send_mail_method等待发送电子邮件完成,所以我不认为这是一个并发问题

修改

发送电子邮件的代码看起来像假设它在同一个线程中运行:

import timeout_decorator
from django.core import mail
from django.test import TestCase

@timeout_decorator.timeout(120, use_signals=False)
def test_send_email():
    mail.send_mail('Subject here', 'Here is the message.',
        'from@example.com', ['to@example.com'],
        fail_silently=False)

问题是我的方法有timeout_decorator(方法中有更多的东西可能会挂起处理,我只是想在一段时间后终止进程。)

我检查了装饰器的代码,据我所知它创建一个新线程执行它,等待响应并返回它。但是,我对Python并不十分精通,可能会遗漏一些明显的东西。

0 个答案:

没有答案
相关问题