From bcc95a436da6aea7bd47e61ffcbedf070f35f2d2 Mon Sep 17 00:00:00 2001 From: Dade Lamkins Date: Thu, 23 Jan 2020 22:16:15 -0500 Subject: [PATCH 1/2] Changed `FileMode.Truncate` to `FileMode.OpenOrCreate` in the ArchiveCacheMethod.FlushAsync method. --- Gw2Sharp/WebApi/Caching/ArchiveCacheMethod.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gw2Sharp/WebApi/Caching/ArchiveCacheMethod.cs b/Gw2Sharp/WebApi/Caching/ArchiveCacheMethod.cs index a63c0f36a..6f4035484 100644 --- a/Gw2Sharp/WebApi/Caching/ArchiveCacheMethod.cs +++ b/Gw2Sharp/WebApi/Caching/ArchiveCacheMethod.cs @@ -219,7 +219,7 @@ public override async Task FlushAsync() this.archive.Dispose(); this.archiveStream.Close(); this.archiveStream.Dispose(); - this.archiveStream = File.Open(fileName, FileMode.Truncate, FileAccess.ReadWrite, FileShare.None); + this.archiveStream = File.Open(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None); this.archive = new ZipArchive(this.archiveStream, ZipArchiveMode.Update); } } From 2cb7136ff6a19490730caa2862e1e55b9131a179 Mon Sep 17 00:00:00 2001 From: Archomeda Date: Sat, 25 Jan 2020 13:17:55 +0100 Subject: [PATCH 2/2] Add additional test to check ArchiveCache for object storage --- .../WebApi/Caching/ArchiveCacheMethodTests.cs | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/Gw2Sharp.Tests/WebApi/Caching/ArchiveCacheMethodTests.cs b/Gw2Sharp.Tests/WebApi/Caching/ArchiveCacheMethodTests.cs index fb8499027..1cc543572 100644 --- a/Gw2Sharp.Tests/WebApi/Caching/ArchiveCacheMethodTests.cs +++ b/Gw2Sharp.Tests/WebApi/Caching/ArchiveCacheMethodTests.cs @@ -1,6 +1,8 @@ using System; +using System.Collections.Generic; using System.IO; using System.IO.Compression; +using System.Text; using System.Threading.Tasks; using Gw2Sharp.Tests.Helpers; using Gw2Sharp.WebApi.Caching; @@ -21,7 +23,7 @@ public ArchiveCacheMethodTests() [Fact] - public async Task StoresCacheIntoArchiveTest() + public async Task StoresRawCacheIntoArchiveTest() { string category = "testdata"; string id = "bytearray.dat"; @@ -44,6 +46,32 @@ public async Task StoresCacheIntoArchiveTest() Assert.Equal(data, actual); } + [Fact] + public async Task StoresArrayCacheIntoArchiveTest() + { + string category = "testdata"; + string id = "stringarray.dat"; + var expiryTime = DateTimeOffset.UtcNow.AddMinutes(1); + + string[] data = new[] { "Hello", "World!" }; + await this.cacheMethod.SetAsync(category, id, data, expiryTime); + var actualCache = await this.cacheMethod.TryGetAsync>(category, id); + + Assert.Equal(data, actualCache?.Item); + this.cacheMethod.Dispose(); + + using var stream = File.OpenRead(ARCHIVE_FILENAME); + var archive = new ZipArchive(stream); + var entry = archive.GetEntry($"{category}/{id}"); + using var entryStream = entry.Open(); + using var memoryStream = new MemoryStream(); + await entryStream.CopyToAsync(memoryStream); + + string expected = $"[\"{data[0]}\",\"{data[1]}\"]"; + string actual = Encoding.UTF8.GetString(memoryStream.ToArray()); + Assert.Equal(expected, actual); + } + [Fact] public async Task LoadExistingArchiveTest() {