public void ConnectionManagerAddsNewServicesFromServiceDiscovery()
        {
            var manualResetEvent = new System.Threading.ManualResetEvent(false);
            var serviceUri1 = new ServiceUri() { Address = "1" };
            var serviceUri2 = new ServiceUri() { Address = "2" };
            Dictionary<ServiceUri, PerformanceStatistics> services
                = new Dictionary<ServiceUri, PerformanceStatistics>()
                {
                    {serviceUri1, new PerformanceStatistics()},
                    {serviceUri2, new PerformanceStatistics()}
                };
            var serviceDiscoveryMock = new Mock<IServiceDiscovery>(MockBehavior.Strict);
            serviceDiscoveryMock.Setup(sd => sd.GetPerformanceStatistics()).Returns(() => services).Callback(() => manualResetEvent.Set());

            var manager = new ConnectionManager(remoteService: null, listener: null,
                serviceDiscovery: serviceDiscoveryMock.Object,
                serviceDiscoveryPeriod: new TimeSpan(days: 0, hours: 0, minutes: 0, seconds: 0, milliseconds: 10));

            manualResetEvent.WaitOne();
            manager.RemoteServices.Count().ShouldBe(2);
            services.Add(new ServiceUri(), new PerformanceStatistics());
            manualResetEvent.Reset();
            manualResetEvent.WaitOne();

            manager.RemoteServices.Count().ShouldBe(3);
        }
Esempio n. 2
0
 public CentralizedDiscovery(Bluepath.Services.ServiceUri masterUri, Bluepath.Services.BluepathListener listener)
 {
     this.services = new List<ServiceUri>();
     this.client = new ServiceReferences.CentralizedDiscoveryServiceClient(masterUri.Binding, masterUri.ToEndpointAddress());
     this.listenerUri = listener.CallbackUri;
     var registerThread = new Thread(() =>
             this.client.Register(this.ConvertToClientServiceUri(this.listenerUri)));
     registerThread.Start();
 }
        public void RoundRobinLocalSchedulerGoesSequentiallyThroughAllSpecifiedServiceUris()
        {
            int noOfUris = 10;
            var serviceUris = new ServiceUri[noOfUris];
            for(int i=0;i<noOfUris;i++)
            {
                serviceUris[i] = new ServiceUri() { Address = i.ToString() };
            }

            var scheduler = new RoundRobinLocalScheduler(serviceUris);
            for(int i=0;i<noOfUris;i++)
            {
                scheduler.GetRemoteServiceUri().Address.ShouldBe(serviceUris[i].Address);
            }

            scheduler.GetRemoteServiceUri().Address.ShouldBe(serviceUris[0].Address);
        }
Esempio n. 4
0
        public void ServiceUriCanBeUsedForLookupAsKeyInTheDictionaryTest()
        {
            var dictionary = new Dictionary<ServiceUri, int>();

            dictionary.Add(new ServiceUri("http://localhost:1234/test.svc",  BindingType.BasicHttpBinding),  1);
            dictionary.Add(new ServiceUri("http://localhost:1234/test2.svc", BindingType.BasicHttpBinding),  2);
            dictionary.Add(new ServiceUri("http://localhost:1234/test2.svc", BindingType.BasicHttpsBinding), 3);

            var key1 = new ServiceUri("http://localhost:1234/test.svc", BindingType.BasicHttpBinding);
            dictionary.ContainsKey(key1).ShouldBe(true);
            var service1 = dictionary[key1];
            service1.ShouldBe(1);

            var key2 = new ServiceUri("http://localhost:1234/test2.svc", BindingType.BasicHttpBinding);
            dictionary.ContainsKey(key2).ShouldBe(true);
            var service2 = dictionary[key2];
            service2.ShouldBe(2);

            var key3 = new ServiceUri("http://localhost:1234/test2.svc", BindingType.BasicHttpsBinding);
            dictionary.ContainsKey(key3).ShouldBe(true);
            var service3 = dictionary[key3];
            service3.ShouldBe(3);
        }
        public void ThreadNumberSchedulerChoosesServiceWithSmallestNoOfThreads()
        {
            var serviceUri1 = new ServiceUri(){Address="1"};
            var serviceUri2 = new ServiceUri() { Address = "2" };
            var noOfTasks1 = new Dictionary<ExecutorState, int>()
            {
                {ExecutorState.Running, 15}
            };
            var noOfTasks2 = new Dictionary<ExecutorState, int>()
            {
                {ExecutorState.Running, 10}
            };
            Dictionary<ServiceUri, PerformanceStatistics> services
                = new Dictionary<ServiceUri, PerformanceStatistics>()
                {
                    {serviceUri1, new PerformanceStatistics(){NumberOfTasks=noOfTasks1}},
                    {serviceUri2, new PerformanceStatistics(){NumberOfTasks=noOfTasks2}},
                };
            var connectionManagerMock = new Mock<IConnectionManager>(MockBehavior.Strict);
            connectionManagerMock.Setup(cm => cm.RemoteServices).Returns(services);

            var scheduler = new ThreadNumberScheduler(connectionManagerMock.Object);
            scheduler.GetRemoteServiceUri().ShouldBe(serviceUri2);
        }
        public void ConnectionManagerRemovesServicesNotExistingInServiceDiscovery()
        {
            var manualResetEvent = new System.Threading.ManualResetEvent(false);
            var serviceUri1 = new ServiceUri()
            {
                Address = "jack",
                BindingType = BindingType.BasicHttpBinding
            };

            var serviceUri2 = new ServiceUri()
            {
                Address = "jackie",
                BindingType = BindingType.BasicHttpBinding
            };

            Dictionary<ServiceUri, PerformanceStatistics> services
                = new Dictionary<ServiceUri, PerformanceStatistics>()
                {
                    {serviceUri1, new PerformanceStatistics()},
                    {serviceUri2, new PerformanceStatistics()}
                };
            var serviceDiscoveryMock = new Mock<IServiceDiscovery>(MockBehavior.Strict);
            serviceDiscoveryMock.Setup(sd => sd.GetPerformanceStatistics()).Returns(() => services).Callback(() => manualResetEvent.Set());

            var manager = new ConnectionManager(remoteService: null, listener: null,
                serviceDiscovery: serviceDiscoveryMock.Object,
                serviceDiscoveryPeriod: new TimeSpan(days: 0, hours: 0, minutes: 0, seconds: 0, milliseconds: 10));

            manualResetEvent.WaitOne();
            System.Threading.Thread.Sleep(10);
            manager.RemoteServices.Count().ShouldBe(2);
            services.Remove(serviceUri1);
            manualResetEvent.Reset();
            manualResetEvent.WaitOne();

            manager.RemoteServices.Count().ShouldBe(1);
        }
 public RoundRobinLocalScheduler(ServiceUri[] availableServices)
 {
     this.availableServices = new List<ServiceUri>(availableServices);
 }
Esempio n. 8
0
        /// <summary>
        /// Executes method set previously by Initialize method.
        /// </summary>
        /// <param name="eid">Unique identifier of the executor.</param>
        /// <param name="parameters">Parameters for the method. Note that it gets interpreted as object1, object2, etc.
        /// So if method expects one argument of type object[], you need to wrap it with additional object[] 
        /// (object[] { object[] } - outer array indicates that method accepts one parameter, and inner is actual parameter).</param>
        /// <param name="callbackUri">Specifies uri which will be used for callback.
        /// Null for no callback.</param>
        public void Execute(Guid eid, object[] parameters, ServiceUri callbackUri)
        {
            var executor = GetExecutor(eid);

            Log.TraceMessage(
                callbackUri != null ? Log.Activity.Starting_local_executor_with_callback : Log.Activity.Starting_local_executor_without_callback,
                string.Format(
                        "Starting local executor. Upon completion callback will{0} be sent{1}{2}.",
                        callbackUri != null ? string.Empty : " not",
                        callbackUri != null ? " to " : string.Empty,
                        callbackUri != null ? callbackUri.Address : string.Empty),
                        keywords: executor.Eid.EidAsLogKeywords());

            executor.Execute(parameters);

            if (callbackUri != null)
            {
                var t = new Thread(() =>
                {
                    try
                    {
                        using (var client =
                            new Bluepath.ServiceReferences.RemoteExecutorServiceClient(
                                callbackUri.Binding,
                                callbackUri.ToEndpointAddress()))
                        {
                            // Join on local executor doesn't throw exceptions by design
                            // Exception caused by user code (if any) can be accessed using Exception property
                            executor.Join();

                            Log.TraceMessage(Log.Activity.Sending_callback_with_result, string.Format("Sending callback with result. State: {0}. Elapsed time: {1}.", executor.ExecutorState, executor.ElapsedTime), keywords: executor.Eid.EidAsLogKeywords());

                            var result = new ServiceReferences.RemoteExecutorServiceResult
                            {
                                Result = executor.IsResultAvailable ? executor.Result : null,
                                ElapsedTime = executor.ElapsedTime,
                                ExecutorState = (ServiceReferences.ExecutorState)((int)executor.ExecutorState),
                                Error = executor.Exception
                            };

                            // TODO: Serialization of result can fail and we should do something about it (like send an error message back)
                            client.ExecuteCallback(eid, result);
                        }
                    }
                    catch(Exception ex)
                    {
                        Log.ExceptionMessage(ex, Log.Activity.Send_callback_failed, "Send callback failed.");
                    }
                });

                t.Name = string.Format("Execute callback sender thread for executor '{0}'", executor.Eid);
                t.Start();
            }
        }