@Override
    public Optional<P> authenticate(C credentials) throws AuthenticationException {
        try (Timer.Context context = gets.time()) {
            return cache.get(credentials);
        } catch (CompletionException e) {
            final Throwable cause = e.getCause();
            if (cause instanceof InvalidCredentialsException) {
                return Optional.empty();
            }
            if (cause instanceof AuthenticationException) {
                throw (AuthenticationException) cause;
            }
            if (cause == null) {
                throw new AuthenticationException(e);
            }
            throw new AuthenticationException(cause);
        }
    }
@Test
    void shouldPropagateAuthenticationException() throws AuthenticationException {
        final AuthenticationException e = new AuthenticationException("Auth failed");
        when(underlying.authenticate(anyString())).thenThrow(e);

        assertThatExceptionOfType(AuthenticationException.class)
                .isThrownBy(() -> cached.authenticate("credentials"))
                .isEqualTo(e);
    }