cs-codex-dist-tests/Tools/AutoClient/CodexUser.cs

58 lines
1.7 KiB
C#
Raw Normal View History

2024-09-12 10:05:42 +00:00
using CodexOpenApi;
using Logging;
using Utils;
namespace AutoClient
{
public class CodexUser
{
2024-09-12 12:38:15 +00:00
private readonly App app;
2024-09-12 10:05:42 +00:00
private readonly CodexApi codex;
private readonly HttpClient client;
private readonly Address address;
2024-09-12 12:38:15 +00:00
private readonly List<Purchaser> purchasers = new List<Purchaser>();
private Task starterTask = Task.CompletedTask;
private readonly string nodeId = Guid.NewGuid().ToString();
2024-09-12 10:05:42 +00:00
2024-09-12 12:38:15 +00:00
public CodexUser(App app, CodexApi codex, HttpClient client, Address address)
2024-09-12 10:05:42 +00:00
{
2024-09-12 12:38:15 +00:00
this.app = app;
2024-09-12 10:05:42 +00:00
this.codex = codex;
this.client = client;
this.address = address;
}
2024-09-12 12:38:15 +00:00
public void Start(int index)
2024-09-12 10:05:42 +00:00
{
2024-09-12 12:38:15 +00:00
for (var i = 0; i < app.Config.NumConcurrentPurchases; i++)
2024-09-12 10:05:42 +00:00
{
2024-09-12 12:38:15 +00:00
purchasers.Add(new Purchaser(app, nodeId, new LogPrefixer(app.Log, $"({i}) "), client, address, codex));
2024-09-12 10:05:42 +00:00
}
2024-09-12 12:38:15 +00:00
var delayPerPurchaser =
TimeSpan.FromSeconds(10 * index) +
TimeSpan.FromMinutes(app.Config.ContractDurationMinutes) / app.Config.NumConcurrentPurchases;
starterTask = Task.Run(() => StartPurchasers(delayPerPurchaser));
}
private async Task StartPurchasers(TimeSpan delayPerPurchaser)
{
2024-09-12 10:05:42 +00:00
foreach (var purchaser in purchasers)
{
purchaser.Start();
await Task.Delay(delayPerPurchaser);
}
}
2024-09-12 12:38:15 +00:00
public void Stop()
{
starterTask.Wait();
foreach (var purchaser in purchasers)
{
purchaser.Stop();
}
}
2024-09-12 10:05:42 +00:00
}
}