Skip to content
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

Fix Truncated ArchiveCacheMethod #30

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion Gw2Sharp.Tests/WebApi/Caching/ArchiveCacheMethodTests.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -21,7 +23,7 @@ public ArchiveCacheMethodTests()


[Fact]
public async Task StoresCacheIntoArchiveTest()
public async Task StoresRawCacheIntoArchiveTest()
{
string category = "testdata";
string id = "bytearray.dat";
Expand All @@ -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<IEnumerable<string>>(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()
{
Expand Down
2 changes: 1 addition & 1 deletion Gw2Sharp/WebApi/Caching/ArchiveCacheMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down