66 import unittest
77import re
88import requests
9- import time
10- from infinario import Infinario , SynchronousTransport , AsynchronousTransport
9+ from infinario import Infinario , SynchronousTransport
1110try :
1211 from mock import MagicMock , patch
1312except 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-
2416class 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
11361class TestConvertTimestampArgument (unittest .TestCase ):
0 commit comments