简体   繁体   English

java-在if语句中调用布尔超时函数,这样它就不会发送电子邮件

[英]java - calling a boolean timeout function in a if statement so it doesn't send email

first of all - here is part of my Code: 首先-这是我的代码的一部分:

    public void run() {



            try {

            _conn = new SmtpConnection(this, _socket);
            _state = new SmtpState(_workspace);
            _quitting = false;


            sendGreetings();


                while (!_quitting)
                handleCommand();

        } catch (SocketTimeoutException ste) {
            _conn.send("421 Service shutting down and closing transmission channel");

        } catch (Exception e) {
            // Closing socket on blocked read


            if (!_quitting) {
                log.error("Unexpected error handling connection, quitting=", e);
                throw new IllegalStateException(e);
            }
        } finally {
            if (null != _state) {
                _state.clearMessage();
            }
        }

    }

and I have also one function in another class, which is called timeoutonemail 我在另一个类中也有一个函数,叫做timeoutonemail

    public boolean timeoutOnNextEmail()
{

    boolean timeout = true;
    return timeout;
}

I have a unittest, which should try to send E-Mail, but when i call timeoutfuntion, test should pass - so it should not send an email 我有一个单元测试,应该尝试发送电子邮件,但是当我打电话给timeoutfuntion时,测试应该通过-因此它不应发送电子邮件

/*    @Test(expected = MailException.class)
public void sendEmailFail() throws MailException {

    greenMail.timeoutOnNextEmail();

    try {
        app.sendMail("to", "from", "subject", "body");
    } catch (final MailException e) {
        throw new MailException(e.toString());
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    assertEquals(0, greenMail.getReceivedMessagesForDomain("to").length);
}*/

How can I tell my run funtion, that if I'm calling timeoutfuntion in my unittest, so that it should stop doing that and let my thread sleep.. I tried something like 我该如何告诉我的跑步功能,如果我在单元测试中调用timeoutfuntion,那么它应该停止这样做并让我的线程进入睡眠状态。

*/*                    if(timeout && !_quitting){
                greenMail.timeoutOnNextEmail();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            else
            {
                handleCommand();
            }*/*

and several different Options, but i just don't have idea how to call it correctly.. 和几个不同的选项,但我只是不知道如何正确调用它。

First of all, you can only had the kind of behavior that you want if you have threads. 首先,只有拥有线程,您才能拥有想要的行为。

You can have a Queue of messages to send. 您可以具有要发送的消息队列。 One thread will put messages in the queue and other thread will get messages from the queue. 一个线程将消息放入队列,而另一个线程将从队列中获取消息。 Is a producer / consumer classic problem. 生产者/消费者的经典问题。

There could be another agent who eliminates messages from the queue or, simpler, the consumer could check another data structure to know if discard or process the message. 可能存在另一个从队列中消除消息的代理,或更简单的是,使用者可以检查另一个数据结构以了解是否丢弃或处理该消息。

Now, in your unit test you could replace the consumer with a mock that sleeps a fixed amount of time to give time to the interruption agent to act. 现在,在您的单元测试中,您可以用模拟程序替换使用者,该模拟程序会睡一段固定的时间,以便有时间让中断代理采取行动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM