示例#1
0
        private void SigningService_AggregatorConfigChanged(object sender, AggregatorConfigChangedEventArgs e)
        {
            if (AggregatorConfigChanged == null)
            {
                return;
            }

            lock (_aggregatorConfigChangedLock)
            {
                try
                {
                    if (e.AggregatorConfig == null)
                    {
                        throw new ArgumentNullException(nameof(e.AggregatorConfig));
                    }

                    Logger.Debug("Sub-service AggregationConfig changed: " + e.AggregatorConfig + "; Sub-service: " + e.KsiService.AggregatorAddress);
                    _currentAggregatorConfigList[e.KsiService] = e.AggregatorConfig;
                    RecalculateAggregatorConfig();
                }
                catch (Exception ex)
                {
                    Logger.Warn("HA aggregator configuration change handling failed", ex);
                    throw;
                }
            }
        }
        public void HAGetConfigResultsAndRemoveAllTest()
        {
            // A configuration request with 2 successful sub-requests is made.
            // Then a new configuration request is made with 2 unsuccessful sub-requests.
            // Both configuration are removed from cache.
            // AggregationConfigChanged event handler should get result containing an exception.

            HAKsiService haService = GetHAService(
                new List <PduPayload>()
            {
                GetAggregatorConfigResponsePayload(1, 1, 100, 4, new List <string>()
                {
                    "uri-1"
                })
            },
                new List <PduPayload>()
            {
                GetAggregatorConfigResponsePayload(1, 2, 100, 4, new List <string>()
                {
                    "uri-2"
                })
            });

            haService.GetAggregatorConfig();

            // change first service response so that request fails
            ((TestKsiService)haService.SigningServices[0]).SigningServiceProtocol.RequestResult =
                File.ReadAllBytes(Path.Combine(TestSetup.LocalPath, Resources.KsiService_AggregationResponsePdu_RequestId_1584727637));

            // change second service response so that request fails
            ((TestKsiService)haService.SigningServices[1]).SigningServiceProtocol.RequestResult =
                File.ReadAllBytes(Path.Combine(TestSetup.LocalPath, Resources.KsiService_AggregationResponsePdu_RequestId_1584727637));

            AggregatorConfigChangedEventArgs args = null;
            ManualResetEvent waitHandle           = new ManualResetEvent(false);

            haService.AggregatorConfigChanged += delegate(object sender, AggregatorConfigChangedEventArgs e)
            {
                args = e;
                waitHandle.Set();
            };

            HAKsiServiceException ex = Assert.Throws <HAKsiServiceException>(delegate
            {
                haService.GetAggregatorConfig();
            });

            Assert.That(ex.Message.StartsWith("Could not get aggregator configuration"), "Unexpected exception message: " + ex.Message);

            waitHandle.WaitOne(1000);

            Assert.IsNotNull(args, "AggregatorConfigChangedEventArgs cannot be null.");
            Assert.IsNull(args.AggregatorConfig, "AggregatorConfigChangedEventArgs.AggregatorConfig cannot have value.");
            Assert.IsNotNull(args.Exception, "AggregatorConfigChangedEventArgs.Exception cannot be null.");
            Assert.AreEqual(haService, args.KsiService, "Unexpected AggregatorConfigChangedEventArgs.KsiService");
            Assert.That(args.Exception.Message.StartsWith("Could not get aggregator configuration"), "Unexpected exception message: " + args.Exception.Message);
        }
