Skip to content

Commit

Permalink
Allow for non-static usage of ServiceProvider
Browse files Browse the repository at this point in the history
Allow static BinanceHttpClient singleton initializer to be reset and add new tests for @tash649 use case.
  • Loading branch information
sonvister committed Sep 28, 2018
1 parent 2390193 commit 88b3268
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/Binance/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ public static IServiceCollection AddBinance(this IServiceCollection services, bo
services.AddSingleton<ITimestampProvider, TimestampProvider>();
services.AddSingleton<IBinanceHttpClient>(s =>
{
if (!BinanceHttpClient.Initializer.IsValueCreated)
{
// Replace initializer.
BinanceHttpClient.Initializer = new Lazy<BinanceHttpClient>(() =>
new BinanceHttpClient(
s.GetService<ITimestampProvider>(),
s.GetService<IApiRateLimiter>(),
s.GetService<IOptions<BinanceApiOptions>>(),
s.GetService<ILogger<BinanceHttpClient>>()), true);
}
if (BinanceHttpClient.Initializer.IsValueCreated)
BinanceHttpClient.Initializer.Value.Dispose();

// Replace initializer.
BinanceHttpClient.Initializer = new Lazy<BinanceHttpClient>(() =>
new BinanceHttpClient(
s.GetService<ITimestampProvider>(),
s.GetService<IApiRateLimiter>(),
s.GetService<IOptions<BinanceApiOptions>>(),
s.GetService<ILogger<BinanceHttpClient>>()), true);

return BinanceHttpClient.Instance;
});
Expand Down
1 change: 1 addition & 0 deletions test/Binance.Tests/Api/BinanceApiTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Binance.Tests.Api
{
[Collection("Binance HTTP Client Tests")]
public class BinanceApiTest
{
private readonly IBinanceApi _api;
Expand Down
1 change: 1 addition & 0 deletions test/Binance.Tests/Api/BinanceHttpClientExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace Binance.Tests.Api
{
[Collection("Binance HTTP Client Tests")]
public class BinanceHttpClientExtensionsTest
{
private readonly IBinanceHttpClient _client;
Expand Down
45 changes: 45 additions & 0 deletions test/Binance.Tests/DependencyInjectionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Threading.Tasks;
using Xunit;

namespace Binance.Tests
{
[Collection("Binance HTTP Client Tests")]
public class DependencyInjectionTests
{
[Fact]
public Task ServiceProviderDispose()
{
var serviceProvider = new ServiceCollection()
.AddBinance().BuildServiceProvider();

var httpClient = serviceProvider.GetService<IBinanceHttpClient>();

serviceProvider.Dispose();

// Verify static BinanceHttpClient singleton is disposed.
return Assert.ThrowsAsync<ObjectDisposedException>(() => httpClient.GetAsync("test"));
}

[Fact]
public void NonStaticServiceProviderUse()
{
var serviceProvider = new ServiceCollection()
.AddBinance().BuildServiceProvider();

var httpClient = serviceProvider.GetService<IBinanceHttpClient>();

serviceProvider.Dispose();


serviceProvider = new ServiceCollection()
.AddBinance().BuildServiceProvider();

httpClient = serviceProvider.GetService<IBinanceHttpClient>();

// Verify the RateLimiter is not disposed (no exception is thrown).
httpClient.RateLimiter.Configure(TimeSpan.FromSeconds(1), 1);
}
}
}

0 comments on commit 88b3268

Please sign in to comment.