def get_client(secret): '''Returns or creates a new client by the secret''' if secret not in clients: clients[secret] = Client(secret) return clients[secret]
def test_synchronous(self): client = Client('testsecret', sync_mode=True) success, _ = client.identify('userId') self.assertFalse(client.consumers) self.assertTrue(client.queue.empty()) self.assertTrue(success)
def init(secret, **kwargs): """Create a default instance of a analytics-python client :param str secret: The Segment.io API Secret Kwargs: :param logging.LOG_LEVEL log_level: The logging log level for the client talks to. Use log_level=logging.DEBUG to troubleshoot : param bool log: False to turn off logging completely, True by default : param int flush_at: Specicies after how many messages the client will flush to the server. Use flush_at=1 to disable batching : param datetime.timedelta flush_after: Specifies after how much time of no flushing that the server will flush. Used in conjunction with the flush_at size policy : param bool async: True to have the client flush to the server on another thread, therefore not blocking code (this is the default). False to enable blocking and making the request on the calling thread. """ from analytics.client import Client # if we have already initialized, no-op if hasattr(this_module, 'default_client'): return default_client = Client(secret=secret, stats=stats, **kwargs) setattr(this_module, 'default_client', default_client)
def _proxy(method, *args, **kwargs): """Create an analytics client if one doesn't exist and send to it.""" global default_client if not default_client: default_client = Client(write_key, host=host, debug=debug, on_error=on_error, send=send) fn = getattr(default_client, method) fn(*args, **kwargs)
def test_synchronous(self): # Max queue size at 0 -> synchronous mode client = Client('testsecret', max_queue_size=0) # Ensure the consumer thread is not running client.consumer.pause() success, message = client.identify('userId') self.assertTrue(client.queue.empty()) self.assertTrue(success)
def test_overflow(self): client = Client('testsecret', max_queue_size=1) client.consumer.pause() time.sleep(5.1) # allow time for consumer to exit for i in range(10): client.identify('userId') success, msg = client.identify('userId') self.assertFalse(success)
def test_overflow(self): client = Client('testsecret', max_queue_size=1) # Ensure consumer thread is no longer uploading client.join() for _ in range(10): client.identify('userId') success, _ = client.identify('userId') # Make sure we are informed that the queue is at capacity self.assertFalse(success)
def _proxy(method, *args, **kwargs): """Create an analytics client if one doesn't exist and send to it.""" global default_client if not default_client: default_client = Client(write_key, host=host, debug=debug, max_queue_size=max_queue_size, send=send, on_error=on_error, gzip=gzip, max_retries=max_retries, sync_mode=sync_mode, timeout=timeout) fn = getattr(default_client, method) fn(*args, **kwargs)
def test_user_defined_flush_at(self): client = Client('testsecret', on_error=self.fail, flush_at=10, flush_interval=3) def mock_post_fn(*args, **kwargs): self.assertEqual(len(kwargs['batch']), 10) # the post function should be called 2 times, with a batch size of 10 # each time. with mock.patch('analytics.consumer.post', side_effect=mock_post_fn) \ as mock_post: for _ in range(20): client.identify('userId', {'trait': 'value'}) time.sleep(1) self.assertEqual(mock_post.call_count, 2)
def test_gzip(self): client = Client('testsecret', on_error=self.fail, gzip=True) for _ in range(10): client.identify('userId', {'trait': 'value'}) client.flush() self.assertFalse(self.failed)
def test_debug(self): Client('bad_key', debug=True)
def test_unicode(self): Client(six.u('unicode_key'))
def test_success_on_invalid_write_key(self): client = Client('bad_key', on_error=self.fail) client.track('userId', 'event') client.flush() self.assertFalse(self.failed)
def test_default_timeout_15(self): client = Client('testsecret') self.assertEquals(client.consumer.timeout, 15)
def test_default_timeout_15(self): client = Client('testsecret') for consumer in client.consumers: self.assertEqual(consumer.timeout, 15)
def test_max_retries(self): self.assertIsNone(analytics.default_client) client = Client('testsecret', max_retries=42) for consumer in client.consumers: self.assertEqual(consumer.retries, 42)
def test_unicode(self): Client('unicode_key')
def test_user_defined_timeout(self): client = Client('testsecret', timeout=10) for consumer in client.consumers: self.assertEqual(consumer.timeout, 10)
def setUp(self): self.failed = False self.client = Client('testsecret', on_error=self.fail)
def test_proxies(self): client = Client('testsecret', proxies='203.243.63.16:80') success, msg = client.identify('userId', {'trait': 'value'}) self.assertTrue(success)
def test_user_defined_timeout(self): client = Client('testsecret', timeout=10) self.assertEquals(client.consumer.timeout, 10)