public void HAAggregatorConfigRequestUsingEventHandlerTest()
        {
            // Test getting aggregator configuration with 1 successful and 2 unsuccessful sub-service responses.
            // Get response using AggregatorConfigChanged event handler

            IKsiService haService =
                new HAKsiService(
                    new List <IKsiService>()
            {
                GetStaticKsiService(File.ReadAllBytes(Path.Combine(TestSetup.LocalPath, Resources.KsiService_AggregationResponsePdu_RequestId_1584727637)), 1584727638),
                GetStaticKsiService(File.ReadAllBytes(Path.Combine(TestSetup.LocalPath, Resources.KsiService_AggregatorConfigResponsePdu)), 1584727637),
                GetStaticKsiService(File.ReadAllBytes(Path.Combine(TestSetup.LocalPath, Resources.KsiService_AggregationResponsePdu_RequestId_1584727637)), 1584727637)
            },
                    null, null);

            ManualResetEvent waitHandle       = new ManualResetEvent(false);
            AggregatorConfig aggregatorConfig = null;

            haService.AggregatorConfigChanged += delegate(object sender, AggregatorConfigChangedEventArgs e)
            {
                aggregatorConfig = e.AggregatorConfig;
                waitHandle.Set();
            };
            haService.GetAggregatorConfig();
            waitHandle.WaitOne(1000);
            Assert.IsNotNull(aggregatorConfig, "Could not get aggregator config using event handler.");

            Assert.AreEqual(17, aggregatorConfig.MaxLevel, "Unexpected max level value");
            Assert.AreEqual(1, aggregatorConfig.AggregationAlgorithm, "Unexpected algorithm value");
            Assert.AreEqual(400, aggregatorConfig.AggregationPeriod, "Unexpected aggregation period value");
            Assert.AreEqual(1024, aggregatorConfig.MaxRequests, "Unexpected max requests value");
            Assert.AreEqual(0, aggregatorConfig.ParentsUris.Count, "Unexpected parent uri count");
        }
        public void HAGetConfigResultsOutOfLimitTest()
        {
            // Test getting aggregator configuration with 2 successful sub-service responses
            // Some values are out of bounds

            IKsiService haService = GetHAService(
                new List <PduPayload>()
            {
                GetAggregatorConfigResponsePayload(1, 2, 99, 4, new List <string>()
                {
                    "uri-1"
                })
            },
                new List <PduPayload>()
            {
                GetAggregatorConfigResponsePayload(21, null, 20001, 16001, null)
            });

            AggregatorConfig config = haService.GetAggregatorConfig();

            Assert.AreEqual(1, config.MaxLevel, "Unexpected max level value");
            Assert.AreEqual(2, config.AggregationAlgorithm, "Unexpected algorithm value");
            Assert.IsNull(config.AggregationPeriod, "Unexpected aggregation period value");
            Assert.AreEqual(4, config.MaxRequests, "Unexpected max requests value");
            Assert.AreEqual(1, config.ParentsUris.Count, "Unexpected parent uri count");
            Assert.AreEqual("uri-1", config.ParentsUris[0], "Unexpected parent uri value at position 0");
        }
Ejemplo n.º 3
0
        public void HAAggregatorGetConfTest()
        {
            Ksi ksi = new Ksi(new HAKsiService(SigningServices, null, null));
            AggregatorConfig conf = ksi.GetAggregatorConfig();

            Assert.NotNull(conf);
        }
        public void AsyncGetAggregatorConfigTest(KsiService service)
        {
            if (TestSetup.PduVersion == PduVersion.v1)
            {
                return;
            }

            ManualResetEvent waitHandle     = new ManualResetEvent(false);
            AggregatorConfig config         = null;
            object           testObject     = new object();
            bool             isAsyncCorrect = false;

            service.BeginGetAggregatorConfig(delegate(IAsyncResult ar)
            {
                try
                {
                    isAsyncCorrect = ar.AsyncState == testObject;
                    config         = service.EndGetAggregatorConfig(ar);
                }
                finally
                {
                    waitHandle.Set();
                }
            }, testObject);

            Assert.IsTrue(waitHandle.WaitOne(10000), "Wait handle timed out.");

            Assert.IsNotNull(config, "Aggregator configuration should not be null.");
            Assert.AreEqual(true, isAsyncCorrect, "Unexpected async state.");
        }
Ejemplo n.º 5
0
        private void RecalculateAggregatorConfig()
        {
            AggregatorConfig mergedConfig = null;

            foreach (IKsiService service in SigningServices)
            {
                if (!_currentAggregatorConfigList.ContainsKey(service))
                {
                    continue;
                }

                AggregatorConfig config = _currentAggregatorConfigList[service];
                Logger.Debug("AggregatorConfig in cache: " + config + "; Sub-service: " + service.AggregatorAddress);
                mergedConfig = HAAggregatorConfigRequestRunner.MergeConfigs(mergedConfig, config);
            }

            if (_currentAggregatorConfig == null || !_currentAggregatorConfig.Equals(mergedConfig))
            {
                Logger.Debug("New merged AggregatorConfig: " + mergedConfig);
                _currentAggregatorConfig = mergedConfig;
                AggregatorConfigChanged?.Invoke(this, new AggregatorConfigChangedEventArgs(mergedConfig, this));
            }
            else
            {
                Logger.Debug("Merged AggregationConfig not changed.");
            }
        }
        public void HAGetConfigTwoResultsTest4()
        {
            // Test getting aggregator configuration with 2 successful sub-service responses

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

            AggregatorConfig config = haService.GetAggregatorConfig();

            Assert.AreEqual(1, config.MaxLevel, "Unexpected max level value");
            Assert.AreEqual(2, config.AggregationAlgorithm, "Unexpected algorithm value");
            Assert.AreEqual(100, config.AggregationPeriod, "Unexpected aggregation period value");
            Assert.AreEqual(4, config.MaxRequests, "Unexpected max requests value");
            Assert.AreEqual(1, config.ParentsUris.Count, "Unexpected parent uri count");
            Assert.AreEqual("uri-1", config.ParentsUris[0], "Unexpected parent uri value at position 0");
        }
