def runInTransaction(self, keyPathElement, toRun, waitForKeyToLoad = True): """ Runs a specified function in a shared state transaction. toRun is a function that takes the following argumenst: self - a self reference key - the shared state key used by the test view - the a shared state view subscribed to the key range that contains key. waitForKeyToLoad - wait for SharedState to actually load the key """ view = self.sharedState.newView() view.subscribe(self.keyRange) passes = 0 while passes < 100: with SharedState.Transaction(view): key = SharedState.Key(self.keyspace, (Node.keyPathToKeyName((keyPathElement,)),)) if not waitForKeyToLoad or view[key] is not None: toRun(self, key, view) return passes += 1 time.sleep(.1) assert False, "Test timed out"
def synchronizerWaitForKey(self, keyName, synchronizer): isSet = [False] updateDict = {} keyName = Node.keyPathToKeyName((keyName,)) class Listener: def keysLoaded(listenerSelf, keyValueDict, isInitialLoad): if keyName in keyValueDict: isSet[0] = True updateDict.update(keyValueDict) synchronizer.addKeyspaceListener(self.keyspaceName, Listener(), NativeJson.Json(())) passes = 0 while not isSet[0] and passes < 100: time.sleep(.1) synchronizer.update() passes += 1 if not isSet[0]: assert False, "Failed to load %s. did see %s" % (keyName, updateDict)