Bug 1502849 - Create reusable ping filters for telemetry-tests-client harness; r=Dexter,davehunt
authorRaphael Pierzina <rpierzina@mozilla.com>
Tue, 20 Nov 2018 13:37:06 +0000
changeset 504135 e786fe6c9aa153a5889ba2a46138620cb5927de2
parent 504134 fd8847c43e0d193513451e4a68e343cb915bdd2a
child 504136 a92340f218538b107ed3a8b9ec19a980b9a7c160
push id10290
push userffxbld-merge
push dateMon, 03 Dec 2018 16:23:23 +0000
treeherdermozilla-beta@700bed2445e6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersDexter, davehunt
bugs1502849
milestone65.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1502849 - Create reusable ping filters for telemetry-tests-client harness; r=Dexter,davehunt Differential Revision: https://phabricator.services.mozilla.com/D12288
toolkit/components/telemetry/tests/marionette/harness/telemetry_harness/ping_filters.py
toolkit/components/telemetry/tests/marionette/harness/telemetry_harness/testcase.py
toolkit/components/telemetry/tests/marionette/tests/client/test_main_tab_scalars.py
new file mode 100644
--- /dev/null
+++ b/toolkit/components/telemetry/tests/marionette/harness/telemetry_harness/ping_filters.py
@@ -0,0 +1,42 @@
+# 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/.
+
+
+class PingFilter(object):
+    """Ping filter that accepts any pings."""
+
+    def __call__(self, ping):
+        return True
+
+
+class MainPingFilter(PingFilter):
+    """Ping filter that accepts main pings."""
+
+    def __call__(self, ping):
+        if not super(MainPingFilter, self).__call__(ping):
+            return False
+
+        return ping["type"] == "main"
+
+
+class MainPingReasonFilter(MainPingFilter):
+    """Ping filter that accepts main pings that match the
+    specified reason.
+    """
+
+    def __init__(self, reason):
+        super(MainPingReasonFilter, self).__init__()
+        self.reason = reason
+
+    def __call__(self, ping):
+        if not super(MainPingReasonFilter, self).__call__(ping):
+            return False
+
+        return ping["payload"]["info"]["reason"] == self.reason
+
+
+ANY_PING = PingFilter()
+MAIN_PING = MainPingFilter()
+MAIN_SHUTDOWN_PING = MainPingReasonFilter("shutdown")
+MAIN_ENVIRONMENT_CHANGE_PING = MainPingReasonFilter("environment_change")
--- a/toolkit/components/telemetry/tests/marionette/harness/telemetry_harness/testcase.py
+++ b/toolkit/components/telemetry/tests/marionette/harness/telemetry_harness/testcase.py
@@ -78,32 +78,32 @@ class TelemetryTestCase(PuppeteerMixin, 
         }
 
         # Firefox will be forced to restart with the prefs enforced.
         self.marionette.enforce_gecko_prefs(telemetry_prefs)
 
         # Wait 5 seconds to ensure that telemetry has reinitialized
         time.sleep(5)
 
-    def wait_for_pings(self, action_func, ping_filter_func, count):
+    def wait_for_pings(self, action_func, ping_filter, count):
         """Call the given action and wait for pings to come in and return
         the `count` number of pings, that match the given filter.
         """
         # Keep track of the current number of pings
         current_num_pings = len(self.pings)
 
         # New list to store new pings that satisfy the filter
         filtered_pings = []
 
         def wait_func(*args, **kwargs):
             # Ignore existing pings in self.pings
             new_pings = self.pings[current_num_pings:]
 
             # Filter pings to make sure we wait for the correct ping type
-            filtered_pings[:] = [p for p in new_pings if ping_filter_func(p)]
+            filtered_pings[:] = [p for p in new_pings if ping_filter(p)]
 
             return len(filtered_pings) >= count
 
         self.logger.info(
             "wait_for_pings running action '{action}'.".format(
                 action=action_func.__name__,
             )
         )
--- a/toolkit/components/telemetry/tests/marionette/tests/client/test_main_tab_scalars.py
+++ b/toolkit/components/telemetry/tests/marionette/tests/client/test_main_tab_scalars.py
@@ -1,25 +1,14 @@
 # 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/.
 
 from telemetry_harness.testcase import TelemetryTestCase
-
-
-def check_is_shutdown_ping(ping):
-    """Check for a shutdown ping."""
-
-    if ping["type"] != "main":
-        return False
-
-    if ping["payload"]["info"]["reason"] != "shutdown":
-        return False
-
-    return True
+from telemetry_harness.ping_filters import MAIN_SHUTDOWN_PING
 
 
 class TestMainTabScalars(TelemetryTestCase):
     """Tests for Telemetry Scalars."""
 
     def test_main_tab_scalars(self):
         """Test for Telemetry Scalars."""
 
@@ -32,17 +21,17 @@ class TestMainTabScalars(TelemetryTestCa
             tab3 = self.browser.tabbar.open_tab()
             self.browser.tabbar.switch_to(tab3)
 
             self.browser.tabbar.close_tab(tab3, force=True)
             self.browser.tabbar.close_tab(tab2, force=True)
 
             self.browser.tabbar.switch_to(tab1)
 
-        [ping] = self.wait_for_pings(self.restart_browser, check_is_shutdown_ping, 1)
+        [ping] = self.wait_for_pings(self.restart_browser, MAIN_SHUTDOWN_PING, 1)
 
         assert ping["type"] == "main"
         assert ping["clientId"] == self.client_id
 
         scalars = ping["payload"]["processes"]["parent"]["scalars"]
         assert scalars["browser.engagement.max_concurrent_tab_count"] == 3
         assert scalars["browser.engagement.tab_open_event_count"] == 2
         assert scalars["browser.engagement.max_concurrent_window_count"] == 1