コード例 #1
0
        public void ParallelBuilding()
        {
            var functionBasedOBP = new ObjectBuilders.Providers.FunctionBasedObjectBuilderProvider <Tuple <string, int> >(
                (tuple) =>
            {
                switch (tuple.Item1)
                {
                case "Data":
                    return(new ObjectBuilders.FixedObjectBuilder <Tuple <string, int> >(0));

                case "Logic":
                    return(new ObjectBuilders.FunctionBasedObjectBuilder <Tuple <string, int> >(
                               (builderContext, address) =>
                    {
                        var dependencySet = new ObjectBuilders.DependencySet <Tuple <string, int> >();
                        dependencySet.AddDependency("dep" + address.Item2, Tuple.Create("Data", address.Item2));
                        return dependencySet;
                    },
                               (builderContext, address, dependencies) => null,
                               (builderContext, address, dependencies) =>
                    {
                        var startTime = DateTime.UtcNow;
                        double val = 1;
                        for (int i = 0; i < 100000000; ++i)
                        {
                            val += Math.Sqrt(i);
                        }

                        var timeSpan = DateTime.UtcNow - startTime;
                        return timeSpan;
                    }));

                default:
                    throw new NotSupportedException();
                }
            });

            var context     = new ObjectContext <Tuple <string, int> >(null, functionBasedOBP, _taskRunner);
            var infoObjects = new List <IObjectBuildingInfo <Tuple <string, int> > >(300);

            for (int idx = 0; idx < 16; idx++)
            {
                var address = Tuple.Create("Logic", idx);
                infoObjects.Add(context.BuildObject(address));
            }

            foreach (var infoObj in infoObjects)
            {
                infoObj.ObjectBuiltOrFailureWaitHandle.WaitOne();
                Assert.AreEqual(ObjectBuildingStates.ObjectBuilt, infoObj.ObjectBuildingState);
            }
        }
        public void CheckProviderBehaviour()
        {
            var provider = new FunctionBasedObjectBuilderProvider <int>(
                (address) =>
            {
                switch (address)
                {
                case 0:
                    return(new FixedObjectBuilder <int>(0));

                default:
                    return(null);
                }
            });

            Assert.IsTrue(provider.TryGetObjectBuilder(0, out var objectBuilder));
            Assert.IsNotNull(objectBuilder);

            Assert.IsFalse(provider.TryGetObjectBuilder(1, out objectBuilder));
        }