import unittest

from linemode import PrintSpooler
from linemode.base import Printer


class TestPrintSpooler(unittest.TestCase):
    def test_shutdown(self):
        class DummyPrinter(Printer):
            pass
        spooler = PrintSpooler(DummyPrinter())
        spooler.shutdown()

    def test_submit_job(self):
        testcase = self
        dummy_commands = object()
        compiled = False

        dummy_program = object()
        executed = False

        class DummyPrinter(Printer):
            def compile(self, commands):
                nonlocal testcase
                nonlocal dummy_commands
                nonlocal compiled
                nonlocal dummy_program

                testcase.assertIs(commands, dummy_commands)
                testcase.assertFalse(compiled)

                compiled = True

                return dummy_program

            def execute(self, program):
                nonlocal testcase
                nonlocal dummy_program
                nonlocal executed

                testcase.assertIs(program, dummy_program)
                testcase.assertFalse(executed)

                executed = True

        spooler = PrintSpooler(DummyPrinter())
        spooler.submit(dummy_commands)

        spooler.shutdown()

        self.assertTrue(compiled)
        self.assertTrue(executed)

@@ -8,16 +8,33 @@
 unittest
+%0Aimport threading
 %0A%0Afrom l
@@ -294,24 +294,1065 @@
 shutdown()%0A%0A
+    def test_shutdown_with_pending(self):%0A        testcase = self%0A        condition = threading.Condition()%0A        executed = False%0A%0A        class DummyPrinter(Printer):%0A            def compile(self, commands):%0A                return None%0A%0A            def execute(self, program):%0A                nonlocal testcase%0A                nonlocal condition%0A                nonlocal executed%0A%0A                with condition:%0A                    condition.wait()%0A%0A                    # only one job should run%0A                    testcase.assertFalse(executed)%0A%0A        spooler = PrintSpooler(DummyPrinter())%0A        spooler.submit(None)%0A        spooler.submit(None)%0A%0A        shutdown_thread = threading.Thread(target=spooler.shutdown)%0A        shutdown_thread.start()%0A%0A        # first task should still be blocking%0A        self.assertFalse(executed)%0A%0A        with condition:%0A            condition.notify_all()%0A%0A        shutdown_thread.join()%0A%0A        # the first print job should have finished before shutting down%0A        self.assertTrue(executed)%0A%0A
     def test

test shutdown with jobs on queue