Skip to content

Commit a94870a

Browse files
committed
Mocking hangfire
1 parent b57f263 commit a94870a

15 files changed

Lines changed: 82 additions & 109 deletions

File tree

Mimir.Initializer/Migrators/ProductMigrator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public async Task RunAsync(CancellationToken stoppingToken)
6363

6464
int? crystal = null;
6565
int? crystalPerPrice = null;
66-
int? combatPoint = null;
66+
long? combatPoint = null;
6767
try
6868
{
6969
(crystal, crystalPerPrice) = await _itemProductCalculationService.CalculateCrystalMetricsAsync(itemProduct);

Mimir.MongoDB/Bson/ProductDocument.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public record ProductDocument : MimirBsonDocument
1010
public Address AvatarAddress { get; init; }
1111
public Address ProductsStateAddress { get; init; }
1212
public Product Object { get; init; }
13-
public int? CombatPoint { get; init; }
13+
public long? CombatPoint { get; init; }
1414
public decimal? UnitPrice { get; init; }
1515
public int? Crystal { get; init; }
1616
public int? CrystalPerPrice { get; init; }
@@ -36,7 +36,7 @@ public ProductDocument(
3636
Address productsStateAddress,
3737
Product product,
3838
decimal unitPrice,
39-
int? combatPoint,
39+
long? combatPoint,
4040
int? crystal,
4141
int? crystalPerPrice
4242
)

Mimir.Tests/QueryTests/AgentTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Libplanet.Crypto;
33
using Mimir.MongoDB.Bson;
44
using Mimir.MongoDB.Repositories;
5+
using Mimir.Services;
56
using Moq;
67
using Nekoyume;
78

Mimir.Tests/QueryTests/BalanceQueryTest.cs

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -61,32 +61,6 @@ public async Task GraphQL_Query_Balance_NCG_Returns_CorrectValue()
6161
await Verify(result);
6262
}
6363

64-
[Fact]
65-
public async Task GraphQL_Query_Balance_NCG_Enqueues_Recovery_When_Not_Found()
66-
{
67-
var mockAddress = new Address("0x0000000000000000000000000000000000000000");
68-
var mockRepo = new Mock<IBalanceRepository>();
69-
var mockStateRecoveryService = new Mock<IStateRecoveryService>();
70-
71-
mockRepo
72-
.Setup(repo => repo.GetByAddressAsync("NCG".ToCurrency(), mockAddress))
73-
.ThrowsAsync(new DocumentNotFoundInMongoCollectionException("balance_ncg", mockAddress.ToHex()));
74-
75-
var serviceProvider = TestServices.Builder
76-
.With(mockRepo.Object)
77-
.With(mockStateRecoveryService.Object)
78-
.Build();
79-
80-
var query = $$"""
81-
query {
82-
balance(currencyTicker: "NCG", address: "{{mockAddress}}")
83-
}
84-
""";
85-
86-
var result = await TestServices.ExecuteRequestAsync(serviceProvider, b => b.SetDocument(query));
87-
await Verify(result);
88-
}
89-
9064
[Fact]
9165
public async Task GraphQL_Query_Balance_Throws_When_No_Inputs_Provided()
9266
{

Mimir.Tests/TestServices.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
using System.Linq.Expressions;
12
using System.Numerics;
23
using System.Security.Cryptography;
4+
using Hangfire;
35
using HotChocolate;
46
using HotChocolate.Execution;
57
using Lib9c.GraphQL.Types;
@@ -10,6 +12,7 @@
1012
using Mimir.MongoDB.Bson;
1113
using Mimir.MongoDB.Repositories;
1214
using Mimir.MongoDB.Services;
15+
using Mimir.Services;
1316
using MongoDB.Driver;
1417
using Moq;
1518

@@ -32,9 +35,13 @@ public ServiceProviderBuilder()
3235
.BindRuntimeType(typeof(Address), typeof(AddressType))
3336
.BindRuntimeType(typeof(BigInteger), typeof(BigIntegerType))
3437
.BindRuntimeType(typeof(HashDigest<SHA256>), typeof(HashDigestSHA256Type));
38+
39+
var mockClient = new Mock<IBackgroundJobClient>();
40+
_serviceCollection.AddSingleton<IBackgroundJobClient>(mockClient.Object);
3541
}
3642

