Skip to content

Instantly share code, notes, and snippets.

@Kikimora
Last active May 30, 2020 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kikimora/0e35be6ac4dce6c848727c6099fb60c2 to your computer and use it in GitHub Desktop.
Save Kikimora/0e35be6ac4dce6c848727c6099fb60c2 to your computer and use it in GitHub Desktop.
using System;
using System.Diagnostics;
using System.Linq;
using System.Reactive;
using System.Threading.Tasks;
using FluentAssertions;
using NUnit.Framework;
using Qoden.Test.Util;
using Qoden.Validation;
using Qoden.Util;
namespace Qoden.Bots.Test.BotAccount
{
public class LoginTest : BotAccountTest
{
[Test]
public void LoginOnCreate()
{
//arrange
var observer = Observer.Create((StubAccountRequest x) =>
{
x.Operation.Should().Be(StubAccountOperationType.Login);
x.ReplyOk();
});
using var _ = Controller.Requests.Subscribe(observer);
//act
var account = GetOrCreateBotAccount();
//assert
Wait.For(() => account.IsLoggedIn.Should().BeTrue());
}
[Test]
public void ReLoginOnAuthError()
{
//arrange
BotAccountOptions.RetryTimeout = TimeSpan.FromSeconds(1);
async Task FaultyServer()
{
await using var requests = Controller.Requests.ToAsyncEnumerable().GetAsyncEnumerator();
var request = await requests.NextAsync();
request.Operation.Should().Be(StubAccountOperationType.Login);
request.ReplyOk();
request = await requests.NextAsync();
request.Operation.Should().Be(StubAccountOperationType.LoadOrders);
request.Reply(StubExchangeController.MakeAuthError());
var loginDelaySw = Stopwatch.StartNew();
request = await requests.NextAsync();
loginDelaySw.Elapsed.Should().BeCloseTo(BotAccountOptions.RetryTimeout);
request.Operation.Should().Be(StubAccountOperationType.Login);
request.ReplyOk();
}
//act
var serverTask = FaultyServer();
var account = GetOrCreateBotAccount();
Wait.For(() => account.IsLoggedIn.Should().BeTrue());
Action act = () => Controller.LoadOrdersAsync("TEST").Timeout(); //trigger auth error
act.Should().Throw<ErrorException>();
//assert
Wait.For(() => serverTask.IsCompleted.Should().BeTrue());
}
[Test]
public void LoginUntilSuccess()
{
//arrange
BotAccountOptions.RetryTimeout = TimeSpan.FromMilliseconds(10);
async Task FaultyServer()
{
await using var requests = Controller.Requests.ToAsyncEnumerable().GetAsyncEnumerator();
var errors = 5;
StubAccountRequest request;
while (errors-- > 0)
{
request = await requests.NextAsync();
request.Operation.Should().Be(StubAccountOperationType.Login);
request.Reply(StubExchangeController.MakeAuthError());
}
request = await requests.NextAsync();
request.Operation.Should().Be(StubAccountOperationType.Login);
request.ReplyOk();
}
//act
var serverTask = FaultyServer();
var account = GetOrCreateBotAccount();
//assert
Wait.For(() => serverTask.IsCompleted.Should().BeTrue());
account.IsLoggedIn.Should().BeTrue();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment