Exemple #1
0
        public async Task TestExecuteBatchLambda()
        {
            var l = Enumerable.Range(1, 10).Select(x => new DummyInt(x)).ToList();

            var container = GetRandomContainerGrain <DummyInt>();
            var consumer  = new MultiStreamListConsumer <ContainerHostedElement <DummyInt> >(_provider);
            await consumer.SetInput(new List <StreamIdentity <ContainerHostedElement <DummyInt> > >() { await container.GetStreamIdentity() });

            var reference = await container.AddRange(l);

            Assert.AreEqual(10, reference.Count);
            Assert.AreEqual(0, reference.First().Offset);
            Assert.AreEqual(container.GetPrimaryKey(), reference.First().ContainerId);

            await container.ExecuteSync(x => x.Value += 2);

            var tid = await container.EnumerateToStream();

            await consumer.TransactionComplete(tid);

            var expectedList = l.Select(x => x.Value + 2).ToList();
            var actualList   = consumer.Items.Select(e => e.Item.Value).ToList();

            CollectionAssert.AreEquivalent(expectedList, actualList);
        }
Exemple #2
0
        public async Task TestExecuteOnItemReference()
        {
            var item = new DummyInt(5);

            var container = GetRandomContainerGrain <DummyInt>();
            var consumer  = new MultiStreamListConsumer <ContainerHostedElement <DummyInt> >(_provider);
            await consumer.SetInput(new List <StreamIdentity <ContainerHostedElement <DummyInt> > >() { await container.GetStreamIdentity() });

            var reference = (await container.AddRange(new List <DummyInt>()
            {
                item
            })).First();

            Assert.IsNotNull(reference);
            Assert.AreEqual(0, reference.Offset);
            Assert.AreEqual(container.GetPrimaryKey(), reference.ContainerId);

            await container.ExecuteSync(i => { i.Value += 2; }, reference);

            var tid = await container.EnumerateToStream();

            await consumer.TransactionComplete(tid);


            Assert.AreEqual(7, consumer.Items.First().Item.Value);
        }
        public async Task ExecuteLambdaIncrement()
        {
            var distributedCollection = GetRandomDistributedCollection <DummyInt>();

            var l = (new List <int>()
            {
                int.MinValue, 0, 231, -1, 23
            }).Select(x => new DummyInt(x)).ToList();
            await distributedCollection.BatchAdd(l);

            Assert.AreEqual(await distributedCollection.Count(), l.Count);

            await distributedCollection.ExecuteSync(x => { x.Value += 232; });

            var listConsumer = new MultiStreamListConsumer <ContainerHostedElement <DummyInt> >(_provider);
            await listConsumer.SetInput(await distributedCollection.GetStreamIdentities());

            var tid = await distributedCollection.EnumerateToStream();

            await listConsumer.TransactionComplete(tid);

            Assert.AreEqual(l.Count, listConsumer.Items.Count);
            var expectedList = l.Select(x => x.Value + 232).ToList();
            var actualList   = listConsumer.Items.Select(x => x.Item.Value).ToList();

            CollectionAssert.AreEquivalent(expectedList, actualList);
        }
        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);
        }
Exemple #5
0
        public async Task TestExecuteSync()
        {
            var l = Enumerable.Range(1, 10).Select(x => new DummyInt(x)).ToList();

            var container = GetRandomContainerGrain <DummyInt>();
            var consumer  = new MultiStreamListConsumer <ContainerHostedElement <DummyInt> >(_provider);
            await consumer.SetInput(new List <StreamIdentity <ContainerHostedElement <DummyInt> > >() { await container.GetStreamIdentity() });

            var references = await container.AddRange(l);

            // Action
            await container.ExecuteSync(x => { x.Value += 2; });

            await container.EnumerateToStream();

            l.ForEach(x => x.Value += 2);
            CollectionAssert.AreEquivalent(l.Select(x => x.Value).ToList(), consumer.Items.Select(x => x.Item.Value).ToList());
            consumer.Items.Clear();

            // Action with state
            await container.ExecuteSync((x, s) => { x.Value += (int)s; }, 2);

            await container.EnumerateToStream();

            l.ForEach(x => x.Value += 2);
            CollectionAssert.AreEquivalent(l.Select(x => x.Value).ToList(), consumer.Items.Select(x => x.Item.Value).ToList());
            consumer.Items.Clear();

            // Func
            var resultSyncFunc = await container.ExecuteSync(x => x.Value += 2);

            l.ForEach(x => x.Value += 2);
            CollectionAssert.AreEquivalent(l.Select(x => x.Value).ToList(), resultSyncFunc.ToList());

            // Func with State
            var resultSyncFuncState = await container.ExecuteSync((x, s) => x.Value += (int)s, 2);

            l.ForEach(x => x.Value += 2);
            CollectionAssert.AreEquivalent(l.Select(x => x.Value).ToList(), resultSyncFuncState.ToList());


            var ref1 = references.First();
            // Func with reference
            var resultSyncFuncWithReference = await container.ExecuteSync(x => x.Value += 2, ref1);

            l.First().Value += 2;
            Assert.AreEqual(resultSyncFuncWithReference, l.First().Value);

            // Func with reference and state
            var resultAsyncFuncStateWithReference = await container.ExecuteSync((x, s) => x.Value += (int)s, 2, ref1);

            l.First().Value += 2;
            Assert.AreEqual(resultAsyncFuncStateWithReference, l.First().Value);
        }
