Skip to content
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

Improve submitter client ext #39

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net48</TargetFrameworks>
<Company>ANEO</Company>
<Copyright>Copyright (C) ANEO, 2021-2022</Copyright>
<IsPackable>true</IsPackable>
<LangVersion>10</LangVersion>
<Optimize>true</Optimize>
<DebugType>Embedded</DebugType>
<IncludeSymbols>true</IncludeSymbols>
<OutputType>Exe</OutputType>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<DefineConstants>DEBUG;TRACE</DefineConstants>
</PropertyGroup>


<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="6.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ArmoniK.Api.Client\ArmoniK.Api.Client.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// This file is part of the ArmoniK project
//
// Copyright (C) ANEO, 2021-2022. All rights reserved.
// W. Kirschenmann <[email protected]>
// J. Gurhem <[email protected]>
// D. Dubuc <[email protected]>
// L. Ziane Khodja <[email protected]>
// F. Lemaitre <[email protected]>
// S. Djebbar <[email protected]>
// J. Fonseca <[email protected]>
//
// Licensed under the Apache License, Version 2.0 (the "License")
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


using System;

using ArmoniK.Api.Client.Options;
using ArmoniK.Api.Client.Submitter;
using ArmoniK.Api.gRPC.V1;
using ArmoniK.Api.gRPC.V1.Submitter;

using Microsoft.Extensions.Configuration;

var configuration = new ConfigurationBuilder().AddEnvironmentVariables()
.Build();

var options = configuration.GetRequiredSection(GrpcClient.SettingSection)
.Get<GrpcClient>();

var channel = GrpcChannelFactory.CreateChannel(options);

var client = new Submitter.SubmitterClient(channel);

var reply = client.GetServiceConfiguration(new Empty());

Console.WriteLine(reply.DataChunkMaxSize);

