-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #683 from qdraw/feature/202203_package_tel_to_bg_s…
…ervice Add package Telemetry to Background Service
- Loading branch information
Showing
6 changed files
with
161 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
starsky/starsky.foundation.webtelemetry/Services/PackageTelemetryBackgroundService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using starsky.foundation.http.Interfaces; | ||
using starsky.foundation.injection; | ||
using starsky.foundation.platform.Models; | ||
using starsky.foundation.webtelemetry.Helpers; | ||
|
||
namespace starsky.foundation.webtelemetry.Services | ||
{ | ||
|
||
[Service(typeof(IHostedService), InjectionLifetime = InjectionLifetime.Singleton)] | ||
public class PackageTelemetryBackgroundService : BackgroundService | ||
{ | ||
private readonly IServiceScopeFactory _serviceScopeFactory; | ||
|
||
public PackageTelemetryBackgroundService(IServiceScopeFactory serviceScopeFactory) | ||
{ | ||
_serviceScopeFactory = serviceScopeFactory; | ||
} | ||
|
||
/// <summary> | ||
/// Running scoped services | ||
/// @see: https://thinkrethink.net/2018/07/12/injecting-a-scoped-service-into-ihostedservice/ | ||
/// </summary> | ||
/// <param name="stoppingToken">Cancellation Token, but it ignored</param> | ||
/// <returns>CompletedTask</returns> | ||
protected override async Task ExecuteAsync(CancellationToken stoppingToken) | ||
{ | ||
using (var scope = _serviceScopeFactory.CreateScope()) | ||
{ | ||
var appSettings = scope.ServiceProvider.GetRequiredService<AppSettings>(); | ||
var httpClientHelper = scope.ServiceProvider.GetRequiredService<IHttpClientHelper>(); | ||
if ( appSettings.ApplicationType == AppSettings.StarskyAppType.WebController ) | ||
{ | ||
await new PackageTelemetry(httpClientHelper, appSettings).PackageTelemetrySend(); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
...starskytest/starsky.foundation.platform/Services/PackageTelemetryBackgroundServiceTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using starsky.foundation.http.Interfaces; | ||
using starsky.foundation.http.Services; | ||
using starsky.foundation.platform.Interfaces; | ||
using starsky.foundation.platform.Models; | ||
using starsky.foundation.storage.Interfaces; | ||
using starsky.foundation.webtelemetry.Helpers; | ||
using starsky.foundation.webtelemetry.Services; | ||
using starskytest.FakeMocks; | ||
|
||
namespace starskytest.starsky.foundation.platform.Services; | ||
|
||
public static class ExtensionMethods | ||
{ | ||
public static async Task InvokeAsync(this MethodInfo @this, object obj, params object[] parameters) | ||
{ | ||
dynamic awaitable = @this.Invoke(obj, parameters); | ||
await awaitable; | ||
awaitable.GetAwaiter(); | ||
} | ||
} | ||
|
||
[TestClass] | ||
public class PackageTelemetryBackgroundServiceTest | ||
{ | ||
private readonly IServiceScopeFactory _serviceScopeFactory; | ||
|
||
public PackageTelemetryBackgroundServiceTest() | ||
{ | ||
var services = new ServiceCollection(); | ||
services.AddSingleton<AppSettings>(); | ||
services.AddSingleton<IHttpClientHelper, HttpClientHelper>(); | ||
services.AddSingleton<IHttpProvider, FakeIHttpProvider>(); | ||
services.AddSingleton<IWebLogger, FakeIWebLogger>(); | ||
services.AddSingleton<ISelectorStorage, FakeSelectorStorage>(); | ||
|
||
var serviceProvider = services.BuildServiceProvider(); | ||
_serviceScopeFactory = serviceProvider.GetRequiredService<IServiceScopeFactory>(); | ||
|
||
var appSettings = serviceProvider.GetRequiredService<AppSettings>(); | ||
appSettings.EnablePackageTelemetry = true; | ||
} | ||
|
||
|
||
[TestMethod] | ||
[Timeout(3000)] | ||
public async Task ExecuteAsyncTest_WebController() | ||
{ | ||
var appSettings = _serviceScopeFactory.CreateScope().ServiceProvider | ||
.GetService<AppSettings>(); | ||
appSettings!.ApplicationType = AppSettings.StarskyAppType.WebController; | ||
|
||
var service = new PackageTelemetryBackgroundService(_serviceScopeFactory); | ||
|
||
CancellationTokenSource source = new CancellationTokenSource(); | ||
CancellationToken token = source.Token; | ||
source.Cancel(); // <- cancel before start | ||
|
||
MethodInfo dynMethod = service.GetType().GetMethod("ExecuteAsync", | ||
BindingFlags.NonPublic | BindingFlags.Instance); | ||
if ( dynMethod == null ) | ||
throw new Exception("missing ExecuteAsync"); | ||
await dynMethod.InvokeAsync(service, new object[] | ||
{ | ||
token | ||
}); | ||
|
||
var httpProvider = _serviceScopeFactory.CreateScope().ServiceProvider | ||
.GetService<IHttpProvider>(); | ||
|
||
var fakeHttpProvider = httpProvider as FakeIHttpProvider; | ||
Assert.IsTrue(fakeHttpProvider?.UrlCalled.Any(p => p.Contains(PackageTelemetry.PackageTelemetryUrl))); | ||
} | ||
|
||
[TestMethod] | ||
[Timeout(2000)] | ||
public void ExecuteAsyncTest_NotWhenDisabled() | ||
{ | ||
var appSettings = _serviceScopeFactory.CreateScope().ServiceProvider | ||
.GetService<AppSettings>(); | ||
appSettings.ApplicationType = AppSettings.StarskyAppType.Admin; | ||
var httpProvider1 = _serviceScopeFactory.CreateScope().ServiceProvider | ||
.GetService<IHttpProvider>(); | ||
|
||
var fakeHttpProvider1 = httpProvider1 as FakeIHttpProvider; | ||
fakeHttpProvider1.UrlCalled = new List<string>(); | ||
|
||
var service = new PackageTelemetryBackgroundService(_serviceScopeFactory); | ||
|
||
CancellationTokenSource source = new CancellationTokenSource(); | ||
CancellationToken token = source.Token; | ||
source.Cancel(); // <- cancel before start | ||
|
||
MethodInfo dynMethod = service.GetType().GetMethod("ExecuteAsync", | ||
BindingFlags.NonPublic | BindingFlags.Instance); | ||
if ( dynMethod == null ) | ||
throw new Exception("missing ExecuteAsync"); | ||
dynMethod.Invoke(service, new object[] | ||
{ | ||
token | ||
}); | ||
|
||
var httpProvider = _serviceScopeFactory.CreateScope().ServiceProvider | ||
.GetService<IHttpProvider>(); | ||
|
||
var fakeHttpProvider = httpProvider as FakeIHttpProvider; | ||
Assert.IsFalse(fakeHttpProvider?.UrlCalled.Contains(PackageTelemetry.PackageTelemetryUrl)); | ||
} | ||
} |