def test_gevent_watcher(self, mock_APIClient, mock_riw_spawn, mock_spawn):
     bridge = EdgeDataBridge(self.config)
     return_dict = {
         'type':
         'indexer',
         'database':
         bridge.db_name,
         'design_document':
         '_design/{}'.format(bridge.workers_config['resource']),
         'progress':
         99
     }
     bridge.server.tasks = MagicMock(return_value=[return_dict])
     bridge.filler = MagicMock()
     bridge.filler.exception = Exception('test_filler')
     bridge.input_queue_filler = MagicMock()
     bridge.input_queue_filler.exception = Exception('test_temp_filler')
     self.assertEqual(bridge.workers_pool.free_count(), bridge.workers_max)
     self.assertEqual(bridge.retry_workers_pool.free_count(),
                      bridge.retry_workers_max)
     bridge.gevent_watcher()
     self.assertEqual(bridge.workers_pool.free_count(),
                      bridge.workers_max - bridge.workers_min)
     self.assertEqual(bridge.retry_workers_pool.free_count(),
                      bridge.retry_workers_max - bridge.retry_workers_min)
     del bridge
    def test_bridge_stats(self):
        bridge = EdgeDataBridge(self.config)
        bridge.feeder = MagicMock()
        bridge.feeder.queue.qsize.return_value = 44
        bridge.feeder.backward_info = {
            'last_response': datetime.datetime.now(),
            'status': 1,
            'resource_item_count': 13
        }
        bridge.feeder.forward_info = {
            'last_response': datetime.datetime.now(),
            'resource_item_count': 31
        }
        bridge.filler = MagicMock()
        bridge.filler.exception = Exception('test_filler')
        bridge.input_queue_filler = MagicMock()
        bridge.input_queue_filler.exception = Exception('test_temp_filler')

        sleep(1)
        res = bridge.bridge_stats()
        keys = ['resource_items_queue_size', 'retry_resource_items_queue_size', 'workers_count',
                'api_clients_count', 'avg_request_duration', 'filter_workers_count',
                'retry_workers_count', 'min_avg_request_duration', 'max_avg_request_duration']
        for k, v in bridge.log_dict.items():
            self.assertEqual(res[k], v)
        for k in keys:
            self.assertEqual(res[k], 0)
        self.assertEqual(res['sync_backward_response_len'],
                         bridge.feeder.backward_info['resource_item_count'])
        self.assertEqual(res['sync_forward_response_len'],
                         bridge.feeder.forward_info['resource_item_count'])
        self.assertGreater(res['vms'], 0)
        self.assertGreater(res['rss'], 0)
        self.assertGreater(res['sync_backward_last_response'], 0)
        self.assertGreater(res['sync_forward_last_response'], 0)
        self.assertEqual(res['sync_queue'], 44)
        self.assertEqual(res['resource'], bridge.workers_config['resource'])
        self.assertEqual(res['_id'], bridge.workers_config['resource'])
        self.assertNotEqual(res['time'], '')

        bridge.feeder.backward_info['status'] = 0
        bridge.api_clients_info['c1'] = {'request_durations': {datetime.datetime.now(): 1}}
        bridge.api_clients_info['c2'] = {'request_durations': {datetime.datetime.now(): 2}}

        res = bridge.bridge_stats()
        self.assertEqual(res['sync_backward_last_response'], 0)
        self.assertNotEqual(res['max_avg_request_duration'], 0)
        self.assertNotEqual(res['min_avg_request_duration'], 0)