VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/tests/api/tdPython1.py@ 78351

Last change on this file since 78351 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette