async public Task <IActionResult> Run(string roomId, FunctionCallViewModel fcall)
        {
            var normalisedRoomId = roomId.ToUpperInvariant();

            if (!RoomRepository.Exists(normalisedRoomId))
            {
                return(NotFound("Room not found"));
            }

            var room = RoomRepository.Get(normalisedRoomId);

            return(await RoomHelpers.WithRoomLock <IActionResult>(room, async() =>
            {
                if (room.Library == null)
                {
                    return base.BadRequest();
                }

                if (!room.Library.GetFunctionList().Any(f => f.Name == fcall.Name))
                {
                    return base.NotFound("Function not found");
                }

                var results = room.Library.Call(fcall.Name, fcall.Arguments);
                ResultGroup resultGroup = GroupResults(results);
                await ResultPublisher.Publish(room, resultGroup);
                return base.Ok();
            }));
        }
        public void ResultCanBeUpdated()
        {
            var intercepted = new List <DomainResult>();
            var result      = new DomainResult(_mockTrackedMessage.Object, ResultCode.Unknown, string.Empty);

            _mockResultStore.Setup(x => x.Save(It.IsAny <DomainResult>())).Callback <DomainResult>(x => intercepted.Add(x));
            _publisher = new ResultPublisher(_mockResultStore.Object, new TimeoutProvider(200));
            _publisher.Publish(result);
            result = new DomainResult(_mockTrackedMessage.Object, ResultCode.OK, "finished");
            _publisher.Publish(result);
            Assert.AreEqual(2, intercepted.Count);
            Assert.AreEqual(intercepted[0].Id, intercepted[1].Id);
            Assert.AreEqual(intercepted[0].TrackingId, intercepted[1].TrackingId);
            Assert.AreEqual(intercepted[0].ResultCode, ResultCode.Unknown);
            Assert.AreEqual(intercepted[1].ResultCode, ResultCode.OK);
            Assert.AreEqual(string.Empty, intercepted[0].ResultMessage);
            Assert.AreEqual("finished", intercepted[1].ResultMessage);
        }
        public void ResultAreCleanupUpAfterTwiceAGivenTime()
        {
            var result = new DomainResult(_mockTrackedMessage.Object, ResultCode.OK, string.Empty);

            _mockResultStore.Setup(x => x.Save(It.IsAny <DomainResult>())).Verifiable();
            _mockResultStore.Setup(x => x.Delete(_id.ToString()));
            _publisher = new ResultPublisher(_mockResultStore.Object, new TimeoutProvider(200));
            _publisher.Publish(result);
            Thread.Sleep(250);
            _mockResultStore.Verify(x => x.Delete(_trackingId.ToString()), Times.Exactly(0));
            Thread.Sleep(200);
            _mockResultStore.Verify(x => x.Delete(_trackingId.ToString()), Times.Exactly(1));
        }
        public void ResultCanBePublished()
        {
            var result = new DomainResult(_mockTrackedMessage.Object, ResultCode.OK, string.Empty);

            _mockResultStore.Setup(x => x.Save(It.IsAny <DomainResult>())).Verifiable();
            _publisher = new ResultPublisher(_mockResultStore.Object, new TimeoutProvider(200));
            _publisher.Publish(result);

            Assert.AreEqual(ResultCode.OK, result.ResultCode);
            Assert.AreEqual(_id.ToString(), result.Id);
            Assert.AreEqual(string.Empty, result.ResultMessage);
            _mockResultStore.Verify();
            _mockResultStore.Verify(x => x.Delete(_id.ToString()), Times.Never);
        }