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