func testQueuedDataNotInOverdueMetricsPings()

in glean-core/ios/GleanTests/Scheduler/MetricsPingSchedulerTests.swift [141:227]


    func testQueuedDataNotInOverdueMetricsPings() {
        // Reset Glean and do not start it right away
        Glean.shared.testDestroyGleanHandle()
        Glean.shared.enableTestingMode()

        // Set the last time the "metrics" ping was sent to now. This is required for us to not
        // send a metrics pings the first time we initialize Glean.
        let now = Date()
        MetricsPingScheduler(true).updateSentDate(now)

        // Create a metric and set its value. We expect this to be sent in the first ping
        // that gets generated the SECOND time we start Glean.
        let expectedStringMetric = StringMetricType(CommonMetricData(
            category: "telemetry",
            name: "expected_metric",
            sendInPings: ["metrics"],
            lifetime: Lifetime.ping,
            disabled: false
        ))
        let expectedValue = "must-exist-in-the-first-ping"

        resetGleanDiscardingInitialPings(testCase: self, tag: "MetricsPingSchedulerTests", clearStores: false)

        expectedStringMetric.set(expectedValue)

        // Destroy Glean, it will retain the recorded metric.
        Glean.shared.testDestroyGleanHandle(false)

        // Create data and attempt to record data before Glean is initialized.  This will
        // be queued in the dispatcher.
        let stringMetric = StringMetricType(CommonMetricData(
            category: "telemetry",
            name: "canary_metric",
            sendInPings: ["metrics"],
            lifetime: Lifetime.ping,
            disabled: false
        ))
        let canaryValue = "must-not-be-in-the-first-ping"
        stringMetric.set(canaryValue)

        // Set the last time the "metrics" ping was sent to yesterday, which should make
        // the ping overdue and trigger it at startup.
        let yesterday = Calendar.current.date(byAdding: Calendar.Component.day, value: -1, to: now)
        MetricsPingScheduler(true).updateSentDate(yesterday!)

        stubServerReceive { pingType, json in
            if pingType != "metrics" {
                // Skip initial "active" baseline ping
                return
            }
            XCTAssert(json != nil)
            let metrics = json?["metrics"] as? [String: Any]
            let strings = metrics?["string"] as? [String: Any]

            // Ensure there is only the expected metric
            XCTAssertEqual(1, strings?.count,
                           "Must contain only the expected metric, content: \(JSONStringify(metrics!))")

            // Check the received metric's value against the expected value
            let receivedValue = strings?["telemetry.expected_metric"] as? String
            XCTAssertEqual(expectedValue, receivedValue, "Values must match")

            DispatchQueue.main.async {
                // let the response get processed before we mark the expectation fulfilled
                self.expectation?.fulfill()
            }
        }

        // Set our expectation that will be fulfilled by the stub above
        expectation = expectation(description: "Metrics Ping Received")

        // Initialize Glean the SECOND time: it will send the expected string metric (stored from
        // the previous run) but must not send the canary string, which would be sent the next time
        // the "metrics" ping is collected after this one.
        // Glean.shared.initialize(uploadEnabled: true)
        Glean.shared.enableTestingMode()
        Glean.shared.setLogPings(true)
        Glean.shared.initialize(uploadEnabled: true, buildInfo: stubBuildInfo())
        // Enable ping logging for all tests
        waitForExpectations(timeout: 5.0) { error in
            XCTAssertNil(error, "Test timed out waiting for upload: \(error!)")
        }

        // Clean up
        resetGleanDiscardingInitialPings(testCase: self, tag: "MetricsPingSchedulerTests")
        Glean.shared.testDestroyGleanHandle()
    }