示例#3
0
        /// <summary>
        /// End get additional aggregator configuration data (async)
        /// </summary>
        /// <param name="asyncResult"></param>
        /// <returns>Aggregator configuration data</returns>
        public AggregatorConfig EndGetAggregatorConfig(IAsyncResult asyncResult)
        {
            HAAsyncResult haAsyncResult            = GetHAAsyncResult(asyncResult);
            HAAggregatorConfigRequestRunner runner = GetRequestRunner <HAAggregatorConfigRequestRunner>(haAsyncResult);
            AggregatorConfig config = runner.EndGetAggregatorConfig(haAsyncResult);

            if (config == null)
            {
                lock (_aggregatorConfigChangedLock)
                {
                    _currentAggregatorConfigList.Clear();
                    _currentAggregatorConfig = null;
                }

                HAKsiServiceException ex = new HAKsiServiceException("Could not get aggregator configuration.", runner.SubServiceErrors);
                Logger.Warn(ex);
                AggregatorConfigChangedEventArgs aggregatorConfigChangedEventArgs = new AggregatorConfigChangedEventArgs(ex, this);
                AggregatorConfigChanged?.Invoke(this, aggregatorConfigChangedEventArgs);
                throw ex;
            }

            // if sub-service config request failed then remove corresponding config from cache
            foreach (HAKsiSubServiceException ex in runner.SubServiceErrors)
            {
                if (ex.ThrownBySubService == null)
                {
                    continue;
                }

                lock (_aggregatorConfigChangedLock)
                {
                    if (_currentAggregatorConfigList.ContainsKey(ex.ThrownBySubService))
                    {
                        _currentAggregatorConfigList.Remove(ex.ThrownBySubService);
                        RecalculateAggregatorConfig();
                    }
                }
            }

            return(config);
        }
 private void Service_AggregatorConfigChanged(object sender, AggregatorConfigChangedEventArgs e)
 {
     _aggregatorConfig = e.AggregatorConfig;
     _waitHandle.Set();
 }
        public void HAGetConfigResultsAndRemoveOneTest()
        {
            // A configuration request with 2 successful sub-requests is made.
            // Then a new configuration request is made with 1 successful and 1 unsuccessful sub-requests.
            // Unsuccessful service config should be removed from cache and merged config should be recalculated

            HAKsiService haService = GetHAService(
                new List <PduPayload>()
            {
                GetAggregatorConfigResponsePayload(2, 1, 100, 4, new List <string>()
                {
                    "uri-1"
                })
            },
                new List <PduPayload>()
            {
                GetAggregatorConfigResponsePayload(1, 2, 100, 4, new List <string>()
                {
                    "uri-2"
                })
            });

            ManualResetEvent waitHandle = new ManualResetEvent(false);

            haService.AggregatorConfigChanged += delegate
            {
            };

            AggregatorConfig resultConf = haService.GetAggregatorConfig();

            waitHandle.WaitOne(1000);

            Assert.AreEqual(2, resultConf.MaxLevel, "Unexpected max level value");
            Assert.AreEqual(100, resultConf.AggregationPeriod, "Unexpected aggregation period value");
            Assert.AreEqual(4, resultConf.MaxRequests, "Unexpected max requests value");
            Assert.AreEqual(1, resultConf.ParentsUris.Count, "Unexpected parent uri count");

            // change first service response so that request fails
            ((TestKsiService)haService.SigningServices[0]).SigningServiceProtocol.RequestResult =
                File.ReadAllBytes(Path.Combine(TestSetup.LocalPath, Resources.KsiService_AggregationResponsePdu_RequestId_1584727637));

            // change second service response so that a valid configuration is returned
            TestKsiService newService = (TestKsiService)GetService(new List <PduPayload>()
            {
                GetAggregationResponsePayload(Resources.KsiService_AggregationResponsePdu_RequestId_1584727638),
                GetAggregatorConfigResponsePayload(2, 3, 400, 3, new List <string>()
                {
                    "uri-2-changed"
                })
            });

            ((TestKsiService)haService.SigningServices[1]).SigningServiceProtocol.RequestResult = newService.SigningServiceProtocol.RequestResult;

            AggregatorConfigChangedEventArgs args = null;

            waitHandle = new ManualResetEvent(false);

            haService.AggregatorConfigChanged += delegate(object sender, AggregatorConfigChangedEventArgs e)
            {
                args = e;
            };

            resultConf = haService.GetAggregatorConfig();

            Assert.AreEqual(2, resultConf.MaxLevel, "Unexpected max level value");
            Assert.AreEqual(3, resultConf.AggregationAlgorithm, "Unexpected algorithm value");
            Assert.AreEqual(400, resultConf.AggregationPeriod, "Unexpected aggregation period value");
            Assert.AreEqual(3, resultConf.MaxRequests, "Unexpected max requests value");
            Assert.AreEqual(1, resultConf.ParentsUris.Count, "Unexpected parent uri count");
            Assert.AreEqual("uri-2-changed", resultConf.ParentsUris[0], "Unexpected parent uri value at position 0");

            waitHandle.WaitOne(1000);

            Assert.IsNotNull(args, "AggregatorConfigChangedEventArgs cannot be null.");
            Assert.AreEqual(resultConf, args.AggregatorConfig, "Unexpected AggregatorConfigChangedEventArgs.AggregatorConfig.");
            Assert.IsNull(args.Exception, "AggregatorConfigChangedEventArgs.Exception cannot have value.");
            Assert.AreEqual(haService, args.KsiService, "Unexpected AggregatorConfigChangedEventArgs.KsiService");
        }