Skip to content

Commit

Permalink
Test that flipping upload enabled respects order of events
Browse files Browse the repository at this point in the history
  • Loading branch information
badboy committed Jul 9, 2020
1 parent 414d619 commit b89154e
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import org.junit.runner.RunWith
import org.mockito.Mockito.mock
import org.mockito.Mockito.spy
import org.robolectric.shadows.ShadowProcess
import org.robolectric.shadows.ShadowLog
import java.io.File
import java.util.Calendar
import java.util.Locale
Expand Down Expand Up @@ -830,4 +831,73 @@ class GleanTest {
val request = server.takeRequest(20L, TimeUnit.SECONDS)
assertEquals(request.getHeader("X-Debug-ID"), "valid-tag")
}

@Test
fun `flipping upload enabled respects order of events`() {
// NOTES(janerik):
// I'm reasonably sure this test is excercising the right code paths
// and from the log output it does the right thing:
//
// * It fully initializes with the assumption uploadEnabled=true
// * It then disables upload
// * Then it submits the custom ping, which rightfully is ignored because uploadEnabled=false.
//
// The test passes.
// But it also does that for the old code and I think it's because of some weird WorkManager behaviour,
// where it doesn't actually start the work (= the upload).

// Redirecting log output, usually done by resetGlean, which we don't use here.
ShadowLog.stream = System.out
// This test relies on Glean not being initialized, we do that ourselves.
Glean.testDestroyGleanHandle()

// This test relies on testing mode to be disabled, since we need to prove the
// real-world async behaviour of this.
// We don't need to care about clearing it,
// the test-unit hooks will call `resetGlean` anyway.
Dispatchers.API.setTaskQueueing(true)
Dispatchers.API.setTestingMode(false)

// We create a ping and a metric before we initialize Glean
val pingName = "sample_ping_1"
val ping = PingType<NoReasonCodes>(
name = pingName,
includeClientId = true,
sendIfEmpty = false,
reasonCodes = listOf()
)
val stringMetric = StringMetricType(
disabled = false,
category = "telemetry",
lifetime = Lifetime.Ping,
name = "string_metric",
sendInPings = listOf(pingName)
)

val server = getMockWebServer()
val context = getContextWithMockedInfo()
val config = Glean.configuration.copy(
serverEndpoint = "http://" + server.hostName + ":" + server.port
)
Glean.initialize(context, true, config)

// Glean might still be initializing. Disable upload.
Glean.setUploadEnabled(false)

// Set data and try to submit a custom ping.
val testValue = "SomeTestValue"
stringMetric.set(testValue)
ping.submit()

// Trigger worker task to upload any submitted ping.
// We need to wait for the work to be enqueued first,
// since this test runs asynchronously.
waitForEnqueuedWorker(context, PingUploadWorker.PING_WORKER_TAG)
triggerWorkManager(context)

// Validate the received data.
val request = server.takeRequest(20L, TimeUnit.SECONDS)
val docType = request.path.split("/")[3]
assertEquals("deletion-request", docType)
}
}
58 changes: 58 additions & 0 deletions glean-core/ios/GleanTests/GleanTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -327,4 +327,62 @@ class GleanTests: XCTestCase {
)
}
}

func testFlippingUploadEnabledRespectsOrderOfEvents() {
// This test relies on Glean not being initialized
Glean.shared.testDestroyGleanHandle()
// This test relies on testing mode to be disabled, since we need to prove the
// real-world async behaviour of this.
// We don't need to care about clearing it,
// the test-unit hooks will call `resetGlean` anyway.
Dispatchers.shared.setTaskQueuing(enabled: true)
Dispatchers.shared.setTestingMode(enabled: false)

// We expect only a single ping later
stubServerReceive { pingType, _ in
XCTAssertEqual("deletion-request", pingType)

// Fulfill test's expectation once we parsed the incoming data.
DispatchQueue.main.async {
// Let the response get processed before we mark the expectation fulfilled
self.expectation?.fulfill()
}
}

let customPing = Ping<NoReasonCodes>(
name: "custom",
includeClientId: true,
sendIfEmpty: false,
reasonCodes: []
)

let counter = CounterMetricType(
category: "telemetry",
name: "counter_metric",
sendInPings: ["custom"],
lifetime: .application,
disabled: false
)

expectation = expectation(description: "Completed upload")

// 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 and to keep it from interfering
// with these tests.
let now = Date()
Glean.shared.metricsPingScheduler.updateSentDate(now)
// Restart glean
Glean.shared.resetGlean(clearStores: false)

// Glean might still be initializing. Disable upload.
Glean.shared.setUploadEnabled(false)

// Set data and try to submit a custom ping.
counter.add(1)
customPing.submit()

waitForExpectations(timeout: 5.0) { error in
XCTAssertNil(error, "Test timed out waiting for upload: \(error!)")
}
}
}
45 changes: 45 additions & 0 deletions glean-core/python/tests/test_glean.py
Original file line number Diff line number Diff line change
Expand Up @@ -756,3 +756,48 @@ def test_presubmit_makes_a_valid_ping(tmpdir, ping_schema_url, monkeypatch):
assert 0 == validate_ping.validate_ping(
io.StringIO(serialized_ping), sys.stdout, schema_url=ping_schema_url,
)


def test_flipping_upload_enabled_respects_order_of_events(tmpdir, monkeypatch):
Glean._reset()

info_path = Path(str(tmpdir)) / "info.txt"

# We create a ping and a metric before we initialize Glean
ping = PingType(
name="sample_ping_1",
include_client_id=True,
send_if_empty=True,
reason_codes=[],
)

# This test relies on testing mode to be disabled, since we need to prove the
# real-world async behaviour of this.
Dispatcher._testing_mode = False
Dispatcher._queue_initial_tasks = True

configuration = Glean._configuration
configuration.ping_uploader = _RecordingUploader(info_path)
Glean.initialize(
application_id=GLEAN_APP_ID,
application_version=glean_version,
upload_enabled=True,
configuration=Glean._configuration,
)

# Glean might still be initializing. Disable upload.
Glean.set_upload_enabled(False)
# Submit a custom ping.
ping.submit()

# Wait until the work is complete
Dispatcher._task_worker._queue.join()

while not info_path.exists():
time.sleep(0.1)

with info_path.open("r") as fd:
url_path = fd.readline()

# Validate we got the deletion-request ping
assert "deletion-request" == url_path.split("/")[3]

0 comments on commit b89154e

Please sign in to comment.