简体   繁体   中英

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

    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

/*    @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

*/*                    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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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