This repository has been archived on 2020-12-20. You can view files and clone it, but cannot push or open issues or pull requests.
aurora-sharp-desktop/Aurora.test/ControllerTests/EventTests.cs

100 lines
3.3 KiB
C#
Raw Normal View History

using NUnit.Framework;
using Aurora.Proto.Party;
using Aurora.Services.Server;
using Aurora.Services.EventManager;
using Grpc.Core;
using System.Threading.Tasks;
using System.Threading;
using Autofac;
namespace Aurora.test.ControllerTests
{
public class EventTest
{
private RemotePartyService.RemotePartyServiceClient _remotePartyService;
private Channel _channel;
private IContainer _container;
private IServerService _serverService;
#region Setup
[SetUp]
public void Setup()
{
_container = SetupUtil.SetupOneTime();
_remotePartyService = SetupUtil.Setup(ref _container, ref _serverService, ref _channel);
}
[TearDown]
public async Task TearDown()
{
await _serverService.Stop();
await _channel.ShutdownAsync();
_container.Dispose();
}
#endregion Setup
[Test]
[TestCase(EventType.MediaPlaying)]
public void Asdf(EventType value)
{
Assert.AreEqual(1, 1);
}
[Test]
[TestCase(EventType.MediaPlaying)]
[TestCase(EventType.MediaStopped)]
[TestCase(EventType.MemberCreated)]
[TestCase(EventType.MemberDeleted)]
public async Task TestEventSubscriptions(EventType value)
{
using (var scope = _container.BeginLifetimeScope())
{
IEventManager eventManager = scope.Resolve<IEventManager>();
//Create new party member
Member member = _remotePartyService.CreateMember(new CreateMemberRequest()
{
Parent = "party1",
Member = new Member()
{
UserName = "newMember1",
IpAddress = ServerService.GetLocalIPAddress(),
}
});
//Subscribe to event type
_remotePartyService.CreateEventSubscription(new CreateEventSubscriptionRequest()
{
Parent = member.Name,
EventSubscription = new EventSubscription()
{
Type = value
}
});
BaseEvent @event = new BaseEvent
{
EventType = value,
};
//Fire event
CancellationTokenSource eventCancellationTokenSource = new CancellationTokenSource();
BaseEvent newEvent = null;
Task.Run(async () => { await Task.Delay(1000); eventManager.FireEvent(@event); });
using (AsyncServerStreamingCall<BaseEvent> eventStream = _remotePartyService
.GetEvents(new GetEventsRequest() { Parent = member.Name }))
{
while ((!eventCancellationTokenSource.Token.IsCancellationRequested &&
await eventStream.ResponseStream.MoveNext(eventCancellationTokenSource.Token)))
{
newEvent = new BaseEvent(eventStream.ResponseStream.Current);
break;
}
};
Assert.AreEqual(newEvent.EventType, value);
}
}
}
}