コード例 #1
0
def test_missing_segment_name():

    with pytest.raises(SegmentNameMissingException):
        Segment(None)
コード例 #2
0
def test_reference_counting():

    segment = Segment('seg')
    subsegment = Subsegment('sub', 'local', segment)
    segment.add_subsegment(subsegment)
    subsegment = Subsegment('sub', 'local', segment)
    subsubsegment = Subsegment('subsub', 'local', segment)
    subsegment.add_subsegment(subsubsegment)

    assert not segment.ready_to_send()
    assert segment.ref_counter.get_current() == 2

    subsubsegment.close()
    assert not segment.ready_to_send()
    assert segment.ref_counter.get_current() == 1

    subsegment.close()
    assert not segment.ready_to_send()
    assert segment.ref_counter.get_current() == 0

    segment.close()
    assert segment.ready_to_send()
    assert segment.get_total_subsegments_size() == 2
コード例 #3
0
def test_flags_on_status_code():

    segment1 = Segment('seg')
    segment1.apply_status_code(429)
    assert segment1.throttle
    assert segment1.error

    segment2 = Segment('seg')
    segment2.apply_status_code(503)
    assert segment2.fault

    segment3 = Segment('seg')
    segment3.apply_status_code(403)
    assert segment3.error
コード例 #4
0
def test_mutate_closed_entity():

    segment = Segment('seg')
    segment.close()

    with pytest.raises(AlreadyEndedException):
        segment.put_annotation('key', 'value')

    with pytest.raises(AlreadyEndedException):
        segment.put_metadata('key', 'value')

    with pytest.raises(AlreadyEndedException):
        segment.put_http_meta('url', 'my url')

    with pytest.raises(AlreadyEndedException):
        segment.close()
コード例 #5
0
def test_segment_user():
    segment = Segment('seg')
    segment.set_user('whoami')
    doc = entity_to_dict(segment)

    assert doc['user'] == 'whoami'
コード例 #6
0
def test_serialize_segment_with_exception():

    class TestException(Exception):
        def __init__(self, message):
            super(TestException, self).__init__(message)

    segment_one = Segment('test')
    
    stack_one = [
        ('/path/to/test.py', 10, 'module', 'another_function()'),
        ('/path/to/test.py', 3, 'another_function', 'wrong syntax')
    ]
    
    stack_two = [
        ('/path/to/test.py', 11, 'module', 'another_function()'),
        ('/path/to/test.py', 4, 'another_function', 'wrong syntax')
    ]

    exception_one = TestException('test message one')
    exception_two = TestException('test message two')

    segment_one.add_exception(exception_one, stack_one, True)
    segment_one.add_exception(exception_two, stack_two, False)
    
    segment_one.close()
    
    expected_segment_one_dict = {
    "id": segment_one.id,
    "name": "test",
    "start_time": segment_one.start_time,
    "in_progress": False,
    "cause": {
        "working_directory": segment_one.cause['working_directory'],
        "exceptions": [
            {
                "id": exception_one._cause_id,
                "message": "test message one",
                "type": "TestException",
                "remote": True,
                "stack": [
                    {
                        "path": "test.py",
                        "line": 10,
                        "label": "module"
                    },
                    {
                        "path": "test.py",
                        "line": 3,
                        "label": "another_function"
                    }
                ]
            },
            {
                "id": exception_two._cause_id,
                "message": "test message two",
                "type": "TestException",
                "remote": False,
                "stack": [
                    {
                        "path": "test.py",
                        "line": 11,
                        "label": "module"
                    },
                    {
                        "path": "test.py",
                        "line": 4,
                        "label": "another_function"
                    }
                ]
            }
        ]
    },
    "trace_id": segment_one.trace_id,
    "fault": True,
    "end_time": segment_one.end_time
    }

    segment_two = Segment('test')
    subsegment = Subsegment('test', 'local', segment_two)

    subsegment.add_exception(exception_one, stack_one, True)
    subsegment.add_exception(exception_two, stack_two, False)
    subsegment.close()
    
    # will record cause id instead as same exception already recorded in its subsegment
    segment_two.add_exception(exception_one, stack_one, True)
    
    segment_two.close()
    
    expected_segment_two_dict = {
    "id": segment_two.id,
    "name": "test",
    "start_time": segment_two.start_time,
    "in_progress": False,
    "cause": exception_one._cause_id,
    "trace_id": segment_two.trace_id,
    "fault": True,
    "end_time": segment_two.end_time
    }

    actual_segment_one_dict = entity_to_dict(segment_one)
    actual_segment_two_dict = entity_to_dict(segment_two)
    
    assert expected_segment_one_dict == actual_segment_one_dict
    assert expected_segment_two_dict == actual_segment_two_dict
