Example #1
0
        public void TestMultipleConstructor()
        {
            ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder();

            cb.BindNamedParameter <MultiConstructorTest.NamedString, string>(GenericType <MultiConstructorTest.NamedString> .Class, "foo");
            cb.BindNamedParameter <MultiConstructorTest.NamedInt, int>(GenericType <MultiConstructorTest.NamedInt> .Class, "8");
            cb.BindNamedParameter <MultiConstructorTest.NamedBool, bool>(GenericType <MultiConstructorTest.NamedBool> .Class, "true");
            IInjector i = TangFactory.GetTang().NewInjector(cb.Build());
            var       o = i.GetInstance <MultiConstructorTest>();

            o.Verify("foo", 8, true);
        }
Example #2
0
        public void TestSimpleConstructor()
        {
            Type simpleConstructorType = typeof(Com.Microsoft.Tang.Examples.SimpleConstructors);

            ITang tang = TangFactory.GetTang();
            ICsConfigurationBuilder cb   = tang.NewConfigurationBuilder(new string[] { file });
            IConfiguration          conf = cb.Build();
            IInjector injector           = tang.NewInjector(conf);
            var       simpleConstructor  = (Com.Microsoft.Tang.Examples.SimpleConstructors)injector.GetInstance(simpleConstructorType);

            Assert.IsNotNull(simpleConstructor);
        }
Example #3
0
        public void TestGenericEventHandlers()
        {
            ICsConfigurationBuilder cba = TangFactory.GetTang().NewConfigurationBuilder();

            cba.BindNamedParameter <ABCName.XName, ABCName.XXBB, ABCName.X <ABCName.BB> >(GenericType <ABCName.XName> .Class, GenericType <ABCName.XXBB> .Class);
            TangFactory.GetTang().NewInjector(cba.Build()).GetNamedInstance(typeof(ABCName.XName));

            ICsConfigurationBuilder cbb = TangFactory.GetTang().NewConfigurationBuilder();

            cbb.BindNamedParameter <ABCName.XName, ABCName.XBB, ABCName.X <ABCName.BB> >(GenericType <ABCName.XName> .Class, GenericType <ABCName.XBB> .Class);
            TangFactory.GetTang().NewInjector(cbb.Build()).GetNamedInstance(typeof(ABCName.XName));
        }
Example #4
0
        public void TestSingletonWithMoreSpecificConstructors()
        {
            ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder();

            cb.BindImplementation(GenericType <SMC> .Class, GenericType <SingletonMultiConst> .Class);
            cb.BindNamedParameter <SingletonMultiConst.A, string>(GenericType <SingletonMultiConst.A> .Class, "foo");
            cb.BindNamedParameter <SingletonMultiConst.B, string>(GenericType <SingletonMultiConst.B> .Class, "bar");
            IInjector i = TangFactory.GetTang().NewInjector(cb.Build());
            var       o = i.GetInstance <SMC>();

            Assert.IsNotNull(o);
        }
Example #5
0
        public void TestStreamActivity2()
        {
            Type activityType = typeof(StreamTask2);

            ITang tang = TangFactory.GetTang();
            ICsConfigurationBuilder cb   = tang.NewConfigurationBuilder(new string[] { FileNames.Tasks, FileNames.Common });
            IConfiguration          conf = cb.Build();
            IInjector injector           = tang.NewInjector(conf);
            var       activityRef        = (ITask)injector.GetInstance(activityType);

            Assert.NotNull(activityRef);
        }
Example #6
0
        public IInjector NewInjector(string[] assemblies, IList <KeyValuePair <string, string> > configurations)
        {
            ITang tang = TangFactory.GetTang();
            ICsConfigurationBuilder cb1 = tang.NewConfigurationBuilder(assemblies);

            ConfigurationFile.ProcessConfigData(cb1, configurations);
            IConfiguration conf = cb1.Build();

            IInjector injector = tang.NewInjector(conf);

            return(injector);
        }
