-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pass through ping schedule for Swift and Kotlin
- Loading branch information
Showing
9 changed files
with
177 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
glean-core/android/src/test/java/mozilla/telemetry/glean/pings/RidealongPingTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package mozilla.telemetry.glean.scheduler | ||
|
||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import mozilla.telemetry.glean.Glean | ||
import mozilla.telemetry.glean.delayMetricsPing | ||
import mozilla.telemetry.glean.getContext | ||
import mozilla.telemetry.glean.private.NoReasonCodes | ||
import mozilla.telemetry.glean.private.PingType | ||
import mozilla.telemetry.glean.resetGlean | ||
import mozilla.telemetry.glean.testing.GleanTestRule | ||
import mozilla.telemetry.glean.triggerWorkManager | ||
import okhttp3.mockwebserver.MockWebServer | ||
import org.junit.After | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import java.util.concurrent.TimeUnit | ||
|
||
/** | ||
* Testing behavior of custom pings. | ||
* | ||
* We already rely on the Rust side to test custom pings, | ||
* but this enables us to test the upload mechanism specifically. | ||
* | ||
* Even if this seemingly duplicates some of the testing, this should be kept around. | ||
*/ | ||
@RunWith(AndroidJUnit4::class) | ||
class RidealongPingTest { | ||
private val context = getContext() | ||
private lateinit var server: MockWebServer | ||
|
||
@get:Rule | ||
val gleanRule = GleanTestRule(context) | ||
|
||
@Before | ||
fun setup() { | ||
server = getMockWebServer() | ||
} | ||
|
||
@After | ||
fun teardown() { | ||
server.shutdown() | ||
} | ||
|
||
@Test | ||
fun `sends a ride-along custom ping on baseline schedule`() { | ||
delayMetricsPing(context) | ||
resetGlean( | ||
context, | ||
Glean.configuration.copy( | ||
serverEndpoint = "http://" + server.hostName + ":" + server.port, | ||
pingSchedule = mapOf("baseline" to listOf("custom-ping")), | ||
), | ||
clearStores = true, | ||
uploadEnabled = true, | ||
) | ||
|
||
// Define a new custom ping inline. | ||
PingType<NoReasonCodes>( | ||
name = "custom-ping", | ||
includeClientId = true, | ||
sendIfEmpty = true, | ||
preciseTimestamps = true, | ||
includeInfoSections = true, | ||
enabled = true, | ||
schedulesPings = emptyList(), | ||
reasonCodes = emptyList(), | ||
) | ||
|
||
Glean.handleBackgroundEvent() | ||
// Trigger it to upload | ||
triggerWorkManager(context) | ||
|
||
var request = server.takeRequest(2L, TimeUnit.SECONDS)!! | ||
var docType = request.path!!.split("/")[3] | ||
assertEquals("baseline", docType) | ||
|
||
request = server.takeRequest(2L, TimeUnit.SECONDS)!! | ||
docType = request.path!!.split("/")[3] | ||
assertEquals("custom-ping", docType) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
@testable import Glean | ||
import OHHTTPStubs | ||
import OHHTTPStubsSwift | ||
import XCTest | ||
|
||
final class RidealongPingTests: XCTestCase { | ||
var expectation: XCTestExpectation? | ||
|
||
override func tearDown() { | ||
Glean.shared.testDestroyGleanHandle() | ||
expectation = nil | ||
tearDownStubs() | ||
} | ||
|
||
func testSendRidealongPingWithBaseline() { | ||
|
||
let configuration = Configuration(pingSchedule: ["baseline": ["ridealong"]]) | ||
resetGleanDiscardingInitialPings(testCase: self, tag: "RidealongPingTests", configuration: configuration) | ||
|
||
// Register ping _after_ Glean has been initialized to avoid this being sent multiple times. | ||
_ = Ping<NoReasonCodes>( | ||
name: "ridealong", | ||
includeClientId: true, | ||
sendIfEmpty: true, | ||
preciseTimestamps: true, | ||
includeInfoSections: true, | ||
enabled: true, | ||
schedulesPings: [], | ||
reasonCodes: [] | ||
) | ||
|
||
// We receive a baseline ping, and a ridealong ping. | ||
// The order might vary. | ||
var pingsToReceive = ["baseline", "ridealong"] | ||
|
||
stubServerReceive { pingType, _ in | ||
XCTAssertTrue(!pingsToReceive.isEmpty, "No more pings expected") | ||
XCTAssertTrue(pingsToReceive.contains(pingType), "Expected ping types: \(pingsToReceive), got \(pingType)") | ||
pingsToReceive.removeAll(where: { $0 == pingType }) | ||
|
||
if pingsToReceive.isEmpty { | ||
DispatchQueue.main.async { | ||
// let the response get processed before we mark the expectation fulfilled | ||
self.expectation?.fulfill() | ||
} | ||
} | ||
} | ||
|
||
// Set up the expectation that will be fulfilled by the stub above | ||
expectation = expectation(description: "Pings Received") | ||
|
||
Glean.shared.submitPingByName("baseline") | ||
|
||
waitForExpectations(timeout: 5.0) { error in | ||
XCTAssertNil(error, "Test timed out waiting for upload: \(error!)") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters