import time


def make_side_effect(messages, delay=None):
    """Make a side effect from a list of messages, optionally adding a delay."""
    msg_queue = list(reversed(messages))
    sleep_delay = delay

    def side_effect(*args, **kwargs):
        if sleep_delay is not None:
            time.sleep(sleep_delay)
        return msg_queue.pop()

    return side_effect