-
-
Notifications
You must be signed in to change notification settings - Fork 402
Breaking change: Blazor StateManager does not use the provided timeout parameter #3910
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
760e17b
Updated ISessionManager and SessionManager with timeout and cancellat…
luizbicalhoagl d54e552
`Refactor SessionManager and StateManager for improved session handling`
luizbicalhoagl 911eb96
Refactor SessionManagerTests and remove certain test methods
luizbicalhoagl 2b35219
Refactor SessionManager and update related tests
luizbicalhoagl d648d77
Merge branch 'main' into main
luizfbicalho 33a0c17
`Convert SaveState to async in StateManager.cs`
luizbicalhoagl 8e4617b
Merge branch 'main' of https://github.com/luizfbicalho/csla
luizbicalhoagl 74ffc4c
`Update packages, refactor code, add tests, and remove project`
luizbicalhoagl ce84444
Updated SessionManager and its tests for async operations and better …
luizbicalhoagl 73e9986
Subject: Refactored test methods and updated session retrieval
luizbicalhoagl 5f59d4a
Refactor variable names to follow C# naming convention
luizbicalhoagl d9201aa
Merge branch 'main' into main
rockfordlhotka b6c01b9
Switch from Moq to NSubstitute in Csla.Blazor.WebAssembly.Tests
luizbicalhoagl 76c6b75
Merge branch 'main' of https://github.com/luizfbicalho/csla
luizbicalhoagl 57c3c5b
Merge branch 'main' into main
luizfbicalho File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
Source/Csla.Blazor.WebAssembly.Tests/Csla.Blazor.WebAssembly.Tests.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFrameworks>net8.0</TargetFrameworks> | ||
| <IsPackable>false</IsPackable> | ||
| <ApplicationIcon /> | ||
| <OutputType>Library</OutputType> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" /> | ||
| <PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.0" /> | ||
| <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" /> | ||
| <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" /> | ||
| <PackageReference Include="Moq" Version="4.20.70" /> | ||
| <PackageReference Include="MSTest.TestAdapter" Version="3.3.1" /> | ||
| <PackageReference Include="MSTest.TestFramework" Version="3.3.1" /> | ||
| <PackageReference Include="coverlet.collector" Version="6.0.2"> | ||
| <PrivateAssets>all</PrivateAssets> | ||
| <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
| </PackageReference> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup Condition="'$(TargetFramework)'=='netcoreapp3.1'"> | ||
| <PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="6.0.0" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\Csla.AspNetCore\Csla.AspNetCore.csproj" /> | ||
| <ProjectReference Include="..\Csla.Blazor.WebAssembly\Csla.Blazor.WebAssembly.csproj" /> | ||
| <ProjectReference Include="..\Csla.Blazor\Csla.Blazor.csproj" /> | ||
| <ProjectReference Include="..\Csla.TestHelpers\Csla.TestHelpers.csproj" /> | ||
| <ProjectReference Include="..\Csla\Csla.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> | ||
204 changes: 204 additions & 0 deletions
204
Source/Csla.Blazor.WebAssembly.Tests/SessionManagerTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,204 @@ | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using System; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Csla.Blazor.WebAssembly.State; | ||
| using Csla.State; | ||
| using Moq; | ||
| using System.Net.Http; | ||
| using Csla.Blazor.WebAssembly.Configuration; | ||
| using Csla.Core; | ||
| using Csla.Runtime; | ||
| using Moq.Protected; | ||
| using System.Net; | ||
| using Csla.Serialization; | ||
| using System.Runtime.Serialization.Formatters.Binary; | ||
| using Csla.Serialization.Mobile; | ||
| using Microsoft.AspNetCore.Components.Authorization; | ||
|
|
||
| namespace Csla.Test.State | ||
| { | ||
| [TestClass] | ||
| public class SessionManagerTests | ||
| { | ||
| private SessionManager _sessionManager; | ||
| private SessionMessage SessionValue; | ||
luizfbicalho marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| [TestInitialize] | ||
| public void Initialize() | ||
| { | ||
| var mockServiceProvider = new Mock<IServiceProvider>(); | ||
|
|
||
| // Mock AuthenticationStateProvider | ||
| var mockAuthStateProvider = new Mock<AuthenticationStateProvider>(); | ||
|
|
||
| // Mock IServiceProvider | ||
| mockServiceProvider.Setup(x => x.GetService(typeof(AuthenticationStateProvider))).Returns(mockAuthStateProvider.Object); | ||
|
|
||
| SessionValue = new SessionMessage | ||
| { | ||
| // Set properties here | ||
| // For example: | ||
| Principal = new Security.CslaClaimsPrincipal() { }, | ||
| Session = [] | ||
| }; | ||
|
|
||
| // Mock ISerializationFormatter | ||
| var mockFormatter = new Mock<ISerializationFormatter>(); | ||
| mockFormatter.Setup(x => x.Serialize(It.IsAny<Stream>(), It.IsAny<object>())); | ||
| mockFormatter.Setup(x => x.Deserialize(It.IsAny<Stream>())).Returns(SessionValue); | ||
|
|
||
| // Mock IServiceProvider | ||
| mockServiceProvider.Setup(x => x.GetService(typeof(Csla.Serialization.Mobile.MobileFormatter))).Returns(mockFormatter.Object); | ||
|
|
||
| var mockActivator = new Mock<Csla.Server.IDataPortalActivator>(); | ||
| mockActivator.Setup(x => x.CreateInstance(It.Is<Type>(t => t == typeof(Csla.Serialization.Mobile.MobileFormatter)))).Returns(mockFormatter.Object); | ||
| mockActivator.Setup(x => x.InitializeInstance(It.IsAny<object>())); | ||
|
|
||
| // Mock IServiceProvider | ||
| mockServiceProvider.Setup(x => x.GetService(typeof(Csla.Server.IDataPortalActivator))).Returns(mockActivator.Object); | ||
|
|
||
| // Mock IServiceProvider | ||
luizfbicalho marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Mock IContextManager | ||
| var mockContextManager = new Mock<IContextManager>(); | ||
| mockContextManager.Setup(x => x.IsValid).Returns(true); | ||
|
|
||
| // Mock IContextManagerLocal | ||
| var mockLocalContextManager = new Mock<IContextManagerLocal>(); | ||
|
|
||
| // Mock IServiceProvider | ||
| mockServiceProvider.Setup(x => x.GetService(typeof(IRuntimeInfo))).Returns(new RuntimeInfo()); | ||
|
|
||
| // Mock IEnumerable<IContextManager> | ||
| var mockContextManagerList = new List<IContextManager> { mockContextManager.Object }; | ||
|
|
||
| // Mock ApplicationContextAccessor | ||
| var mockApplicationContextAccessor = new Mock<ApplicationContextAccessor>(mockContextManagerList, mockLocalContextManager.Object, mockServiceProvider.Object); | ||
|
|
||
| var _applicationContext = new ApplicationContext(mockApplicationContextAccessor.Object); | ||
|
|
||
|
|
||
| _sessionManager = new SessionManager(_applicationContext, GetHttpClient(SessionValue, _applicationContext), new BlazorWebAssemblyConfigurationOptions { SyncContextWithServer = true }); | ||
| _sessionManager.RetrieveSession(TimeSpan.FromHours(1)); | ||
| } | ||
|
|
||
| private static HttpClient GetHttpClient(SessionMessage session, ApplicationContext _applicationContext) | ||
| { | ||
| var handlerMock = new Mock<HttpMessageHandler>(MockBehavior.Strict); | ||
| handlerMock | ||
| .Protected() | ||
| // Setup the PROTECTED method to mock | ||
| .Setup<Task<HttpResponseMessage>>( | ||
| "SendAsync", | ||
| ItExpr.IsAny<HttpRequestMessage>(), | ||
| ItExpr.IsAny<CancellationToken>() | ||
| ) | ||
| // prepare the expected response of the mocked http call | ||
| .ReturnsAsync((HttpRequestMessage request, CancellationToken cancellationToken) => | ||
| { | ||
|
|
||
luizfbicalho marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if (cancellationToken.IsCancellationRequested) | ||
| { | ||
| throw new OperationCanceledException(cancellationToken); | ||
| } | ||
| else | ||
| { | ||
| return new HttpResponseMessage() | ||
| { | ||
| StatusCode = HttpStatusCode.OK, | ||
| Content = new StringContent("{\"ResultStatus\":0, \"SessionData\":\"" + Convert.ToBase64String(GetSession(session, _applicationContext)) + "\"}"), | ||
| }; | ||
| } | ||
| }) | ||
| .Verifiable(); | ||
|
|
||
| // use real http client with mocked handler here | ||
| var httpClient = new HttpClient(handlerMock.Object) | ||
| { | ||
| BaseAddress = new Uri("http://test.com/"), | ||
| }; | ||
| return httpClient; | ||
| } | ||
|
|
||
| private static byte[] GetSession(SessionMessage session, ApplicationContext _applicationContext) | ||
| { | ||
| var info = new MobileFormatter(_applicationContext).SerializeToDTO(session); | ||
| var ms = new MemoryStream(); | ||
| new CslaBinaryWriter(_applicationContext).Write(ms, info); | ||
| ms.Position = 0; | ||
| var array = ms.ToArray(); | ||
| return array; | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task RetrieveSession_WithTimeoutValue_ShouldNotThrowException() | ||
| { | ||
| var timeout = TimeSpan.FromHours(1); | ||
| var session = await _sessionManager.RetrieveSession(timeout); | ||
| Assert.AreEqual(session, SessionValue.Session); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task RetrieveSession_WithZeroTimeout_ShouldThrowTimeoutException() | ||
| { | ||
| var timeout = TimeSpan.Zero; | ||
| await Assert.ThrowsExceptionAsync<TimeoutException>(() => _sessionManager.RetrieveSession(timeout)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task RetrieveSession_WithValidCancellationToken_ShouldNotThrowException() | ||
| { | ||
| var cts = new CancellationTokenSource(); | ||
| var session = await _sessionManager.RetrieveSession(cts.Token); | ||
| Assert.AreEqual(session, SessionValue.Session); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task RetrieveSession_WithCancelledCancellationToken_ShouldThrowTaskCanceledException() | ||
| { | ||
| var cts = new CancellationTokenSource(); | ||
| cts.Cancel(); | ||
| await Assert.ThrowsExceptionAsync<TaskCanceledException>(() => _sessionManager.RetrieveSession(cts.Token)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task SendSession_WithTimeoutValue_ShouldNotThrowException() | ||
| { | ||
| var timeout = TimeSpan.FromHours(1); | ||
| await _sessionManager.SendSession(timeout); | ||
| Assert.IsTrue(true); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task SendSession_WithZeroTimeout_ShouldThrowTimeoutException() | ||
| { | ||
| var timeout = TimeSpan.Zero; | ||
| await Assert.ThrowsExceptionAsync<TimeoutException>(() => _sessionManager.SendSession(timeout)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task SendSession_WithValidCancellationToken_ShouldNotThrowException() | ||
| { | ||
| var cts = new CancellationTokenSource(); | ||
| await _sessionManager.SendSession(cts.Token); | ||
| Assert.IsTrue(true); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task SendSession_WithCancelledCancellationToken_ShouldThrowTaskCanceledException() | ||
| { | ||
| var cts = new CancellationTokenSource(); | ||
| cts.Cancel(); | ||
| await Assert.ThrowsExceptionAsync<TaskCanceledException>(() => _sessionManager.SendSession(cts.Token)); | ||
| } | ||
|
|
||
|
|
||
| [TestMethod] | ||
| public void RetrieveCAchedSessionSession() | ||
luizfbicalho marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| _sessionManager.GetCachedSession(); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.