Skip to content

Commit

Permalink
Apply formatting on codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
gpailler committed Sep 27, 2021
1 parent ffdc1db commit b7aa5ed
Show file tree
Hide file tree
Showing 59 changed files with 1,288 additions and 1,386 deletions.
2 changes: 1 addition & 1 deletion MegaApiClient.Tests/Context/AnonymousAsyncTestContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class AnonymousAsyncTestContext : AnonymousTestContext, IDisposable
{
public void Dispose()
{
((MegaApiClientAsyncWrapper)this.Client).Dispose();
((MegaApiClientAsyncWrapper)Client).Dispose();
}

protected override IMegaApiClient CreateClient()
Expand Down
2 changes: 1 addition & 1 deletion MegaApiClient.Tests/Context/AnonymousTestContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ protected override void ConnectClient(IMegaApiClient client)

protected override IEnumerable<string> GetProtectedNodes()
{
return this.Client.GetNodes()
return Client.GetNodes()
.Where(x => x.Type == NodeType.Inbox || x.Type == NodeType.Root || x.Type == NodeType.Trash)
.Select(x => x.Id);
}
Expand Down
6 changes: 2 additions & 4 deletions MegaApiClient.Tests/Context/AuthenticatedAsyncTestContext.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using Xunit;

namespace CG.Web.MegaApiClient.Tests.Context
namespace CG.Web.MegaApiClient.Tests.Context
{
public class AuthenticatedAsyncTestContext : AuthenticatedTestContext
{
public override void Dispose()
{
base.Dispose();
((MegaApiClientAsyncWrapper)this.Client).Dispose();
((MegaApiClientAsyncWrapper)Client).Dispose();
}

protected override IMegaApiClient CreateClient()
Expand Down
2 changes: 1 addition & 1 deletion MegaApiClient.Tests/Context/AuthenticatedTestContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ static AuthenticatedTestContext()

public virtual void Dispose()
{
this.Client.Logout();
Client.Logout();
}

protected override void ConnectClient(IMegaApiClient client)
Expand Down
66 changes: 25 additions & 41 deletions MegaApiClient.Tests/Context/TestContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,62 +13,53 @@ public abstract class TestContext : ITestContext
{
private const int MaxRetry = 3;

private readonly Lazy<IMegaApiClient> lazyClient;
private readonly Lazy<IEnumerable<string>> lazyProtectedNodes;
private readonly Lazy<IEnumerable<string>> lazyPermanentNodes;
private readonly Action<string> logMessageAction;
private ITestOutputHelper testOutputHelper;
private readonly Lazy<IMegaApiClient> _lazyClient;
private readonly Lazy<IEnumerable<string>> _lazyProtectedNodes;
private readonly Lazy<IEnumerable<string>> _lazyPermanentNodes;
private readonly Action<string> _logMessageAction;
private ITestOutputHelper _testOutputHelper;

protected TestContext()
{
this.WebTimeout = 60000;
this.lazyClient = new Lazy<IMegaApiClient>(this.InitializeClient);
this.lazyProtectedNodes = new Lazy<IEnumerable<string>>(() => this.GetProtectedNodes().ToArray());
this.lazyPermanentNodes = new Lazy<IEnumerable<string>>(() => this.GetPermanentNodes().ToArray());
this.logMessageAction = x =>
WebTimeout = 60000;
_lazyClient = new Lazy<IMegaApiClient>(InitializeClient);
_lazyProtectedNodes = new Lazy<IEnumerable<string>>(() => GetProtectedNodes().ToArray());
_lazyPermanentNodes = new Lazy<IEnumerable<string>>(() => GetPermanentNodes().ToArray());
_logMessageAction = x =>
{
Debug.WriteLine(x);
testOutputHelper?.WriteLine(x);
_testOutputHelper?.WriteLine(x);
};
}

public IMegaApiClient Client
{
get { return this.lazyClient.Value; }
}
public IMegaApiClient Client => _lazyClient.Value;

public IWebClient WebClient { get; private set; }

public Options Options { get; private set; }

public int WebTimeout { get; }

public IEnumerable<string> ProtectedNodes
{
get { return this.lazyProtectedNodes.Value; }
}
public IEnumerable<string> ProtectedNodes => _lazyProtectedNodes.Value;

public IEnumerable<string> PermanentRootNodes
{
get { return this.lazyPermanentNodes.Value; }
}
public IEnumerable<string> PermanentRootNodes => _lazyPermanentNodes.Value;

public void SetLogger(ITestOutputHelper testOutputHelper)
{
this.testOutputHelper = testOutputHelper;
_testOutputHelper = testOutputHelper;
}

public void ClearLogger()
{
this.testOutputHelper = null;
_testOutputHelper = null;
}

protected virtual IMegaApiClient CreateClient()
{
this.Options = new Options(applicationKey: "ewZQFBBC");
this.WebClient = new TestWebClient(new WebClient(this.WebTimeout, null, new TestMessageHandler(this.logMessageAction), false), MaxRetry, this.logMessageAction);
Options = new Options(applicationKey: "ewZQFBBC");
WebClient = new TestWebClient(new WebClient(WebTimeout, null, new TestMessageHandler(), false), MaxRetry, _logMessageAction);

return new MegaApiClient(this.Options, this.WebClient);
return new MegaApiClient(Options, WebClient);
}

protected abstract IEnumerable<string> GetProtectedNodes();
Expand All @@ -79,29 +70,22 @@ protected virtual IMegaApiClient CreateClient()

private IMegaApiClient InitializeClient()
{
var client = this.CreateClient();
client.ApiRequestFailed += this.OnApiRequestFailed;
this.ConnectClient(client);
var client = CreateClient();
client.ApiRequestFailed += OnApiRequestFailed;
ConnectClient(client);

this.logMessageAction($"Client created for context {this.GetType().Name}");
_logMessageAction($"Client created for context {GetType().Name}");

return client;
}

private void OnApiRequestFailed(object sender, ApiRequestFailedEventArgs e)
private void OnApiRequestFailed(object _, ApiRequestFailedEventArgs e)
{
this.logMessageAction($"ApiRequestFailed: {e.ApiResult}, {e.ApiUrl}, {e.AttemptNum}, {e.RetryDelay}, {e.ResponseJson}, {e.Exception} {e.Exception?.Message}");
_logMessageAction($"ApiRequestFailed: {e.ApiResult}, {e.ApiUrl}, {e.AttemptNum}, {e.RetryDelay}, {e.ResponseJson}, {e.Exception} {e.Exception?.Message}");
}

private class TestMessageHandler : HttpClientHandler
{
private readonly Action<string> logMessageAction;

public TestMessageHandler(Action<string> logMessageAction)
{
this.logMessageAction = logMessageAction;
}

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
return await base.SendAsync(request, cancellationToken);
Expand Down
Loading

0 comments on commit b7aa5ed

Please sign in to comment.