public async Task SubscriptionHandlesShouldHaveIdentity()
        {
            var stream = Silo.AddStreamProbe <ChatMessage>(Guid.Empty, null);

            await Silo.CreateGrainAsync <Listener>(1);

            var handlers = await stream.GetAllSubscriptionHandles();

            handlers.Count.Should().Be(1);
            stream.Subscribed.Should().Be(1);


            foreach (var handle in handlers)
            {
                handle.ProviderName.Should().Be("Default");
                handle.HandleId.Should().NotBeEmpty();
                handle.StreamIdentity.Should().NotBeNull();
                handle.StreamIdentity.Namespace.Should().BeNull();
            }

            await handlers[0].UnsubscribeAsync();

            handlers.Count.Should().Be(1);
            stream.Subscribed.Should().Be(0);
        }
        public async Task GetMessageSinceReturnsAllMessagesIfInBoundsLargerSet()
        {
            var maxRewind = 10;

            Silo.ServiceProvider.AddService(new OrgnalRSiloConfig
            {
                MaxMessageRewind = maxRewind
            });
            var grain = await Silo.CreateGrainAsync <RewindableMessageGrain <AnonymousMessage> >(Guid.NewGuid().ToString());

            var handles = Enumerable.Range(0, 20)
                          .Select(i => grain.PushMessageAsync(new AnonymousMessage(new HashSet <string>(), new MethodMessage(i.ToString(), new object[0]))))
                          .Select(x => x.Result)
                          .ToList();

            for (int i = 0; i < handles.Count; i++)
            {
                if (i + 1 < maxRewind)
                {
                    await Assert.ThrowsAsync <ArgumentOutOfRangeException>(async() => await grain.GetMessagesSinceAsync(handles[i]));
                }
                else
                {
                    var since = await grain.GetMessagesSinceAsync(handles[i]);

                    var expectedSinceHandles = handles.SkipWhile(x => x.MessageId <= handles[i].MessageId).ToList();
                    Assert.Equal(expectedSinceHandles, since.Select(x => x.handle).ToList());
                    if (i != handles.Count - 1)
                    {
                        Assert.NotEmpty(since);
                    }
                }
            }
        }
示例#3
0
        public async Task GivenEmptyHandlerInState_WhenGrainActivates_ThenResumeHandler()
        {
            //Arrange
            var onResumeCalled      = false;
            var onAttachingObserver = new Action <IAsyncObserver <ChatMessage> >(obs => onResumeCalled = true);

            _stateWithHandle.ChatMessageStreamSubscriptionHandle =
                await _stream.AddEmptyStreamHandler(onAttachingObserver);

            //Check to see there is a handler registered
            var handles = await _stream.GetAllSubscriptionHandles();

            Assert.Equal(1, handles.Count);
            Assert.False(onResumeCalled);

            //Act
            var grain = await Silo.CreateGrainAsync <PersistentListenerWithHandleInState>(1);

            //Assert
            Assert.True(onResumeCalled);

            //Act
            await _stream.OnNextAsync(new ChatMessage("here's a message"));

            //Assert
            var receivedMessages = await grain.ReceivedCount();

            Assert.Equal(1, receivedMessages);
        }
示例#4
0
        public async Task FactoryProbeWithMultipleNewProbes_FakeItEasy()
        {
            var firstUnknownGrain  = A.Fake <IUnknownGrain>();
            var secondUnknownGrain = A.Fake <IUnknownGrain>();
            var probeQueue         = new Queue <IUnknownGrain>(new[] { firstUnknownGrain, secondUnknownGrain });

            Silo.AddProbe <IUnknownGrain>(identity =>
            {
                var nextProbe = probeQueue.Dequeue();

                A.CallTo(() => nextProbe.WhatsMyId())
                .Returns(identity.PrimaryKeyString);

                return(nextProbe);
            });

            var grain = await Silo.CreateGrainAsync <UnknownGrainResolver>("1");

            await grain.CreateAndPingMultiple();

            var resolvedIds = await grain.GetResolvedUnknownGrainIdsAsync();

            resolvedIds[0].Should().Be("unknownGrainOne");
            resolvedIds[1].Should().Be("unknownGrainTwo");
        }
        public async Task GetMessageSinceReturnsAllMessagesIfInBounds()
        {
            Silo.ServiceProvider.AddService(
                new OrgnalRSiloConfig
            {
                MaxMessageRewind = 1,
            });
            var grain = await Silo.CreateGrainAsync <RewindableMessageGrain <AnonymousMessage> >(Guid.NewGuid().ToString());

            var handle = await grain.PushMessageAsync(new AnonymousMessage(new HashSet <string>(), new MethodMessage("TestTarget1", new object[0])));

            var since = await grain.GetMessagesSinceAsync(handle);

            Assert.Empty(since);
            var secondMsg = new AnonymousMessage(new HashSet <string>(), new MethodMessage("TestTarget2", new object[0]));
            var handle2   = await grain.PushMessageAsync(secondMsg);

            since = await grain.GetMessagesSinceAsync(handle2);

            Assert.Empty(since);
            since = await grain.GetMessagesSinceAsync(handle);

            Assert.NotEmpty(since);
            Assert.Equal(handle2, since.Single().handle);
            Assert.Equal(secondMsg, since.Single().message);
        }