Example #7
0
        public IInjector NewInjector(string[] assemblies, string configurationFileName)
        {
            ITang tang = TangFactory.GetTang();
            ICsConfigurationBuilder cb1 = tang.NewConfigurationBuilder(assemblies);

            ConfigurationFile.AddConfigurationFromFile(cb1, configurationFileName);
            IConfiguration conf = cb1.Build();

            IInjector injector = tang.NewInjector(conf);

            return(injector);
        }
Example #8
0
        public void TestByteArrayParameter()
        {
            ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder();
            string input = "abcde";

            cb.BindNamedParameter <ByteArrayTest.NamedByteArray, byte[]>(GenericType <ByteArrayTest.NamedByteArray> .Class, input);
            IInjector i = TangFactory.GetTang().NewInjector(cb.Build());
            var       o = i.GetInstance <ByteArrayTest>();

            byte[] bytes = new byte[input.Length * sizeof(char)];
            System.Buffer.BlockCopy(input.ToCharArray(), 0, bytes, 0, bytes.Length);
            Assert.True(o.Verify(bytes));
        }
Example #9
0
        public void TestDocumentLoadNamedParameter()
        {
            Type  documentedLocalNamedParameterType = typeof(DocumentedLocalNamedParameter);
            ITang tang = TangFactory.GetTang();
            ICsConfigurationBuilder cb = tang.NewConfigurationBuilder(new string[] { FileNames.Examples });

            cb.BindNamedParameter <DocumentedLocalNamedParameter.Foo, string>(GenericType <DocumentedLocalNamedParameter.Foo> .Class, "Hello");
            IConfiguration conf     = cb.Build();
            IInjector      injector = tang.NewInjector(conf);
            var            doc      = (DocumentedLocalNamedParameter)injector.GetInstance(documentedLocalNamedParameterType);

            Assert.IsNotNull(doc);
        }
        public void TestExternalLegacyConstructor()
        {
            ICsConfigurationBuilder cb = tang.NewConfigurationBuilder();

            cb.BindConstructor(GenericType <ExternalConstructorExample.Legacy> .Class, GenericType <ExternalConstructorExample.LegacyWrapper> .Class);
            IInjector i = tang.NewInjector(cb.Build());

            i.BindVolatileInstance(GenericType <int> .Class, 42);
            i.BindVolatileInstance(GenericType <string> .Class, "The meaning of life is ");
            ExternalConstructorExample.Legacy l = i.GetInstance <ExternalConstructorExample.Legacy>();
            Assert.AreEqual(42, l.X);
            Assert.AreEqual("The meaning of life is ", l.Y);
        }
Example #11
0
        public void TestExternalObject()
        {
            ITang tang = TangFactory.GetTang();
            ICsConfigurationBuilder cb = tang.NewConfigurationBuilder();

            IInjector injector = tang.NewInjector(cb.Build());

            // bind an object to the injetor so that Tang will get this instance from cache directly instead of inject it when injecting ClassWithExternalObject
            injector.BindVolatileInstance(GenericType <ExternalClass> .Class, new ExternalClass());
            ClassWithExternalObject o = injector.GetInstance <ClassWithExternalObject>();

            Assert.IsNotNull(o.ExternalObject is ExternalClass);
        }
Example #12
0
        public void OnNext(IActiveContext activeContext)
        {
            ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder();

            cb.AddConfiguration(TaskConfiguration.ConfigurationModule
                                .Set(TaskConfiguration.Identifier, "bridgeCLRShellTask_" + DateTime.Now.Ticks)
                                .Set(TaskConfiguration.Task, GenericType <ShellTask> .Class)
                                .Build());
            cb.BindNamedParameter <ShellTask.Command, string>(GenericType <ShellTask.Command> .Class, "echo");

            IConfiguration taskConfiguration = cb.Build();

            activeContext.SubmitTask(taskConfiguration);
        }
