Skip to content

Commit

Permalink
Query collection to remove. Fix #62 (#66)
Browse files Browse the repository at this point in the history
* Query collection to remove. Fix #62

* Lock Azure Storage creation

* Use Empty try...catch
  • Loading branch information
JimBobSquarePants authored Aug 4, 2019
1 parent 75fd113 commit 5d381f0
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0.

using System;
using System.Linq;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
Expand Down Expand Up @@ -164,7 +165,16 @@ public static IImageSharpBuilder AddProvider<TProvider>(this IImageSharpBuilder
public static IImageSharpBuilder RemoveProvider<TProvider>(this IImageSharpBuilder builder)
where TProvider : class, IImageProvider
{
builder.Services.Remove(ServiceDescriptor.Singleton<IImageProvider, TProvider>());
ServiceDescriptor descriptor = builder.Services.FirstOrDefault(x =>
x.ServiceType == typeof(IImageProvider)
&& x.Lifetime == ServiceLifetime.Singleton
&& x.ImplementationType == typeof(TProvider));

if (descriptor != null)
{
builder.Services.Remove(descriptor);
}

return builder;
}

Expand Down Expand Up @@ -204,7 +214,16 @@ public static IImageSharpBuilder AddProcessor<TProcessor>(this IImageSharpBuilde
public static IImageSharpBuilder RemoveProcessor<TProcessor>(this IImageSharpBuilder builder)
where TProcessor : class, IImageWebProcessor
{
builder.Services.Remove(ServiceDescriptor.Singleton<IImageWebProcessor, TProcessor>());
ServiceDescriptor descriptor = builder.Services.FirstOrDefault(x =>
x.ServiceType == typeof(IImageWebProcessor)
&& x.Lifetime == ServiceLifetime.Singleton
&& x.ImplementationType == typeof(TProcessor));

if (descriptor != null)
{
builder.Services.Remove(descriptor);
}

return builder;
}

Expand Down
27 changes: 16 additions & 11 deletions tests/ImageSharp.Web.Tests/Actions/ActionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ void OnParseCommands(ImageCommandContext context)
complete = true;
}

TestServer server = ImageSharpTestServer.CreateWithActions(OnParseCommands);
using (TestServer server = ImageSharpTestServer.CreateWithActions(OnParseCommands))
{
await server.CreateClient().GetAsync(url + "?width=20").ConfigureAwait(false);
}

await server.CreateClient().GetAsync(url + "?width=20").ConfigureAwait(false);
Assert.True(complete);
}

Expand All @@ -69,9 +71,10 @@ void OnBeforeSave(FormattedImage image)
complete = true;
}

TestServer server = ImageSharpTestServer.CreateWithActions(null, OnBeforeSave);

await server.CreateClient().GetAsync(ImageSharpTestServer.PhysicalTestImage + "?width=20").ConfigureAwait(false);
using (TestServer server = ImageSharpTestServer.CreateWithActions(null, OnBeforeSave))
{
await server.CreateClient().GetAsync(ImageSharpTestServer.PhysicalTestImage + "?width=20").ConfigureAwait(false);
}
Assert.True(complete);
}

Expand All @@ -88,9 +91,10 @@ void OnProcessed(ImageProcessingContext context)
complete = true;
}

TestServer server = ImageSharpTestServer.CreateWithActions(null, null, OnProcessed);

await server.CreateClient().GetAsync(ImageSharpTestServer.PhysicalTestImage + "?width=20").ConfigureAwait(false);
using (TestServer server = ImageSharpTestServer.CreateWithActions(null, null, OnProcessed))
{
await server.CreateClient().GetAsync(ImageSharpTestServer.PhysicalTestImage + "?width=20").ConfigureAwait(false);
}
Assert.True(complete);
}

Expand All @@ -105,9 +109,10 @@ void OnPrepareResponse(HttpContext context)
complete = true;
}

TestServer server = ImageSharpTestServer.CreateWithActions(null, null, null, OnPrepareResponse);

await server.CreateClient().GetAsync(ImageSharpTestServer.PhysicalTestImage + "?width=20").ConfigureAwait(false);
using (TestServer server = ImageSharpTestServer.CreateWithActions(null, null, null, OnPrepareResponse))
{
await server.CreateClient().GetAsync(ImageSharpTestServer.PhysicalTestImage + "?width=20").ConfigureAwait(false);
}
Assert.True(complete);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Microsoft.AspNetCore.Http;
using SixLabors.ImageSharp.Web.Providers;
using SixLabors.ImageSharp.Web.Resolvers;
using System;
using System.Threading.Tasks;

namespace SixLabors.ImageSharp.Web.Tests.DependencyInjection
{
public class MockImageProvider : IImageProvider
{
public Func<HttpContext, bool> Match { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }

public Task<IImageResolver> GetAsync(HttpContext context) => throw new NotImplementedException();

public bool IsValidRequest(HttpContext context) => throw new NotImplementedException();
}
}
14 changes: 14 additions & 0 deletions tests/ImageSharp.Web.Tests/DependencyInjection/MockWebProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Microsoft.Extensions.Logging;
using SixLabors.ImageSharp.Web.Processors;
using System;
using System.Collections.Generic;