Exemple #6
0
        public async Task TestNewContainerWriterReturnsNothing()
        {
            var container = GetRandomContainerGrain <int>();
            var consumer  = new MultiStreamListConsumer <ContainerHostedElement <int> >(_provider);
            await consumer.SetInput(new List <StreamIdentity <ContainerHostedElement <int> > >() { await container.GetStreamIdentity() });;
            var tid = await container.EnumerateToStream();

            await consumer.TransactionComplete(tid);

            Assert.AreEqual(0, consumer.Items.Count);
        }
        public async Task TestObservingAdd()
        {
            var collection    = GrainClient.GrainFactory.GetGrain <IObservableContainerGrain <DummyInt> >(Guid.NewGuid());
            int numContainers = 2;
            await collection.SetNumberOfNodes(numContainers);

            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);

            CollectionAssert.AreEquivalent(inputList.Select(x => x.Value).ToList(), observedCollectionConsumer.Items.Select(x => x.Item.Value).ToList());
        }
Exemple #8
0
        public async Task TestObserveAddItemsToContainer()
        {
            var l = Enumerable.Range(1, 1).ToList();

            var container = GetRandomObservableContainerGrain <int>();

            var resultConsumer = new MultiStreamListConsumer <ContainerHostedElement <int> >(_provider);
            await resultConsumer.SetInput(new List <StreamIdentity <ContainerHostedElement <int> > >() { await container.GetStreamIdentity() });

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

            await container.AddRange(l);

            Assert.AreEqual(l.Count, resultConsumer.Items.Count);
        }
Exemple #9
0
        public async Task TestAddItemsToContainer()
        {
            var l = Enumerable.Range(1, 10).ToList();

            var container = GetRandomContainerGrain <int>();
            var consumer  = new MultiStreamListConsumer <ContainerHostedElement <int> >(_provider);
            await consumer.SetInput(new List <StreamIdentity <ContainerHostedElement <int> > >() { await container.GetStreamIdentity() });

            var reference = await container.AddRange(l);

            Assert.AreEqual(10, reference.Count);
            Assert.AreEqual(0, reference.First().Offset);
            Assert.AreEqual(container.GetPrimaryKey(), reference.First().ContainerId);

            var tid = await container.EnumerateToStream();

            await consumer.TransactionComplete(tid);

            CollectionAssert.AreEquivalent(l, consumer.Items.Select(x => x.Item).ToList());
        }
        public async Task TestAdd()
        {
            var distributedCollection = GetRandomDistributedCollection <int>();
            await distributedCollection.SetNumberOfNodes(4);

            var l = Enumerable.Range(0, 20000).ToList();

            var references = await distributedCollection.BatchAdd(l);

            CollectionAssert.AllItemsAreNotNull(references);
            // TODO reference sanity check: Should range form 0 to 20000

            var consumer = new MultiStreamListConsumer <ContainerHostedElement <int> >(_provider);
            await consumer.SetInput(await distributedCollection.GetStreamIdentities());

            var tid = await distributedCollection.EnumerateToStream();

            await consumer.TransactionComplete(tid);

            CollectionAssert.AreEquivalent(l, consumer.Items.Select(x => x.Item).ToList());
        }
        public async Task TestObservingDelete()
        {
            var collection    = GrainClient.GrainFactory.GetGrain <IObservableContainerGrain <DummyInt> >(Guid.NewGuid());
            int numContainers = 2;
            await collection.SetNumberOfNodes(numContainers);

            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();
            var references = await collection.BatchAdd(inputList);

            observedCollectionConsumer.Items.Clear();
            var value = (DummyInt)await collection.ExecuteSync(x => x, references.First());

            await collection.Remove(references.First());

            Assert.AreEqual(1, observedCollectionConsumer.Items.Count);
            var receivedItem = observedCollectionConsumer.Items.First();

            Assert.IsFalse(receivedItem.Reference.Exists);
            Assert.AreEqual(value, receivedItem.Item);
        }
 private async Task SubscribeConsumer(IStreamProcessorSelectNodeGrain <int, int> processor, MultiStreamListConsumer <int> testConsumer)
 {
     await testConsumer.SetInput(new List <StreamIdentity <int> >() { await processor.GetStreamIdentity() });
 }