Skip to content

Commit

Permalink
ConfigureServices don't allow async; otherwise it runs into a race co…
Browse files Browse the repository at this point in the history
…ndition. (#2157)

Resolve dependency injection issue in WebHost ConfigureServices method; which should be implemented synchronously.
  • Loading branch information
philipktlin authored Dec 18, 2019
1 parent 0edce07 commit a003c3e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ namespace TestResultCoordinator.Controllers
public class TestOperationResultController : Controller
{
static readonly ILogger Logger = ModuleUtil.CreateLogger(nameof(TestOperationResultController));
// readonly ITestOperationResultStorage storage;

// TODO: uncomment this when dependency injection is fixed
// public TestOperationResultController(ITestOperationResultStorage storage)
// {
// this.storage = Preconditions.CheckNotNull(storage);
// }
readonly ITestOperationResultStorage storage;

public TestOperationResultController(ITestOperationResultStorage storage)
{
this.storage = Preconditions.CheckNotNull(storage);
}

// POST api/TestOperationResult
[HttpPost]
public async Task<StatusCodeResult> PostAsync(TestOperationResult result)
{
try
{
bool success = await TestOperationResultStorage.Instance.AddResultAsync(result);
bool success = await this.storage.AddResultAsync(result);
Logger.LogDebug($"Received test result: {result.Source}, {result.Type}, {success}");
return success ? this.StatusCode((int)HttpStatusCode.NoContent) : this.StatusCode((int)HttpStatusCode.BadRequest);
}
Expand Down
15 changes: 11 additions & 4 deletions test/modules/TestResultCoordinator/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ public Startup(IConfiguration configuration)
public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public async void ConfigureServices(IServiceCollection services)
public void ConfigureServices(IServiceCollection services)
{
Logger.LogInformation("Calling Startup.ConfigureServices");

services.AddMvc();

IStoreProvider storeProvider;
try
{
Expand All @@ -49,11 +52,15 @@ public async void ConfigureServices(IServiceCollection services)
storeProvider = new StoreProvider(new InMemoryDbStoreProvider());
}

services.AddSingleton<ITestOperationResultStorage>(await TestOperationResultStorage.Create(
storeProvider,
Settings.Current.ResultSources));
services.AddSingleton<ITestOperationResultStorage>(
TestOperationResultStorage.Create(
storeProvider,
Settings.Current.ResultSources).Result);

services.AddHostedService<TestResultReportingService>();
services.AddHostedService<TestResultEventReceivingService>();

Logger.LogInformation("Calling Startup.ConfigureServices Completed.");
}

// TODO: Remove warning disable for Obsolete when project is moved to dotnetcore 3.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ public class TestOperationResultStorage : ITestOperationResultStorage
static readonly ILogger Logger = Microsoft.Azure.Devices.Edge.ModuleUtil.ModuleUtil.CreateLogger(nameof(TestOperationResultStorage));
private Dictionary<string, ISequentialStore<TestOperationResult>> resultStores;

// HACK: unblock testing due to controller dependency injection issue
internal static ITestOperationResultStorage Instance { get; private set; }

public static async Task<TestOperationResultStorage> Create(IStoreProvider storeProvider, List<string> resultSources)
{
Preconditions.CheckNotNull(storeProvider, nameof(storeProvider));
Expand All @@ -26,10 +23,7 @@ public static async Task<TestOperationResultStorage> Create(IStoreProvider store
resultSourcesToStores.Add(resultSource, await storeProvider.GetSequentialStore<TestOperationResult>(resultSource));
}

var testOperationResultStorage = new TestOperationResultStorage(resultSourcesToStores);
Instance = testOperationResultStorage;

return testOperationResultStorage;
return new TestOperationResultStorage(resultSourcesToStores);
}

private TestOperationResultStorage(Dictionary<string, ISequentialStore<TestOperationResult>> resultStores)
Expand Down

0 comments on commit a003c3e

Please sign in to comment.