def test_trace_integration(self): mock_wrap_start = mock.Mock() mock_wrap_run = mock.Mock() mock_threading = mock.Mock() wrap_start_result = 'wrap start result' wrap_run_result = 'wrap run result' mock_wrap_start.return_value = wrap_start_result mock_wrap_run.return_value = wrap_run_result mock_start_func = mock.Mock() mock_run_func = mock.Mock() mock_start_func.__name__ = 'start' mock_run_func.__name__ = 'run' setattr(mock_threading.Thread, 'start', mock_start_func) setattr(mock_threading.Thread, 'run', mock_run_func) patch_wrap_start = mock.patch( 'opencensus.trace.ext.threading.trace.wrap_threading_start', mock_wrap_start) patch_wrap_run = mock.patch( 'opencensus.trace.ext.threading.trace.wrap_threading_run', mock_wrap_run) patch_threading = mock.patch( 'opencensus.trace.ext.threading.trace.threading', mock_threading) with patch_wrap_start, patch_wrap_run, patch_threading: trace.trace_integration() self.assertEqual(getattr(mock_threading.Thread, 'start'), wrap_start_result) self.assertEqual(getattr(mock_threading.Thread, 'run'), wrap_run_result)
def test_handles_noop(self): trace.trace_integration() def assert_noop(): _tracer = execution_context.get_opencensus_tracer() self.assertTrue(isinstance(_tracer, NoopTracer)) return _tracer.span_context.trace_id pool = ThreadPoolExecutor(max_workers=1) with execution_context.get_opencensus_tracer().span(name='span1'): future = pool.submit(assert_noop) future.result()
def test_wrap_pool(self): _tracer = tracer.Tracer() execution_context.set_opencensus_tracer(tracer) trace.trace_integration() context = tracer.Tracer().span_context print(context.trace_id) pool = Pool(processes=1) with _tracer.span(name='span1'): result = pool.apply_async(fake_pooled_func, ()).get(timeout=1) self.assertEqual(result, context.trace_id)
def test_wrap_threading(self): global global_tracer mock_span = mock.Mock() span_id = '1234' mock_span.span_id = span_id mock_tracer = MockTracer(mock_span) execution_context.set_opencensus_tracer(mock_tracer) trace.trace_integration() t = threading.Thread(target=self.fake_threaded_func) t.start() t.join() assert isinstance(global_tracer, MockTracer)
def test_wrap_futures(self): _tracer = tracer.Tracer() execution_context.set_opencensus_tracer(tracer) trace.trace_integration() context = tracer.Tracer().span_context print(context.trace_id) pool = ThreadPoolExecutor(max_workers=1) with _tracer.span(name='span1'): future = pool.submit(fake_pooled_func) result = future.result() self.assertEqual(result, context.trace_id)