namespace SixLabors.ImageSharp.Web.Tests.DependencyInjection
{
public class MockWebProcessor : IImageWebProcessor
{
public IEnumerable<string> Commands => throw new NotImplementedException();

public FormattedImage Process(FormattedImage image, ILogger logger, IDictionary<string, string> commands) => throw new NotImplementedException();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using SixLabors.ImageSharp.Web.DependencyInjection;
using System;
using Xunit;

namespace SixLabors.ImageSharp.Web.Tests.DependencyInjection
{
public class ServiceRegistrationExtensionsTests
{
[Fact]
public void CanAddRemoveImageProviders()
{
void RemoveServices(IServiceCollection services)
{
var builder = services.AddImageSharp()
.AddProvider<MockImageProvider>();

Assert.Contains(services, x => x.ImplementationType == typeof(MockImageProvider));

builder.RemoveProvider<MockImageProvider>();

Assert.DoesNotContain(services, x => x.ImplementationType == typeof(MockImageProvider));
}

using (TestServer server = ImageSharpTestServer.Create(ImageSharpTestServer.DefaultConfig, RemoveServices))
{
}
}

[Fact]
public void CanAddRemoveImageProcessors()
{
void RemoveServices(IServiceCollection services)
{
var builder = services.AddImageSharp()
.AddProcessor<MockWebProcessor>();

Assert.Contains(services, x => x.ImplementationType == typeof(MockWebProcessor));

builder.RemoveProcessor<MockWebProcessor>();

Assert.DoesNotContain(services, x => x.ImplementationType == typeof(MockWebProcessor));
}

using (TestServer server = ImageSharpTestServer.Create(ImageSharpTestServer.DefaultConfig, RemoveServices))
{
}
}
}
}
56 changes: 40 additions & 16 deletions tests/ImageSharp.Web.Tests/ImageSharpTestServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,26 +122,50 @@ void DefaultConfigureServices(IServiceCollection services)

private static void InitializeAzureStorage(TestServer server)
{
// Upload an image to the Azure Test Storage;
var storageAccount = CloudStorageAccount.Parse(AzureConnectionString);
CloudBlobClient client = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = client.GetContainerReference(AzureContainerName);

if (!container.Exists())
try
{
container.Create();
container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });
}
// Upload an image to the Azure Test Storage;
var storageAccount = CloudStorageAccount.Parse(AzureConnectionString);
CloudBlobClient client = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = client.GetContainerReference(AzureContainerName);

IHostingEnvironment environment = server.Host.Services.GetRequiredService<IHostingEnvironment>();
CloudBlockBlob blob = container.GetBlockBlobReference(ImagePath);
if (!blob.Exists())
{
IFileInfo file = environment.WebRootFileProvider.GetFileInfo(ImagePath);
using (System.IO.Stream stream = file.CreateReadStream())
if (!container.Exists())
{
blob.UploadFromStream(stream);
container.Create();
container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });
}

IHostingEnvironment environment = server.Host.Services.GetRequiredService<IHostingEnvironment>();
CloudBlockBlob blob = container.GetBlockBlobReference(ImagePath);
if (!blob.Exists())
{
IFileInfo file = environment.WebRootFileProvider.GetFileInfo(ImagePath);
using (System.IO.Stream stream = file.CreateReadStream())
{
blob.UploadFromStream(stream);
}
}
}
catch (StorageException)
{
// On Appveyor "Exists" appears to fail and the following exception is thrown.
// I cannot replicate this locally via Visual Studio Test Explorer or "dotnet test".
//
// Failed CanAddRemoveImageProcessors
// Error Message:
// Microsoft.Azure.Storage.StorageException : The specified container already exists.
// Stack Trace:
// at Microsoft.Azure.Storage.Core.Executor.Executor.ExecuteAsync[T](RESTCommand`1 cmd, IRetryPolicy policy, OperationContext operationContext, CancellationToken token)
// at Microsoft.Azure.Storage.Core.Executor.Executor.<> c__DisplayClass0_0`1.< ExecuteSync > b__0()
// at Microsoft.Azure.Storage.Core.Util.CommonUtility.RunWithoutSynchronizationContext[T](Func`1 actionToRun)
// at Microsoft.Azure.Storage.Core.Executor.Executor.ExecuteSync[T](RESTCommand`1 cmd, IRetryPolicy policy, OperationContext operationContext)
// at Microsoft.Azure.Storage.Blob.CloudBlobContainer.Create(BlobContainerPublicAccessType accessType, BlobRequestOptions requestOptions, OperationContext operationContext)
// at Microsoft.Azure.Storage.Blob.CloudBlobContainer.Create(BlobRequestOptions requestOptions, OperationContext operationContext)
// at SixLabors.ImageSharp.Web.Tests.ImageSharpTestServer.InitializeAzureStorage(TestServer server) in C:\projects\imagesharp - web\tests\ImageSharp.Web.Tests\ImageSharpTestServer.cs:line 143
// at SixLabors.ImageSharp.Web.Tests.ImageSharpTestServer.Create(Action`1 configureApp, Action`1 configureServices)
// in C:\projects\imagesharp - web\tests\ImageSharp.Web.Tests\ImageSharpTestServer.cs:line 123
// at SixLabors.ImageSharp.Web.Tests.DependencyInjection.ServiceRegistrationExtensionsTests.CanAddRemoveImageProcessors()
// in C:\projects\imagesharp - web\tests\ImageSharp.Web.Tests\DependencyInjection\ServiceRegistrationExtensionsTests.cs:line 46
}
}

Expand Down

0 comments on commit 5d381f0

Please sign in to comment.