public IEnumerator MaximumNumberOfEvents_ShouldntDropEventsWhenSpaceIsAvailable_DataShouldBeAvailable()
        {
            var requestHandler = new BacktraceHttpClientMock()
            {
                IsHttpError = true
            };
            const int expectedMaximumNumberOfSummedEvents = 10;
            const int expectedNumberOfSummedEvents        = 3;
            var       backtraceMetrics = new BacktraceMetrics(_attributeProvider, 0, _defaultUniqueEventsSubmissionUrl, _defaultSummedEventsSubmissionUrl); backtraceMetrics.OverrideHttpClient(requestHandler);

            backtraceMetrics.MaximumSummedEvents = expectedMaximumNumberOfSummedEvents;
            for (int i = 0; i < expectedMaximumNumberOfSummedEvents - expectedNumberOfSummedEvents; i++)
            {
                backtraceMetrics.AddSummedEvent(MetricsEventName);
            }
            backtraceMetrics.Send();
            for (int i = 0; i < expectedNumberOfSummedEvents; i++)
            {
                backtraceMetrics.AddSummedEvent(MetricsEventName);
            }

            for (int i = 0; i < BacktraceMetrics.MaxNumberOfAttempts; i++)
            {
                yield return(new WaitForSeconds(1));

                // immidiately run next update
                var time = BacktraceMetrics.MaxTimeBetweenRequests + (BacktraceMetrics.MaxTimeBetweenRequests * i) + i + 1;
                backtraceMetrics.Tick(time);
            }
            yield return(new WaitForSeconds(1));

            Assert.AreEqual(expectedNumberOfSummedEvents, backtraceMetrics.Count());
        }
        public IEnumerator MetricsSubmission_ShouldTry3TimesOn503AndDropSummedEventsOnMaximumNumberOfEvents_DataWasDeleted()
        {
            const int expectedNumberOfEventsAfterFailure = 1; // unique events and we have enough place for session event so also session event
            var       requestHandler = new BacktraceHttpClientMock()
            {
                StatusCode = 503
            };
            var backtraceMetrics = new BacktraceMetrics(_attributeProvider, 0, _defaultUniqueEventsSubmissionUrl, _defaultSummedEventsSubmissionUrl);

            backtraceMetrics.OverrideHttpClient(requestHandler);
            for (int i = 0; i < backtraceMetrics.MaximumSummedEvents; i++)
            {
                backtraceMetrics.AddSummedEvent(MetricsEventName);
            }
            backtraceMetrics.AddUniqueEvent(UniqueAttributeName);

            backtraceMetrics.Send();
            for (int i = 0; i < BacktraceMetrics.MaxNumberOfAttempts; i++)
            {
                yield return(new WaitForSeconds(1));

                // immidiately run next update
                var time = BacktraceMetrics.MaxTimeBetweenRequests + (BacktraceMetrics.MaxTimeBetweenRequests * i) + i + 1;
                backtraceMetrics.Tick(time);
            }

            yield return(new WaitForSeconds(1));

            Assert.AreEqual(expectedNumberOfEventsAfterFailure, backtraceMetrics.Count());
            Assert.AreEqual(BacktraceMetrics.MaxNumberOfAttempts * 2, requestHandler.NumberOfRequests);
        }
        public IEnumerator MetricsSubmission_ShouldTry3TimesOn502BeforeDroppingEvents_DataWasntSendToBacktrace()
        {
            const int expectedNumberOfEventsAfterFailure = 2;
            var       requestHandler = new BacktraceHttpClientMock()
            {
                StatusCode = 502
            };
            var backtraceMetrics = new BacktraceMetrics(_attributeProvider, 0, _defaultUniqueEventsSubmissionUrl, _defaultSummedEventsSubmissionUrl);

            backtraceMetrics.OverrideHttpClient(requestHandler);

            backtraceMetrics.AddSummedEvent(MetricsEventName);
            backtraceMetrics.AddUniqueEvent(UniqueAttributeName);
            backtraceMetrics.Send();
            for (int i = 0; i < BacktraceMetrics.MaxNumberOfAttempts; i++)
            {
                yield return(new WaitForSeconds(1));

                // immidiately run next update
                var time = BacktraceMetrics.MaxTimeBetweenRequests + (BacktraceMetrics.MaxTimeBetweenRequests * i) + i + 1;
                backtraceMetrics.Tick(time);
            }

            yield return(new WaitForSeconds(1));

            Assert.AreEqual(BacktraceMetrics.MaxNumberOfAttempts * 2, requestHandler.NumberOfRequests);
            Assert.AreEqual(backtraceMetrics.Count(), expectedNumberOfEventsAfterFailure);
        }
        /// <summary>
        /// Update native client internal ANR timer.
        /// </summary>
        private void LateUpdate()
        {
            if (_nativeClient != null)
            {
                _nativeClient.UpdateClientTime(Time.unscaledTime);
            }

#if !UNITY_WEBGL
            if (_metrics != null)
            {
                _metrics.Tick(Time.unscaledTime);
            }
#endif

            if (BackgroundExceptions.Count == 0)
            {
                return;
            }
            while (BackgroundExceptions.Count > 0)
            {
                // use SendReport method isntead of Send method
                // because we already applied all watchdog/skipReport rules
                // so we don't need to apply them once again
                SendReport(BackgroundExceptions.Pop());
            }
        }
        public void BacktraceMetrics_ShouldntTriggerDownloadAfterTimeIntervalFirstHit_DataWasSendToBacktrace()
        {
            const int timeInterval     = 10;
            var       requestHandler   = new BacktraceHttpClientMock();
            var       backtraceMetrics = new BacktraceMetrics(_attributeProvider, timeInterval, _defaultUniqueEventsSubmissionUrl, _defaultSummedEventsSubmissionUrl);

            backtraceMetrics.OverrideHttpClient(requestHandler);

            backtraceMetrics.AddSummedEvent(MetricsEventName);
            backtraceMetrics.AddUniqueEvent(UniqueAttributeName);

            backtraceMetrics.Tick(timeInterval + 1);

            backtraceMetrics.AddSummedEvent(MetricsEventName);
            backtraceMetrics.AddUniqueEvent(UniqueAttributeName);

            backtraceMetrics.Tick(timeInterval + 2);

            Assert.AreEqual(2, requestHandler.NumberOfRequests);
            Assert.AreEqual(2, backtraceMetrics.Count());
        }
        public void BacktraceMetrics_ShouldTriggerUploadAfterTimeIntervalHitAgain_DataWasSendToBacktrace()
        {
            const int timeInterval     = 10;
            var       requestHandler   = new BacktraceHttpClientMock();
            var       backtraceMetrics = new BacktraceMetrics(_attributeProvider, timeInterval, _defaultUniqueEventsSubmissionUrl, _defaultSummedEventsSubmissionUrl);

            backtraceMetrics.OverrideHttpClient(requestHandler);

            backtraceMetrics.AddSummedEvent(MetricsEventName);
            backtraceMetrics.AddUniqueEvent(UniqueAttributeName);

            backtraceMetrics.Tick(timeInterval + 1);

            backtraceMetrics.AddSummedEvent(MetricsEventName);
            backtraceMetrics.AddUniqueEvent(UniqueAttributeName);

            backtraceMetrics.Tick((timeInterval * 2) + 1);

            // we added two unique events - but because we added two the same reports
            // they should be combined
            Assert.AreEqual(1, backtraceMetrics.Count());
            Assert.AreEqual(4, requestHandler.NumberOfRequests);
        }
        public void BacktraceMetrics_ShouldTriggerUploadProcessWhenTimeIntervalIsEqualToZero_DataWasntSendToTheService()
        {
            var requestHandler   = new BacktraceHttpClientMock();
            var backtraceMetrics = new BacktraceMetrics(_attributeProvider, 0, _defaultUniqueEventsSubmissionUrl, _defaultSummedEventsSubmissionUrl);

            backtraceMetrics.OverrideHttpClient(requestHandler);
            backtraceMetrics.AddSummedEvent(MetricsEventName);

            for (int i = 0; i < 1000; i++)
            {
                backtraceMetrics.Tick(i);
            }

            Assert.AreEqual(0, requestHandler.NumberOfRequests);
        }
        public void BacktraceMetrics_ShouldTriggerUploadAfterTimeIntervalHit_DataWasSendToBacktrace()
        {
            const int timeInterval             = 10;
            const int expectedNumberOfEvents   = 1; // we send successfully session event so we have one unique event
            const int expectedNumberOfRequests = 2;
            var       requestHandler           = new BacktraceHttpClientMock();
            var       backtraceMetrics         = new BacktraceMetrics(_attributeProvider, timeInterval, _defaultUniqueEventsSubmissionUrl, _defaultSummedEventsSubmissionUrl);

            backtraceMetrics.OverrideHttpClient(requestHandler);

            backtraceMetrics.AddSummedEvent(MetricsEventName);
            backtraceMetrics.AddUniqueEvent(UniqueAttributeName);
            backtraceMetrics.Tick(timeInterval + 1);


            Assert.AreEqual(expectedNumberOfEvents, backtraceMetrics.Count());
            Assert.AreEqual(expectedNumberOfRequests, requestHandler.NumberOfRequests);
        }
        public void BacktraceMetrics_ShouldntTriggerDownloadViaTickMethodWhenDidntReachMaximumNumberOfEvents_DataWasSendToBacktrace()
        {
            const int maximumNumberOfEvents  = 3;
            const int expectedNumberOfEvents = 2;
            var       backtraceMetrics       = new BacktraceMetrics(_attributeProvider, 0, _defaultUniqueEventsSubmissionUrl, _defaultSummedEventsSubmissionUrl)
            {
                MaximumSummedEvents = maximumNumberOfEvents
            };
            var requestHandler = new BacktraceHttpClientMock();

            backtraceMetrics.OverrideHttpClient(requestHandler);

            for (int i = 0; i < expectedNumberOfEvents; i++)
            {
                backtraceMetrics.AddSummedEvent(string.Format("{0} {1}", MetricsEventName, i));
            }

            backtraceMetrics.Tick(0);

            Assert.AreEqual(expectedNumberOfEvents, backtraceMetrics.Count());
            Assert.AreEqual(0, requestHandler.NumberOfRequests);
        }
        public void BacktraceMetrics_ShouldTriggerUploadViaTickMethodWhenReachedMaximumNumberOfEvents_DataWasSendToBacktrace()
        {
            const int maximumNumberOfEvents    = 3;
            const int defaultTimeIntervalInSec = 10;
            var       requestHandler           = new BacktraceHttpClientMock();
            var       backtraceMetrics         = new BacktraceMetrics(_attributeProvider, defaultTimeIntervalInSec, _defaultUniqueEventsSubmissionUrl, _defaultSummedEventsSubmissionUrl)
            {
                MaximumSummedEvents = maximumNumberOfEvents
            };

            backtraceMetrics.OverrideHttpClient(requestHandler);

            for (int i = 0; i < maximumNumberOfEvents; i++)
            {
                backtraceMetrics.AddSummedEvent(string.Format("{0} {1}", MetricsEventName, i));
            }

            backtraceMetrics.Tick(defaultTimeIntervalInSec + 1);

            Assert.AreEqual(0, backtraceMetrics.Count());
            Assert.AreEqual(1, requestHandler.NumberOfRequests);
        }