Exemplo n.º 1
0
        async def test_probe_async_generator(self):
            output = io.StringIO()
            trace = record(StreamingTrace(output))

            @probe(trace)
            async def agen(x):
                await asyncio.sleep(0.0001)
                yield x
                await asyncio.sleep(0.0001)
                yield x * x

            async for y in agen(2):
                print(y)

            json_trace = parse_json_trace(output.getvalue())
            name = ("tests.test_async_probe."
                    "TestAsyncProbe.test_probe_async_generator."
                    "<locals>.agen")

            agen_traces = [x for x in json_trace if x["name"] == name]
            self.assertEquals(len(agen_traces), 12)
            self.assertEquals(agen_traces[0]["args"]["x"], repr(2))
            self.assertEquals(
                agen_traces[5]["args"][FunctionTracer._RETURN_KEY],
                repr(2),
            )
            self.assertEquals(
                agen_traces[9]["args"][FunctionTracer._RETURN_KEY],
                repr(4),
            )
Exemplo n.º 2
0
        async def test_probe_async_function(self):
            output = io.StringIO()
            trace = record(StreamingTrace(output))

            @probe(trace)
            async def test_function(alpha):
                print("Step 1")
                await asyncio.sleep(0.0001)
                print("Step 2")
                await asyncio.sleep(0.0001)
                print("Step 3")
                return alpha * alpha

            await test_function(2)
            json_trace = parse_json_trace(output.getvalue())

            name = ("tests.test_async_probe." +
                    "TestAsyncProbe.test_probe_async_function." +
                    "<locals>.test_function")
            coroutine_traces = [x for x in json_trace if x["name"] == name]

            self.assertEquals(len(coroutine_traces), 8)
            self.assertEquals(coroutine_traces[0]["args"]["alpha"], repr(2))
            self.assertEquals(
                coroutine_traces[-1]["args"][FunctionTracer._RETURN_KEY],
                repr(4),
            )
Exemplo n.º 3
0
    def test_method_name_with_self_removal(self):
        stream = io.StringIO()
        trace = record(StreamingTrace(stream))

        tr = TabulaRasa()
        with FunctionTracer(trace) as ft:
            tr.clear()

        trace_json = parse_json_trace(stream.getvalue())
        for key in ["name", "cat"]:
            self.assertEqual(
                trace_json[0][key], trace_json[-1][key],
            )
Exemplo n.º 4
0
    def test_argument_capture(self):
        with FunctionTracer(capture_args=lambda _1, _2, _3: True) as ft:
            some_function()

        json_trace = parse_json_trace(str(record(ft.get_trace())))

        self.assertEqual(
            json_trace["traceEvents"][1]["args"]["x"], "2",
        )
        self.assertEquals(
            json_trace["traceEvents"][2]["args"][FunctionTracer._RETURN_KEY],
            "4",
        )
Exemplo n.º 5
0
        async def test_async_method_name_with_self_removal(self):
            stream = io.StringIO()
            trace = record(StreamingTrace(stream))

            tr = TabulaRasa()
            with AsyncioTracer(trace) as at:
                await tr.async_clear()

            expected_name = "test_tracer.TabulaRasa.async_clear"
            trace_json = parse_json_trace(stream.getvalue())
            self.assertEqual(
                sum(1 for x in trace_json if x["name"] == expected_name), 6
            )

            self.assertEqual(len(at._name_cache), 0)
Exemplo n.º 6
0
    def test_simple_trace(self):
        with tempfile.NamedTemporaryFile(mode="w+") as outfile:
            with record_trace(outfile.name):
                print("Hello")
            trace_contents = record(outfile.read())

        # Add trailing ] to make it valid json
        trace_json = parse_json_trace(trace_contents)

        tests = {"name": "<built-in function print>", "cat": "c function"}

        for key, expected in tests.items():
            for i in [1, 2]:
                self.assertEqual(trace_json[i][key], expected)

        self.assertEquals(trace_json[1]["ph"], "B")
        self.assertEquals(trace_json[2]["ph"], "E")
Exemplo n.º 7
0
        async def test_nested_async_functions(self):
            output = io.StringIO()
            trace = record(StreamingTrace(output))

            @probe(trace)
            async def outer():
                return await unprobed()

            async def unprobed():
                return await inner()

            @probe(trace)
            async def inner():
                return 1

            await outer()

            json_trace = parse_json_trace(output.getvalue())
            name = "... nose2.<module> ..."
            self.assertEquals(sum(1 for x in json_trace if x["name"] == name),
                              4)
Exemplo n.º 8
0
        async def test_nested_async_generator(self):
            output = io.StringIO()
            trace = record(StreamingTrace(output))

            @probe(trace)
            async def outer():
                async for x in unprobed():
                    yield x

            async def unprobed():
                async for x in inner():
                    yield x

            @probe(trace)
            async def inner():
                yield 1337

            async for x in outer():
                print(x)

            json_trace = parse_json_trace(output.getvalue())
            name = "... nose2.<module> ..."
            self.assertEquals(sum(1 for x in json_trace if x["name"] == name),
                              6)