コード例 #7
0
def test_serialize_segment_with_http():

    segment = Segment('test')
    
    segment.put_http_meta(http.URL, 'https://aws.amazon.com')
    segment.put_http_meta(http.METHOD, 'get')
    segment.put_http_meta(http.USER_AGENT, 'test')
    segment.put_http_meta(http.CLIENT_IP, '127.0.0.1')
    segment.put_http_meta(http.X_FORWARDED_FOR, True)
    segment.put_http_meta(http.STATUS, 200)
    segment.put_http_meta(http.CONTENT_LENGTH, 0)
    
    segment.close()
    
    expected_segment_dict = {
    "id": segment.id,
    "name": "test",
    "start_time": segment.start_time,
    "in_progress": False,
    "http": {
        "request": {
            "url": "https://aws.amazon.com",
            "method": "get",
            "user_agent": "test",
            "client_ip": "127.0.0.1",
            "x_forwarded_for": True
        },
        "response": {
            "status": 200,
            "content_length": 0
        }
    },
    "trace_id": segment.trace_id,
    "end_time": segment.end_time
    }

    actual_segment_dict = entity_to_dict(segment)
    
    assert expected_segment_dict == actual_segment_dict
コード例 #8
0
def test_serialize_segment_with_metadata():

    class TestMetadata():
        def __init__(self, parameter_one, parameter_two):
            self.parameter_one = parameter_one
            self.parameter_two = parameter_two
        
            self.parameter_three = {'test'} #set
            self.parameter_four = {'a': [1, 2, 3], 'b': True, 'c': (1.1, 2.2), 'd': list} #dict
            self.parameter_five = [TestSubMetadata(datetime.time(9, 25, 31)), TestSubMetadata(datetime.time(23, 14, 6))] #list
        
    class TestSubMetadata():
        def __init__(self, time):
            self.time = time

    segment = Segment('test')
    
    segment.put_metadata('key_one', TestMetadata(1,2), 'namespace_one')
    segment.put_metadata('key_two', TestMetadata(3,4), 'namespace_two')
    
    segment.close()
    
    expected_segment_dict = {
    "id": segment.id,
    "name": "test",
    "start_time": segment.start_time,
    "in_progress": False,
    "metadata": {
        "namespace_one": {
            "key_one": {
                "parameter_one": 1,
                "parameter_two": 2,
                "parameter_three": [
                    "test"
                ],
                "parameter_four": {
                    "a": [
                        1,
                        2,
                        3
                    ],
                    "b": True,
                    "c": [
                        1.1,
                        2.2
                    ],
                    "d": str(list)
                },
                "parameter_five": [
                    {
                        "time": "09:25:31"
                    },
                    {
                        "time": "23:14:06"
                    }
                ]
            }
        },
        "namespace_two": {
            "key_two": {
                "parameter_one": 3,
                "parameter_two": 4,
                "parameter_three": [
                    "test"
                ],
                "parameter_four": {
                    "a": [
                        1,
                        2,
                        3
                    ],
                    "b": True,
                    "c": [
                        1.1,
                        2.2
                    ],
                    "d": str(list)
                },
                "parameter_five": [
                    {
                        "time": "09:25:31"
                    },
                    {
                        "time": "23:14:06"
                    }
                ]
            }
        }
    },
    "trace_id": segment.trace_id,
    "end_time": segment.end_time
    }

    actual_segment_dict = entity_to_dict(segment) 
    
    assert  expected_segment_dict == actual_segment_dict
コード例 #9
0
def test_create_segment():
    segment = Segment('test')
    assert segment.name == 'test'