Debian Codegraph @

testing/mozbase/mozgeckoprofiler/tests/test_view_gecko_profiler.py

raw
1 #!/usr/bin/env python
2
3 import os
4 import re
5 import shutil
6 import tempfile
7 import threading
8 import time
9 import unittest
10 from unittest import mock
11
12 import mozunit
13 import requests
14 import six
15 from mozgeckoprofiler import view_gecko_profile
16
17 if six.PY2:
18 # Import for Python 2
19 from urllib import unquote
20 else:
21 # Import for Python 3
22 from urllib.parse import unquote
23
24
25 def access_profiler_link(file_url, response):
26 """Attempts to access the profile in a loop for 5 seconds.
27
28 This is run from a separate thread.
29 """
30 timeout = 5 # seconds
31 start = time.time()
32
33 while time.time() - start < timeout:
34 # Poll the server to try and get a response.
35 result = requests.get(url=file_url)
36 if result.ok:
37 # Return the text back in a list.
38 response[0] = result.text
39 return
40 time.sleep(0.1)
41
42 response[0] = "Accessing the profiler link timed out after %s seconds" % timeout
43
44
45 class TestViewGeckoProfile(unittest.TestCase):
46 """Tests the opening local profiles in the Firefox Profiler."""
47
48 def setUp(self):
49 self.firefox_profiler_url = None
50 self.thread = None
51 self.response = [None]
52
53 def test_view_gecko_profile(self):
54 # Create a temporary fake performance profile.
55 temp_dir = tempfile.mkdtemp()
56 profile_path = os.path.join(temp_dir, "fakeprofile.json")
57 with open(profile_path, "w") as f:
58 f.write("FAKE_PROFILE")
59
60 # Mock the open_new_tab function so that we know when the view_gecko_profile
61 # function has done all of its work, and we can assert ressult of the
62 # user behavior.
63 def mocked_open_new_tab(firefox_profiler_url):
64 self.firefox_profiler_url = firefox_profiler_url
65 encoded_file_url = firefox_profiler_url.split("/")[-1]
66 decoded_file_url = unquote(encoded_file_url)
67 # Extract the actual file from the path.
68 self.thread = threading.Thread(
69 target=access_profiler_link, args=(decoded_file_url, self.response)
70 )
71 print("firefox_profiler_url %s" % firefox_profiler_url)
72 print("encoded_file_url %s" % encoded_file_url)
73 print("decoded_file_url %s" % decoded_file_url)
74 self.thread.start()
75
76 with mock.patch("webbrowser.open_new_tab", new=mocked_open_new_tab):
77 # Run the test
78 view_gecko_profile(profile_path)
79
80 self.thread.join()
81
82 # Compare the URLs, but replace the PORT value supplied, as that is dynamic.
83 expected_url = (
84 "https://profiler.firefox.com/from-url/"
85 "http%3A%2F%2F127.0.0.1%3A{PORT}%2Ffakeprofile.json"
86 )
87 actual_url = re.sub(r"%3A\d+%2F", "%3A{PORT}%2F", self.firefox_profiler_url)
88
89 self.assertEqual(
90 actual_url,
91 expected_url,
92 "The URL generated was correct for the Firefox Profiler.",
93 )
94 self.assertEqual(
95 self.response[0],
96 "FAKE_PROFILE",
97 "The response from the serve provided the profile contents.",
98 )
99
100 shutil.rmtree(temp_dir)
101
102
103 if __name__ == "__main__":
104 mozunit.main()