Exemplo n.º 1
0
        public async Task TestTearDownStreamBroken()
        {
            var source = new MultiStreamProvider <int>(_provider, 2);

            var query = await source.Select(x => x, GrainClient.GrainFactory);

            var queryOutputStreams = await query.GetStreamIdentities();

            var resultConsumer = new TestTransactionalStreamConsumerAggregate <int>(_provider);
            await resultConsumer.SetInput(queryOutputStreams);

            Assert.AreEqual(2, queryOutputStreams.Count);
            Assert.AreEqual(0, resultConsumer.Items.Count);

            Assert.IsFalse(await resultConsumer.AllConsumersTearDownCalled());

            await query.TearDown();

            await resultConsumer.TearDown();

            Assert.IsTrue(await resultConsumer.AllConsumersTearDownCalled());

            await source.SendItems(new List <int>() { 2, 3 });

            Assert.AreEqual(0, resultConsumer.Items.Count);
            Assert.IsTrue(await resultConsumer.AllConsumersTearDownCalled());
        }
        public async Task TestItemAggregation()
        {
            var aggregate = GrainClient.GrainFactory.GetGrain <IStreamProcessorSelectAggregate <int, int> >(Guid.NewGuid());
            await aggregate.SetFunction(_ => _);

            var itemsToSend = new List <int> {
                1, 5, 32, -12
            };

            var provider       = GrainClient.GetStreamProvider(StreamProvider);
            var inputAggregate = new MultiStreamProvider <int>(provider, 2);

            await aggregate.SetInput(await inputAggregate.GetStreamIdentities());

            Assert.AreEqual(2, (await aggregate.GetStreamIdentities()).Count);

            var consumerAggregate = new TestTransactionalStreamConsumerAggregate <int>(provider);
            await consumerAggregate.SetInput(await aggregate.GetStreamIdentities());

            var tid = await inputAggregate.SendItems(itemsToSend);

            var waitForTransaction = consumerAggregate.TransactionComplete(tid);

            var resultItems = consumerAggregate.Items;

            Assert.AreEqual(4, resultItems.Count);
            CollectionAssert.AreEquivalent(itemsToSend, resultItems);

            Assert.IsFalse(await consumerAggregate.AllConsumersTearDownCalled());
            await inputAggregate.TearDown();

            Assert.IsTrue(await consumerAggregate.AllConsumersTearDownCalled());
        }
        public async Task Execute()
        {
            var provider = GetStreamProvider("CollectionStreamProvider");

            // Simple collection sample
            var container = GrainClient.GrainFactory.GetGrain <IContainerGrain <DummyInt> >(Guid.NewGuid());
            await container.SetNumberOfNodes(4);

            await container.BatchAdd(Enumerable.Range(0, 10).Select(x => new DummyInt(x)).ToList());

            await container.ExecuteSync((i) => { i.Value += 2; }); // Pass action to container

            var consumer = new MultiStreamListConsumer <ContainerHostedElement <DummyInt> >(provider);
            await consumer.SetInput(await container.GetStreamIdentities());

            var transactionId = await container.EnumerateToStream();

            await consumer.TransactionComplete(transactionId);

            Console.WriteLine(consumer.Items);


            // Sample with observable collection and data query.
            var collection    = GrainClient.GrainFactory.GetGrain <IObservableContainerGrain <DummyInt> >(Guid.NewGuid());
            int numContainers = 4;
            await collection.SetNumberOfNodes(numContainers);

            var query = await collection.Select(x => x.Item.Value, GrainClient.GrainFactory).Where(x => x > 500);

            var matchingItemConsumer = new MultiStreamListConsumer <int>(provider);
            await matchingItemConsumer.SetInput(await query.GetStreamIdentities());

            var observedCollectionConsumer = new MultiStreamListConsumer <ContainerHostedElement <DummyInt> >(provider);
            await observedCollectionConsumer.SetInput(await collection.GetStreamIdentities());

            var inputList = Enumerable.Range(0, 1000).Select(x => new DummyInt(x)).ToList();
            await collection.BatchAdd(inputList);

            Console.WriteLine("#Items: {0}", observedCollectionConsumer.Items.Count);
            Console.WriteLine("#Items resultQuery: {0}", matchingItemConsumer.Items.Count);

            // Simple query using stream provider and consumer.

            var simpleProvider = new MultiStreamProvider <int>(provider, numberOutputStreams: 10);

            var queryNumbersLessThan1000 = await simpleProvider.Where(x => x < 1000, GrainClient.GrainFactory);

            var simpleResultConsumer = new MultiStreamListConsumer <int>(provider);
            await simpleResultConsumer.SetInput(await queryNumbersLessThan1000.GetStreamIdentities());

            var rand         = new Random(123);
            var transaction1 = await simpleProvider.SendItems(Enumerable.Repeat(2000, 10000).Select(x => rand.Next(x)).ToList());

            await simpleResultConsumer.TransactionComplete(transaction1);

            Console.WriteLine("#Items less than 1000: {0}", simpleResultConsumer.Items.Count);
        }
        public async Task TestAggregateCleanupSuccessful()
        {
            var aggregate = GrainClient.GrainFactory.GetGrain <IStreamProcessorSelectAggregate <int, int> >(Guid.NewGuid());
            await aggregate.SetFunction(_ => _);

            var itemsToSend = new List <int> {
                1, 5, 32, -12
            };

            var provider       = GrainClient.GetStreamProvider(StreamProvider);
            var inputAggregate = new MultiStreamProvider <int>(provider, 2);;

            await aggregate.SetInput(await inputAggregate.GetStreamIdentities());

            var streamIdentitiesProcessor = await aggregate.GetStreamIdentities();

            Assert.AreEqual(2, (await aggregate.GetStreamIdentities()).Count);

            var consumerAggregate = new TestTransactionalStreamConsumerAggregate <int>(provider);
            await consumerAggregate.SetInput(await aggregate.GetStreamIdentities());

            var subscriptionHdl1 = await GetStreamSubscriptionHandles <IStreamMessage>(streamIdentitiesProcessor[0].StreamIdentifier);

            var subscriptionHdl2 = await GetStreamSubscriptionHandles <IStreamMessage>(streamIdentitiesProcessor[1].StreamIdentifier);

            Assert.AreEqual(1, subscriptionHdl1.Count);
            Assert.AreEqual(1, subscriptionHdl2.Count);

            await inputAggregate.TearDown();

            var tid = await inputAggregate.SendItems(itemsToSend);

            var taskCompleted = consumerAggregate.TransactionComplete(tid).Wait(TimeSpan.FromSeconds(5));

            Assert.IsFalse(taskCompleted);

            subscriptionHdl1 = await GetStreamSubscriptionHandles <IStreamMessage>(streamIdentitiesProcessor[0].StreamIdentifier);

            subscriptionHdl2 = await GetStreamSubscriptionHandles <IStreamMessage>(streamIdentitiesProcessor[1].StreamIdentifier);

            Assert.AreEqual(0, subscriptionHdl1.Count);
            Assert.AreEqual(0, subscriptionHdl2.Count);
        }
Exemplo n.º 5
0
        private async Task TestMultiLevelDataPass <TIn, TOut>(
            Func <MultiStreamProvider <TIn>, IStreamProcessorAggregateFactory, Task <IStreamProcessorChain <TOut> > >
            createStreamProcessingChainFunc, List <List <TIn> > inputChunks, List <List <TOut> > outputChunks,
            Action <List <TOut>, List <TOut> > resultAssertion)
        {
            if (inputChunks.Count != outputChunks.Count)
            {
                throw new ArgumentException();
            }

            var source = new MultiStreamProvider <TIn>(_provider, 2);

            var query = await createStreamProcessingChainFunc(source, new DefaultStreamProcessorAggregateFactory(GrainFactory));

            var queryOutputStreams = await query.GetStreamIdentities();

            var resultConsumer = new TestTransactionalStreamConsumerAggregate <TOut>(_provider);
            await resultConsumer.SetInput(queryOutputStreams);

            Assert.AreEqual(2, queryOutputStreams.Count);
            Assert.AreEqual(0, resultConsumer.Items.Count);

            for (int i = 0; i < inputChunks.Count; i++)
            {
                var input          = inputChunks[i];
                var expectedOutput = outputChunks[i];
                await source.SendItems(input);

                resultAssertion(expectedOutput, resultConsumer.Items);
                resultConsumer.Items.Clear();
            }

            await query.TearDown();

            await resultConsumer.TearDown();
        }