/// <summary>
 /// Initializes a new instance of the <see cref="QuickPulseTelemetryModule"/> class. Internal constructor for unit tests only.
 /// </summary>
 /// <param name="collectionTimeSlotManager">Collection time slot manager.</param>
 /// <param name="dataAccumulatorManager">Data hub to sink QuickPulse data to.</param>
 /// <param name="serviceClient">QPS service client.</param>
 /// <param name="performanceCollector">Performance counter collector.</param>
 /// <param name="timings">Timings for the module.</param>
 internal QuickPulseTelemetryModule(
     QuickPulseCollectionTimeSlotManager collectionTimeSlotManager,
     QuickPulseDataAccumulatorManager dataAccumulatorManager,
     IQuickPulseServiceClient serviceClient,
     IPerformanceCollector performanceCollector,
     QuickPulseTimings timings)
     : this()
 {
     this.collectionTimeSlotManager = collectionTimeSlotManager;
     this.dataAccumulatorManager = dataAccumulatorManager;
     this.serviceClient = serviceClient;
     this.performanceCollector = performanceCollector;
     this.timings = timings;
 }
        public void QuickPulseCollectionTimeSlotManagerHandlesSecondHalfOfSecond()
        {
            // ARRANGE
            var manager = new QuickPulseCollectionTimeSlotManager();

            var now = new DateTimeOffset(2016, 1, 1, 0, 0, 1, TimeSpan.Zero);
            now = now.AddMilliseconds(501);

            // ACT
            DateTimeOffset slot = manager.GetNextCollectionTimeSlot(now);

            // ASSERT
            Assert.AreEqual(now.AddSeconds(1).AddMilliseconds(-1), slot);
        }
        /// <summary>
        /// Initialize method is called after all configuration properties have been loaded from the configuration.
        /// </summary>
        /// <param name="configuration">TelemetryConfiguration passed to the module.</param>
        public void Initialize(TelemetryConfiguration configuration)
        {
            if (!this.isInitialized)
            {
                lock (this.lockObject)
                {
                    if (!this.isInitialized)
                    {
                        QuickPulseEventSource.Log.ModuleIsBeingInitializedEvent(
                            string.Format(CultureInfo.InvariantCulture, "QuickPulseServiceEndpoint: '{0}'", this.QuickPulseServiceEndpoint));

                        QuickPulseEventSource.Log.TroubleshootingMessageEvent("Validating configuration...");
                        this.ValidateConfiguration(configuration);
                        this.config = configuration;

                        QuickPulseEventSource.Log.TroubleshootingMessageEvent("Initializing members...");
                        this.collectionTimeSlotManager = this.collectionTimeSlotManager ?? new QuickPulseCollectionTimeSlotManager();
                        this.dataAccumulatorManager = this.dataAccumulatorManager ?? new QuickPulseDataAccumulatorManager();
                        this.performanceCollector = this.performanceCollector ?? new PerformanceCollector();
                        this.timeProvider = this.timeProvider ?? new Clock();
                        this.timings = timings ?? QuickPulseTimings.Default;

                        this.InitializeServiceClient(configuration);

                        this.stateManager = new QuickPulseCollectionStateManager(
                            this.serviceClient,
                            this.timeProvider,
                            this.timings,
                            this.OnStartCollection,
                            this.OnStopCollection,
                            this.OnSubmitSamples,
                            this.OnReturnFailedSamples);

                        this.CreateStateThread();

                        this.isInitialized = true;
                    }
                }
            }
        }