Esempio n. 1
0
        public async Task ShouldExecuteAction()
        {
            // Arrange
            var semaphore = new SemaphoreSlim(1);

            // Act + Assert
            await SpotifySemaphoreUtils.ExecuteAsync(semaphore, async innerCt => await Task.Yield(), CancellationToken.None);
        }
Esempio n. 2
0
 public async Task <bool> RemoveSessionAsync(CancellationToken cancellationToken)
 {
     return(await SpotifySemaphoreUtils.ExecuteAsync(
                this.semaphore,
                async innerCt =>
     {
         return await this.UnloadAuthenticationTicketAsync(innerCt).ConfigureAwait(false);
     },
                cancellationToken).ConfigureAwait(false));
 }
Esempio n. 3
0
        public async Task ShouldExecuteFunc()
        {
            // Arrange
            var semaphore = new SemaphoreSlim(1);

            // Act
            var result = await SpotifySemaphoreUtils.ExecuteAsync(semaphore, async innerCt => { await Task.Yield(); return(3); }, CancellationToken.None);

            // Assert
            result.Should().Be(3);
        }
Esempio n. 4
0
        public async Task <IAuthenticationTicket> RenewAccessTokenAsync(IAuthenticationTicket currentAuthenticationTicket, CancellationToken cancellationToken)
        {
            var result = await SpotifySemaphoreUtils.ExecuteAsync(
                this.semaphoreProvider.Get(),
                async innerCt =>
            {
                var authenticateResult   = await this.authenticationManager.GetAsync(innerCt).ConfigureAwait(false);
                var authenticationTicket = new AuthenticationTicket(authenticateResult);

                return(await this.GetNewAccessTokenAsync(authenticationTicket, authenticateResult, innerCt).ConfigureAwait(false));
            },
                cancellationToken).ConfigureAwait(false);

            return(result);
        }
        public async Task <IAuthenticationTicket> GetAsync(bool ensureValidAccessToken, CancellationToken cancellationToken)
        {
            var result = await SpotifySemaphoreUtils.ExecuteAsync(
                this.semaphore,
                async innerCt =>
            {
                if (this.authenticationTicket == null || (ensureValidAccessToken && this.authenticationTicket.AccessToken.IsExpired(this.clock)))
                {
                    await this.LoadAuthenticationTicketAsync(innerCt).ConfigureAwait(false);
                }

                return(this.authenticationTicket);
            },
                cancellationToken).ConfigureAwait(false);

            return(result);
        }
Esempio n. 6
0
        public async Task <IAuthenticationTicket> GetAsync(bool ensureValidAccessToken, CancellationToken cancellationToken)
        {
            var result = await SpotifySemaphoreUtils.ExecuteAsync(
                this.semaphoreProvider.Get(),
                async innerCt =>
            {
                var authenticateResult   = await this.authenticationManager.GetAsync(innerCt).ConfigureAwait(false);
                var authenticationTicket = new AuthenticationTicket(authenticateResult);

                if (ensureValidAccessToken && authenticationTicket.AccessToken.IsExpired(this.clock))
                {
                    authenticationTicket = await this.GetNewAccessTokenAsync(authenticationTicket, authenticateResult, innerCt).ConfigureAwait(false);
                }

                return(authenticationTicket);
            },
                cancellationToken).ConfigureAwait(false);

            return(result);
        }
Esempio n. 7
0
        public async Task <bool> RestoreSessionAsync(CancellationToken cancellationToken)
        {
            return(await SpotifySemaphoreUtils.ExecuteAsync(
                       this.semaphore,
                       async innerCt =>
            {
                if (this.authenticationTicket == null)
                {
                    if (!(await this.TryRestoreAuthenticationTicketAsync(innerCt).ConfigureAwait(false)))
                    {
                        throw new SpotifySessionNotFoundException();
                    }

                    return true;
                }

                return false;
            },
                       cancellationToken).ConfigureAwait(false));
        }
Esempio n. 8
0
 public async Task <SessionState> GetSessionStateAsync(CancellationToken cancellationToken)
 {
     return(await SpotifySemaphoreUtils.ExecuteAsync(
                this.semaphore,
                async innerCt =>
     {
         if (this.authenticationTicket != null)
         {
             return SessionState.CachedInMemory;
         }
         else if ((await this.authenticationTicketRepository.GetAsync(innerCt).ConfigureAwait(false)) != null)
         {
             return SessionState.StoredInLocalStorage;
         }
         else
         {
             return SessionState.NotFound;
         }
     },
                cancellationToken).ConfigureAwait(false));
 }
Esempio n. 9
0
        public async Task <RestoreSessionOrAuthorizeUserResult> RestoreSessionOrAuthorizeUserAsync(CancellationToken cancellationToken)
        {
            return(await SpotifySemaphoreUtils.ExecuteAsync(
                       this.semaphore,
                       async innerCt =>
            {
                if (this.authenticationTicket == null)
                {
                    if (!(await this.TryRestoreAuthenticationTicketAsync(innerCt).ConfigureAwait(false)))
                    {
                        await this.LoadAuthenticationTicketAsync(innerCt).ConfigureAwait(false);
                        return RestoreSessionOrAuthorizeUserResult.PerfomedUserAuthorization;
                    }

                    return RestoreSessionOrAuthorizeUserResult.RestoredSessionFromLocalStorage;
                }

                return RestoreSessionOrAuthorizeUserResult.NoAction;
            },
                       cancellationToken).ConfigureAwait(false));
        }
        public async Task <IAuthenticationTicket> RenewAccessTokenAsync(IAuthenticationTicket currentAuthenticationTicket, CancellationToken cancellationToken)
        {
            SpotifyArgumentAssertUtils.ThrowIfNull(currentAuthenticationTicket, nameof(currentAuthenticationTicket));

            var result = await SpotifySemaphoreUtils.ExecuteAsync(
                this.semaphore,
                async innerCt =>
            {
                if (this.authenticationTicket == null ||
                    this.authenticationTicket.AccessToken.IsExpired(this.clock) ||
                    this.authenticationTicket == currentAuthenticationTicket)
                {
                    await this.LoadAuthenticationTicketAsync(innerCt).ConfigureAwait(false);
                }

                return(this.authenticationTicket);
            },
                cancellationToken).ConfigureAwait(false);

            return(result);
        }