Example #13
0
        public void TestSetOfTimeshift()
        {
            ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder();

            cb.BindSetEntry <SetOfTimeshifts, Timeshift, ITimeshift>(GenericType <SetOfTimeshifts> .Class, GenericType <Timeshift> .Class)
            .BindNamedParameter <Timeshift.TimeshiftLinkId, string>(GenericType <Timeshift.TimeshiftLinkId> .Class, "123")
            .BindNamedParameter <Timeshift.TimeshiftInTicks, long>(GenericType <Timeshift.TimeshiftInTicks> .Class, "10");

            IInjector i = TangFactory.GetTang().NewInjector(cb.Build());

            ISet <ITimeshift> actual = i.GetInstance <SetofTimeShiftClass>().Timeshifts;

            Assert.True(actual.Count == 1);
        }
Example #14
0
 public void TestSerirializeInjectionPlanForTimer()
 {
     Type timerType = typeof(Timer);
     ITang tang = TangFactory.GetTang();
     ICsConfigurationBuilder cb = tang.NewConfigurationBuilder(new string[] { FileNames.Examples });
     cb.BindNamedParameter<Timer.Seconds, Int32>(GenericType < Timer.Seconds>.Class, "2");
     IConfiguration conf = cb.Build();
     IInjector injector = tang.NewInjector(conf);
     Org.Apache.REEF.Tang.Implementations.InjectionPlan.InjectionPlan ip = injector.GetInjectionPlan(timerType);
     ProtocolBufferInjectionPlan.Serialize("timerplan.bin", ip);
     var ch = conf.GetClassHierarchy();
     var ip1 = ProtocolBufferInjectionPlan.DeSerialize("timerplan.bin", ch);
     Assert.IsNotNull(ip1);
 }
Example #15
0
        public void TestForkWorks()
        {
            Type checkChildIfaceType   = typeof(CheckChildIface);
            ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(new string[] { FileNames.Examples });

            cb.BindImplementation(GenericType <CheckChildIface> .Class, GenericType <CheckChildImpl> .Class);

            IInjector       i  = TangFactory.GetTang().NewInjector(cb.Build());
            IInjector       i1 = i.ForkInjector();
            CheckChildIface c1 = (CheckChildIface)i1.GetInstance(checkChildIfaceType);
            IInjector       i2 = i.ForkInjector();
            CheckChildIface c2 = (CheckChildIface)i2.GetInstance(checkChildIfaceType);

            Assert.AreNotEqual(c1, c2);
        }
Example #16
0
        public void TestNestedClass()
        {
            ITang tang = TangFactory.GetTang();
            ICsConfigurationBuilder cb = tang.NewConfigurationBuilder();

            IConfiguration conf = cb
                                  .BindNamedParameter <ClassParameter.Named1, int>(GenericType <ClassParameter.Named1> .Class, "5")
                                  .BindNamedParameter <ClassParameter.Named2, string>(GenericType <ClassParameter.Named2> .Class, "hello")
                                  .Build();

            IInjector           injector = tang.NewInjector(conf);
            ClassHasNestedClass h        = injector.GetInstance <ClassHasNestedClass>();

            Assert.IsNotNull(h);
        }
Example #17
0
        public void TestTimer()
        {
            Type  timerType            = typeof(Timer);
            ITang tang                 = TangFactory.GetTang();
            ICsConfigurationBuilder cb = tang.NewConfigurationBuilder(new string[] { FileNames.Examples });

            cb.BindNamedParameter <Timer.Seconds, int>(GenericType <Timer.Seconds> .Class, "2");
            IConfiguration conf     = cb.Build();
            IInjector      injector = tang.NewInjector(conf);
            var            timer    = (Timer)injector.GetInstance(timerType);

            Assert.IsNotNull(timer);

            timer.sleep();
        }
Example #18
0
        public void TestStreamActivity2()
        {
            var  a            = Assembly.Load(@"com.microsoft.reef.activity");
            Type activityType = a.GetType("com.microsoft.reef.activity.StreamActivity2");

            ITang tang = TangFactory.GetTang();
            ICsConfigurationBuilder cb   = tang.NewConfigurationBuilder(new string[] { @"com.microsoft.reef.activity", @"com.microsoft.reef.ActivityInterface" });
            IConfiguration          conf = cb.Build();
            IInjector injector           = tang.NewInjector(conf);
            var       activityRef        = (com.microsoft.reef.activity.IActivity)injector.GetInstance(activityType);

            Assert.IsNotNull(activityRef);

            //activityRef.Call(null);
        }
