public async Task AcquireLockAsync_WithTwoTasksOnMainThread_SerializesAccess() { var tcs1 = new TaskCompletionSource <bool>(); var tcs2 = new TaskCompletionSource <bool>(); var secondTaskAcquiredTheLock = false; var jt1 = _jtf.RunAsync(async() => { await _jtf.SwitchToMainThreadAsync(); // awaits global lock acquisition on the main thread (JTF) await _lockService.ExecuteNuGetOperationAsync(async() => { using (var resource = new ProtectedResource()) { await _jtf.RunAsync(async() => { // once lock is acquired proceeds with long running task on a pool thread. await TaskScheduler.Default; // signals the second task the lock is acquired tcs1.TrySetResult(true); // waits for the second task get started await tcs2.Task; // dealys for some extra time emulating long operation await Task.Delay(TimeSpan.FromSeconds(5)); }); } }, _cts.Token); }); var jt2 = _jtf.RunAsync(async() => { // ensure first task acquired the lock await tcs1.Task; await _jtf.SwitchToMainThreadAsync(); var awaiter = _lockService.ExecuteNuGetOperationAsync(() => { using (var resource = new ProtectedResource()) { secondTaskAcquiredTheLock = true; } return(Task.FromResult(true)); }, _cts.Token); // signal the first task tcs2.TrySetResult(true); var x = await awaiter; }); await Task.WhenAll(jt1.Task, jt2.Task); Assert.True(secondTaskAcquiredTheLock); }
/// <summary> /// Gets the <see cref="ProtectedOperation"/> of this operation descriptor. /// </summary> /// <param name="context">The <see cref="IMansionContext"/>.</param> /// <param name="resource">The <see cref="ProtectedResource"/>.</param> /// <returns>Returns the <see cref="ProtectedOperation"/> instance.</returns> public ProtectedOperation GetOperation(IMansionContext context, ProtectedResource resource) { // validate arguments if (context == null) throw new ArgumentNullException("context"); if (resource == null) throw new ArgumentNullException("resource"); // create the operation return new ProtectedOperation(Properties.Get<string>(context, "id"), resource) { Name = Properties.Get(context, "name", Properties.Get<string>(context, "id")) }; }
public void WhenItHasAccessTokenItCanAccessProtectedResource() { // Arrange Request protectedResourceRequest = Session.Bind(OAuth2TestConstants.ProtectedResourcePath); AssertThrowsWebException(() => protectedResourceRequest.Get(), HttpStatusCode.Unauthorized); // Act Session.OAuth2_Configure(GetSettings()) .OAuth2_GetAccessTokenUsingOwnerUsernamePassword(OAuth2TestConstants.Username, OAuth2TestConstants.UserPassword); using (var response = protectedResourceRequest.AcceptJson().Get <ProtectedResource>()) { ProtectedResource r = response.Body; // Assert Assert.IsNotNull(r); Assert.AreEqual("Got it", r.Title); } }
/// <summary> /// Gets the <see cref="ProtectedResource"/> of this security descriptor. /// </summary> /// <param name="context">The <see cref="IMansionContext"/>.</param> /// <returns>Returns the <see cref="ProtectedResource"/> instance.</returns> public ProtectedResource GetResource(IMansionContext context) { // validate arguments if (context == null) throw new ArgumentNullException("context"); // create the resource var resource = new ProtectedResource(TypeDefinition.Name); // loop over all the operation foreach (var operationDescriptor in GetDescriptors<OperationDescriptor>()) { // create the operation var operation = operationDescriptor.GetOperation(context, resource); // add the operation to the resource resource.Add(operation); } return resource; }
public void CanRestoreSessionStateWithAccessToken() { // Arrange Session.OAuth2_Configure(GetSettings()) .OAuth2_GetAccessTokenUsingOwnerUsernamePassword(OAuth2TestConstants.Username, OAuth2TestConstants.UserPassword); OAuth2SessionState state = Session.OAuth2_GetState(); // Act ISession newSession = TestService.NewSession(); Request protectedResourceRequest = newSession.OAuth2_RestoreState(state).Bind(OAuth2TestConstants.ProtectedResourcePath); using (var response = protectedResourceRequest.AcceptJson().Get <ProtectedResource>()) { ProtectedResource r = response.Body; // Assert Assert.IsNotNull(r); Assert.AreEqual("Got it", r.Title); } }
public ValueTask <AuthorizationScope> RemoveResourceAsync(AuthorizationScope scope, ProtectedResource resource) { throw new System.NotImplementedException(); }