Skip to content

Commit 362723e

Browse files
author
Peter Dolak
committed
Deprecate AsynchronousTransport
1 parent 2e08cf7 commit 362723e

3 files changed

Lines changed: 9 additions & 56 deletions

File tree

README.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,15 @@ Transport types
174174

175175
By default the client uses a simple non-buffered synchronous transport. The three available transport types are:
176176
* `NullTransport` - No requests, useful for disabling tracking in the Infinario constructor.
177-
* `SynchronousTransport` - Most operations are blocking for the time of a request to the Infinario API
177+
* `SynchronousTransport` - (default) Most operations are blocking for the time of a request to the Infinario API
178178
* `AsynchronousTransport` - Most operations are non-blocking (see the code for more information),
179179
buffered and using a single worker thread. Infinario client must be closed when no more data is to be tracked.
180+
**We recommend against using the AsynchronousTransport, as it cannot be guaranteed the data will be sent.**
181+
Data loss can for example happen in various events of system failure or even due to misuse.
182+
If you would like to track data from your code asynchronously, consider creating your own asynchronous workers
183+
using a library such as celery and use the SynchronousTransport to send the data from there.
180184

181-
Example of choosing a transport:
185+
Example of choosing `AsynchronousTransport`:
182186

183187
.. code-block:: python
184188

infinario.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ def __init__(self, **kwargs):
143143
self.__dict__.update(**kwargs)
144144

145145

146+
# DEPRECATED - we recommend against using this transport mode, we cannot guarantee all data will be sent
146147
class AsynchronousTransport(object):
147148
"""
148149
AsynchronousTransport is a buffered asynchronous transport using one lazy-initialized thread and requests.Session.

test_infinario_sdk.py

Lines changed: 2 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,13 @@
66
import unittest
77
import re
88
import requests
9-
import time
10-
from infinario import Infinario, SynchronousTransport, AsynchronousTransport
9+
from infinario import Infinario, SynchronousTransport
1110
try:
1211
from mock import MagicMock, patch
1312
except ImportError:
1413
from unittest.mock import MagicMock, patch
1514

1615

17-
def _fake_post_response(*args, **kwargs):
18-
mock = MagicMock()
19-
mock.status_code = 200
20-
mock.json = lambda: {'success': True, 'results': [{'status': 'ok'}]*20 + [{'status': 'retry'}]*50}
21-
return mock
22-
23-
2416
class TestInfinarioSDK(unittest.TestCase):
2517

2618
# helper for assertions about API requests
@@ -34,19 +26,6 @@ def _pop_post_calls(self, mock):
3426
mock.reset_mock()
3527
return calls
3628

37-
# helper for assertions about API bulk requests
38-
def _assert_bulk_events(self, mock, expected_leftovers, type):
39-
calls = self._pop_post_calls(mock)
40-
self.assertEquals(len(expected_leftovers), len(calls))
41-
for call, expected in zip(calls, expected_leftovers):
42-
self.assertEquals('bulk', call[0])
43-
events = 0
44-
for command in call[1]['commands']:
45-
self.assertEquals('crm/events', command['name'])
46-
self.assertEquals(type, command['data']['type'])
47-
events += 1
48-
self.assertEquals(expected, events)
49-
5029
# testing unidentified track, identify, identified track, update and identify at init with simple sync transport
5130
@patch.object(requests, 'Session')
5231
def test_synchronous_transport(self, session_mock):
@@ -76,38 +55,7 @@ def test_synchronous_transport(self, session_mock):
7655
]
7756
self.assertEquals(expected, self._pop_post_calls(session_mock))
7857

79-
# testing async transport - flush, buffer fill, timeout and close
80-
@unittest.skip("This test uses sleep() which means that it fails randomly. Skip it until it is fixed.")
81-
@patch.object(requests, 'Session')
82-
def test_asynchronous_transport(self, session_mock):
83-
session_mock().post.side_effect = _fake_post_response
84-
85-
# flush
86-
infinario = Infinario('t', target='nope/', transport=AsynchronousTransport)
87-
try:
88-
infinario.track('e1')
89-
infinario.flush()
90-
91-
time.sleep(0.2)
92-
self._assert_bulk_events(session_mock, [1], 'e1')
93-
94-
# full buffer, will 2x push 20 through, attempts max 50
95-
for _ in range(80):
96-
infinario.track('e2')
97-
98-
time.sleep(0.2)
99-
self._assert_bulk_events(session_mock, [50, 50], 'e2')
100-
101-
# timeout, again 2x 20
102-
time.sleep(1.1)
103-
self._assert_bulk_events(session_mock, [40, 20], 'e2')
104-
finally:
105-
# close
106-
infinario.track('e3')
107-
infinario.close()
108-
109-
time.sleep(0.2)
110-
self._assert_bulk_events(session_mock, [1], 'e3')
58+
# asynchronous transport test removed as it is deprecated
11159

11260

11361
class TestConvertTimestampArgument(unittest.TestCase):

0 commit comments

Comments
 (0)