Skip to content

Commit 78817ae

Browse files
authored
Merge pull request #72 from Archomeda/fix/archive-caching
Fix archive caching
2 parents 0bf97c3 + cb570ed commit 78817ae

File tree

3 files changed

+199
-101
lines changed

3 files changed

+199
-101
lines changed

Gw2Sharp.Tests/WebApi/Caching/ArchiveCacheMethodTests.cs

+43-14
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.IO;
43
using System.IO.Compression;
4+
using System.Net;
55
using System.Text;
66
using System.Threading.Tasks;
7+
using FluentAssertions;
8+
using FluentAssertions.Extensions;
79
using Gw2Sharp.Tests.Helpers;
810
using Gw2Sharp.WebApi.Caching;
11+
using Gw2Sharp.WebApi.Http;
912
using Xunit;
1013

1114
#pragma warning disable S3881 // "IDisposable" should be implemented correctly
@@ -27,10 +30,10 @@ public async Task StoresRawCacheIntoArchiveTest()
2730
{
2831
string category = "testdata";
2932
string id = "bytearray.dat";
30-
var expiryTime = DateTimeOffset.UtcNow.AddMinutes(1);
33+
var expiresAt = 1.Minutes().After(DateTime.Now);
3134

3235
byte[] data = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
33-
await this.cacheMethod.SetAsync(category, id, data, expiryTime);
36+
await this.cacheMethod.SetAsync(category, id, data, expiresAt);
3437
var actualCache = await this.cacheMethod.TryGetAsync<byte[]>(category, id);
3538

3639
Assert.Equal(data, actualCache?.Item);
@@ -47,17 +50,18 @@ public async Task StoresRawCacheIntoArchiveTest()
4750
}
4851

4952
[Fact]
50-
public async Task StoresArrayCacheIntoArchiveTest()
53+
public async Task StoresStringCacheIntoArchiveTest()
5154
{
5255
string category = "testdata";
53-
string id = "stringarray.dat";
54-
var expiryTime = DateTimeOffset.UtcNow.AddMinutes(1);
56+
string id = "string.dat";
57+
var expiresAt = 1.Minutes().After(DateTime.Now);
5558

56-
string[] data = new[] { "Hello", "World!" };
57-
await this.cacheMethod.SetAsync(category, id, data, expiryTime);
58-
var actualCache = await this.cacheMethod.TryGetAsync<IEnumerable<string>>(category, id);
59+
var cache = new WebApiResponse("Hello world", HttpStatusCode.OK, null);
60+
await this.cacheMethod.SetAsync(category, id, cache, expiresAt);
61+
var actualCache = await this.cacheMethod.TryGetAsync<IWebApiResponse>(category, id);
5962

60-
Assert.Equal(data, actualCache?.Item);
63+
actualCache.Should().NotBeNull();
64+
actualCache.Item.Should().BeEquivalentTo(cache);
6165
this.cacheMethod.Dispose();
6266

6367
using var stream = File.OpenRead(ARCHIVE_FILENAME);
@@ -67,7 +71,7 @@ public async Task StoresArrayCacheIntoArchiveTest()
6771
using var memoryStream = new MemoryStream();
6872
await entryStream.CopyToAsync(memoryStream);
6973

70-
string expected = $"[\"{data[0]}\",\"{data[1]}\"]";
74+
string expected = "{\"content\":\"Hello world\",\"responseHeaders\":{},\"statusCode\":200}";
7175
string actual = Encoding.UTF8.GetString(memoryStream.ToArray());
7276
Assert.Equal(expected, actual);
7377
}
@@ -77,10 +81,10 @@ public async Task LoadExistingArchiveTest()
7781
{
7882
string category = "testdata";
7983
string id = "bytearray.dat";
80-
var expiryTime = DateTimeOffset.UtcNow.AddMinutes(1);
84+
var expiresAt = 1.Minutes().After(DateTime.Now);
8185

8286
byte[] data = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
83-
await this.cacheMethod.SetAsync(category, id, data, expiryTime);
87+
await this.cacheMethod.SetAsync(category, id, data, expiresAt);
8488
var actualCache = await this.cacheMethod.TryGetAsync<byte[]>(category, id);
8589

8690
Assert.Equal(data, actualCache?.Item);
@@ -89,10 +93,35 @@ public async Task LoadExistingArchiveTest()
8993
this.cacheMethod = new ArchiveCacheMethod(ARCHIVE_FILENAME);
9094
actualCache = await this.cacheMethod.TryGetAsync<byte[]>(category, id);
9195
Assert.Equal(data, actualCache?.Item);
92-
Assert.Equal(expiryTime, actualCache?.ExpiryTime);
96+
Assert.Equal(expiresAt, actualCache?.ExpiryTime);
9397
}
9498

99+
[Fact]
100+
public async Task GetsUnsupportedTypeFromArchiveTest()
101+
{
102+
const string CATEGORY = "category";
103+
const string ID = "id";
104+
var expiresAt = 1.Minutes().After(DateTime.Now);
105+
106+
// We need to store something valid first
107+
byte[] data = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
108+
await this.cacheMethod.SetAsync(CATEGORY, ID, data, expiresAt);
95109

110+
await this.cacheMethod.Invoking(x => x.TryGetAsync<object>(CATEGORY, ID))
111+
.Should().ThrowAsync<NotSupportedException>();
112+
}
113+
114+
[Fact]
115+
public async Task StoresUnsupportedTypeIntoArchiveTest()
116+
{
117+
const string CATEGORY = "category";
118+
const string ID = "id";
119+
var expiresAt = 1.Minutes().After(DateTime.Now);
120+
121+
object data = new object();
122+
await this.cacheMethod.Invoking(x => x.SetAsync(CATEGORY, ID, data, expiresAt))
123+
.Should().ThrowAsync<NotSupportedException>();
124+
}
96125

97126
public void Dispose()
98127
{

0 commit comments

Comments
 (0)