Skip to content

Commit

Permalink
#65 Increased upload/download buffersize (#68)
Browse files Browse the repository at this point in the history
* #65 Increased default buffer size used for upload/download to 64KB

* Ensure ReportProgressChunkSize is lower than BufferSize.
Improved ProgressionStream to send 100% event only once
Added associated UTs

* Throw proper assert if MEGAAPICLIENT_PASSWORD env variable is not set

* Fixed other UTs
  • Loading branch information
gpailler authored Sep 1, 2017
1 parent 67be4e7 commit 4070ba7
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 13 deletions.
5 changes: 3 additions & 2 deletions MegaApiClient.Tests/Context/AuthenticatedTestContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ public class AuthenticatedLoginTestsCollection : ICollectionFixture<Authenticate

public class AuthenticatedTestContext : TestContext, IDisposable
{
private const string MegaApiClientPasswordEnvironment = "MEGAAPICLIENT_PASSWORD";
internal const string Username = "[email protected]";
internal static readonly string Password = Environment.GetEnvironmentVariable("MEGAAPICLIENT_PASSWORD");
internal static readonly string Password = Environment.GetEnvironmentVariable(MegaApiClientPasswordEnvironment);

internal const string FileLink = "https://mega.nz/#!bkwkHC7D!AWJuto8_fhleAI2WG0RvACtKkL_s9tAtvBXXDUp2bQk";
internal const string FolderLink = "https://mega.nz/#F!e1ogxQ7T!ee4Q_ocD1bSLmNeg9B6kBw";
Expand Down Expand Up @@ -84,7 +85,7 @@ public virtual void Dispose()

protected override void ConnectClient(IMegaApiClient client)
{
Assert.NotEmpty(Password);
Assert.False(string.IsNullOrEmpty(Password), $"Environment variable {MegaApiClientPasswordEnvironment} not set.");
client.Login(Username, Password);
}

Expand Down
17 changes: 10 additions & 7 deletions MegaApiClient.Tests/DownloadUploadAuthenticatedAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ public override void Dispose()
}

[Theory]
[InlineData(null, 10L)]
[InlineData(10L, 65L)]
[InlineData(null, 8)]
[InlineData(1024 * 64, 8)]
[InlineData(100000, 4)]
[InlineData(200000, 2)]
public void DownloadFileAsync_FromNode_Succeeds(long? reportProgressChunkSize, long expectedResult)
{
// Arrange
Expand Down Expand Up @@ -69,15 +71,16 @@ public void DownloadFileAsync_FromLink_Succeeds()

// Assert
Assert.True(result);
Assert.Equal(10, eventTester.Calls);
Assert.Equal(8, eventTester.Calls);
this.AreFileEquivalent(this.GetAbsoluteFilePath(expectedResultFile), outputFile);
}

[Fact]
public void UploadStreamAsync_DownloadLink_Succeeds()
[Theory]
[InlineData(123456, 2)]
public void UploadStreamAsync_DownloadLink_Succeeds(int dataSize, int expectedProgressionCalls)
{
//Arrange
byte[] data = new byte[123456];
byte[] data = new byte[dataSize];
this.random.NextBytes(data);

INode parent = this.GetNode(NodeType.Root);
Expand Down Expand Up @@ -105,7 +108,7 @@ public void UploadStreamAsync_DownloadLink_Succeeds()
// Assert
Assert.True(result);
Assert.NotNull(task.Result);
Assert.Equal(3, eventTester.Calls);
Assert.Equal(expectedProgressionCalls, eventTester.Calls);

Uri uri = this.context.Client.GetDownloadLink(task.Result);
stream.Position = 0;
Expand Down
27 changes: 27 additions & 0 deletions MegaApiClient.Tests/Options_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace CG.Web.MegaApiClient.Tests
{
using System;
using CG.Web.MegaApiClient.Tests.Context;
using Xunit;
using Xunit.Abstractions;

[Collection("NotLoggedTests")]
public class Options_Tests : TestsBase, IDisposable
{
public Options_Tests(NotLoggedTestContext context, ITestOutputHelper testOutputHelper)
: base(context, testOutputHelper)
{
}

public void Dispose()
{
}

[Fact]
public void ReportProgressChunkSize_LowerThan_BufferSize_Throws()
{
var bufferSize = new Options().BufferSize;
Assert.Throws<ArgumentException>("reportProgressChunkSize", () => new Options(reportProgressChunkSize: bufferSize - 1));
}
}
}
14 changes: 11 additions & 3 deletions MegaApiClient/Options.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
namespace CG.Web.MegaApiClient
{
using System;

public class Options
{
public const string DefaultApplicationKey = "axhQiYyQ";
Expand All @@ -9,11 +11,11 @@ public class Options
public const int DefaultApiRequestDelay = 500;
public const float DefaultApiRequestDelayExponentialFactor = 1.5f;

public const int DefaultBufferSize = 8192;
public const int DefaultBufferSize = 1024 * 64;
public const int DefaultChunksPackSize = 1024 * 1024;

#if ASYNC
public const long DefaultReportProgressChunkSize = 1024 * 50;
public const long DefaultReportProgressChunkSize = DefaultBufferSize;
#endif

public Options(
Expand Down Expand Up @@ -41,6 +43,12 @@ public Options(
this.ChunksPackSize = chunksPackSize;

#if ASYNC
if (reportProgressChunkSize < this.BufferSize)
{
throw new ArgumentException(
$"ReportProgressChunkSize ({reportProgressChunkSize}) cannot have a value lower than BufferSize ({bufferSize})",
nameof(reportProgressChunkSize));
}
this.ReportProgressChunkSize = reportProgressChunkSize;
#endif
}
Expand Down Expand Up @@ -76,4 +84,4 @@ public Options(
public long ReportProgressChunkSize { get; internal set;}
#endif
}
}
}
7 changes: 6 additions & 1 deletion MegaApiClient/Stream/ProgressionStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ public override void Write(byte[] buffer, int offset, int count)
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
this.progress.Report(100);

// Report 100% progress only if it was not already sent
if (this.chunkSize != 0)
{
this.progress.Report(100);
}
}

#region Forwards
Expand Down

0 comments on commit 4070ba7

Please sign in to comment.