Skip to content

Commit

Permalink
improved task scheduling in connection (#101)
Browse files Browse the repository at this point in the history
* instead of a flag in connections cancelation wait task will now be bound to each function call.
* update Dbosoft.Function to 2.1 for improved Agent task scheduling
  • Loading branch information
fw2568 authored Jul 2, 2021
1 parent f0f596e commit e7e4303
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 25 deletions.
3 changes: 3 additions & 0 deletions YaNco.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/Environment/Filtering/ExcludeCoverageFilters/=SAPSystemTests_003B_002A_003B_002A_003B_002A/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/Filtering/ExcludeCoverageFilters/=YaNco_002ECore_002ETests_003B_002A_003B_002A_003B_002A/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EdotCover_002EIde_002ECore_002EFilterManagement_002EModel_002ESolutionFilterSettingsManagerMigrateSettings/@EntryIndexedValue">True</s:Boolean>
<s:String x:Key="/Default/FilterSettingsManager/CoverageFilterXml/@EntryValue">&lt;data&gt;&lt;IncludeFilters /&gt;&lt;ExcludeFilters&gt;&lt;Filter ModuleMask="SAPSystemTests" ModuleVersionMask="*" ClassMask="*" FunctionMask="*" IsEnabled="True" /&gt;&lt;Filter ModuleMask="YaNco.Core.Tests" ModuleVersionMask="*" ClassMask="*" FunctionMask="*" IsEnabled="True" /&gt;&lt;/ExcludeFilters&gt;&lt;/data&gt;</s:String>
<s:Boolean x:Key="/Default/UserDictionary/Words/=abap/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
33 changes: 19 additions & 14 deletions src/YaNco.Core/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public class Connection : IConnection
public IRfcRuntime RfcRuntime { get; }
private readonly IAgent<AgentMessage, Either<RfcErrorInfo, object>> _stateAgent;
public bool Disposed { get; private set; }
private bool _functionCalled;

public Connection(
IConnectionHandle connectionHandle,
Expand Down Expand Up @@ -50,17 +49,13 @@ public Connection(
case InvokeFunctionMessage invokeFunctionMessage:
{
_functionCalled = true;
StartWaitForFunctionCancellation(invokeFunctionMessage.CancellationToken);
try
using (var callContext = new FunctionCallContext())
{
StartWaitForFunctionCancellation(callContext, invokeFunctionMessage.CancellationToken);
var result = rfcRuntime.Invoke(handle, invokeFunctionMessage.Function.Handle)
.Map(u => (object) u);
return (handle, result);
}
finally
{
_functionCalled = false;
}
}
Expand Down Expand Up @@ -141,20 +136,20 @@ public EitherAsync<RfcErrorInfo, Unit> Cancel()
return res;
}

private async void StartWaitForFunctionCancellation(CancellationToken token)
private async void StartWaitForFunctionCancellation(FunctionCallContext context, CancellationToken token)
{
// ReSharper disable once MethodSupportsCancellation
await Task.Run(() =>
await Task.Factory.StartNew(() =>
{
while (!token.IsCancellationRequested && _functionCalled)
while (!token.IsCancellationRequested && !context.Exited)
{
if(token.WaitHandle.WaitOne(1000))
if(token.WaitHandle.WaitOne(500,true))
break;
}
}).ConfigureAwait(false);
}, TaskCreationOptions.LongRunning).ConfigureAwait(false);

if (token.IsCancellationRequested && _functionCalled)
if (token.IsCancellationRequested && !context.Exited)
await Cancel().ToEither().ConfigureAwait(false);
}

Expand Down Expand Up @@ -230,5 +225,15 @@ public void Dispose()
_stateAgent.Tell(new DisposeMessage(RfcErrorInfo.EmptyResult()));

}