示例#6
0
        public async Task MissingProbe()
        {
            IPing grain = await Silo.CreateGrainAsync <PingGrain>(1);

            //There should not be an exception, since we are using loose grain generation
            grain.Invoking(p => p.Ping()).Should().NotThrow();
        }
示例#7
0
        public async Task FactoryProbeWithMultipleNewProbes_Moq()
        {
            // useful for when a grain needs to create other grains whose identities are not known beforehand
            // and probes for the new grains need to return certain values

            var firstUnknownGrain  = new Mock <IUnknownGrain>();
            var secondUnknownGrain = new Mock <IUnknownGrain>();
            var probeQueue         = new Queue <Mock <IUnknownGrain> >(new [] { firstUnknownGrain, secondUnknownGrain });

            Silo.AddProbe <IUnknownGrain>(identity =>
            {
                var nextProbe = probeQueue.Dequeue();

                nextProbe.Setup(probe => probe.WhatsMyId())
                .ReturnsAsync(identity.PrimaryKeyString);

                return(nextProbe);
            });

            var grain = await Silo.CreateGrainAsync <UnknownGrainResolver>("1");

            await grain.CreateAndPingMultiple();

            var resolvedIds = await grain.GetResolvedUnknownGrainIdsAsync();

            resolvedIds[0].Should().Be("unknownGrainOne");
            resolvedIds[1].Should().Be("unknownGrainTwo");
        }
示例#8
0
        public async Task IntegerCompoundKeyGrain()
        {
            const int id  = int.MaxValue;
            var       ext = "Thing";

            var grain = await Silo.CreateGrainAsync <IntegerCompoundKeyGrain>(id, ext);

            var key = await grain.GetKey();

            var keyLong        = grain.GetPrimaryKeyLong(out var keyExt);
            var referenceKey   = grain.GrainReference.GetPrimaryKeyLong(out var referenceKeyExt);
            var addressableKey = ((IAddressable)grain).GetPrimaryKeyLong(out var addressableKeyExt);

            key.Item1.Should().Be(id);
            key.Item2.Should().Be(ext);

            keyLong.Should().Be(id);
            keyExt.Should().Be(ext);

            referenceKey.Should().Be(id);
            referenceKeyExt.Should().Be(ext);

            addressableKey.Should().Be(id);
            addressableKeyExt.Should().Be(ext);
        }
示例#9
0
        public async Task GuidCompoundKeyGrain()
        {
            var id  = Guid.NewGuid();
            var ext = "Thing";

            var grain = await Silo.CreateGrainAsync <GuidCompoundKeyGrain>(id, ext);

            var key = await grain.GetKey();

            var keyGuid        = grain.GetPrimaryKey(out var keyExt);
            var referenceKey   = grain.GrainReference.GetPrimaryKey(out var referenceKeyExt);
            var addressableKey = ((IAddressable)grain).GetPrimaryKey(out var addressableKeyExt);

            key.Item1.Should().Be(id);
            key.Item2.Should().Be(ext);

            keyGuid.Should().Be(id);
            keyExt.Should().Be(ext);

            referenceKey.Should().Be(id);
            referenceKeyExt.Should().Be(ext);

            addressableKey.Should().Be(id);
            addressableKeyExt.Should().Be(ext);
        }
