Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
caioavidal committed Dec 16, 2024
1 parent 86c59cd commit eb53077
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using NeoServer.Game.Common.Contracts.World;
using NeoServer.Game.Common.Services;
using NeoServer.Game.Items.Events;
using NeoServer.Game.Items.Items.Attributes;
using NeoServer.Game.Items.Items.UsableItems.Runes;
using NeoServer.Game.Systems.SafeTrade;
using NeoServer.Server.Common.Contracts;
using NeoServer.Server.Events.Combat;
Expand Down Expand Up @@ -64,11 +66,10 @@ public void AttachEvents()
NotificationSenderService.OnNotificationSent += _container.GetRequiredService<NotificationSentEventHandler>().Execute;
_gameServer.OnOpened += _container.GetRequiredService<ServerOpenedEventHandler>().Execute;

IDecayable.OnStarted += _itemStartedDecayingEventHandler.Execute;
IUsableOnTile.OnUsedOnTile += _itemUsedOnTileEventHandler.Execute;
Decayable.OnStarted += _itemStartedDecayingEventHandler.Execute;

IConsumable.OnUsed += _itemUsedEventHandler.Execute;
IUsableOnTile.OnUsedOnTile += _fieldRuneUsedEventHandler.Execute;
FieldRune.OnUsedOnTile += _fieldRuneUsedEventHandler.Execute;

AddTradeHandlers();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public interface IDecayable : IDecay
bool IsPaused { get; }
bool TryDecay();
event PauseDecay OnPaused;
static event StartDecay OnStarted;
}

public interface IDecay
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ public bool Use(ICreature usedBy, IItem onItem)

public interface IUsableOnTile : IUsableOn
{
static event UseOnTile OnUsedOnTile;

/// <summary>
/// Usable by creatures on items (ground, weapon, stairs..)
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ public Decayable(IItem item)
public bool StartedToDecay => _startedToDecayTime != default;
public bool IsPaused { get; private set; } = true;
public event PauseDecay OnPaused;
public event StartDecay OnStarted;

public static event StartDecay OnStarted;
public ushort DecaysTo => _item.Metadata.Attributes.GetAttribute<ushort>(ItemAttribute.ExpireTarget);

public uint Duration => _duration = _item.Metadata.Attributes.GetAttribute<uint>(ItemAttribute.Duration) == 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public FieldRune(IItemType type, Location location, byte amount) : base(type, lo
}

public override ushort Duration => 2;
public event UseOnTile OnUsedOnTile;
public static event UseOnTile OnUsedOnTile;
public ushort Field => Metadata.Attributes.GetAttribute<ushort>(ItemAttribute.Field);

public virtual string Area => Metadata.Attributes.GetAttribute(ItemAttribute.Area);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ public void StartDecay_Expired_DoNotStart()
var sut = new Decayable(decayableItem);

var emitted = false;
sut.OnStarted += _ => emitted = true;
Decayable.OnStarted += _ => emitted = true;
//act
sut.StartDecay();
//assert
Expand All @@ -331,7 +331,7 @@ public void Start_EmitEvent()
var sut = new Decayable(decayableItem);

var emitted = false;
sut.OnStarted += _ => emitted = true;
Decayable.OnStarted += _ => emitted = true;
//act
sut.StartDecay();
//assert
Expand Down Expand Up @@ -578,7 +578,7 @@ public async Task Item_that_has_decay_behavior_decays_after_expiration()

var decayableItemManager = DecayableItemManagerTestBuilder.Build(map, itemTypeStore);

IDecayable.OnStarted += decayableItemManager.Add;
Decayable.OnStarted += decayableItemManager.Add;

//act
item1.StartDecay();
Expand All @@ -602,11 +602,13 @@ public Task InitializeAsync()
return Task.CompletedTask;
}

[ThreadBlocking]
public Task DisposeAsync()
{
ThreadBlocking.Wait();
// Cleanup logic after each test
EventSubscriptionCleanUp.CleanUp<IDecayable>(nameof(IDecayable.OnStarted));
EventSubscriptionCleanUp.CleanUp<Decayable>(nameof(Decayable.OnStarted));

ThreadBlocking.Release();
return Task.CompletedTask;
}
}
15 changes: 8 additions & 7 deletions tests/NeoServer.Game.Items.Tests/Items/EquipmentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using NeoServer.Game.Common.Creatures;
using NeoServer.Game.Common.Creatures.Players;
using NeoServer.Game.Common.Item;
using NeoServer.Game.Items.Items.Attributes;
using NeoServer.Game.Tests.Helpers;
using NeoServer.Game.Tests.Helpers.Map;
using NeoServer.Game.Tests.Helpers.Player;
Expand Down Expand Up @@ -328,7 +329,7 @@ public void OnDecayed_NoItemToDecayTo_UndressFromPlayer()
};

