コード例 #1
0
        public async Task TelemetryIsSentAsync(string testFilename, int configuredSessions,
                                               int configuredSubscriptions, int configuredMonitoredItems)
        {
            string methodName     = UnitTestHelper.GetMethodName();
            string fqTempFilename = string.Empty;
            string fqTestFilename = $"{Directory.GetCurrentDirectory()}/testdata/telemetry/{testFilename}";

            fqTempFilename = $"{Directory.GetCurrentDirectory()}/tempdata/{methodName}_{testFilename}";
            if (File.Exists(fqTempFilename))
            {
                File.Delete(fqTempFilename);
            }
            File.Copy(fqTestFilename, fqTempFilename);
            PublisherNodeConfiguration.PublisherNodeConfigurationFilename = fqTempFilename;
            _output.WriteLine($"now testing: {PublisherNodeConfiguration.PublisherNodeConfigurationFilename}");
            Assert.True(File.Exists(PublisherNodeConfiguration.PublisherNodeConfigurationFilename));

            UnitTestHelper.SetPublisherDefaults();

            // mock IoTHub communication
            var hubMockBase = new Mock <HubCommunicationBase>();
            var hubMock     = hubMockBase.As <IHubCommunication>();

            hubMock.CallBase = true;
            Hub = hubMock.Object;

            // configure hub client mock
            var hubClientMockBase = new Mock <HubClient>();
            var hubClientMock     = hubClientMockBase.As <IHubClient>();
            int eventsReceived    = 0;

            hubClientMock.Setup(m => m.SendEventAsync(It.IsAny <Message>())).Callback <Message>(m => eventsReceived++);
            IotHubCommunication.IotHubClient = hubClientMock.Object;
            Hub.InitHubCommunicationAsync(hubClientMockBase.Object).Wait();

            try
            {
                long eventsAtStart = HubCommunicationBase.NumberOfEvents;
                NodeConfiguration = PublisherNodeConfiguration.Instance;
                Assert.True(NodeConfiguration.OpcSessions.Count == configuredSessions, "wrong # of sessions");
                Assert.True(NodeConfiguration.NumberOfOpcSessionsConfigured == configuredSessions, "wrong # of sessions");
                Assert.True(NodeConfiguration.NumberOfOpcSubscriptionsConfigured == configuredSubscriptions, "wrong # of subscriptions");
                Assert.True(NodeConfiguration.NumberOfOpcMonitoredItemsConfigured == configuredMonitoredItems, "wrong # of monitored items");
                int  seconds            = UnitTestHelper.WaitTilItemsAreMonitoredAndFirstEventReceived();
                long eventsAfterConnect = HubCommunicationBase.NumberOfEvents;
                await Task.Delay(2500).ConfigureAwait(false);

                long eventsAfterDelay = HubCommunicationBase.NumberOfEvents;
                _output.WriteLine($"# of events at start: {eventsAtStart}, # events after connect: {eventsAfterConnect}, # events after delay: {eventsAfterDelay}");
                _output.WriteLine($"sessions configured {NodeConfiguration.NumberOfOpcSessionsConfigured}, connected {NodeConfiguration.NumberOfOpcSessionsConnected}");
                _output.WriteLine($"subscriptions configured {NodeConfiguration.NumberOfOpcSubscriptionsConfigured}, connected {NodeConfiguration.NumberOfOpcSubscriptionsConnected}");
                _output.WriteLine($"items configured {NodeConfiguration.NumberOfOpcMonitoredItemsConfigured}, monitored {NodeConfiguration.NumberOfOpcMonitoredItemsMonitored}, toRemove {NodeConfiguration.NumberOfOpcMonitoredItemsToRemove}");
                _output.WriteLine($"waited {seconds} seconds till monitoring started, events generated {eventsReceived}");
                hubClientMock.VerifySet(m => m.ProductInfo = "OpcPublisher");
                Assert.True(eventsAfterDelay - eventsAtStart == 3);
            }
            finally
            {
                NodeConfiguration.Dispose();
                NodeConfiguration = null;
                Hub.Dispose();
                IotHubCommunication.IotHubClient = null;
                Hub = null;
            }
        }