示例#10
0
        public async Task ResetColor_WithDefaultState_DoesNotClearState()
        {
            // Arrange
            var state = new ColorGrainState();

            var mockState = new Mock <IPersistentState <ColorGrainState> >();

            mockState.SetupGet(o => o.State).Returns(state);
            mockState.Setup(o => o.ClearStateAsync());

            var mockMapper = new Mock <IAttributeToFactoryMapper <PersistentStateAttribute> >();

            mockMapper.Setup(o => o.GetFactory(It.IsAny <ParameterInfo>(), It.IsAny <PersistentStateAttribute>())).Returns(context => mockState.Object);

            Silo.AddService(mockMapper.Object);

            var grain = await Silo.CreateGrainAsync <ColorGrain>(GrainId);

            // Act
            await grain.ResetColor();

            // Assert
            state.Color.Should().Be(Color.Unknown);
            state.Id.Should().Be(Guid.Empty);

            // Note that the following assert ties this test to the _implementation_ details. Generally, one should try
            // to avoid tying the test to the implementation details. It can lead to more brittle tests. However, one may
            // choose to accept this as a trade-off when the implementation detail represents an important behavior.
            mockState.Verify(o => o.ClearStateAsync(), Times.Never);
        }
示例#11
0
        public async Task GetColor_WithState_ReturnsColor()
        {
            // Arrange
            var state = new ColorGrainState
            {
                Color = Color.Red,
                Id    = GrainId,
            };

            var mockState = new Mock <IPersistentState <ColorGrainState> >();

            mockState.SetupGet(o => o.State).Returns(state);

            var mockMapper = new Mock <IAttributeToFactoryMapper <PersistentStateAttribute> >();

            mockMapper.Setup(o => o.GetFactory(It.IsAny <ParameterInfo>(), It.IsAny <PersistentStateAttribute>())).Returns(context => mockState.Object);

            Silo.AddService(mockMapper.Object);

            var grain = await Silo.CreateGrainAsync <ColorGrain>(GrainId);

            // Act
            var color = await grain.GetColor();

            // Assert
            color.Should().Be(Color.Red);
        }
示例#12
0
        public async Task SetColor_WithDefaultState_SetsState()
        {
            // Arrange
            var state = new ColorGrainState();

            var mockState = new Mock <IPersistentState <ColorGrainState> >();

            mockState.SetupGet(o => o.State).Returns(state);
            mockState.Setup(o => o.WriteStateAsync());

            var mockMapper = new Mock <IAttributeToFactoryMapper <PersistentStateAttribute> >();

            mockMapper.Setup(o => o.GetFactory(It.IsAny <ParameterInfo>(), It.IsAny <PersistentStateAttribute>())).Returns(context => mockState.Object);

            Silo.AddService(mockMapper.Object);

            var grain = await Silo.CreateGrainAsync <ColorGrain>(GrainId);

            // Act
            await grain.SetColor(Color.Blue);

            // Assert
            state.Color.Should().Be(Color.Blue);
            state.Id.Should().Be(GrainId);
        }
示例#13
0
        public async Task GrainIsSubscribed()
        {
            var stream = Silo.AddStreamProbe <ChatMessage>(Guid.Empty, null);

            await Silo.CreateGrainAsync <Listener>(1);

            stream.Subscribed.Should().Be(1);
        }
        public async Task ApiDataReaderGrainReturningValue()
        {
            var grain = await Silo.CreateGrainAsync <ApiDataReaderGrain>(_url);

            var result = await grain.GetResult();

            Assert.That(HttpResultValue, Is.EqualTo(result));
        }
示例#15
0
        public async Task ConsoleLog()
        {
            const string greeting = "Bonjour";

            IHello grain = await Silo.CreateGrainAsync <HelloGrain>(1);

            grain.Invoking(g => g.SayHello(greeting)).Should().NotThrow();
        }
示例#16
0
        public async Task XUnitLog()
        {
            const string greeting = "Bonjour";

            IHello grain = await Silo.CreateGrainAsync <HelloGrain>(2);

            await grain.Invoking(g => g.SayHello(greeting)).Should().NotThrowAsync();
        }
示例#17
0
        public async Task SecondGrainCreated()
        {
            await Silo.CreateGrainAsync <LifecycleGrain>(new Random().Next());

            Func <Task> creatingSecondGrainAsync = async() => await Silo.CreateGrainAsync <LifecycleGrain>(new Random().Next());

            creatingSecondGrainAsync.ShouldThrow <Exception>();
        }
