Esempio n. 1
0
        public async Task AssertSingleGatewaySource(Func <string, bool> predicate, int maxAttempts = 5)
        {
            int numberOfGw = this.Configuration.NumberOfGateways;
            var sourceIds  = new HashSet <string>(numberOfGw);

            for (int i = 0; i < maxAttempts && sourceIds.Count < numberOfGw; i++)
            {
                if (i > 0)
                {
                    var timeToWait = i * this.Configuration.EnsureHasEventDelayBetweenReadsInSeconds;
                    await Task.Delay(TimeSpan.FromSeconds(timeToWait));
                }

                foreach (var item in this.udpLogListener.GetEvents())
                {
                    var parsed = SearchLogEvent.Parse(item);
                    if (predicate(parsed.Message))
                    {
                        if (!string.IsNullOrEmpty(parsed.SourceId))
                        {
                            sourceIds.Add(parsed.SourceId);
                        }
                    }
                }

                if (sourceIds.Count == 1)
                {
                    return;
                }
            }

            Assert.True(sourceIds.Count == 1, $"Not 1 source, but {sourceIds.Count}");
        }
Esempio n. 2
0
        // Searches IoT Hub for messages
        internal async Task <SearchLogResult> SearchIoTHubMessageAsync(Func <EventData, string, string, bool> predicate, SearchLogOptions options = null)
        {
            var maxAttempts     = options?.MaxAttempts ?? this.Configuration.EnsureHasEventMaximumTries;
            var processedEvents = new HashSet <SearchLogEvent>();

            for (int i = 0; i < maxAttempts; i++)
            {
                if (i > 0)
                {
                    var timeToWait = i * this.Configuration.EnsureHasEventDelayBetweenReadsInSeconds;
                    if (!string.IsNullOrEmpty(options?.Description))
                    {
                        TestLogger.Log($"IoT Hub message '{options.Description}' not found, attempt {i}/{maxAttempts}, waiting {timeToWait} secs");
                    }
                    else
                    {
                        TestLogger.Log($"IoT Hub message not found, attempt {i}/{maxAttempts}, waiting {timeToWait} secs");
                    }

                    await Task.Delay(TimeSpan.FromSeconds(timeToWait));
                }

                foreach (var item in this.IoTHubMessages.GetEvents())
                {
                    try
                    {
                        var bodyText       = item.Body.Count > 0 ? Encoding.UTF8.GetString(item.Body) : string.Empty;
                        var searchLogEvent = new SearchLogEvent
                        {
                            SourceId = item.GetDeviceId(),
                            Message  = bodyText
                        };

                        processedEvents.Add(searchLogEvent);

                        item.SystemProperties.TryGetValue("iothub-connection-device-id", out var deviceId);
                        if (predicate(item, deviceId?.ToString() ?? string.Empty, bodyText))
                        {
                            return(new SearchLogResult(true, processedEvents, bodyText));
                        }
                    }
                    catch (Exception ex)
                    {
                        TestLogger.Log("Error searching in IoT Hub message log: " + ex.ToString());
                    }
                }
            }

            return(new SearchLogResult(false, processedEvents, string.Empty));
        }
Esempio n. 3
0
        async Task <SearchLogResult> SearchUdpLogs(Func <SearchLogEvent, bool> predicate, SearchLogOptions options = null)
        {
            var maxAttempts     = options?.MaxAttempts ?? this.Configuration.EnsureHasEventMaximumTries;
            var processedEvents = new HashSet <SearchLogEvent>();

            for (int i = 0; i < maxAttempts; i++)
            {
                if (i > 0)
                {
                    var timeToWait = i * this.Configuration.EnsureHasEventDelayBetweenReadsInSeconds;
                    if (!string.IsNullOrEmpty(options?.Description))
                    {
                        TestLogger.Log($"UDP log message '{options.Description}' not found, attempt {i}/{maxAttempts}, waiting {timeToWait} secs");
                    }
                    else
                    {
                        TestLogger.Log($"UDP log message not found, attempt {i}/{maxAttempts}, waiting {timeToWait} secs");
                    }

                    await Task.Delay(TimeSpan.FromSeconds(timeToWait));
                }

                var sourceIdFilter = options?.SourceIdFilter;

                foreach (var item in this.udpLogListener.GetEvents())
                {
                    var searchLogEvent = new SearchLogEvent(item);
                    processedEvents.Add(searchLogEvent);
                    if (!string.IsNullOrEmpty(sourceIdFilter) && !sourceIdFilter.Equals(searchLogEvent.SourceId))
                    {
                        continue;
                    }

                    if (predicate(searchLogEvent))
                    {
                        return(new SearchLogResult(true, processedEvents, item)
                        {
                            MatchedEvent = searchLogEvent
                        });
                    }
                }
            }

            return(new SearchLogResult(false, processedEvents));
        }
Esempio n. 4
0
        async Task <SearchLogResult> SearchIoTHubLogs(Func <SearchLogEvent, bool> predicate, SearchLogOptions options = null)
        {
            var maxAttempts     = options?.MaxAttempts ?? this.Configuration.EnsureHasEventMaximumTries;
            var processedEvents = new HashSet <SearchLogEvent>();

            for (int i = 0; i < maxAttempts; i++)
            {
                if (i > 0)
                {
                    var timeToWait = i * this.Configuration.EnsureHasEventDelayBetweenReadsInSeconds;
                    if (!string.IsNullOrEmpty(options?.Description))
                    {
                        TestLogger.Log($"IoT Hub message '{options.Description}' not found, attempt {i}/{maxAttempts}, waiting {timeToWait} secs");
                    }
                    else
                    {
                        TestLogger.Log($"IoT Hub message not found, attempt {i}/{maxAttempts}, waiting {timeToWait} secs");
                    }

                    await Task.Delay(TimeSpan.FromSeconds(timeToWait));
                }

                foreach (var item in this.IoTHubMessages.GetEvents())
                {
                    var bodyText       = item.Body.Count > 0 ? Encoding.UTF8.GetString(item.Body) : string.Empty;
                    var searchLogEvent = new SearchLogEvent
                    {
                        Message  = bodyText,
                        SourceId = item.GetDeviceId()
                    };

                    processedEvents.Add(searchLogEvent);
                    if (predicate(searchLogEvent))
                    {
                        return(new SearchLogResult(true, processedEvents, bodyText)
                        {
                            MatchedEvent = searchLogEvent
                        });
                    }
                }
            }

            return(new SearchLogResult(false, processedEvents));
        }