public void ReceiveServiceRestartTest()
        {
            var testAETConfigModel = GetTestAETConfigModel();

            var configurationDirectory = CreateTemporaryDirectory().FullName;

            var expectedGatewayReceiveConfig1 = TestGatewayReceiveConfigProvider.Config.With(
                receiveServiceConfig: GetTestGatewayReceiveServiceConfig(110),
                configurationServiceConfig: new ConfigurationServiceConfig(
                    configurationRefreshDelaySeconds: 1));

            ConfigurationProviderTests.Serialise(expectedGatewayReceiveConfig1, configurationDirectory, GatewayReceiveConfigProvider.GatewayReceiveConfigFileName);

            using (var client = GetMockInnerEyeSegmentationClient())
                using (var gatewayReceiveConfigProvider = CreateGatewayReceiveConfigProvider(configurationDirectory))
                    using (var receiveService = CreateReceiveService(gatewayReceiveConfigProvider.ReceiveServiceConfig))
                        using (var uploadQueue = receiveService.UploadQueue)
                            using (var configurationService = CreateConfigurationService(
                                       client,
                                       gatewayReceiveConfigProvider.ConfigurationServiceConfig,
                                       receiveService))
                            {
                                // Start the service
                                configurationService.Start();

                                uploadQueue.Clear(); // Clear the message queue

                                var expectedGatewayReceiveConfig2 = TestGatewayReceiveConfigProvider.Config.With(
                                    receiveServiceConfig: GetTestGatewayReceiveServiceConfig(111),
                                    configurationServiceConfig: new ConfigurationServiceConfig(
                                        expectedGatewayReceiveConfig1.ConfigurationServiceConfig.ConfigCreationDateTime.AddSeconds(5),
                                        expectedGatewayReceiveConfig1.ConfigurationServiceConfig.ApplyConfigDateTime.AddSeconds(10)));

                                ConfigurationProviderTests.Serialise(expectedGatewayReceiveConfig2, configurationDirectory, GatewayReceiveConfigProvider.GatewayReceiveConfigFileName);

                                SpinWait.SpinUntil(() => receiveService.StartCount == 2);

                                // Send on the old config
                                var result = DcmtkHelpers.SendFileUsingDCMTK(
                                    @"Images\1ValidSmall\1.dcm",
                                    expectedGatewayReceiveConfig1.ReceiveServiceConfig.GatewayDicomEndPoint.Port,
                                    ScuProfile.LEExplicitCT,
                                    TestContext,
                                    applicationEntityTitle: testAETConfigModel.CallingAET,
                                    calledAETitle: testAETConfigModel.CalledAET);

                                // Check this did not send on the old config
                                Assert.IsFalse(string.IsNullOrWhiteSpace(result));

                                // Send on the new config
                                result = DcmtkHelpers.SendFileUsingDCMTK(
                                    @"Images\1ValidSmall\1.dcm",
                                    expectedGatewayReceiveConfig2.ReceiveServiceConfig.GatewayDicomEndPoint.Port,
                                    ScuProfile.LEExplicitCT,
                                    TestContext,
                                    applicationEntityTitle: testAETConfigModel.CallingAET,
                                    calledAETitle: testAETConfigModel.CalledAET);

                                // Check this did send on the new config
                                Assert.IsTrue(string.IsNullOrWhiteSpace(result));

                                var receiveQueueItem = TransactionalDequeue <UploadQueueItem>(uploadQueue);

                                Assert.IsNotNull(receiveQueueItem);
                                Assert.AreEqual(testAETConfigModel.CallingAET, receiveQueueItem.CallingApplicationEntityTitle);
                                Assert.AreEqual(testAETConfigModel.CalledAET, receiveQueueItem.CalledApplicationEntityTitle);

                                Assert.IsFalse(string.IsNullOrEmpty(receiveQueueItem.AssociationFolderPath));

                                var saveDirectoryInfo = new DirectoryInfo(receiveQueueItem.AssociationFolderPath);

                                Assert.IsTrue(saveDirectoryInfo.Exists);

                                var files = saveDirectoryInfo.GetFiles();

                                // Check we received one file over this association
                                Assert.AreEqual(1, files.Length);

                                // Attempt to get another item from the queue
                                Assert.ThrowsException <MessageQueueReadException>(() => TransactionalDequeue <UploadQueueItem>(uploadQueue));
                            }
        }
        public async Task ProcessorServiceRestartTest()
        {
            var configurationDirectory = CreateTemporaryDirectory().FullName;

            var expectedGatewayProcessorConfig1 = TestGatewayProcessorConfigProvider.Config.With(
                configurationServiceConfig: new ConfigurationServiceConfig(
                    configurationRefreshDelaySeconds: 1));

            ConfigurationProviderTests.Serialise(expectedGatewayProcessorConfig1, configurationDirectory, GatewayProcessorConfigProvider.GatewayProcessorConfigFileName);

            var tempFolder = CreateTemporaryDirectory();

            foreach (var file in new DirectoryInfo(@"Images\1ValidSmall\").GetFiles())
            {
                file.CopyTo(Path.Combine(tempFolder.FullName, file.Name));
            }

            using (var dicomDataReceiver = new ListenerDataReceiver(new ListenerDicomSaver(CreateTemporaryDirectory().FullName)))
            {
                var eventCount = 0;
                var folderPath = string.Empty;

                dicomDataReceiver.DataReceived += (sender, e) =>
                {
                    folderPath = e.FolderPath;
                    Interlocked.Increment(ref eventCount);
                };

                var testAETConfigModel = GetTestAETConfigModel();

                StartDicomDataReceiver(dicomDataReceiver, testAETConfigModel.AETConfig.Destination.Port);

                using (var client = GetMockInnerEyeSegmentationClient())
                    using (var pushService = CreatePushService())
                        using (var uploadService = CreateUploadService(client))
                            using (var uploadQueue = uploadService.UploadQueue)
                                using (var downloadService = CreateDownloadService(client))
                                    using (var gatewayProcessorConfigProvider = CreateGatewayProcessorConfigProvider(configurationDirectory))
                                        using (var configurationService = CreateConfigurationService(
                                                   client,
                                                   gatewayProcessorConfigProvider.ConfigurationServiceConfig,
                                                   downloadService,
                                                   uploadService,
                                                   pushService))
                                        {
                                            // Start the service
                                            configurationService.Start();

                                            uploadQueue.Clear(); // Clear the message queue

                                            // Save a new config, this should be picked up and the services restart in 10 seconds.
                                            var expectedGatewayProcessorConfig2 = TestGatewayProcessorConfigProvider.Config.With(
                                                configurationServiceConfig: new ConfigurationServiceConfig(
                                                    expectedGatewayProcessorConfig1.ConfigurationServiceConfig.ConfigCreationDateTime.AddSeconds(5),
                                                    expectedGatewayProcessorConfig1.ConfigurationServiceConfig.ApplyConfigDateTime.AddSeconds(10)));

                                            ConfigurationProviderTests.Serialise(expectedGatewayProcessorConfig2, configurationDirectory, GatewayProcessorConfigProvider.GatewayProcessorConfigFileName);

                                            SpinWait.SpinUntil(() => pushService.StartCount == 2);
                                            SpinWait.SpinUntil(() => uploadService.StartCount == 2);
                                            SpinWait.SpinUntil(() => downloadService.StartCount == 2);

                                            TransactionalEnqueue(
                                                uploadQueue,
                                                new UploadQueueItem(
                                                    calledApplicationEntityTitle: testAETConfigModel.CalledAET,
                                                    callingApplicationEntityTitle: testAETConfigModel.CallingAET,
                                                    associationFolderPath: tempFolder.FullName,
                                                    rootDicomFolderPath: tempFolder.FullName,
                                                    associationGuid: Guid.NewGuid(),
                                                    associationDateTime: DateTime.UtcNow));

                                            SpinWait.SpinUntil(() => eventCount >= 3);

#pragma warning disable CA1508 // Avoid dead conditional code
                                            Assert.IsFalse(string.IsNullOrWhiteSpace(folderPath));
#pragma warning restore CA1508 // Avoid dead conditional code

                                            var dicomFile = await DicomFile.OpenAsync(new DirectoryInfo(folderPath).GetFiles()[0].FullName).ConfigureAwait(false);

                                            Assert.IsNotNull(dicomFile);
                                        }
            }
        }