示例#18
0
        public async Task ShouldThrowNotSupportedException()
        {
            // Arrange


            // Act and Assert
            await Assert.ThrowsAsync <NotSupportedException>(
                async() => await Silo.CreateGrainAsync <StatefulUnsupportedActivationGrain>(0));
        }
示例#19
0
        public async Task LazyStreamProvider()
        {
            var chatty = await Silo.CreateGrainAsync <Chatty>(4);

            const string msg = "Hello Chat";

            //Send a message without creating a stream probe
            chatty.Invoking(p => p.SendChat(msg).Wait()).Should().NotThrow();
        }
示例#20
0
        public async Task GrainReceives()
        {
            var stream = Silo.AddStreamProbe <ChatMessage>(Guid.Empty, null);

            var grain = await Silo.CreateGrainAsync <Listener>(1);

            await stream.OnNextAsync(new ChatMessage("Ding"));

            (await grain.ReceivedCount()).Should().Be(1);
        }
示例#21
0
        public async Task SetupCompoundProbe()
        {
            IPing grain = await Silo.CreateGrainAsync <PingGrain>(1);

            var pong = Silo.AddProbe <IPongCompound>(44, keyExtension: "Test");

            await grain.PingCompound();

            pong.Verify(p => p.Pong(), Times.Once);
        }
示例#22
0
        public async Task SetupProbe()
        {
            IPing grain = await Silo.CreateGrainAsync <PingGrain>(1);

            var pong = Silo.AddProbe <IPong>(22);

            await grain.Ping();

            pong.Verify(p => p.Pong(), Times.Once);
        }
示例#23
0
        public async Task IncorrectProbeNamespace()
        {
            var chatty = await Silo.CreateGrainAsync <Chatty>(4);

            Silo.AddStreamProbe <ChatMessage>(Guid.Empty, "Wrong");

            const string msg = "Hello Chat";

            chatty.Invoking(p => p.SendChat(msg).Wait()).ShouldNotThrow();
        }
示例#24
0
        public async Task WhenActivated_StoreStreamHandlerInState()
        {
            //Arrange + Act
            await Silo.CreateGrainAsync <PersistentListenerWithHandleInState>(1);

            //Assert
            Assert.Equal(1, _stream.Subscribed);
            Assert.NotNull(_stateWithHandle.ChatMessageStreamSubscriptionHandle);
            _persistentState.Verify(x => x.WriteStateAsync(), Times.Once);
        }
示例#25
0
        public async Task GuidKeyGrain()
        {
            var id = Guid.NewGuid();

            var grain = await Silo.CreateGrainAsync <GuidKeyGrain>(id);

            var key = await grain.GetKey();

            key.Should().Be(id);
        }
示例#26
0
        public async Task IntegerKeyGrain()
        {
            const int id = int.MaxValue;

            var grain = await Silo.CreateGrainAsync <IntegerKeyGrain>(id);

            var key = await grain.GetKey();

            key.Should().Be(id);
        }
示例#27
0
        public async Task GrainDeactivation()
        {
            var grain = await Silo.CreateGrainAsync <LifecycleGrain>(new Random().Next());

            grain.DeactivateCount.Should().Be(0);

            await Silo.DeactivateAsync(grain);

            grain.DeactivateCount.Should().Be(1);
        }
示例#28
0
        public async Task StatefulStringKeyGrain()
        {
            const string id = "TestId";

            var grain = await Silo.CreateGrainAsync <StatefulStringKeyGrain>(id);

            var key = await grain.GetKey();

            key.Should().Be(id);
        }
示例#29
0
        public async Task IncorrectProbeId()
        {
            var chatty = await Silo.CreateGrainAsync <Chatty>(4);

            Silo.AddStreamProbe <ChatMessage>(Guid.NewGuid(), null);

            const string msg = "Hello Chat";

            chatty.Invoking(p => p.SendChat(msg).Wait()).Should().NotThrow();
        }
示例#30
0
        public async Task InvalidProbeType()
        {
            IPing grain = await Silo.CreateGrainAsync <PingGrain>(1);

            //This correct id, but a different grain type
            var pong = Silo.AddProbe <IPong2>(22);

            grain.Invoking(p => p.Ping()).Should().NotThrow();

            pong.Verify(p => p.Pong2(), Times.Never);
        }