コード例 #1
0
def test_cache_invalidation():
    cache = Cache({})
    cache.invalidate(12 * 60 * 60)

    cache["pi"] = 3
    cache.invalidate(12 * 60 * 60)
    assert "pi" in cache
    cache.invalidate(-1)
    assert "pi" not in cache
コード例 #2
0
ファイル: __init__.py プロジェクト: toknapp/telepresence
    def __init__(self, output: Output, kubeinfo, verbose: bool) -> None:
        """
        :param output: The Output instance for the session
        :param kubeinfo: How to run kubectl or equivalent
        :param verbose: Whether subcommand should run in verbose mode.
        """
        self.output = output
        self.kubectl = kubeinfo
        self.verbose = verbose
        self.start_time = time()
        self.current_span = None  # type: typing.Optional[Span]
        self.counter = 0
        self.cleanup_stack = []  # type: typing.List[_CleanupItem]
        self.tracked = None  # type: typing.Optional[TrackedBG]
        self.sudo_held = False

        if sys.platform.startswith("linux"):
            self.platform = "linux"
        elif sys.platform.startswith("darwin"):
            self.platform = "darwin"
        else:
            # For untested platforms...
            self.platform = sys.platform
        self.output.write("Platform: {}".format(self.platform))

        term_width = 99999
        self.chatty = False
        if sys.stderr.isatty():
            err_fd = sys.stderr.fileno()
            try:
                term_width = os.get_terminal_size(err_fd).columns - 1
                self.chatty = True
            except OSError:
                pass
        self.wrapper = textwrap.TextWrapper(
            width=term_width,
            initial_indent="T: ",
            subsequent_indent="T: ",
            replace_whitespace=False,
            drop_whitespace=False,
        )
        self.session_id = uuid.uuid4().hex

        # Log some version info
        self.output.write("Python {}".format(sys.version))
        self.check_call(["uname", "-a"])

        cache_dir = os.path.expanduser("~/.cache/telepresence")
        os.makedirs(cache_dir, exist_ok=True)
        self.cache = Cache.load(os.path.join(cache_dir, "cache.json"))
        self.cache.invalidate(12 * 60 * 60)
        self.add_cleanup("Save caches", self.cache.save)

        # Docker for Mac only shares some folders; the default TMPDIR
        # on OS X is not one of them, so make sure we use /tmp:
        self.temp = Path(mkdtemp(prefix="tel-", dir="/tmp"))
        (self.temp / "session_id.txt").write_text(self.session_id)
        self.add_cleanup("Remove temporary directory", rmtree, self.temp)
コード例 #3
0
def test_cache():
    cache = Cache({})
    assert "foo" not in cache
    assert cache.lookup("foo", lambda: 3) == 3
    assert "foo" in cache