|
15 | 15 | # pylint: disable=too-many-lines |
16 | 16 | # pylint: disable=no-member |
17 | 17 |
|
| 18 | +import copy |
18 | 19 | import shutil |
19 | 20 | import subprocess |
20 | 21 | import unittest |
@@ -708,6 +709,63 @@ def test_link_dropped_attributes(self): |
708 | 709 | ) |
709 | 710 | self.assertEqual(link2.dropped_attributes, 0) |
710 | 711 |
|
| 712 | + def test_deepcopy(self): |
| 713 | + context = trace_api.SpanContext( |
| 714 | + trace_id=0x000000000000000000000000DEADBEEF, |
| 715 | + span_id=0x00000000DEADBEF0, |
| 716 | + is_remote=False, |
| 717 | + ) |
| 718 | + attributes = BoundedAttributes( |
| 719 | + 10, {"key1": "value1", "key2": 42}, immutable=False |
| 720 | + ) |
| 721 | + events = [ |
| 722 | + trace.Event("event1", {"ekey": "evalue"}), |
| 723 | + trace.Event("event2", {"ekey2": "evalue2"}), |
| 724 | + ] |
| 725 | + links = [ |
| 726 | + trace_api.Link( |
| 727 | + context=trace_api.INVALID_SPAN_CONTEXT, |
| 728 | + attributes={"lkey": "lvalue"}, |
| 729 | + ) |
| 730 | + ] |
| 731 | + |
| 732 | + span = trace.ReadableSpan( |
| 733 | + name="test-span", |
| 734 | + context=context, |
| 735 | + attributes=attributes, |
| 736 | + events=events, |
| 737 | + links=links, |
| 738 | + status=Status(StatusCode.OK), |
| 739 | + ) |
| 740 | + |
| 741 | + span_copy = copy.deepcopy(span) |
| 742 | + |
| 743 | + self.assertEqual(span_copy.name, span.name) |
| 744 | + self.assertEqual(span_copy.status.status_code, span.status.status_code) |
| 745 | + self.assertEqual(span_copy.context.trace_id, span.context.trace_id) |
| 746 | + self.assertEqual(span_copy.context.span_id, span.context.span_id) |
| 747 | + |
| 748 | + self.assertEqual(dict(span_copy.attributes), dict(span.attributes)) |
| 749 | + attributes["key1"] = "mutated" |
| 750 | + self.assertNotEqual( |
| 751 | + span_copy.attributes["key1"], span.attributes["key1"] |
| 752 | + ) |
| 753 | + |
| 754 | + self.assertEqual(len(span_copy.events), len(span.events)) |
| 755 | + events[0] = trace.Event("mutated-event", {"mutated": "value"}) |
| 756 | + self.assertNotEqual(span_copy.events[0].name, events[0].name) |
| 757 | + self.assertEqual(span_copy.events[0].name, "event1") |
| 758 | + |
| 759 | + self.assertEqual(len(span_copy.links), len(span.links)) |
| 760 | + self.assertEqual( |
| 761 | + span_copy.links[0].attributes, span.links[0].attributes |
| 762 | + ) |
| 763 | + links[0] = trace_api.Link( |
| 764 | + context=trace_api.INVALID_SPAN_CONTEXT, |
| 765 | + attributes={"mutated": "link"}, |
| 766 | + ) |
| 767 | + self.assertNotIn("mutated", span_copy.links[0].attributes) |
| 768 | + |
711 | 769 |
|
712 | 770 | class DummyError(Exception): |
713 | 771 | pass |
|
0 commit comments