def testServerDownloadWithStopNow(self, output='/tmp/out'):
    log.debug('===> Running testcase testServerDownloadWithStopNow')
    root = os.path.dirname(os.path.abspath(__file__))
    server = tftpy.TftpServer(root)
    client = tftpy.TftpClient('localhost', 20001, {})
    child_pid = os.fork()
    if child_pid:
        try:
            stopped_early = False
            time.sleep(1)

            def delay_hook(pkt):
                time.sleep(0.005)
            client.download('640KBFILE', output, delay_hook)
        except:
            log.warn('client threw exception as expected')
            stopped_early = True
        finally:
            os.kill(child_pid, 15)
            os.waitpid(child_pid, 0)
        self.assertTrue((stopped_early == True), 'Server should not exit early')
    else:
        import signal

        def handlealarm(signum, frame):
            server.stop(now=True)
        signal.signal(signal.SIGALRM, handlealarm)
        signal.alarm(2)
        try:
            server.listen('localhost', 20001)
            log.error("server didn't throw exception")
        except Exception as err:
            log.error(('server got unexpected exception %s' % err))
        while True:
            time.sleep(1)
def testServerDownloadWithStopNow(self, output='/tmp/out'):
    log.debug('===> Running testcase testServerDownloadWithStopNow')
    root = os.path.dirname(os.path.abspath(__file__))
    server = tftpy.TftpServer(root)
    client = tftpy.TftpClient('localhost', 20001, {})
    child_pid = os.fork()
    if child_pid:
        try:
            stopped_early = False
            time.sleep(1)

            def delay_hook(pkt):
                time.sleep(0.005)
            client.download('640KBFILE', output, delay_hook)
        except:
            log.warn('client threw exception as expected')
            stopped_early = True
        finally:
            os.kill(child_pid, 15)
            os.waitpid(child_pid, 0)
        self.assertTrue((stopped_early == True), 'Server should not exit early')
    else:
        import signal

        def handlealarm(signum, frame):
            server.stop(now=True)
        signal.signal(signal.SIGALRM, handlealarm)
        signal.alarm(2)
        try:
            server.listen('localhost', 20001)
            log.error("server didn't throw exception")
        except Exception as err:
            log.error(('server got unexpected exception %s' % err))
        while True:
            time.sleep(1)