private class FunctionCallContext : IDisposable
{
public bool Exited { get; private set; }
public void Dispose()
{
Exited = true;
}
}
}

}
2 changes: 1 addition & 1 deletion src/YaNco.Core/YaNco.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ This package contains the core implementation.</Description>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Dbosoft.Functional" Version="2.0.1" />
<PackageReference Include="Dbosoft.Functional" Version="2.1.0" />
<PackageReference Include="GitVersion.MsBuild" Version="5.6.10">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
Expand Down
18 changes: 9 additions & 9 deletions test/SAPSystemTests/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ static async Task Main(string[] args)

var connectionBuilder = new ConnectionBuilder(settings)
.WithStartProgramCallback(callback)
.ConfigureRuntime(c =>
.ConfigureRuntime(c =>
c.WithLogger(new SimpleConsoleLogger()));

using (var context = new RfcContext(connectionBuilder.Build()))
Expand Down Expand Up @@ -126,7 +126,7 @@ private static async Task RunIntegrationTest01(IRfcContext context)
QUAN = 99.123,
RAW = RandomByteArray(10),
SSTRING = RandomString(10),
TIMS = default(DateTime).Add(new TimeSpan(23, 59,59)),
TIMS = default(DateTime).Add(new TimeSpan(23, 59, 59)),
STRING = "ABCDE",
RAWSTRING = RandomByteArray(300),
UNIT = "ABC"
Expand Down Expand Up @@ -188,7 +188,7 @@ from fieldUNIT in s.GetField<string>("FIELD_UNIT")
// ReSharper restore InconsistentNaming
// ReSharper restore IdentifierTypo
select new TypesTestData
select new TypesTestData
{
ACCP = fieldACCP,
CHAR = fieldCHAR,
Expand Down Expand Up @@ -300,8 +300,8 @@ private static async Task RunIntegrationTest02(IRfcContext context)


var outputData = await context.CallFunction("ZYANCO_IT_1",
Input: f => f.SetStructure("IS_IN", es =>
es.Bind(s=>s.SetFromDictionary(outputDictionary))),
Input: f => f.SetStructure("IS_IN", es =>
es.Bind(s => s.SetFromDictionary(outputDictionary))),

Output: f => f.MapStructure("ES_ECHO", s =>
// ReSharper disable InconsistentNaming
Expand Down Expand Up @@ -368,14 +368,14 @@ from fieldUNIT in s.GetField<string>("FIELD_UNIT")

// ReSharper restore StringLiteralTypo

var compareLogic = new CompareLogic(new ComparisonConfig(){MaxDifferences = int.MaxValue});
var compareLogic = new CompareLogic(new ComparisonConfig() { MaxDifferences = int.MaxValue });
var result = compareLogic.Compare(inputData, outputData);

Console.WriteLine(!result.AreEqual ? result.DifferencesString : "Test succeed");
}


private static async Task<long> RunPerformanceTest01(IRfcContext context, int rows=0)
private static async Task<long> RunPerformanceTest01(IRfcContext context, int rows = 0)
{
var watch = Stopwatch.StartNew();

Expand All @@ -385,7 +385,7 @@ private static async Task<long> RunPerformanceTest01(IRfcContext context, int ro
from char40 in s.GetField<string>("FIELD_CHAR40")
from char01 in s.GetField<string>("FIELD_CHAR01")
from int04 in s.GetField<int>("FIELD_INT4")
//from s in s.GetField<string>("FIELD_STRING")
//from s in s.GetField<string>("FIELD_STRING")
select new TestData
{
Expand Down Expand Up @@ -462,7 +462,7 @@ public class TestData
public int Int04 { get; set; }

public string String { get; set; }

}

public class TypesTestData
Expand Down
2 changes: 1 addition & 1 deletion test/SAPSystemTests/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"SAPSystemTests": {
"commandName": "Project",
"commandLineArgs": "/tests:repeats=2 /tests:rows=0",
"commandLineArgs": "/tests:repeats=100000 /tests:rows=0",
"nativeDebugging": true
}
}
Expand Down

0 comments on commit e7e4303

Please sign in to comment.