Ejemplo n.º 7
0
        public void HAAsyncGetAggregatorConfigTest()
        {
            HAKsiService haService = new HAKsiService(SigningServices, ExtendingServices, PublicationsFileService);

            ManualResetEvent waitHandle     = new ManualResetEvent(false);
            AggregatorConfig config         = null;
            object           testObject     = new object();
            bool             isAsyncCorrect = false;

            haService.BeginGetAggregatorConfig(delegate(IAsyncResult ar)
            {
                try
                {
                    isAsyncCorrect = ar.AsyncState == testObject;
                    config         = haService.EndGetAggregatorConfig(ar);
                }
                finally
                {
                    waitHandle.Set();
                }
            }, testObject);

            Assert.IsTrue(waitHandle.WaitOne(10000), "Wait handle timed out.");

            Assert.IsNotNull(config, "Aggregator configuration should not be null.");
            Assert.AreEqual(true, isAsyncCorrect, "Unexpected async state.");
        }
        public void AggregatorConfigRequestStaticTest()
        {
            Ksi ksi = GetStaticKsi(Resources.KsiService_AggregatorConfigResponsePdu);

            AggregatorConfig config = ksi.GetAggregatorConfig();

            Assert.AreEqual(17, config.MaxLevel, "Unexpected max level value");
            Assert.AreEqual(1, config.AggregationAlgorithm, "Unexpected algorithm value");
            Assert.AreEqual(400, config.AggregationPeriod, "Unexpected aggregation period value");
            Assert.AreEqual(1024, config.MaxRequests, "Unexpected max requests value");
        }
        public void AggregatorConfigRequestWithMultiPayloadsResponseStaticTest()
        {
            // Response has multiple payloads (2 signature payloads and a configuration payload)
            Ksi ksi = GetStaticKsi(Resources.KsiService_AggregationResponsePdu_Multi_Payloads);

            AggregatorConfig config = ksi.GetAggregatorConfig();

            Assert.AreEqual(17, config.MaxLevel, "Unexpected max level value");
            Assert.AreEqual(1, config.AggregationAlgorithm, "Unexpected algorithm value");
            Assert.AreEqual(400, config.AggregationPeriod, "Unexpected aggregation period value");
            Assert.AreEqual(1024, config.MaxRequests, "Unexpected max requests value");
        }
        public void AggregatorConfigRequestWithAcknowledgmentStaticTest()
        {
            // Response has multiple payloads (a configuration payload and an acknowledgment payload)
            Ksi ksi = GetStaticKsi(Resources.KsiService_AggregatorConfigResponsePdu_With_Acknowledgment);

            AggregatorConfig config = ksi.GetAggregatorConfig();

            Assert.AreEqual(17, config.MaxLevel, "Unexpected max level value");
            Assert.AreEqual(1, config.AggregationAlgorithm, "Unexpected algorithm value");
            Assert.AreEqual(400, config.AggregationPeriod, "Unexpected aggregation period value");
            Assert.AreEqual(1024, config.MaxRequests, "Unexpected max requests value");
        }
        public void HAGetConfigSingleResultAllNullsTest()
        {
            // Test getting aggregator configuration with 1 successful sub-service response
            // All the values are empty

            IKsiService haService = GetHAService(
                new List <PduPayload>()
            {
                GetAggregatorConfigResponsePayload(null, null, null, null, null),
            });

            AggregatorConfig config = haService.GetAggregatorConfig();

            Assert.IsNull(config.MaxLevel, "Unexpected max level value");
            Assert.IsNull(config.AggregationAlgorithm, "Unexpected algorithm value");
            Assert.IsNull(config.AggregationPeriod, "Unexpected aggregation period value");
            Assert.IsNull(config.MaxRequests, "Unexpected max requests value");
            Assert.AreEqual(0, config.ParentsUris.Count, "Unexpected parent uri list");
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Merge two configurations into one taking into account consolidation rules.
        /// </summary>
        /// <param name="currentConfig"></param>
        /// <param name="newConfig"></param>
        /// <returns></returns>
        public static AggregatorConfig MergeConfigs(AggregatorConfig currentConfig, AggregatorConfig newConfig)
        {
            if (newConfig == null)
            {
                throw new ArgumentNullException(nameof(newConfig));
            }

            ulong?maxLevel             = GetMergedMaxLevel(currentConfig?.MaxLevel, newConfig.MaxLevel);
            ulong?aggregationAlgorithm = currentConfig?.AggregationAlgorithm ?? newConfig.AggregationAlgorithm;
            ulong?aggregationPeriod    = GetMergedAggregationPeriod(currentConfig?.AggregationPeriod, newConfig.AggregationPeriod);
            ulong?maxRequests          = GetMergedMaxRequests(currentConfig?.MaxRequests, newConfig.MaxRequests);

            return(new AggregatorConfig(
                       maxLevel,
                       aggregationAlgorithm,
                       aggregationPeriod,
                       maxRequests,
                       currentConfig?.ParentsUris == null || currentConfig.ParentsUris.Count == 0 ? newConfig.ParentsUris : currentConfig.ParentsUris));
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Ends HA aggregator configuration request and returns consolidated successful sub-service configurations.
        /// </summary>
        /// <param name="asyncResult"></param>
        /// <returns></returns>
        public AggregatorConfig EndGetAggregatorConfig(HAAsyncResult asyncResult)
        {
            object[]         list   = EndRequestMulti(asyncResult);
            AggregatorConfig config = null;

            foreach (object obj in list)
            {
                AggregatorConfig conf = obj as AggregatorConfig;

                if (conf == null)
                {
                    throw new HAKsiServiceException(string.Format("Invalid request result object. Expected type: AggregatorConfig; Received type: {0}",
                                                                  obj?.GetType().ToString() ?? ""));
                }

                config = MergeConfigs(config, conf);
            }

            return(config);
        }
Ejemplo n.º 14
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);
        }
        public void GetAggregatorConfigUsingEventHandlerTest(KsiService service)
        {
            service.AggregatorConfigChanged += Service_AggregatorConfigChanged;
            _waitHandle       = new ManualResetEvent(false);
            _aggregatorConfig = null;

            if (TestSetup.PduVersion == PduVersion.v1)
            {
                Exception ex = Assert.Throws <KsiServiceException>(delegate
                {
                    service.GetAggregatorConfig();
                });

                Assert.That(ex.Message.StartsWith("Aggregator config request is not supported using PDU version v1"), "Unexpected exception message: " + ex.Message);
                return;
            }

            service.GetAggregatorConfig();
            Assert.IsTrue(_waitHandle.WaitOne(10000), "Wait handle timed out.");
            Assert.IsNotNull(_aggregatorConfig, "Could not get aggregator config using event handler.");
        }
        public void HAAggregatorConfigRequestTest()
        {
            // Test getting aggregator configuration with 1 successful and 2 unsuccessful sub-service responses

            IKsiService haService =
                new HAKsiService(
                    new List <IKsiService>()
            {
                GetStaticKsiService(File.ReadAllBytes(Path.Combine(TestSetup.LocalPath, Resources.KsiService_AggregationResponsePdu_RequestId_1584727637)), 1584727638),
                GetStaticKsiService(File.ReadAllBytes(Path.Combine(TestSetup.LocalPath, Resources.KsiService_AggregatorConfigResponsePdu)), 1584727637),
                GetStaticKsiService(File.ReadAllBytes(Path.Combine(TestSetup.LocalPath, Resources.KsiService_AggregationResponsePdu_RequestId_1584727637)), 1584727637)
            },
                    null, null);

            AggregatorConfig config = haService.GetAggregatorConfig();

            Assert.AreEqual(17, config.MaxLevel, "Unexpected max level value");
            Assert.AreEqual(1, config.AggregationAlgorithm, "Unexpected algorithm value");
            Assert.AreEqual(400, config.AggregationPeriod, "Unexpected aggregation period value");
            Assert.AreEqual(1024, config.MaxRequests, "Unexpected max requests value");
            Assert.AreEqual(0, config.ParentsUris.Count, "Unexpected parent uri count");
        }
        public void HAGetConfigResultsWithSignRequestTest()
        {
            // Test getting aggregator configurations via AggregatorConfigChanged event handler when using Sign method.
            // Testing getting different configurations in a sequence

            HAKsiService haService = GetHAService(
                new List <PduPayload>()
            {
                GetAggregationResponsePayload(Resources.KsiService_AggregationResponsePdu_RequestId_1584727637),
                GetAggregatorConfigResponsePayload(1, 1, 200, 4, new List <string>()
                {
                    "uri-1"
                })
            },
                new List <PduPayload>()
            {
                GetAggregationResponsePayload(Resources.KsiService_AggregationResponsePdu_RequestId_1584727638),
                GetAggregatorConfigResponsePayload(1, 1, 200, 4, new List <string>()
                {
                    "uri-2"
                })
            });

            TestKsiService secondService = (TestKsiService)haService.SigningServices[1];

            secondService.RequestId = 1584727638;

            AggregatorConfig resultConf = null;
            int changeCount             = 0;
            ManualResetEvent waitHandle = new ManualResetEvent(false);

            haService.AggregatorConfigChanged += delegate(object sender, AggregatorConfigChangedEventArgs e)
            {
                resultConf = e.AggregatorConfig;
                changeCount++;
                if (changeCount == 2)
                {
                    waitHandle.Set();
                }
            };

            DataHash      inputHash = new DataHash(Base16.Decode("019f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"));
            IKsiSignature signature = haService.Sign(inputHash);

            Assert.AreEqual(inputHash, signature.InputHash, "Unexpected signature input hash.");
            waitHandle.WaitOne(1000);
            Assert.IsNotNull(resultConf, "Could not get aggregator config using event handler.");

            Assert.AreEqual(1, resultConf.MaxLevel, "Unexpected max level value");
            Assert.IsTrue(resultConf.AggregationAlgorithm == 1 || resultConf.AggregationAlgorithm == 2, "Unexpected algorithm value");
            Assert.AreEqual(200, 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");
            Assert.IsTrue(resultConf.ParentsUris[0] == "uri-1" || resultConf.ParentsUris[0] == "uri-2", "Unexpected parent uri value at position 0");

            // changing aggregation algorithm or parent uri should not change merged config
            TestKsiService newService = (TestKsiService)GetService(new List <PduPayload>()
            {
                GetAggregationResponsePayload(Resources.KsiService_AggregationResponsePdu_RequestId_1584727638),
                GetAggregatorConfigResponsePayload(1, 3, 200, 4, new List <string>()
                {
                    "uri-2-changed"
                })
            });

            secondService.SigningServiceProtocol.RequestResult = newService.SigningServiceProtocol.RequestResult;

            resultConf  = null;
            changeCount = 0;
            haService.Sign(inputHash);
            Thread.Sleep(1000);
            Assert.IsNull(resultConf, "Aggregator config should have not changed (2nd request)");
            Assert.AreEqual(0, changeCount, "Unexpected change count.");

            // changing max level should change merged config
            newService = (TestKsiService)GetService(new List <PduPayload>()
            {
                GetAggregationResponsePayload(Resources.KsiService_AggregationResponsePdu_RequestId_1584727638),
                GetAggregatorConfigResponsePayload(2, 2, 200, 4, new List <string>()
                {
                    "uri-2", "uri-3"
                })
            });

            secondService.SigningServiceProtocol.RequestResult = newService.SigningServiceProtocol.RequestResult;

            waitHandle.Reset();
            resultConf  = null;
            changeCount = 0;

            haService.Sign(inputHash);
            waitHandle.WaitOne(1000);
            Assert.IsNotNull(resultConf, "Could not get aggregator config using event handler (after 3rd sign request).");

            Assert.AreEqual(2, resultConf.MaxLevel, "Unexpected max level value (after 3rd sign request)");
            Assert.IsTrue(resultConf.AggregationAlgorithm == 1 || resultConf.AggregationAlgorithm == 2, "Unexpected algorithm value (after 3rd sign request)");
            Assert.AreEqual(200, resultConf.AggregationPeriod, "Unexpected aggregation period value (after 3rd sign request)");
            Assert.AreEqual(4, resultConf.MaxRequests, "Unexpected max requests value (after 3rd sign request)");
            Assert.AreEqual(1, resultConf.ParentsUris.Count, "Unexpected parent uri count (after 3rd sign request)");
            Assert.IsTrue(resultConf.ParentsUris[0] == "uri-1" || resultConf.ParentsUris[0] == "uri-2", "Unexpected parent uri value at position 0 (after 3rd sign request)");

            // changing aggegation period should change merged config
            newService = (TestKsiService)GetService(new List <PduPayload>()
            {
                GetAggregationResponsePayload(Resources.KsiService_AggregationResponsePdu_RequestId_1584727638),
                GetAggregatorConfigResponsePayload(2, 2, 100, 4, new List <string>()
                {
                    "uri-2", "uri-3"
                })
            });

            secondService.SigningServiceProtocol.RequestResult = newService.SigningServiceProtocol.RequestResult;

            waitHandle.Reset();
            resultConf  = null;
            changeCount = 0;

            haService.Sign(inputHash);
            waitHandle.WaitOne(1000);
            Assert.IsNotNull(resultConf, "Could not get aggregator config using event handler (after 4th sign request).");

            Assert.AreEqual(2, resultConf.MaxLevel, "Unexpected max level value (after 4th sign request)");
            Assert.IsTrue(resultConf.AggregationAlgorithm == 1 || resultConf.AggregationAlgorithm == 2, "Unexpected algorithm value (after 4th sign request)");
            Assert.AreEqual(100, resultConf.AggregationPeriod, "Unexpected aggregation period value (after 4th sign request)");
            Assert.AreEqual(4, resultConf.MaxRequests, "Unexpected max requests value (after 4th sign request)");
            Assert.AreEqual(1, resultConf.ParentsUris.Count, "Unexpected parent uri count (after 4th sign request)");
            Assert.IsTrue(resultConf.ParentsUris[0] == "uri-1" || resultConf.ParentsUris[0] == "uri-2", "Unexpected parent uri value at position 0 (after 4th sign request)");

            // changing max requests should change merged config
            newService = (TestKsiService)GetService(new List <PduPayload>()
            {
                GetAggregationResponsePayload(Resources.KsiService_AggregationResponsePdu_RequestId_1584727638),
                GetAggregatorConfigResponsePayload(2, 2, 200, 5, new List <string>()
                {
                    "uri-2", "uri-3"
                })
            });

            secondService.SigningServiceProtocol.RequestResult = newService.SigningServiceProtocol.RequestResult;

            waitHandle.Reset();
            resultConf  = null;
            changeCount = 0;
            haService.Sign(inputHash);
            waitHandle.WaitOne(1000);

            Assert.IsNotNull(resultConf, "Could not get aggregator config using event handler (after 5th sign request).");

            Assert.AreEqual(2, resultConf.MaxLevel, "Unexpected max level value (after 5th sign request)");
            Assert.IsTrue(resultConf.AggregationAlgorithm == 1 || resultConf.AggregationAlgorithm == 2, "Unexpected algorithm value (after 5th sign request)");
            Assert.AreEqual(200, resultConf.AggregationPeriod, "Unexpected aggregation period value (after 5th sign request)");
            Assert.AreEqual(5, resultConf.MaxRequests, "Unexpected max requests value (after 5th sign request)");
            Assert.AreEqual(1, resultConf.ParentsUris.Count, "Unexpected parent uri count (after 5th sign request)");
            Assert.IsTrue(resultConf.ParentsUris[0] == "uri-1" || resultConf.ParentsUris[0] == "uri-2", "Unexpected parent uri value at position 0 (after 5th sign request)");

            // signing again should not change merged config
            waitHandle.Reset();
            resultConf  = null;
            changeCount = 0;

            haService.Sign(inputHash);
            waitHandle.WaitOne(1000);
            Assert.IsNull(resultConf, "Aggregator config should have not changed (after 6th sign request");
            Assert.AreEqual(0, changeCount, "Unexpected change count.");
        }
        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");
        }
 private void Service_AggregatorConfigChanged(object sender, AggregatorConfigChangedEventArgs e)
 {
     _aggregatorConfig = e.AggregatorConfig;
     _waitHandle.Set();
 }