def checkTelnetHooks(scpiObj): _printHeader("Telnet hooks") try: ipv4 = Telnet("127.0.0.1", 5025) ipv6 = Telnet("::1", 5025) cmd = "*IDN?" def hook(who, what): _printInfo("\t\thook call, received: (%r, %r)" % (who, what)) scpiObj.addConnectionHook(hook) _printInfo("\tipv4 send %s" % (cmd)) ipv4.write(cmd) _printInfo("\tipv4 answer %r" % ipv4.read_until('\n')) _printInfo("\tipv6 send %s" % (cmd)) ipv6.write(cmd) _printInfo("\tipv6 answer %r" % ipv6.read_until('\n')) scpiObj.removeConnectionHook(hook) ipv4.close() ipv6.close() result = True, "Telnet hooks test PASSED" except Exception as e: print("\tUnexpected kind of exception! %s" % e) print_exc() result = False, "Telnet hooks test FAILED" _printFooter(result[1]) return result
def _print(self, msg, level=1, top=False, bottom=False): _printInfo(msg, level=level, lock=self._printLock, top=top, bottom=bottom)
def multithreadingTake(lockObj): def sendEvent(eventLst, who): eventLst[who].set() while eventLst[who].isSet(): # wait to the thread to work _sleep(1) testName = "Lock take test" _printHeader("%s for %s" % (testName, lockObj)) joinerEvent = _Event() joinerEvent.clear() userThreads = [] requestEvents = [] accessEvents = [] releaseEvents = [] printLock = _Lock() for i in range(2): requestEvent = _Event() accessEvent = _Event() releaseEvent = _Event() userThread = _Thread(target=threadFunction, args=(lockObj, joinerEvent, requestEvent, accessEvent, releaseEvent, printLock), name='%d' % (i)) requestEvents.append(requestEvent) accessEvents.append(accessEvent) releaseEvents.append(releaseEvent) userThreads.append(userThread) userThread.start() # here is where the test starts --- try: _printInfo("Initial state %r\n" % (lockObj), level=1, lock=printLock) if lockObj.isLock(): return False, "%s FAILED" % (testName) _printInfo("Tell the threads to access", level=1, lock=printLock, top=True) sendEvent(accessEvents, 0) sendEvent(accessEvents, 1) _printInfo("both should have had access", level=1, lock=printLock, bottom=True) _printInfo("Thread 0 take the lock", level=1, lock=printLock, top=True) sendEvent(requestEvents, 0) if not lockObj.isLock() or lockObj.owner != '0': raise Exception("It shall be lock by 0") _printInfo("Tell the threads to access", level=1, lock=printLock) sendEvent(accessEvents, 0) sendEvent(accessEvents, 1) _printInfo("0 should, but 1 don't", level=1, lock=printLock, bottom=True) _printInfo("Try to lock when it is already", level=1, lock=printLock, top=True) sendEvent(requestEvents, 1) if not lockObj.isLock() or lockObj.owner != '0': raise Exception("It shall be lock by user 0") _printInfo("Tell the threads to access", level=1, lock=printLock) sendEvent(accessEvents, 0) sendEvent(accessEvents, 1) _printInfo("0 should, but 1 don't", level=1, lock=printLock, bottom=True) _printInfo("Try to release by a NON-owner", level=1, lock=printLock, top=True) sendEvent(releaseEvents, 1) if not lockObj.isLock() or lockObj.owner != '0': raise Exception("It shall be lock by user 0") _printInfo("Tell the threads to access", level=1, lock=printLock) sendEvent(accessEvents, 0) sendEvent(accessEvents, 1) _printInfo("0 should, but 1 don't", level=1, lock=printLock, bottom=True) _printInfo("release the lock", level=1, lock=printLock, top=True) sendEvent(releaseEvents, 0) if lockObj.isLock(): raise Exception("It shall be released") _printInfo("Tell the threads to access", level=1, lock=printLock) sendEvent(accessEvents, 0) sendEvent(accessEvents, 1) _printInfo("both should have had to", level=1, lock=printLock, bottom=True) # TODO: timeout _printInfo("Thread 1 take the lock and expire it", level=1, lock=printLock, top=True) sendEvent(requestEvents, 1) if not lockObj.isLock() or lockObj.owner != '1': raise Exception("It shall be lock by 1") _printInfo("Tell the threads to access", level=1, lock=printLock) sendEvent(accessEvents, 0) sendEvent(accessEvents, 1) _printInfo("1 should, but 0 don't", level=1, lock=printLock) _printInfo("Sleep %d seconds to expire the lock" % TEST_EXPIRATION_TIME, level=1, lock=printLock) _sleep(TEST_EXPIRATION_TIME) _printInfo("Tell the threads to access", level=1, lock=printLock) sendEvent(accessEvents, 0) sendEvent(accessEvents, 1) _printInfo("both should have had to", level=1, lock=printLock, bottom=True) answer = True, "%s PASSED" % (testName) except Exception as e: print(e) print_exc() answer = False, "%s FAILED" % (testName) joinerEvent.set() while len(userThreads) > 0: userThread = userThreads.pop() userThread.join(1) if userThread.isAlive(): userThreads.append(userThread) print("All threads has finished") return answer
def lockTake(lockObj): testName = "Initial state test" _printHeader("Test the initial state of the %s object" % lockObj) _printInfo("%r" % (lockObj), 1) _printInfo("Check if it is lock", level=1, top=True) if lockObj.isLock(): return False, "%s FAILED" % (testName) _printInfo("%s is not lock" % (lockObj), level=1, bottom=True) _printInfo("Check if it lock can be requested", level=1, top=True) if not lockObj.request(): return False, "%s FAILED" % (testName) _printInfo("%r is now lock" % (lockObj), level=1, bottom=True) _printInfo("Check if it lock can be released", level=1, top=True) if not lockObj.release(): return False, "%s FAILED" % (testName) _printInfo("%r is now released" % (lockObj), level=1, bottom=True) return True, "%s PASSED" % (testName)
def threadFunction(lockObj, joinedEvent, request, access, release, printLock): _printInfo("started", level=1, lock=printLock) while not joinedEvent.isSet(): # _printInfo("loop", level=1, lock=printLock) if request.isSet(): take = lockObj.request(TEST_EXPIRATION_TIME) request.clear() _printInfo("request %s" % take, level=2, lock=printLock) if access.isSet(): if lockObj.access(): _printInfo("access allowed", level=2, lock=printLock) else: _printInfo("rejected to access", level=2, lock=printLock) access.clear() if release.isSet(): free = lockObj.release() release.clear() _printInfo("release %s" % free, level=2, lock=printLock) _sleep(1) _printInfo("exit", level=1, lock=printLock)
def hook(who, what): _printInfo("\t\thook call, received: (%r, %r)" % (who, what))