if (reply.DataChunkMaxSize == 0)
{
throw new Exception("DataChunkMaxSize is 0");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
<Company>ANEO</Company>
<Copyright>Copyright (C) ANEO, 2021-2022</Copyright>
<IsPackable>false</IsPackable>
<LangVersion>10</LangVersion>
<Optimize>true</Optimize>
<DebugType>Embedded</DebugType>
<IncludeSymbols>true</IncludeSymbols>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<DefineConstants>DEBUG;TRACE</DefineConstants>
</PropertyGroup>


<ItemGroup>
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="6.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ArmoniK.Api.Client\ArmoniK.Api.Client.csproj" />
</ItemGroup>

</Project>
122 changes: 122 additions & 0 deletions Api/csharp/ArmoniK.Api.Client.Tests/SubmitterClientExtTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// This file is part of the ArmoniK project
//
// Copyright (C) ANEO, 2021-2022. All rights reserved.
// W. Kirschenmann <[email protected]>
// J. Gurhem <[email protected]>
// D. Dubuc <[email protected]>
// L. Ziane Khodja <[email protected]>
// F. Lemaitre <[email protected]>
// S. Djebbar <[email protected]>
// J. Fonseca <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY, without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

using ArmoniK.Api.Client.Internals;
using ArmoniK.Api.Client.Submitter;

using NUnit.Framework;

namespace ArmoniK.Api.Client.Tests;

[TestFixture]
public class PayloadTest
{
[SetUp]
public void SetUp()
{
}

[TearDown]
public virtual void TearDown()
{
}

public static IEnumerable TestCases(int n)
{
var bytes = Encoding.ASCII.GetBytes("test");

for (var i = 1; i < bytes.Length + n; i++)
{
yield return new TestCaseData(new ReadOnlyByteArrayPayload(bytes),
bytes,
i);
yield return new TestCaseData(new ReadOnlyByteMemoryPayload(bytes),
bytes,
i);
yield return new TestCaseData(new StreamPayload(new MemoryStream(bytes)),
bytes,
i);
}
}


[Test]
[TestCaseSource(nameof(TestCases),
new object[]
{
3,
})]
public async Task ChunkingShouldSucceed(IPayload payload,
byte[] bytes,
int maxChunkSize)
{
var res = new byte[bytes.Length];
var idx = 0;
await foreach (var rm in payload.ToChunkedByteStringAsync(maxChunkSize,
CancellationToken.None))
{
rm.Memory.CopyTo(res.AsMemory(idx,
rm.Length));
idx += rm.Length;
}

Assert.AreEqual(bytes,
res);
}

[Test]
[TestCaseSource(nameof(TestCases),
new object[]
{
0,
})]
public void ChunkingWithCancel(IPayload payload,
byte[] bytes,
int maxChunkSize)
{
var res = new byte[bytes.Length];
var idx = 0;

var cancellationTokenSource = new CancellationTokenSource();

Assert.ThrowsAsync<OperationCanceledException>(async () =>
{
await foreach (var rm in payload.ToChunkedByteStringAsync(maxChunkSize,
cancellationTokenSource.Token))
{
rm.Memory.CopyTo(res.AsMemory(idx,
rm.Length));
idx += rm.Length;
cancellationTokenSource.Cancel();
}
});
}
}
4 changes: 2 additions & 2 deletions Api/csharp/ArmoniK.Api.Client/ArmoniK.Api.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ArmoniK.Api.Common\ArmoniK.Api.Common.csproj" />
<ItemGroup>
<ProjectReference Include="..\ArmoniK.Api.Common\ArmoniK.Api.Common.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// This file is part of the ArmoniK project
//
// Copyright (C) ANEO, 2021-2022. All rights reserved.
// W. Kirschenmann <[email protected]>
// J. Gurhem <[email protected]>
// D. Dubuc <[email protected]>
// L. Ziane Khodja <[email protected]>
// F. Lemaitre <[email protected]>
// S. Djebbar <[email protected]>
// J. Fonseca <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY, without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

using System;

namespace ArmoniK.Api.Client.Internals
{
public class ReadOnlyByteArrayPayload : ReadOnlyByteMemoryPayload
{
public ReadOnlyByteArrayPayload(byte[] bytes)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just add a second constructor to the parent class?

: base(new ReadOnlyMemory<byte>(bytes))
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// This file is part of the ArmoniK project
//
// Copyright (C) ANEO, 2021-2022. All rights reserved.
// W. Kirschenmann <[email protected]>
// J. Gurhem <[email protected]>
// D. Dubuc <[email protected]>
// L. Ziane Khodja <[email protected]>
// F. Lemaitre <[email protected]>
// S. Djebbar <[email protected]>
// J. Fonseca <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY, without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

using ArmoniK.Api.Client.Submitter;

using Google.Protobuf;

namespace ArmoniK.Api.Client.Internals
{
public class ReadOnlyByteMemoryPayload : IPayload
{
private readonly ReadOnlyMemory<byte> readOnlyMemory_;

public ReadOnlyByteMemoryPayload(ReadOnlyMemory<byte> readOnlyMemory)
=> readOnlyMemory_ = readOnlyMemory;

public IAsyncEnumerable<ByteString> ToChunkedByteStringAsync(int maxChunkSize,
CancellationToken cancellationToken = default)
=> ToChunkedByteString(maxChunkSize,
cancellationToken)
.ToAsyncEnumerable();

private IEnumerable<ByteString> ToChunkedByteString(int maxChunkSize,
CancellationToken cancellationToken = default)
{
var start = 0;

while (start < readOnlyMemory_.Length)
{
cancellationToken.ThrowIfCancellationRequested();
var chunkSize = Math.Min(maxChunkSize,
readOnlyMemory_.Length - start);
yield return UnsafeByteOperations.UnsafeWrap(readOnlyMemory_.Slice(start,
chunkSize));
start += chunkSize;
}
}
}
}
Loading