Browse code

Run tests in parallel with multiprocessing.

Tests run much faster like that.

Logging should be improved, but that is left to another opportunity.

Jose Fonseca authored on 15/11/2016 16:11:41
Showing 1 changed files

  • test.py index 2101289..e87bb25 100755
... ...
@@ -1,6 +1,6 @@
1 1
 #!/usr/bin/env python3
2 2
 #
3
-# Copyright 2015 Jose Fonseca
3
+# Copyright 2015-2016 Jose Fonseca
4 4
 #
5 5
 # This program is free software: you can redistribute it and/or modify it
6 6
 # under the terms of the GNU Lesser General Public License as published
... ...
@@ -20,104 +20,125 @@
20 20
 import sys
21 21
 import os.path
22 22
 import traceback
23
+import multiprocessing
23 24
 
24
-import gi
25
-gi.require_version('Gtk', '3.0')
26 25
 
27
-from gi.repository import Gtk
28
-from gi.repository import Gdk
26
+def test(arg):
27
+    sys.stdout.write(arg + '\n')
28
+    sys.stdout.flush()
29 29
 
30
-from xdot import DotWidget, DotWindow
31 30
 
31
+    import gi
32
+    gi.require_version('Gtk', '3.0')
32 33
 
33
-class TestDotWidget(DotWidget):
34
+    from gi.repository import Gtk
35
+    from gi.repository import Gdk
34 36
 
35
-    def __init__(self, name):
36
-        DotWidget.__init__(self)
37
-        self.name = name
37
+    from xdot import DotWidget, DotWindow
38 38
 
39
-    def on_draw(self, widget, cr):
40
-        DotWidget.on_draw(self, widget, cr)
41 39
 
42
-        if True:
43
-            # Cairo screenshot
40
+    class TestDotWidget(DotWidget):
44 41
 
45
-            import cairo
42
+        def __init__(self, name):
43
+            DotWidget.__init__(self)
44
+            self.name = name
46 45
 
47
-            # Scale to give 96 dpi instead of 72 dpi
48
-            dpi = 96.0
49
-            scale = dpi/72.0
50
-            w = int(self.graph.width*scale)
51
-            h = int(self.graph.height*scale)
46
+        def on_draw(self, widget, cr):
47
+            DotWidget.on_draw(self, widget, cr)
52 48
 
53
-            CAIRO_XMAX = 32767
54
-            CAIRO_YMAX = 32767
55
-            if w >= CAIRO_XMAX:
56
-                w = CAIRO_XMAX
57
-                scale = w/self.graph.width
58
-                h = int(self.graph.height*scale)
59
-            if h >= CAIRO_YMAX:
60
-                h = CAIRO_YMAX
61
-                scale = h/self.graph.height
49
+            if True:
50
+                # Cairo screenshot
51
+
52
+                import cairo
53
+
54
+                # Scale to give 96 dpi instead of 72 dpi
55
+                dpi = 96.0
56
+                scale = dpi/72.0
62 57
                 w = int(self.graph.width*scale)
58
+                h = int(self.graph.height*scale)
63 59
 
64
-            assert w <= CAIRO_XMAX
65
-            assert h <= CAIRO_YMAX
60
+                CAIRO_XMAX = 32767
61
+                CAIRO_YMAX = 32767
62
+                if w >= CAIRO_XMAX:
63
+                    w = CAIRO_XMAX
64
+                    scale = w/self.graph.width
65
+                    h = int(self.graph.height*scale)
66
+                if h >= CAIRO_YMAX:
67
+                    h = CAIRO_YMAX
68
+                    scale = h/self.graph.height
69
+                    w = int(self.graph.width*scale)
66 70
 
67
-            surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h)
71
+                assert w <= CAIRO_XMAX
72
+                assert h <= CAIRO_YMAX
68 73
 
69
-            cr = cairo.Context(surface)
74
+                surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h)
70 75
 
71
-            cr.set_source_rgba(1.0, 1.0, 1.0, 1.0)
72
-            cr.paint()
76
+                cr = cairo.Context(surface)
73 77
 
74
-            cr.scale(scale, scale)
78
+                cr.set_source_rgba(1.0, 1.0, 1.0, 1.0)
79
+                cr.paint()
75 80
 
76
-            self.graph.draw(cr, highlight_items=self.highlight)
81
+                cr.scale(scale, scale)
77 82
 
78
-            surface.write_to_png(self.name + '.png')
83
+                self.graph.draw(cr, highlight_items=self.highlight)
79 84
 
80
-        if False:
81
-            # GTK 3 screenshot
85
+                surface.write_to_png(self.name + '.png')
82 86
 
83
-            window = self.get_window()
87
+            if False:
88
+                # GTK 3 screenshot
84 89
 
85
-            w = window.get_width()
86
-            h = window.get_height()
90
+                window = self.get_window()
87 91
 
88
-            pixbuf = Gdk.pixbuf_get_from_window(window, 0, 0, w, h)
92
+                w = window.get_width()
93
+                h = window.get_height()
89 94
 
90
-            pixbuf.savev(self.name + '.png', 'png', (), ())
95
+                pixbuf = Gdk.pixbuf_get_from_window(window, 0, 0, w, h)
91 96
 
92
-        Gtk.main_quit()
97
+                pixbuf.savev(self.name + '.png', 'png', (), ())
93 98
 
94
-    def error_dialog(self, message):
95
-        sys.stderr.write(message)
96
-        sys.stderr.write("\n")
99
+            Gtk.main_quit()
97 100
 
101
+        def error_dialog(self, message):
102
+            sys.stderr.write(message)
103
+            sys.stderr.write("\n")
98 104
 
99
-def main():
100
-    status = 0
101
-    for arg in sys.argv[1:]:
102
-        sys.stdout.write(arg + '\n')
103
-        sys.stdout.flush()
104
-        name, ext = os.path.splitext(os.path.basename(arg))
105
-        widget = TestDotWidget(name)
106
-        window = DotWindow(widget)
107
-        window.connect('delete-event', Gtk.main_quit)
105
+
106
+    result = True
107
+
108
+    name, ext = os.path.splitext(os.path.basename(arg))
109
+    widget = TestDotWidget(name)
110
+    window = DotWindow(widget)
111
+    window.connect('delete-event', Gtk.main_quit)
112
+    try:
108 113
         try:
109
-            try:
110
-                dotcode = open(arg, 'rb').read()
111
-                window.set_dotcode(dotcode)
112
-            except:
113
-                exc_info = sys.exc_info()
114
-                traceback.print_exception(*exc_info)
115
-                status = 1
116
-            else:
117
-                window.show()
118
-                Gtk.main()
119
-        finally:
120
-            window.destroy()
114
+            dotcode = open(arg, 'rb').read()
115
+            window.set_dotcode(dotcode)
116
+        except:
117
+            exc_info = sys.exc_info()
118
+            traceback.print_exception(*exc_info)
119
+            result = False
120
+        else:
121
+            window.show()
122
+            Gtk.main()
123
+    finally:
124
+        window.destroy()
125
+
126
+    return result
127
+
128
+
129
+def main():
130
+    args = sys.argv[1:]
131
+
132
+    pool = multiprocessing.Pool(multiprocessing.cpu_count())
133
+    results = pool.map(test, args)
134
+
135
+    # Exit with status 1 if any failed
136
+    try:
137
+        results.index(False)
138
+    except ValueError:
139
+        status = 0
140
+    else:
141
+        status = 1
121 142
     sys.exit(status)
122 143
 
123 144