Skip to content

Commit ff9682e

Browse files
authored
Simplify opencensus quickstart tests. (#2990)
* Simplify opencensus tests. * Fix lint error * Only check stdout. * Remember this is python, not java.
1 parent fc80b56 commit ff9682e

File tree

2 files changed

+8
-65
lines changed

2 files changed

+8
-65
lines changed

opencensus/metrics_quickstart.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,13 @@ def main():
6262
# Record 100 fake latency values between 0 and 5 seconds.
6363
for num in range(100):
6464
ms = random() * 5 * 1000
65-
print("Latency {}: {}".format(num, ms))
6665

6766
mmap = stats.stats.stats_recorder.new_measurement_map()
6867
mmap.measure_float_put(LATENCY_MS, ms)
6968
mmap.record()
7069

70+
print("Fake latency recorded ({}: {})".format(num, ms))
71+
7172
# Keep the thread alive long enough for the exporter to export at least
7273
# once.
7374
time.sleep(65)

opencensus/metrics_quickstart_test.py

Lines changed: 6 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -12,69 +12,11 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from unittest import mock
16-
import unittest
15+
import metrics_quickstart
1716

18-
from opencensus.ext.stackdriver import stats_exporter
19-
from opencensus.stats import aggregation
20-
from opencensus.stats import measure
21-
from opencensus.stats import stats
22-
from opencensus.stats import view
2317

24-
25-
class TestMetricsQuickstart(unittest.TestCase):
26-
"""Sanity checks for the OpenCensus metrics quickstart examples.
27-
28-
These tests check that a few basic features of the library work as expected
29-
in order for the demo to run. See the opencensus and
30-
opencensus-ext-stackdriver source for actual library tests.
31-
"""
32-
def test_measure_creation(self):
33-
measure.MeasureFloat(
34-
"task_latency",
35-
"The task latency in milliseconds",
36-
"ms")
37-
38-
def test_view_creation(self):
39-
test_view = view.View(
40-
"task_latency_distribution",
41-
"The distribution of the task latencies",
42-
[],
43-
mock.Mock(),
44-
aggregation.DistributionAggregation([1.0, 2.0, 3.0]))
45-
# Check that metric descriptor conversion doesn't crash
46-
test_view.get_metric_descriptor()
47-
48-
# Don't modify global stats
49-
@mock.patch('opencensus.ext.stackdriver.stats_exporter.stats.stats',
50-
stats._Stats())
51-
def test_measurement_map_record(self):
52-
mock_measure = mock.Mock()
53-
mock_measure_name = mock.Mock()
54-
mock_measure.name = mock_measure_name
55-
mock_view = mock.Mock()
56-
mock_view.columns = []
57-
mock_view.measure = mock_measure
58-
59-
stats.stats.view_manager.register_view(mock_view)
60-
61-
mmap = stats.stats.stats_recorder.new_measurement_map()
62-
mmap.measure_float_put(mock_measure, 1.0)
63-
mmap.record()
64-
65-
# Reaching into the stats internals here to check that recording the
66-
# measurement map affects view data.
67-
m2vd = (stats.stats.view_manager.measure_to_view_map
68-
._measure_to_view_data_list_map)
69-
self.assertIn(mock_measure_name, m2vd)
70-
[view_data] = m2vd[mock_measure_name]
71-
agg_data = view_data.tag_value_aggregation_data_map[tuple()]
72-
agg_data.add_sample.assert_called_once()
73-
74-
@mock.patch('opencensus.ext.stackdriver.stats_exporter'
75-
'.monitoring_v3.MetricServiceClient')
76-
def test_new_stats_exporter(self, mock_client):
77-
transport = stats_exporter.new_stats_exporter()
78-
self.assertIsNotNone(transport)
79-
self.assertIsNotNone(transport.options)
80-
self.assertIsNotNone(transport.options.project_id)
18+
def test_quickstart_main(capsys):
19+
# Run the quickstart, making sure that it runs successfully
20+
metrics_quickstart.main()
21+
output = capsys.readouterr()
22+
assert "Fake latency recorded" in output.out

0 commit comments

Comments
 (0)