Example #19
0
        public void TestSerirializeInjectionPlanForTimer()
        {
            Type timerType      = typeof(Com.Microsoft.Tang.Examples.Timer);
            Type namedParameter = asm.GetType(@"Com.Microsoft.Tang.Examples.Timer+Seconds");

            ITang tang = TangFactory.GetTang();
            ICsConfigurationBuilder cb = tang.NewConfigurationBuilder(new string[] { file });

            cb.BindNamedParameter(namedParameter, "2");
            IConfiguration conf     = cb.Build();
            IInjector      injector = tang.NewInjector(conf);
            InjectionPlan  ip       = injector.GetInjectionPlan(timerType);

            ProtocolBufferInjectionPlan.Serialize("timerplan.bin", ip);
        }
Example #20
0
        public void TestSerirializeInjectionPlanForSimpleConstructor()
        {
            Type simpleConstructorType = typeof(SimpleConstructors);

            ITang tang = TangFactory.GetTang();
            ICsConfigurationBuilder cb = tang.NewConfigurationBuilder(new string[] { FileNames.Examples });
            IConfiguration conf = cb.Build();
            IInjector injector = tang.NewInjector(conf);
            Org.Apache.REEF.Tang.Implementations.InjectionPlan.InjectionPlan ip = injector.GetInjectionPlan(simpleConstructorType);

            ProtocolBufferInjectionPlan.Serialize("plan.bin", ip);
            var ch = conf.GetClassHierarchy();
            var ipRecovered = ProtocolBufferInjectionPlan.DeSerialize("plan.bin", ch);
            Assert.IsNotNull(ipRecovered);
        }
Example #21
0
        public void CanInjectAndExecuteTask()
        {
            //To enforce that shell task dll be copied to output directory.
            ShellTask tmpTask = new ShellTask("invalid");

            Assert.IsNotNull(tmpTask);

            string tmp = Directory.GetCurrentDirectory();

            Assert.IsNotNull(tmp);

            AvroConfigurationSerializer serializer        = new AvroConfigurationSerializer();
            AvroConfiguration           avroConfiguration = serializer.AvroDeseriaizeFromFile("evaluator.conf");

            Assert.IsNotNull(avroConfiguration);

            ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder();

            cb.AddConfiguration(TaskConfiguration.ConfigurationModule
                                .Set(TaskConfiguration.Identifier, "Test_CLRContext_task")
                                .Set(TaskConfiguration.Task, GenericType <ShellTask> .Class)
                                .Build());
            cb.BindNamedParameter <ShellTask.Command, string>(GenericType <ShellTask.Command> .Class, "dir");

            IConfiguration taskConfiguration = cb.Build();

            string taskConfig = serializer.ToString(taskConfiguration);

            ITask             task   = null;
            TaskConfiguration config = new TaskConfiguration(taskConfig);

            Assert.IsNotNull(config);
            try
            {
                IInjector injector = TangFactory.GetTang().NewInjector(config.TangConfig);
                task = (ITask)injector.GetInstance(typeof(ITask));
            }
            catch (Exception e)
            {
                throw new InvalidOperationException("unable to inject task with configuration: " + taskConfig, e);
            }

            byte[] bytes  = task.Call(null);
            string result = System.Text.Encoding.Default.GetString(bytes);

            //a dir command is executed in the container directory, which includes the file "evaluator.conf"
            Assert.IsTrue(result.Contains("evaluator.conf"));
        }