37-
public ServiceProviderBuilder With<T>(T service) where T : class
43+
public ServiceProviderBuilder With<T>(T service)
44+
where T : class
3845
{
3946
_serviceCollection.AddSingleton(service);
4047
return this;
@@ -58,7 +65,8 @@ public IServiceProvider Build()
5865
public static async Task<string> ExecuteRequestAsync(
5966
IServiceProvider serviceProvider,
6067
Action<OperationRequestBuilder> configureRequest,
61-
CancellationToken cancellationToken = default)
68+
CancellationToken cancellationToken = default
69+
)
6270
{
6371
await using var scope = serviceProvider.CreateAsyncScope();
6472

@@ -67,8 +75,8 @@ public static async Task<string> ExecuteRequestAsync(
6775
configureRequest(requestBuilder);
6876
var request = requestBuilder.Build();
6977

70-
var executor = await scope.ServiceProvider
71-
.GetRequiredService<IRequestExecutorResolver>()
78+
var executor = await scope
79+
.ServiceProvider.GetRequiredService<IRequestExecutorResolver>()
7280
.GetRequestExecutorAsync(cancellationToken: cancellationToken);
7381
await using var result = await executor.ExecuteAsync(request, cancellationToken);
7482
result.ExpectOperationResult();

Mimir.Worker/ActionHandler/ProductStateHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ Product product
203203

204204
int? crystal = null;
205205
int? crystalPerPrice = null;
206-
int? combatPoint = null;
206+
long? combatPoint = null;
207207
try
208208
{
209209
(crystal, crystalPerPrice) = await itemProductCalculationService.CalculateCrystalMetricsAsync(itemProduct);

Mimir.Worker/Services/IItemProductCalculationService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Mimir.Worker.Services;
77

88
public interface IItemProductCalculationService
99
{
10-
Task<int?> CalculateCombatPointAsync(ItemProduct itemProduct);
10+
Task<long?> CalculateCombatPointAsync(ItemProduct itemProduct);
1111

1212
Task<(int? crystal, int? crystalPerPrice)> CalculateCrystalMetricsAsync(ItemProduct itemProduct);
1313
}

Mimir.Worker/Services/ItemProductCalculationService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ public ItemProductCalculationService(IMongoDbService store)
2323
_store = store;
2424
}
2525

26-
public async Task<int?> CalculateCombatPointAsync(ItemProduct itemProduct)
26+
public async Task<long?> CalculateCombatPointAsync(ItemProduct itemProduct)
2727
{
2828
var costumeStatSheet = await _store.GetSheetAsync<CostumeStatSheet>();
2929

3030
if (costumeStatSheet != null)
3131
{
32-
int? cp = itemProduct.TradableItem switch
32+
long? cp = itemProduct.TradableItem switch
3333
{
3434
ItemUsable itemUsable => CPHelper.GetCP(
3535
(Nekoyume.Model.Item.ItemUsable)

Mimir/GraphQL/Queries/Query.cs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Hangfire;
12
using HotChocolate.AspNetCore;
23
using Lib9c.GraphQL.Extensions;
34
using Lib9c.GraphQL.InputObjects;
@@ -46,21 +47,29 @@ [Service] IActionTypeRepository repo
4647
/// </summary>
4748
/// <param name="address">The address of the agent.</param>
4849
/// <returns>The agent state</returns>
49-
public async Task<AgentState> GetAgentAsync(Address address, [Service] IAgentRepository repo)
50+
public async Task<AgentState> GetAgentAsync(
51+
Address address,
52+
[Service] IAgentRepository repo,
53+
[Service] IBackgroundJobClient jobClient
54+
)
5055
{
5156
try
5257
{
5358
var document = await repo.GetByAddressAsync(address);
5459
if (document.Metadata.StoredBlockIndex != 0)
5560
{
56-
HangfireJobs.EnqueueAgentStateRecovery(address);
61+
jobClient.Enqueue<IStateRecoveryService>(service =>
62+
service.TryRecoverAgentStateAsync(address)
63+
);
5764
}
5865

5966
return document.Object;
6067
}
6168
catch (Mimir.MongoDB.Exceptions.DocumentNotFoundInMongoCollectionException)
6269
{
63-
HangfireJobs.EnqueueAgentStateRecovery(address);
70+
jobClient.Enqueue<IStateRecoveryService>(service =>
71+
service.TryRecoverAgentStateAsync(address)
72+
);
6473
throw;
6574
}
6675
}
@@ -70,15 +79,21 @@ public async Task<AgentState> GetAgentAsync(Address address, [Service] IAgentRep
7079
/// </summary>
7180
/// <param name="address">The address of the avatar.</param>
7281
/// <returns>The avatar state</returns>
73-
public async Task<AvatarState> GetAvatarAsync(Address address, [Service] IAvatarRepository repo)
82+
public async Task<AvatarState> GetAvatarAsync(
83+
Address address,
84+
[Service] IAvatarRepository repo,
85+
[Service] IBackgroundJobClient jobClient
86+
)
7487
{
7588
try
7689
{
7790
return (await repo.GetByAddressAsync(address)).Object;
7891
}
7992
catch (Mimir.MongoDB.Exceptions.DocumentNotFoundInMongoCollectionException)
8093
{
81-
HangfireJobs.EnqueueAvatarStateRecovery(address);
94+
jobClient.Enqueue<IStateRecoveryService>(service =>
95+
service.TryRecoverAvatarStateAsync(address)
96+
);
8297
throw;
8398
}
8499
}
@@ -95,7 +110,8 @@ public async Task<string> GetBalanceAsync(
95110
CurrencyInput? currency,
96111
string? currencyTicker,
97112
Address address,
98-
[Service] IBalanceRepository repo
113+
[Service] IBalanceRepository repo,
114+
[Service] IBackgroundJobClient jobClient
99115
)
100116
{
101117
try
@@ -118,7 +134,9 @@ [Service] IBalanceRepository repo
118134
{
119135
if (currencyTicker?.ToUpper() == "NCG")
120136
{
121-
HangfireJobs.EnqueueNCGBalanceRecovery(address);
137+
jobClient.Enqueue<IStateRecoveryService>(service =>
138+
service.TryRecoverNCGBalanceAsync(address)
139+
);
122140
}
123141
throw;
124142
}

Mimir/Options/HangfireOption.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ public class HangfireOption
44
{
55
public const string SectionName = "Hangfire";
66

7-
public bool IsEnabled { get; set; } = false;
87
public string RedisConnectionString { get; set; } = string.Empty;
98
public string DashboardPath { get; set; } = "/hangfire";
109
public int WorkerCount { get; set; } = Environment.ProcessorCount;

0 commit comments

Comments
 (0)