1 | #!/usr/bin/env python
|
---|
2 | # -*- coding: utf-8 -*-
|
---|
3 | # $Id: tdPython1.py 62484 2016-07-22 18:35:33Z vboxsync $
|
---|
4 |
|
---|
5 | """
|
---|
6 | VirtualBox Validation Kit - Python Bindings Test #1
|
---|
7 | """
|
---|
8 |
|
---|
9 | __copyright__ = \
|
---|
10 | """
|
---|
11 | Copyright (C) 2010-2016 Oracle Corporation
|
---|
12 |
|
---|
13 | This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
14 | available from http://www.virtualbox.org. This file is free software;
|
---|
15 | you can redistribute it and/or modify it under the terms of the GNU
|
---|
16 | General Public License (GPL) as published by the Free Software
|
---|
17 | Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
18 | VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
19 | hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
20 |
|
---|
21 | The contents of this file may alternatively be used under the terms
|
---|
22 | of the Common Development and Distribution License Version 1.0
|
---|
23 | (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
24 | VirtualBox OSE distribution, in which case the provisions of the
|
---|
25 | CDDL are applicable instead of those of the GPL.
|
---|
26 |
|
---|
27 | You may elect to license modified versions of this file under the
|
---|
28 | terms and conditions of either the GPL or the CDDL or both.
|
---|
29 | """
|
---|
30 | __version__ = "$Revision: 62484 $"
|
---|
31 |
|
---|
32 |
|
---|
33 | # Standard Python imports.
|
---|
34 | import os
|
---|
35 | import sys
|
---|
36 | import time
|
---|
37 | import threading
|
---|
38 | import types
|
---|
39 |
|
---|
40 | # Only the main script needs to modify the path.
|
---|
41 | try: __file__
|
---|
42 | except: __file__ = sys.argv[0];
|
---|
43 | g_ksValidationKitDir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))));
|
---|
44 | sys.path.append(g_ksValidationKitDir);
|
---|
45 |
|
---|
46 | # Validation Kit imports.
|
---|
47 | from testdriver import reporter;
|
---|
48 | from testdriver import base;
|
---|
49 | from testdriver import vbox;
|
---|
50 |
|
---|
51 |
|
---|
52 | class tdPython1(vbox.TestDriver):
|
---|
53 | """
|
---|
54 | Python Bindings Test #1.
|
---|
55 | """
|
---|
56 |
|
---|
57 | def __init__(self):
|
---|
58 | vbox.TestDriver.__init__(self);
|
---|
59 | self.asRsrcs = None;
|
---|
60 |
|
---|
61 |
|
---|
62 | #
|
---|
63 | # Overridden methods.
|
---|
64 | #
|
---|
65 |
|
---|
66 | def actionConfig(self):
|
---|
67 | """
|
---|
68 | Import the API.
|
---|
69 | """
|
---|
70 | if not self.importVBoxApi():
|
---|
71 | return False;
|
---|
72 | return True;
|
---|
73 |
|
---|
74 | def actionExecute(self):
|
---|
75 | """
|
---|
76 | Execute the testcase.
|
---|
77 | """
|
---|
78 | return self.testEventQueueWaiting() \
|
---|
79 | and self.testEventQueueInterrupt();
|
---|
80 |
|
---|
81 | #
|
---|
82 | # Test execution helpers.
|
---|
83 | #
|
---|
84 |
|
---|
85 | def testEventQueueWaitingThreadProc(self):
|
---|
86 | """ Thread procedure for checking that waitForEvents fails when not called by the main thread. """
|
---|
87 | try:
|
---|
88 | rc2 = self.oVBoxMgr.waitForEvents(0);
|
---|
89 | except:
|
---|
90 | return True;
|
---|
91 | reporter.error('waitForEvents() returned "%s" when called on a worker thread, expected exception.' % (rc2,));
|
---|
92 | return False;
|
---|
93 |
|
---|
94 | def testEventQueueWaiting(self):
|
---|
95 | """
|
---|
96 | Test event queue waiting.
|
---|
97 | """
|
---|
98 | reporter.testStart('waitForEvents');
|
---|
99 |
|
---|
100 | # Check return values and such.
|
---|
101 | for cMsTimeout in (0, 1, 2, 3, 256, 1000, 0):
|
---|
102 | iLoop = 0;
|
---|
103 | while True:
|
---|
104 | try:
|
---|
105 | rc = self.oVBoxMgr.waitForEvents(cMsTimeout);
|
---|
106 | except:
|
---|
107 | reporter.errorXcpt();
|
---|
108 | break;
|
---|
109 | if not isinstance(rc, types.IntType):
|
---|
110 | reporter.error('waitForEvents returns non-integer type');
|
---|
111 | break;
|
---|
112 | if rc == 1:
|
---|
113 | break;
|
---|
114 | if rc != 0:
|
---|
115 | reporter.error('waitForEvents returns "%s", expected 0 or 1' % (rc,));
|
---|
116 | break;
|
---|
117 | iLoop += 1;
|
---|
118 | if iLoop > 10240:
|
---|
119 | reporter.error('waitForEvents returns 0 (success) %u times. '
|
---|
120 | 'Expected 1 (timeout/interrupt) after a call or two.'
|
---|
121 | % (iLoop,));
|
---|
122 | break;
|
---|
123 | if reporter.testErrorCount() != 0:
|
---|
124 | break;
|
---|
125 |
|
---|
126 | # Check that we get an exception when trying to call the method from
|
---|
127 | # a different thread.
|
---|
128 | reporter.log('If running a debug build, you will see an ignored assertion now. Please ignore it.')
|
---|
129 | sVBoxAssertSaved = os.environ.get('VBOX_ASSERT', 'breakpoint');
|
---|
130 | os.environ['VBOX_ASSERT'] = 'ignore';
|
---|
131 | oThread = threading.Thread(target=self.testEventQueueWaitingThreadProc);
|
---|
132 | oThread.start();
|
---|
133 | oThread.join();
|
---|
134 | os.environ['VBOX_ASSERT'] = sVBoxAssertSaved;
|
---|
135 |
|
---|
136 | return reporter.testDone()[1] == 0;
|
---|
137 |
|
---|
138 | def interruptWaitEventsThreadProc(self):
|
---|
139 | """ Thread procedure that's used for waking up the main thread. """
|
---|
140 | time.sleep(2);
|
---|
141 | try:
|
---|
142 | rc2 = self.oVBoxMgr.interruptWaitEvents();
|
---|
143 | except:
|
---|
144 | reporter.errorXcpt();
|
---|
145 | else:
|
---|
146 | if rc2 is True:
|
---|
147 | return True;
|
---|
148 | reporter.error('interruptWaitEvents returned "%s" when called from other thread, expected True' % (rc2,));
|
---|
149 | return False;
|
---|
150 |
|
---|
151 | def testEventQueueInterrupt(self):
|
---|
152 | """
|
---|
153 | Test interrupting an event queue wait.
|
---|
154 | """
|
---|
155 | reporter.testStart('interruptWait');
|
---|
156 |
|
---|
157 | # interrupt ourselves first and check the return value.
|
---|
158 | for i in range(0, 10):
|
---|
159 | try:
|
---|
160 | rc = self.oVBoxMgr.interruptWaitEvents();
|
---|
161 | except:
|
---|
162 | reporter.errorXcpt();
|
---|
163 | break;
|
---|
164 | if rc is not True:
|
---|
165 | reporter.error('interruptWaitEvents returned "%s" expected True' % (rc,));
|
---|
166 | break
|
---|
167 |
|
---|
168 | if reporter.testErrorCount() == 0:
|
---|
169 | #
|
---|
170 | # Interrupt a waitForEvents call.
|
---|
171 | #
|
---|
172 | # This test ASSUMES that no other events are posted to the thread's
|
---|
173 | # event queue once we've drained it. Also ASSUMES the box is
|
---|
174 | # relatively fast and not too busy because we're timing sensitive.
|
---|
175 | #
|
---|
176 | for i in range(0, 4):
|
---|
177 | # Try quiesce the event queue.
|
---|
178 | for _ in range(1, 100):
|
---|
179 | self.oVBoxMgr.waitForEvents(0);
|
---|
180 |
|
---|
181 | # Create a thread that will interrupt us in 2 seconds.
|
---|
182 | try:
|
---|
183 | oThread = threading.Thread(target=self.interruptWaitEventsThreadProc);
|
---|
184 | oThread.setDaemon(False);
|
---|
185 | except:
|
---|
186 | reporter.errorXcpt();
|
---|
187 | break;
|
---|
188 |
|
---|
189 | cMsTimeout = 20000;
|
---|
190 | if i == 2:
|
---|
191 | cMsTimeout = -1;
|
---|
192 | elif i == 3:
|
---|
193 | cMsTimeout = -999999;
|
---|
194 |
|
---|
195 | # Do the wait.
|
---|
196 | oThread.start();
|
---|
197 | msNow = base.timestampMilli();
|
---|
198 | try:
|
---|
199 | rc = self.oVBoxMgr.waitForEvents(cMsTimeout);
|
---|
200 | except:
|
---|
201 | reporter.errorXcpt();
|
---|
202 | else:
|
---|
203 | msElapsed = base.timestampMilli() - msNow;
|
---|
204 |
|
---|
205 | # Check the return code and elapsed time.
|
---|
206 | if not isinstance(rc, types.IntType):
|
---|
207 | reporter.error('waitForEvents returns non-integer type after %u ms, expected 1' % (msElapsed,));
|
---|
208 | elif rc != 1:
|
---|
209 | reporter.error('waitForEvents returned "%s" after %u ms, expected 1' % (rc, msElapsed));
|
---|
210 | if msElapsed > 15000:
|
---|
211 | reporter.error('waitForEvents after %u ms, expected just above 2-3 seconds' % (msElapsed,));
|
---|
212 | elif msElapsed < 100:
|
---|
213 | reporter.error('waitForEvents after %u ms, expected more than 100 ms.' % (msElapsed,));
|
---|
214 |
|
---|
215 | oThread.join();
|
---|
216 | oThread = None;
|
---|
217 | if reporter.testErrorCount() != 0:
|
---|
218 | break;
|
---|
219 | reporter.log('Iteration %u was successful...' % (i + 1,));
|
---|
220 | return reporter.testDone()[1] == 0;
|
---|
221 |
|
---|
222 |
|
---|
223 | if __name__ == '__main__':
|
---|
224 | sys.exit(tdPython1().main(sys.argv));
|
---|
225 |
|
---|