Example #22
0
        public void TestSetOfClassBound()
        {
            ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder();

            cb.BindSetEntry <SetOfClasses, Integer1, INumber>(GenericType <SetOfClasses> .Class, GenericType <Integer1> .Class) // bind an impl to the interface of the set
            .BindNamedParameter <Integer1.NamedInt, int>(GenericType <Integer1.NamedInt> .Class, "4");                          // bind parameter for the impl

            IInjector i = TangFactory.GetTang().NewInjector(cb.Build());

            ISet <INumber> actual   = i.GetInstance <Pool>().Numbers;
            ISet <INumber> expected = new HashSet <INumber>();

            expected.Add(new Integer1(4));

            Assert.True(Utilities.Utilities.Equals <INumber>(actual, expected));
        }
Example #23
0
        public void TestBindNamedParameter1()
        {
            ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder();

            cb.BindNamedParameter <AImplName, Aimpl, INamedImplA>();
            cb.BindNamedParameter <BImplName, Bimpl, INamedImplA>();

            IInjector i  = TangFactory.GetTang().NewInjector(cb.Build());
            Aimpl     a1 = (Aimpl)i.GetNamedInstance <AImplName, INamedImplA>(GenericType <AImplName> .Class);
            Aimpl     a2 = (Aimpl)i.GetNamedInstance <AImplName, INamedImplA>(GenericType <AImplName> .Class);
            Bimpl     b1 = (Bimpl)i.GetNamedInstance <BImplName, INamedImplA>(GenericType <BImplName> .Class);
            Bimpl     b2 = (Bimpl)i.GetNamedInstance <BImplName, INamedImplA>(GenericType <BImplName> .Class);

            Assert.Same(a1, a2);
            Assert.Same(b1, b2);
        }
Example #24
0
        public void TestDocumentLoadNamedParameter()
        {
            Type documentedLocalNamedParameterType = typeof(Com.Microsoft.Tang.Examples.DocumentedLocalNamedParameter);
            Type namedParameter = asm.GetType(@"Com.Microsoft.Tang.Examples.DocumentedLocalNamedParameter+Foo");

            ITang tang = TangFactory.GetTang();
            ICsConfigurationBuilder cb = tang.NewConfigurationBuilder(new string[] { file });

            cb.BindNamedParameter(namedParameter, "Hello");
            IConfiguration conf     = cb.Build();
            IInjector      injector = tang.NewInjector(conf);
            var            doc      = (Com.Microsoft.Tang.Examples.DocumentedLocalNamedParameter)injector.GetInstance(documentedLocalNamedParameterType);

            Assert.IsNotNull(doc);
            var s = doc.ToString();
        }
Example #25
0
        public void TestHelloStreamingActivityWithBinding()
        {
            Type activityInterfaceType = typeof(ITask);

            ITang tang = TangFactory.GetTang();
            ICsConfigurationBuilder cb = tang.NewConfigurationBuilder(new string[] { FileNames.Tasks, FileNames.Common });

            cb.BindImplementation(GenericType <ITask> .Class, GenericType <HelloTask> .Class);
            cb.BindNamedParameter <TaskConfigurationOptions.Identifier, string>(GenericType <TaskConfigurationOptions.Identifier> .Class, "Hello Stereamingk");
            cb.BindNamedParameter <StreamTask1.IpAddress, string>(GenericType <StreamTask1.IpAddress> .Class, "127.0.0.0");
            IConfiguration conf        = cb.Build();
            IInjector      injector    = tang.NewInjector(conf);
            var            activityRef = (ITask)injector.GetInstance(activityInterfaceType);

            Assert.IsNotNull(activityRef);
        }
Example #26
0
        public void TestBindSetEntryImplValue()
        {
            ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder();

            cb.BindSetEntry <TestSetInjection.SetOfClasses, TestSetInjection.Integer1, INumber>() // bind an impl to the interface of the set
            .BindIntNamedParam <TestSetInjection.Integer1.NamedInt>("4");                         // bind parameter for the impl

            IInjector i = TangFactory.GetTang().NewInjector(cb.Build());

            ISet <INumber> actual   = i.GetInstance <TestSetInjection.Pool>().Numbers;
            ISet <INumber> expected = new HashSet <INumber>();

            expected.Add(new TestSetInjection.Integer1(4));

            Assert.True(Utilities.Utilities.Equals <INumber>(actual, expected));
        }
Example #27
0
        public void TestSetOfTimeshiftMultipleInstances()
        {
            ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder();

            // when adding another Timeshift into the set for named parameter SetOfTimeshifts, it ends up the same entry.
            cb.BindSetEntry <SetOfTimeshifts, Timeshift, ITimeshift>(GenericType <SetOfTimeshifts> .Class, GenericType <Timeshift> .Class);
            cb.BindSetEntry <SetOfTimeshifts, Timeshift, ITimeshift>(GenericType <SetOfTimeshifts> .Class, GenericType <Timeshift> .Class);
            cb.BindNamedParameter <Timeshift.TimeshiftLinkId, string>(GenericType <Timeshift.TimeshiftLinkId> .Class, "123")
            .BindNamedParameter <Timeshift.TimeshiftInTicks, long>(GenericType <Timeshift.TimeshiftInTicks> .Class, "10");

            IInjector i = TangFactory.GetTang().NewInjector(cb.Build());

            ISet <ITimeshift> actual = i.GetInstance <SetofTimeShiftClass>().Timeshifts;

            Assert.True(actual.Count == 1);
        }
Example #28
0
        public void TestTwoNamedStringArgsBindVolatile()
        {
            ICsConfigurationBuilder cb = tang.NewConfigurationBuilder();
            TwoNamedStringArgs      a  = tang.NewInjector(cb.Build()).GetInstance <TwoNamedStringArgs>();

            Assert.AreEqual("defaultA", a.a);
            Assert.AreEqual("defaultB", a.b);
            IInjector i = tang.NewInjector(cb.Build());

            i.BindVolatileParameter(GenericType <TwoNamedStringArgs.A> .Class, "not defaultA");
            i.BindVolatileParameter(GenericType <TwoNamedStringArgs.B> .Class, "not defaultB");
            Assert.AreEqual("not defaultA",
                            i.GetInstance <TwoNamedStringArgs>().a);
            Assert.AreEqual("not defaultB",
                            i.GetInstance <TwoNamedStringArgs>().b);
        }
Example #29
0
        public void TestNamedImpl()
        {
            ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(new string[] { FileNames.Examples });

            cb.BindNamedParameter <AImplName, Aimpl, INamedImplA>(GenericType <AImplName> .Class, GenericType <Aimpl> .Class);
            cb.BindNamedParameter <BImplName, Bimpl, INamedImplA>(GenericType <BImplName> .Class, GenericType <Bimpl> .Class);

            IInjector i  = TangFactory.GetTang().NewInjector(cb.Build());
            Aimpl     a1 = (Aimpl)i.GetNamedInstance <AImplName, INamedImplA>(GenericType <AImplName> .Class);
            Aimpl     a2 = (Aimpl)i.GetNamedInstance <AImplName, INamedImplA>(GenericType <AImplName> .Class);
            Bimpl     b1 = (Bimpl)i.GetNamedInstance <BImplName, INamedImplA>(GenericType <BImplName> .Class);
            Bimpl     b2 = (Bimpl)i.GetNamedInstance <BImplName, INamedImplA>(GenericType <BImplName> .Class);

            Assert.AreSame(a1, a2);
            Assert.AreSame(b1, b2);
        }
Example #30
0
        public void TestRepeatedAmbiguousArgs()
        {
            INode node = null;

            try
            {
                ICsConfigurationBuilder t = tang.NewConfigurationBuilder();
                node =
                    t.GetClassHierarchy()
                    .GetNode(ReflectionUtilities.GetAssemblyQualifiedName(typeof(RepeatedAmbiguousArgs)));
            }
            catch (ClassHierarchyException)
            {
            }
            Assert.IsNull(node);
        }
 public IConfiguration AddFromByteArray(ICsConfigurationBuilder cb, byte[] bytes)
 {
     AvroConfiguration avroConf = AvroDeserialize(bytes);
     return AddFromAvro(cb, avroConf);
 }