Skip to content

Commit

Permalink
Merge pull request #46 from SixLabors/js/fix-cache
Browse files Browse the repository at this point in the history
Js/fix cache
  • Loading branch information
JimBobSquarePants authored Nov 26, 2018
2 parents a109f51 + 3ebaa92 commit dc8659d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/ImageSharp.Web/Caching/CacheHash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public string Create(string value, uint length)
using (var hashAlgorithm = SHA256.Create())
using (IManagedByteBuffer buffer = this.memoryAllocator.AllocateManagedByteBuffer(byteCount))
{
Encoding.ASCII.GetBytes(value, 0, byteCount, buffer.Array, 0);
byte[] hash = hashAlgorithm.ComputeHash(buffer.Array, 0, byteCount);
string ext = this.formatHelper.GetExtensionOrDefault(value);
return $"{HexEncoder.Encode(new Span<byte>(hash).Slice(0, len / 2))}.{ext}";
Expand Down
13 changes: 6 additions & 7 deletions tests/ImageSharp.Web.Tests/Caching/AsyncKeyLockTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using SixLabors.ImageSharp.Web.Caching;
using Xunit;
Expand Down Expand Up @@ -27,15 +26,15 @@ private void AsyncLockCanLockByKey()
bool zeroEntered = false;
bool entered = false;
int index = 0;
Task[] tasks = Enumerable.Range(0, 3).Select(i => Task.Run(async () =>
Task[] tasks = Enumerable.Range(0, 5).Select(i => Task.Run(async () =>
{
using (await AsyncLock.WriterLockAsync(AsyncKey).ConfigureAwait(false))
{
if (i == 0)
{
entered = true;
zeroEntered = true;
Thread.Sleep(3000);
await Task.Delay(3000).ConfigureAwait(false);
entered = false;
}
else if (zeroEntered)
Expand All @@ -48,23 +47,23 @@ private void AsyncLockCanLockByKey()
})).ToArray();

Task.WaitAll(tasks);
Assert.Equal(3, index);
Assert.Equal(5, index);
}

private void AsyncLockAllowsDifferentKeysToRun()
{
bool zeroEntered = false;
bool entered = false;
int index = 0;
Task[] tasks = Enumerable.Range(0, 3).Select(i => Task.Run(async () =>
Task[] tasks = Enumerable.Range(0, 5).Select(i => Task.Run(async () =>
{
using (await AsyncLock.WriterLockAsync(i > 0 ? AsyncKey2 : AsyncKey1).ConfigureAwait(false))
{
if (i == 0)
{
entered = true;
zeroEntered = true;
Thread.Sleep(2000);
await Task.Delay(2000).ConfigureAwait(false);
entered = false;
}
else if (zeroEntered)
Expand All @@ -78,7 +77,7 @@ private void AsyncLockAllowsDifferentKeysToRun()
})).ToArray();

Task.WaitAll(tasks);
Assert.Equal(3, index);
Assert.Equal(5, index);
}
}
}
11 changes: 11 additions & 0 deletions tests/ImageSharp.Web.Tests/Caching/CacheHashTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ public void CachHashProducesIdenticalResults()
Assert.Equal(expected, actual);
}

[Fact]
public void CachHashProducesDifferentResults()
{
const string input = "http://testwebsite.com/image-12345.jpeg?width=400";
const string input2 = "http://testwebsite.com/image-23456.jpeg?width=400";
string expected = cacheHash.Create(input, 8);
string actual = cacheHash.Create(input2, 8);

Assert.NotEqual(expected, actual);
}

[Fact]
public void CachHashLengthIsIdentical()
{
Expand Down

0 comments on commit dc8659d

Please sign in to comment.