Beispiel #1
0
def test_iteratively_disabled(httpserver: HTTPServer):
    p = configure_plugin_and_server(
        httpserver,
        IterativelyOptions(
            disabled=True,
            flush_queue_size=3,
            flush_interval=timedelta(seconds=1),
        ))

    try:
        p.load(PLUGIN_OPTIONS_DEV_NO_LOGGER)

        p.post_identify(*post_identify_1_args)
        p.post_identify(*post_identify_2_args)
        p.post_track(*post_track_1_args)
        p.post_group(*post_group_args)
        p.flush()

        p.post_page(*post_page_args)

        p.flush()
        p.flush()

        p.post_track(*post_track_2_args)
    finally:
        p.shutdown()

        time.sleep(0.1)
        httpserver.stop()
        requests = _get_cleaned_requests(httpserver)
        assert requests == []
Beispiel #2
0
def test_iteratively_retry(httpserver: HTTPServer):
    logger = CustomLogger()
    p = IterativelyPlugin(
        'My-Key', httpserver.url_for('/track'),
        IterativelyOptions(
            flush_queue_size=3,
            flush_interval=timedelta(seconds=1),
            retry_options=IterativelyRetryOptions(
                max_retries=3,
                delay_initial=timedelta(seconds=0.1),
                delay_maximum=timedelta(seconds=0.5)),
        ))
    expected_request = {'objects': [post_track_1_expected_request]}

    try:
        p.load(
            PluginLoadOptions(environment=Environment.DEVELOPMENT,
                              logger=logger))

        httpserver.expect_oneshot_request(
            re.compile('/track')).respond_with_data(status=429)
        httpserver.expect_oneshot_request(
            re.compile('/track')).respond_with_data(status=501)
        httpserver.expect_oneshot_request(
            re.compile('/track')).respond_with_data()
        p.post_track(*post_track_1_args)
        p.flush()
        time.sleep(2)
        requests = _get_cleaned_requests(httpserver)
        assert requests == [
            expected_request, expected_request, expected_request
        ]
        assert logger.log_lines == []

        httpserver.expect_oneshot_request(
            re.compile('/track')).respond_with_data(status=429)
        httpserver.expect_oneshot_request(
            re.compile('/track')).respond_with_data(status=501)
        httpserver.expect_oneshot_request(
            re.compile('/track')).respond_with_data(status=504)
        httpserver.expect_oneshot_request(
            re.compile('/track')).respond_with_data()
        p.post_track(*post_track_1_args)
        p.flush()
        time.sleep(2)
        requests = _get_cleaned_requests(httpserver)
        assert requests == [
            expected_request,
            expected_request,
            expected_request,
            expected_request,
            expected_request,
            expected_request,
        ]
        assert logger.log_lines == [
            'Error. Failed to upload events. Maximum attempts exceeded.'
        ]
    finally:
        p.shutdown()
        httpserver.stop()
Beispiel #3
0
def test_iteratively_omit_values(httpserver: HTTPServer):
    p = configure_plugin_and_server(
        httpserver, IterativelyOptions(omit_values=True, flush_queue_size=10))

    try:
        p.load(PLUGIN_OPTIONS_DEV_NO_LOGGER)

        p.post_identify(*post_identify_1_args)
        p.post_identify(*post_identify_2_args)
        p.post_track(*post_track_1_args)
        p.post_group(*post_group_args)
        p.post_page(*post_page_args)
        p.post_track(*post_track_2_args)
    finally:
        p.shutdown()

        time.sleep(0.1)
        httpserver.stop()
        requests = _get_cleaned_requests(httpserver)
        expected_requests = deepcopy([
            post_identify_1_expected_request, post_identify_2_expected_request,
            post_track_1_expected_request, post_group_expected_request,
            post_page_expected_request, post_track_2_expected_request
        ])

        for req in expected_requests:
            strip_property_values(req)

        assert requests == [{'objects': expected_requests}]
Beispiel #4
0
def test_flush_at_interval(httpserver: HTTPServer):
    p = configure_plugin_and_server(
        httpserver,
        IterativelyOptions(
            flush_queue_size=3,
            flush_interval=timedelta(seconds=1),
        ))

    try:
        p.load(PLUGIN_OPTIONS_DEV_NO_LOGGER)

        p.post_page(*post_page_args)
        time.sleep(0.1)
        requests = _get_cleaned_requests(httpserver)
        assert requests == []

        time.sleep(1.0)
        requests = _get_cleaned_requests(httpserver)
        assert requests == [
            {
                'objects': [post_page_expected_request]
            },
        ]
    finally:
        p.shutdown()
        httpserver.stop()
Beispiel #5
0
def test_flush_on_flush(httpserver: HTTPServer):
    p = configure_plugin_and_server(
        httpserver,
        IterativelyOptions(
            flush_queue_size=3,
            flush_interval=timedelta(seconds=1),
        ))

    try:
        p.load(PLUGIN_OPTIONS_DEV_NO_LOGGER)

        p.post_group(*post_group_args)
        time.sleep(0.1)
        requests = _get_cleaned_requests(httpserver)
        # Assert no requests (not flushed)
        assert requests == []

        p.flush()
        time.sleep(0.1)
        requests = _get_cleaned_requests(httpserver)
        # Assert requests were flushed
        assert requests == [{
            'objects': [
                post_group_expected_request,
            ]
        }]

        # Assert additional flushes do nothing
        p.flush()
        p.flush()
        time.sleep(0.1)
        requests = _get_cleaned_requests(httpserver)
        # Assert requests were flushed
        assert requests == [{
            'objects': [
                post_group_expected_request,
            ]
        }]
    finally:
        p.shutdown()
        httpserver.stop()
Beispiel #6
0
def test_flush_on_shutdown(httpserver: HTTPServer):
    p = configure_plugin_and_server(
        httpserver,
        IterativelyOptions(
            flush_queue_size=3,
            flush_interval=timedelta(seconds=1),
        ))

    assert p.id() == 'iteratively'
    try:
        p.load(PLUGIN_OPTIONS_DEV_NO_LOGGER)
        p.post_track(*post_track_2_args)

    finally:
        p.shutdown()

        time.sleep(0.1)
        httpserver.stop()
        requests = _get_cleaned_requests(httpserver)
        assert requests == [
            {
                'objects': [post_track_2_expected_request]
            },
        ]
Beispiel #7
0
def test_flush_at_queue_size(httpserver: HTTPServer):
    p = configure_plugin_and_server(
        httpserver,
        IterativelyOptions(
            flush_queue_size=3,
            flush_interval=timedelta(seconds=1),
        ))

    try:
        p.load(PLUGIN_OPTIONS_DEV_NO_LOGGER)

        p.post_identify(*post_identify_1_args)
        time.sleep(0.1)
        requests = _get_cleaned_requests(httpserver)
        # Assert no requests (not flushed @ 1)
        assert requests == []

        p.post_identify(*post_identify_2_args)
        time.sleep(0.1)
        requests = _get_cleaned_requests(httpserver)
        # Assert no requests (not flushed @ 2)
        assert requests == []

        p.post_track(*post_track_1_args)
        time.sleep(0.1)
        requests = _get_cleaned_requests(httpserver)
        # Assert requests (flushed @ 3)
        assert requests == [{
            'objects': [
                post_identify_1_expected_request,
                post_identify_2_expected_request, post_track_1_expected_request
            ]
        }]
    finally:
        p.shutdown()
        httpserver.stop()