-
Notifications
You must be signed in to change notification settings - Fork 0
fix: connector doesn't recover from failure after network issue #328
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mohammad-arif662
wants to merge
19
commits into
main
Choose a base branch
from
POFSP-1506
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+179
−50
Open
Changes from 8 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
3d9510b
replace async void with async Task in PublishConnectorStatus
mohammad-arif662 0375d77
added async suffix
mohammad-arif662 b1d36ca
added test
mohammad-arif662 c798d77
formatting
mohammad-arif662 025bc9f
move unit test to separate file
mohammad-arif662 b74c411
code Refactoring
mohammad-arif662 d585f48
revert optional added parameters
mohammad-arif662 97a804f
Update TestUtilities.cs
mohammad-arif662 546ec4b
addressed review comments
mohammad-arif662 95d1703
Update SimulationRunnerUnitTest.cs
mohammad-arif662 fbf17d4
Update .editorconfig
mohammad-arif662 68f7369
Update SimulationRunnerUnitTest.cs
mohammad-arif662 3b2a3d9
revert .editorconfig
mohammad-arif662 581a94a
Update .editorconfig
mohammad-arif662 df7d2f8
Merge branch 'main' into POFSP-1506
abdullah-cognite 1bd1b08
Update SimulationRunnerBase.cs
mohammad-arif662 25bf592
review comments
mohammad-arif662 aa186c6
address review comments
mohammad-arif662 5da2537
review comments
mohammad-arif662 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
148 changes: 148 additions & 0 deletions
148
Cognite.Simulator.Tests/UtilsTests/SimulationRunnerUnitTest.cs
This file contains hidden or 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,148 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Net.Http; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| using Cognite.Extractor.Logging; | ||
| using Cognite.Simulator.Utils; | ||
| using Cognite.Simulator.Utils.Automation; | ||
|
|
||
| using CogniteSdk.Alpha; | ||
|
|
||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| using Moq; | ||
|
|
||
| using Xunit; | ||
|
|
||
| using static Cognite.Simulator.Tests.UtilsTests.TestUtilities; | ||
|
|
||
| namespace Cognite.Simulator.Tests.UtilsTests | ||
| { | ||
| [Collection(nameof(SequentialTestCollection))] | ||
| public class SimulationRunnerUnitTest | ||
| { | ||
| /// <summary> | ||
| /// Tests that when network error occurs during UpdateSimulationRunStatus callback, | ||
| /// the HttpRequestException is caught by DefaultConnectorRuntime and handled gracefully. | ||
| /// This simulates internet disconnection during the run status update. | ||
| /// </summary> | ||
| [Fact] | ||
| public async Task TestSimulationRunner_CallbackNetworkError_HandledGracefully() | ||
| { | ||
| var runsListCallCount = 0; | ||
|
|
||
| var networkErrorMocks = new List<SimpleRequestMocker> | ||
mohammad-arif662 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| new SimpleRequestMocker(uri => uri.EndsWith("/token"), MockAzureAADTokenEndpoint), | ||
| new SimpleRequestMocker(uri => uri.EndsWith("/token/inspect"), MockTokenInspectEndpoint), | ||
| new SimpleRequestMocker(uri => uri.Contains("/extpipes"), MockExtPipesEndpoint), | ||
| new SimpleRequestMocker(uri => uri.EndsWith("/simulators/list") || uri.EndsWith("/simulators") || uri.EndsWith("/simulators/update"), MockSimulatorsEndpoint), | ||
| new SimpleRequestMocker(uri => uri.Contains("/simulators/integrations/list"), MockSimulatorIntegrationsListEndpoint), | ||
| new SimpleRequestMocker(uri => uri.Contains("/simulators/integrations/update"), MockSimulatorsIntegrationsEndpoint), | ||
|
|
||
| new SimpleRequestMocker(uri => uri.Contains("/simulators/runs/list"), () => | ||
| { | ||
| runsListCallCount++; | ||
| if (runsListCallCount == 1) | ||
| { | ||
| return MockSimulationRunsListEndpoint(); | ||
| } | ||
| return MockSimulationRunsListEmptyEndpoint(); | ||
| }), | ||
polomani marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| new SimpleRequestMocker(uri => uri.Contains("/simulators/run/callback"), () => | ||
| { | ||
| throw new HttpRequestException("Network error during callback update"); | ||
| }), | ||
|
|
||
| new SimpleRequestMocker(uri => uri.Contains("/simulators/routines/revisions/list") || uri.Contains("/simulators/routines/revisions/byids"), MockSimulatorRoutineRevWithIntegrationEndpoint), | ||
| new SimpleRequestMocker(uri => uri.Contains("/simulators/models/revisions"), MockSimulatorModelRevListEndpoint), | ||
| new SimpleRequestMocker(uri => uri.Contains("/files/byids"), MockFilesByIdsEndpoint), | ||
| new SimpleRequestMocker(uri => uri.Contains("/files/downloadlink"), MockFilesDownloadLinkEndpoint), | ||
| new SimpleRequestMocker(uri => uri.Contains("/files/download"), () => MockFilesDownloadEndpoint(1)), | ||
| new SimpleRequestMocker(uri => uri.Contains("/simulators/logs"), () => OkItemsResponse("{}")), | ||
| }; | ||
|
|
||
| WriteConfig(); | ||
|
|
||
| using var cts = new CancellationTokenSource(); | ||
| cts.CancelAfter(TimeSpan.FromSeconds(2)); | ||
|
|
||
| var mockedLogger = new Mock<ILogger<DefaultConnectorRuntime<DefaultAutomationConfig, DefaultModelFilestate, DefaultModelFileStatePoco>>>(); | ||
| var mockedSimulationRunnerLogger = new Mock<ILogger<DefaultSimulationRunner<DefaultAutomationConfig, DefaultModelFilestate, DefaultModelFileStatePoco>>>(); | ||
| var mockedConnectorLogger = new Mock<ILogger<DefaultConnector<DefaultAutomationConfig, DefaultModelFilestate, DefaultModelFileStatePoco>>>(); | ||
| var mockFactory = GetMockedHttpClientFactory(MockRequestsAsync(networkErrorMocks)); | ||
|
|
||
| DefaultConnectorRuntime<DefaultAutomationConfig, DefaultModelFilestate, DefaultModelFileStatePoco>.ConfigureServices = (services) => | ||
| { | ||
| services.AddScoped<ISimulatorClient<DefaultModelFilestate, SimulatorRoutineRevision>, EmptySimulatorAutomationClient>(); | ||
| services.AddSingleton(mockFactory.Object); | ||
| services.AddSingleton(mockedLogger.Object); | ||
| services.AddSingleton(mockedSimulationRunnerLogger.Object); | ||
| services.AddSingleton(mockedConnectorLogger.Object); | ||
| }; | ||
| DefaultConnectorRuntime<DefaultAutomationConfig, DefaultModelFilestate, DefaultModelFileStatePoco>.ConnectorName = "Empty"; | ||
| DefaultConnectorRuntime<DefaultAutomationConfig, DefaultModelFilestate, DefaultModelFileStatePoco>.SimulatorDefinition = SeedData.SimulatorCreate; | ||
|
|
||
| try | ||
| { | ||
| await DefaultConnectorRuntime<DefaultAutomationConfig, DefaultModelFilestate, DefaultModelFileStatePoco>.Run(mockedLogger.Object, cts.Token); | ||
| } | ||
| catch (OperationCanceledException) { } | ||
mohammad-arif662 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
mohammad-arif662 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| VerifyLog(mockedSimulationRunnerLogger, LogLevel.Warning, "Simulation run 12345 failed with error:", Times.AtLeastOnce(), true); | ||
mohammad-arif662 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| VerifyLog(mockedSimulationRunnerLogger, LogLevel.Warning, "Network error during callback update", Times.AtLeastOnce(), true); | ||
| VerifyLog(mockedConnectorLogger, LogLevel.Information, "Connector started", Times.AtLeast(2), true); | ||
| } | ||
|
|
||
|
|
||
| private class EmptySimulatorAutomationClient : | ||
mohammad-arif662 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| AutomationClient, | ||
| ISimulatorClient<DefaultModelFilestate, SimulatorRoutineRevision> | ||
| { | ||
| public EmptySimulatorAutomationClient( | ||
| ILogger<EmptySimulatorAutomationClient> logger, | ||
| DefaultConfig<DefaultAutomationConfig> config) : base(logger, config.Automation) | ||
| { | ||
| } | ||
|
|
||
| public Task ExtractModelInformation(DefaultModelFilestate state, CancellationToken _token) | ||
| { | ||
| return Task.CompletedTask; | ||
| } | ||
|
|
||
| public string GetConnectorVersion(CancellationToken _token) | ||
| { | ||
| return CommonUtils.GetAssemblyVersion(); | ||
| } | ||
|
|
||
| public string GetSimulatorVersion(CancellationToken _token) | ||
| { | ||
| return "2.0.1"; | ||
| } | ||
|
|
||
| public Task<Dictionary<string, SimulatorValueItem>> RunSimulation( | ||
| DefaultModelFilestate modelState, | ||
| SimulatorRoutineRevision routineRevision, | ||
| Dictionary<string, SimulatorValueItem> inputData, | ||
| CancellationToken token | ||
| ) | ||
| { | ||
| return Task.FromResult(new Dictionary<string, SimulatorValueItem>()); | ||
| } | ||
|
|
||
| public Task TestConnection(CancellationToken token) | ||
| { | ||
| return Task.CompletedTask; | ||
| } | ||
|
|
||
| protected override void PreShutdown() | ||
| { | ||
| throw new NotImplementedException(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.