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 setUp(self): self.sharedState = SharedStateTestHarness(inMemory = True) self.keyspacePath = (0, 1) self.keyspaceName = Node.keyspacePathToKeyspaceName(self.keyspacePath) self.keyName = 'key' self.keyValue = 'value' self.keyspace = SharedState.Keyspace("TakeHighestIdKeyType", self.keyspaceName, 1) self.keyRange = SharedState.KeyRange(self.keyspace, 0, None, None, True, False)
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)