22
33namespace Drogecode . Blazor . ExpireStorage . Tests . Tests . Services ;
44
5- public class ExpireStorageServiceTests
5+ public class ExpireStorageServiceTests : IDisposable
66{
77 private readonly IExpireStorageService _expireStorageService ;
88
99 public ExpireStorageServiceTests ( IExpireStorageService expireStorageService )
1010 {
1111 _expireStorageService = expireStorageService ;
12+ ExpireStorageService . LogToConsole = true ;
13+ }
14+
15+ public void Dispose ( )
16+ {
17+ ExpireStorageService . Postfix = null ;
18+ ExpireStorageService . LogToConsole = false ;
19+ // We can't easily reset IsOffline because it has a private setter and no public reset method,
20+ // but we can trigger it to be false by a successful request.
1221 }
1322
1423 [ Fact ]
1524 public async Task MinimalRequestTest ( )
1625 {
1726 const string cacheKey = "MinimalRequestTest" ;
18- var response = await _expireStorageService . CachedRequestAsync < string > ( cacheKey , ( ) => Task . FromResult ( "test" ) , clt : TestContext . Current . CancellationToken ) ;
27+ var response = await _expireStorageService . CachedRequestAsync < string > ( cacheKey , ( ) => Task . FromResult ( "test" ) , new CachedRequest { ExpireLocalStorage = DateTime . UtcNow . AddDays ( 7 ) } , clt : TestContext . Current . CancellationToken ) ;
1928 Assert . NotNull ( response ) ;
2029 response . Should ( ) . Be ( "test" ) ;
2130 }
@@ -27,7 +36,7 @@ public async Task ByFunctionTest()
2736 var response = await _expireStorageService . CachedRequestAsync < TestStringResponse > ( cacheKey , ( ) => Task . FromResult ( new TestStringResponse
2837 {
2938 Data = "test"
30- } ) , clt : TestContext . Current . CancellationToken ) ;
39+ } ) , new CachedRequest { ExpireLocalStorage = DateTime . UtcNow . AddDays ( 7 ) } , clt : TestContext . Current . CancellationToken ) ;
3140 Assert . NotNull ( response ? . Data ) ;
3241 response . HandledBy . Should ( ) . Be ( HandledBy . Function ) ;
3342 response . Data . Should ( ) . Be ( "test" ) ;
@@ -41,17 +50,80 @@ public async Task FromCacheTest()
4150 {
4251 Data = "test"
4352 } ) ,
53+ new CachedRequest { ExpireLocalStorage = DateTime . UtcNow . AddDays ( 7 ) } ,
4454 clt : TestContext . Current . CancellationToken ) ;
4555 Assert . NotNull ( addToCache ? . Data ) ;
4656 addToCache . HandledBy . Should ( ) . Be ( HandledBy . Function ) ;
4757 var response = await _expireStorageService . CachedRequestAsync < TestStringResponse > ( cacheKey , ( ) => Task . FromResult ( new TestStringResponse
4858 {
4959 Data = "not called"
5060 } ) ,
51- new CachedRequest { OneCallPerCache = true } ,
61+ new CachedRequest { OneCallPerCache = true , ExpireLocalStorage = DateTime . UtcNow . AddDays ( 7 ) } ,
5262 clt : TestContext . Current . CancellationToken ) ;
5363 Assert . NotNull ( response ? . Data ) ;
5464 response . HandledBy . Should ( ) . Be ( HandledBy . Cache ) ;
5565 response . Data . Should ( ) . Be ( "test" ) ;
5666 }
67+
68+ [ Fact ]
69+ public async Task PostfixTest ( )
70+ {
71+ const string cacheKey = "PostfixTest" ;
72+ ExpireStorageService . Postfix = "user1" ;
73+ await _expireStorageService . CachedRequestAsync < TestStringResponse > ( cacheKey , ( ) => Task . FromResult ( new TestStringResponse { Data = "data1" } ) , new CachedRequest { ExpireLocalStorage = DateTime . UtcNow . AddDays ( 7 ) } , clt : TestContext . Current . CancellationToken ) ;
74+
75+ ExpireStorageService . Postfix = "user2" ;
76+ var response2 = await _expireStorageService . CachedRequestAsync < TestStringResponse > ( cacheKey , ( ) => Task . FromResult ( new TestStringResponse { Data = "data2" } ) , new CachedRequest { ExpireLocalStorage = DateTime . UtcNow . AddDays ( 7 ) } , clt : TestContext . Current . CancellationToken ) ;
77+
78+ Assert . NotNull ( response2 ? . Data ) ;
79+ response2 . Data . Should ( ) . Be ( "data2" ) ;
80+ response2 . HandledBy . Should ( ) . Be ( HandledBy . Function ) ;
81+
82+ ExpireStorageService . Postfix = "user1" ;
83+ var response1FromCache = await _expireStorageService . CachedRequestAsync < TestStringResponse > ( cacheKey , ( ) => Task . FromResult ( new TestStringResponse { Data = "not called" } ) , new CachedRequest { OneCallPerCache = true , ExpireLocalStorage = DateTime . UtcNow . AddDays ( 7 ) } , clt : TestContext . Current . CancellationToken ) ;
84+ Assert . NotNull ( response1FromCache ? . Data ) ;
85+ response1FromCache . Data . Should ( ) . Be ( "data1" ) ;
86+ response1FromCache . HandledBy . Should ( ) . Be ( HandledBy . Cache ) ;
87+ }
88+
89+ [ Fact ]
90+ public async Task IgnoreCacheTest ( )
91+ {
92+ const string cacheKey = "IgnoreCacheTest" ;
93+ await _expireStorageService . CachedRequestAsync < TestStringResponse > ( cacheKey , ( ) => Task . FromResult ( new TestStringResponse { Data = "cached" } ) , new CachedRequest { OneCallPerCache = true , ExpireLocalStorage = DateTime . UtcNow . AddDays ( 7 ) } , clt : TestContext . Current . CancellationToken ) ;
94+
95+ var response = await _expireStorageService . CachedRequestAsync < TestStringResponse > ( cacheKey , ( ) => Task . FromResult ( new TestStringResponse { Data = "fresh" } ) , new CachedRequest { IgnoreCache = true } , clt : TestContext . Current . CancellationToken ) ;
96+ Assert . NotNull ( response ? . Data ) ;
97+ response . Data . Should ( ) . Be ( "fresh" ) ;
98+ response . HandledBy . Should ( ) . Be ( HandledBy . Function ) ;
99+ }
100+
101+ [ Fact ]
102+ public async Task HttpRequestExceptionSetsOfflineTest ( )
103+ {
104+ const string cacheKey = "OfflineTest" ;
105+ try
106+ {
107+ await _expireStorageService . CachedRequestAsync < TestStringResponse > ( cacheKey , ( ) => throw new HttpRequestException ( ) , new CachedRequest { ExpireLocalStorage = DateTime . UtcNow . AddDays ( 7 ) } , clt : TestContext . Current . CancellationToken ) ;
108+ }
109+ catch ( HttpRequestException ) { }
110+
111+ ExpireStorageService . IsOffline . Should ( ) . BeTrue ( ) ;
112+
113+ // Recover
114+ await _expireStorageService . CachedRequestAsync < TestStringResponse > ( cacheKey , ( ) => Task . FromResult ( new TestStringResponse { Data = "recovered" } ) , new CachedRequest { ExpireLocalStorage = DateTime . UtcNow . AddDays ( 7 ) } , clt : TestContext . Current . CancellationToken ) ;
115+ ExpireStorageService . IsOffline . Should ( ) . BeFalse ( ) ;
116+ }
117+
118+ [ Fact ]
119+ public async Task CancellationTokenTest ( )
120+ {
121+ const string cacheKey = "CancellationTokenTest" ;
122+ using var cts = new CancellationTokenSource ( ) ;
123+ cts . Cancel ( ) ;
124+
125+ var response = await _expireStorageService . CachedRequestAsync < TestStringResponse > ( cacheKey , ( ) => Task . FromResult ( new TestStringResponse { Data = "test" } ) , clt : cts . Token ) ;
126+
127+ response . Should ( ) . BeNull ( ) ;
128+ }
57129}
0 commit comments