var decayableItemManager = DecayableItemManagerTestBuilder.Build(null, itemTypeStore);
IDecayable.OnStarted += decayableItemManager.Add;
Decayable.OnStarted += decayableItemManager.Add;

//act
player.Inventory.AddItem(sut, (byte)Slot.Ring);
Expand Down Expand Up @@ -588,7 +589,7 @@ public void Decayed_HasExpirationTarget_ChangeItem()
}, itemTypeStore.Get);

var decayableItemManager = DecayableItemManagerTestBuilder.Build(null, itemTypeStore);
IDecayable.OnStarted += decayableItemManager.Add;
Decayable.OnStarted += decayableItemManager.Add;

//act
player.Inventory.AddItem(sut, (byte)Slot.Ring);
Expand Down Expand Up @@ -635,9 +636,12 @@ public void TransformOnEquip_OldItemHasNoDecayableButNewHas_CreateDecayableInsta
public void Decayed_HasExpirationTargetButNoFound_OnlyRemovesItem()
{
//arrange

var player = PlayerTestDataBuilder.Build();

var itemTypeStore = ItemTestData.GetItemTypeStore();
var decayableItemManager = DecayableItemManagerTestBuilder.Build(null, itemTypeStore);
Decayable.OnStarted += decayableItemManager.Add;

var sut = ItemTestData.CreateDefenseEquipmentItem(1, "ring", 1,
new (ItemAttribute, IConvertible)[]
Expand All @@ -647,9 +651,6 @@ public void Decayed_HasExpirationTargetButNoFound_OnlyRemovesItem()
(ItemAttribute.ExpireTarget, 5)
}, itemTypeStore.Get);

var decayableItemManager = DecayableItemManagerTestBuilder.Build(null, itemTypeStore);
IDecayable.OnStarted += decayableItemManager.Add;

//act
player.Inventory.AddItem(sut, (byte)Slot.Ring);
Thread.Sleep(1100);
Expand All @@ -668,7 +669,7 @@ public Task InitializeAsync()
public Task DisposeAsync()
{
// Cleanup logic after each test
EventSubscriptionCleanUp.CleanUp<IDecayable>(nameof(IDecayable.OnStarted));
EventSubscriptionCleanUp.CleanUp<Decayable>(nameof(Decayable.OnStarted));
return Task.CompletedTask;
}

Expand Down Expand Up @@ -736,7 +737,7 @@ public async Task Item_that_decay_to_different_3_item_decays()
item2Equipped.Metadata, item3.Metadata, item3Equipped.Metadata);

var decayableItemManager = DecayableItemManagerTestBuilder.Build(map, itemTypeStore);
IDecayable.OnStarted += decayableItemManager.Add;
Decayable.OnStarted += decayableItemManager.Add;

//assert first item
item1.Decay?.Duration.Should().Be(0);
Expand Down
33 changes: 26 additions & 7 deletions tests/NeoServer.Game.Tests/Helpers/ThreadBlockingAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,37 @@ namespace NeoServer.Game.Tests.Helpers;
/// Block the unit test to avoid concurrency
/// </summary>
public class ThreadBlockingAttribute : BeforeAfterTestAttribute
{
public override void Before(MethodInfo methodUnderTest) => ThreadBlocking.Wait();

public override void After(MethodInfo methodUnderTest) => ThreadBlocking.Release();
}

public static class ThreadBlocking

Check notice on line 18 in tests/NeoServer.Game.Tests/Helpers/ThreadBlockingAttribute.cs

View check run for this annotation

codefactor.io / CodeFactor

tests/NeoServer.Game.Tests/Helpers/ThreadBlockingAttribute.cs#L18

File may only contain a single type. (SA1402)
{
private static readonly Mutex Mutex = new();
public override void Before(MethodInfo methodUnderTest)
public static void Wait()
{
Mutex.WaitOne();
// Block the unit test thread
try
{
Mutex.WaitOne();
// Mutex acquired
}
catch (AbandonedMutexException)
{
Console.WriteLine("Warning: Mutex was abandoned by another thread.");
}
}

public override void After(MethodInfo methodUnderTest)
public static void Release()
{
Mutex.ReleaseMutex();
// Release the unit test thread
if (Mutex.WaitOne(0)) // Check if mutex is owned before releasing
{
Mutex.ReleaseMutex();
}
else
{
Console.WriteLine("Warning: Mutex was not owned by the current thread.");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void Channel_file_not_found_logs_an_error_message()
sut.Load();

//assert
loggerMock.Verify(x => x.Error("channels.json file not found at channels.json"), Times.Once);
loggerMock.Verify(x => x.Error("channels.json file not found at {Path}", "channels.json"), Times.Once);
chatChannelStore?.All.Should().BeNullOrEmpty();
}

Expand Down

0 comments on commit eb53077

Please sign in to comment.