def checkVolatileCacheWithImmediateLastTransaction(self): # Earlier, a ClientStorage would not have the last transaction id # available right after successful connection, this is required now. addr = self._storage._addr storage2 = ClientStorage(addr) self.assert_(storage2.is_connected()) self.assertEquals(None, storage2.lastTransaction()) storage2.close() self._dostore() storage3 = ClientStorage(addr) self.assert_(storage3.is_connected()) self.assertEquals(8, len(storage3.lastTransaction())) self.assertNotEquals(ZODB.utils.z64, storage3.lastTransaction()) storage3.close()
def _main(host, port, unix=None, days=1, username=None, password=None, realm=None, blob_dir=None, storage='1', shared_blob_dir=True): if unix is not None: addr = unix else: if host is None: host = socket.gethostname() addr = host, int(port) if blob_dir: blob_dir = os.path.abspath(blob_dir) cs = None logger = logging.getLogger(__name__) try: # We do not want to wait until a zeoserver is up and running; it # should already be running, so wait=False cs = ClientStorage( addr, storage=storage, wait=False, read_only=True, username=username, password=password, realm=realm, blob_dir=blob_dir, shared_blob_dir=shared_blob_dir, ) for i in range(60): if cs.is_connected(): break time.sleep(1) else: logger.error("Could not connect to zeoserver. Please make sure it " "is running.") cs.close() sys.exit(1) try: # The script should not exit until the packing is done. # => wait=True cs.pack(wait=True, days=int(days)) except ClientDisconnected: logger.error("Disconnected from zeoserver. Please make sure it " "is still running.") sys.exit(1) finally: if cs is not None: cs.close()
def checkConnectionInvalidationOnReconnect(self): storage = ClientStorage(self.addr, wait=1, min_disconnect_poll=0.1) self._storage = storage # and we'll wait for the storage to be reconnected: for i in range(100): if storage.is_connected(): break time.sleep(0.1) else: raise AssertionError("Couldn't connect to server") class DummyDB: _invalidatedCache = 0 def invalidateCache(self): self._invalidatedCache += 1 def invalidate(*a, **k): pass db = DummyDB() storage.registerDB(db) base = db._invalidatedCache # Now we'll force a disconnection and reconnection storage._connection.close() # and we'll wait for the storage to be reconnected: for i in range(100): if storage.is_connected(): break time.sleep(0.1) else: raise AssertionError("Couldn't connect to server") # Now, the root object in the connection should have been invalidated: self.assertEqual(db._invalidatedCache, base + 1)
def _main( host, port, unix=None, days=1, username=None, password=None, realm=None, blob_dir=None, storage="1", shared_blob_dir=True, ): if unix is not None: addr = unix else: if host is None: host = socket.gethostname() addr = host, int(port) if blob_dir: blob_dir = os.path.abspath(blob_dir) cs = None logger = logging.getLogger(__name__) try: # We do not want to wait until a zeoserver is up and running; it # should already be running, so wait=False cs = ClientStorage( addr, storage=storage, wait=False, read_only=True, username=username, password=password, realm=realm, blob_dir=blob_dir, shared_blob_dir=shared_blob_dir, ) if not cs.is_connected(): logger.error("Could not connect to zeoserver. Please make sure it " "is running.") sys.exit(1) try: # The script should not exit until the packing is done. # => wait=True cs.pack(wait=True, days=int(days)) except ClientDisconnected: logger.error("Disconnected from zeoserver. Please make sure it " "is still running.") sys.exit(1) finally: if cs is not None: cs.close()
class ZEOClassifier(ZODBClassifier): def __init__(self, data_source_name): source_info = data_source_name.split() self.host = "localhost" self.port = None db_name = "SpamBayes" self.username = "" self.password = "" self.storage_name = "1" self.wait = None self.wait_timeout = None for info in source_info: if info.startswith("host"): try: self.host = str(info[5:]) except UnicodeDecodeError as e: print("Couldn't set host", info[5:], str(e), file=sys.stderr) elif info.startswith("port"): self.port = int(info[5:]) elif info.startswith("dbname"): db_name = info[7:] elif info.startswith("user"): self.username = info[5:] elif info.startswith("pass"): self.password = info[5:] elif info.startswith("storage_name"): self.storage_name = info[13:] elif info.startswith("wait_timeout"): self.wait_timeout = int(info[13:]) elif info.startswith("wait"): self.wait = info[5:] == "True" ZODBClassifier.__init__(self, db_name) def create_storage(self): from ZEO.ClientStorage import ClientStorage if self.port: addr = self.host, self.port else: addr = self.host if options["globals", "verbose"]: print("Connecting to ZEO server", addr, self.username, self.password, file=sys.stderr) try: self.storage = ClientStorage( addr, name=self.db_name, read_only=self.mode == "r", username=self.username, client=self.db_name, wait=self.wait, wait_timeout=self.wait_timeout, storage=self.storage_name, var=tempfile.gettempdir(), password=self.password, ) except ValueError: try: os.remove(os.path.join(tempfile.gettempdir(), self.db_name + self.storage_name + ".zec")) except OSError: pass self.storage = ClientStorage( addr, name=self.db_name, read_only=self.mode == "r", username=self.username, wait=self.wait, wait_timeout=self.wait_timeout, storage=self.storage_name, password=self.password, ) def is_connected(self): return self.storage.is_connected()
class ZEOClassifier(ZODBClassifier): def __init__(self, data_source_name): source_info = data_source_name.split() self.host = "localhost" self.port = None db_name = "SpamBayes" self.username = '' self.password = '' self.storage_name = '1' self.wait = None self.wait_timeout = None for info in source_info: if info.startswith("host"): try: # ZEO only accepts strings, not unicode. self.host = six.text_type(info[5:]) except UnicodeDecodeError as e: print("Couldn't set host", \ info[5:], str(e), file=sys.stderr) elif info.startswith("port"): self.port = int(info[5:]) elif info.startswith("dbname"): db_name = info[7:] elif info.startswith("user"): self.username = info[5:] elif info.startswith("pass"): self.password = info[5:] elif info.startswith("storage_name"): self.storage_name = info[13:] elif info.startswith("wait_timeout"): self.wait_timeout = int(info[13:]) elif info.startswith("wait"): self.wait = info[5:] == "True" ZODBClassifier.__init__(self, db_name) def create_storage(self): from ZEO.ClientStorage import ClientStorage if self.port: addr = self.host, self.port else: addr = self.host if options["globals", "verbose"]: print("Connecting to ZEO server", addr, \ self.username, self.password, file=sys.stderr) # Use persistent caches, with the cache in the temp directory. # If the temp directory is cleared out, we lose the cache, but # that doesn't really matter, and we should always be able to # write to it. try: self.storage = ClientStorage(addr, name=self.db_name, read_only=self.mode=='r', username=self.username, client=self.db_name, wait=self.wait, wait_timeout=self.wait_timeout, storage=self.storage_name, var=tempfile.gettempdir(), password=self.password) except ValueError: # Probably bad cache; remove it and try without the cache. try: os.remove(os.path.join(tempfile.gettempdir(), self.db_name + \ self.storage_name + ".zec")) except OSError: pass self.storage = ClientStorage(addr, name=self.db_name, read_only=self.mode=='r', username=self.username, wait=self.wait, wait_timeout=self.wait_timeout, storage=self.storage_name, password=self.password) def is_connected(self): return self.storage.is_connected()