示例#1
0
    def test_choose_host(self, verify_token_mock, verify_connection_mock,
                         start_thread_mock):
        notify_mock = Mock()
        buffer_size = 1000

        # Test when only one host exists and it's the first time
        host = 'mockHost'
        sender = apysdk._Sender(host, 1234, buffer_size, 100, 100,
                                True, notify_mock)

        sender._choose_host()
        assert_equal(host, sender._http_host)

        # Make sure the host is picked again when called and not using random
        sender._choose_host()
        assert_equal(host, sender._http_host)

        # Test when there are multiple hosts
        hosts = ['1', '2', '3', '4', '5', '6']
        sender = apysdk._Sender(hosts, 1234, buffer_size, 100, 100,
                                True, notify_mock)
        sender._choose_host()
        assert_is_not_none(sender._http_host)

        # Make sure the host is not rechosen upon a `_choose_host` call
        for i in range(100):
            before = sender._http_host
            sender._choose_host()
            after = sender._http_host
            assert_not_equal(before, after)

            # Make sure the new host is properly inserted into the URLs
            assert_in(after, sender._rest_url)
            assert_in(after, sender._connection_validation_url)
示例#2
0
    def test_send_batch(self, session_mock):
        notify_mock = Mock()
        sender = apysdk._Sender('mockHost', 1234, 10, 100, 100, True,
                                notify_mock)
        encode = apysdk._json_enc.encode
        batch = [encode({"event": 1}), encode({"event": 2})]

        # Assert send batch sends a stringified batch
        sender._send_batch(batch)
        call_args = sender._session.post.call_args
        assert_equal(call_args[0][0], sender._rest_url)
        assert_equal(call_args[1]['data'], '[' + ','.join(batch) + ']')
        assert_equal(call_args[1]['headers'], consts.CONTENT_TYPE_JSON)

        # Assert send fails with proper exception
        sender._session.post.return_value = Mock(ok=False, status_code=500)
        assert_raises(exceptions.SendFailed, sender._send_batch, batch)

        # Assert bad token raises proper error
        sender._session.post.return_value = Mock(status_code=400)
        assert_raises(exceptions.BadToken, sender._send_batch, batch)

        # Assert batch too big raises proper exception
        sender._session.post.side_effect = requests.exceptions.ConnectionError
        assert_raises(exceptions.BatchTooBig, sender._send_batch, batch)
示例#3
0
    def test_get_batch(self, verify_token_mock, verify_connection_mock,
                       start_thread_mock):
        notify_mock = Mock()
        buffer_size = 1000
        sender = apysdk._Sender('mockHost', 1234, buffer_size, 100, 100,
                                True, notify_mock)

        # Populate the queue
        for i in range(buffer_size):
            sender._event_queue.put_nowait(
                json.dumps({'num': i, 'some_field': 'some_val'}))

        # Assert we comply with the max batch size (ignore last event)
        last_batch_time = datetime.datetime.utcnow()
        batch = sender._get_batch(last_batch_time)
        assert_true(len(''.join(batch)) < sender._batch_max_size)

        # Assert we comply with the max batch interval
        last_batch_time = datetime.datetime.utcnow() - datetime.timedelta(
            seconds=sender._batch_max_interval)
        try:
            raised_empty_batch = False
            sender._get_batch(last_batch_time)
        except exceptions.EmptyBatch:
            raised_empty_batch = True
        assert_true(raised_empty_batch)
示例#4
0
 def test_verify_token(self, session_mock, start_sender_mock):
     # Assert the function throws the right exception when it fails
     sender = apysdk._Sender('12', 1234, 10, 100, 100, 'asd', True)
     sender._notify = Mock()
     sender._connection_validation_url = 'asd'
     sender._session.get.return_value = Mock(ok=False)
     sender._verify_token()
示例#5
0
    def test_get_event(self, start_thread_mock, verify_token_mock,
                           verify_connection_mock):
        # Assert events are properly dequeued
        notify_mock = Mock()
        batch_size = 25
        sender = apysdk._Sender('mockHost', 1234, 2, 10, batch_size, True,
                                notify_mock)
        event = {'rofl': 'lol'}
        event_str = apysdk._json_enc.encode(event)
        sender._event_queue.put(event_str)
        sender._exceeding_event = None
        assert_equal(event_str, sender._Sender__get_event())

        # Assert exceeding event is dequeued first and events aren't lost
        exceeding_event = {'lmao': 'trololol'}
        sender._event_queue.put(event_str)
        exceeding_event_str = apysdk._json_enc.encode(exceeding_event)
        sender._exceeding_event = exceeding_event_str
        assert_equal(exceeding_event_str, sender._Sender__get_event())
        assert_equal(event_str, sender._Sender__get_event())

        # Assert oversized event is omitted
        oversized_event = {'key': ('a' * batch_size)}
        oversized_event_str = apysdk._json_enc.encode(oversized_event)
        oversized_event_size = len(oversized_event_str)
        sender._event_queue.put(oversized_event_str)
        sender._event_queue.put(event_str)

        pulled_event = sender._Sender__get_event()
        assert_equal(event_str, pulled_event)
        assert_equal(sender._notify.call_args[0],
                     (logging.WARNING,
                      consts.LOG_MSG_OMITTED_OVERSIZED_EVENT %
                      oversized_event_size))
示例#6
0
    def test_get_batch_empty_queue_raises(self, verify_token_mock,
                                          verify_connection_mock,
                                          start_thread_mock):
        notify_mock = Mock()
        buffer_size = 1000
        sender = apysdk._Sender('mockHost', 1234, buffer_size, 100, 100,
                                True, notify_mock)

        # Assert empty queue raises EmptyBatch
        last_batch_time = datetime.datetime.utcnow()
        raised = False
        try:
            sender._get_batch(last_batch_time)
        except exceptions.EmptyBatch:
            raised = True
        assert_true(raised)
示例#7
0
    def test_enqueue_event(self, start_thread_mock, verify_token_mock,
                           verify_connection_mock):
        # Test that event is enqueued properly
        notify_mock = Mock()
        sender = apysdk._Sender('mockHost', 1234, 1, 10, 10,
                                True, notify_mock)
        some_event = {'event': 1}
        ret = sender.enqueue_event(some_event, False)
        assert_true(ret)
        assert_equal(sender._event_queue.get_nowait(), some_event)

        # Test failing with notification when buffer is full
        sender.enqueue_event(some_event, False)
        assert_false(sender.enqueue_event(some_event, False))
        assert_equal(notify_mock.call_args[0], (logging.WARNING,
                                                consts.LOG_MSG_BUFFER_FULL))
        assert_true(sender._notified_buffer_full)

        # Test recovering when buffer frees up
        sender._event_queue.get_nowait()
        assert_true(sender.enqueue_event(some_event, False))
        assert_equal(notify_mock.call_args[0], (logging.WARNING,
                                                consts.LOG_MSG_BUFFER_FREED))