def test_500_error_when_sending_offline_heartbeats(self): with NamedTemporaryFile() as fh: with mock.patch('wakatime.offlinequeue.Queue._get_db_file' ) as mock_db_file: mock_db_file.return_value = fh.name response = Response() response.status_code = 500 self.patched[ 'wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response config = 'tests/samples/configs/good_config.cfg' now1 = u(int(time.time())) entity1 = 'tests/samples/codefiles/emptyfile.txt' project1 = 'proj1' args = [ '--file', entity1, '--config', config, '--time', now1, '--project', project1 ] execute(args) now2 = u(int(time.time())) entity2 = 'tests/samples/codefiles/twolinefile.txt' project2 = 'proj2' args = [ '--file', entity2, '--config', config, '--time', now2, '--project', project2 ] execute(args) # send heartbeats from offline queue after 201 response now3 = u(int(time.time())) entity3 = 'tests/samples/codefiles/python.py' project3 = 'proj3' args = [ '--file', entity3, '--config', config, '--time', now3, '--project', project3 ] response = CustomResponse() response.second_response_code = 500 response.limit = 2 response.response_text = '{"responses": [[null,201], [null,201], [null,201]]}' self.patched[ 'wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response retval = execute(args) self.assertEquals(retval, API_ERROR) # offline queue should still have saved heartbeats queue = Queue(None, None) saved_heartbeats = next(queue.pop_many()) self.assertNothingPrinted() self.assertEquals(len(saved_heartbeats), 2)
def test_leftover_heartbeats_saved_when_bulk_response_not_matching_length(self, logs): logging.disable(logging.NOTSET) with NamedTemporaryFile() as fh: with mock.patch('wakatime.offlinequeue.Queue._get_db_file') as mock_db_file: mock_db_file.return_value = fh.name entities = [ 'emptyfile.txt', 'twolinefile.txt', 'python.py', 'go.go', 'java.java', 'php.php', ] with TemporaryDirectory() as tempdir: for entity in entities: shutil.copy(os.path.join('tests/samples/codefiles', entity), os.path.join(tempdir, entity)) now = u(int(time.time())) key = str(uuid.uuid4()) args = ['--file', os.path.join(tempdir, entities[0]), '--key', key, '--config', 'tests/samples/configs/good_config.cfg', '--time', now, '--extra-heartbeats'] response = CustomResponse() response.response_code = 202 response.response_text = '{"responses": [[null,201], [null,201]]}' self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response with mock.patch('wakatime.main.sys.stdin') as mock_stdin: heartbeats = json.dumps([{ 'timestamp': now, 'entity': os.path.join(tempdir, entity), 'entity_type': 'file', 'is_write': False, } for entity in entities[1:]]) mock_stdin.readline.return_value = heartbeats retval = execute(args) self.assertEquals(retval, SUCCESS) self.assertNothingPrinted() queue = Queue(None, None) self.assertEquals(queue._get_db_file(), fh.name) saved_heartbeats = queue.pop_many() self.assertNothingPrinted() expected = "WakaTime WARNING Missing 4 results from api.\nWakaTime WARNING Missing 2 results from api." actual = self.getLogOutput(logs) self.assertEquals(actual, expected) # make sure extra heartbeats not matching server response were saved self.assertEquals(len(saved_heartbeats), 2)
def test_leftover_heartbeats_saved_when_bulk_response_not_matching_length(self, logs): logging.disable(logging.NOTSET) with NamedTemporaryFile() as fh: with mock.patch('wakatime.offlinequeue.Queue._get_db_file') as mock_db_file: mock_db_file.return_value = fh.name entities = [ 'emptyfile.txt', 'twolinefile.txt', 'python.py', 'go.go', 'java.java', 'php.php', ] with TemporaryDirectory() as tempdir: for entity in entities: shutil.copy(os.path.join('tests/samples/codefiles', entity), os.path.join(tempdir, entity)) now = u(int(time.time())) key = str(uuid.uuid4()) args = ['--file', os.path.join(tempdir, entities[0]), '--key', key, '--config', 'tests/samples/configs/good_config.cfg', '--time', now, '--extra-heartbeats'] response = CustomResponse() response.response_code = 202 response.response_text = '{"responses": [[null,201], [null,201]]}' self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response with mock.patch('wakatime.main.sys.stdin') as mock_stdin: heartbeats = json.dumps([{ 'timestamp': now, 'entity': os.path.join(tempdir, entity), 'entity_type': 'file', 'is_write': False, } for entity in entities[1:]]) mock_stdin.readline.return_value = heartbeats retval = execute(args) self.assertEquals(retval, SUCCESS) self.assertNothingPrinted() queue = Queue(None, None) self.assertEquals(queue._get_db_file(), fh.name) saved_heartbeats = next(queue.pop_many()) self.assertNothingPrinted() expected = "WakaTime WARNING Missing 4 results from api.\nWakaTime WARNING Missing 2 results from api." actual = self.getLogOutput(logs) self.assertEquals(actual, expected) # make sure extra heartbeats not matching server response were saved self.assertEquals(len(saved_heartbeats), 2)
def test_500_error_when_sending_offline_heartbeats(self): with NamedTemporaryFile() as fh: with mock.patch('wakatime.offlinequeue.Queue._get_db_file') as mock_db_file: mock_db_file.return_value = fh.name response = Response() response.status_code = 500 self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response config = 'tests/samples/configs/good_config.cfg' now1 = u(int(time.time())) entity1 = 'tests/samples/codefiles/emptyfile.txt' project1 = 'proj1' args = ['--file', entity1, '--config', config, '--time', now1, '--project', project1] execute(args) now2 = u(int(time.time())) entity2 = 'tests/samples/codefiles/twolinefile.txt' project2 = 'proj2' args = ['--file', entity2, '--config', config, '--time', now2, '--project', project2] execute(args) # send heartbeats from offline queue after 201 response now3 = u(int(time.time())) entity3 = 'tests/samples/codefiles/python.py' project3 = 'proj3' args = ['--file', entity3, '--config', config, '--time', now3, '--project', project3] response = CustomResponse() response.second_response_code = 500 response.limit = 2 response.response_text = '{"responses": [[null,201], [null,201], [null,201]]}' self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response retval = execute(args) self.assertEquals(retval, API_ERROR) # offline queue should still have saved heartbeats queue = Queue(None, None) saved_heartbeats = next(queue.pop_many()) self.assertNothingPrinted() self.assertEquals(len(saved_heartbeats), 2)
def test_heartbeats_sent_not_saved_from_bulk_response(self, logs): logging.disable(logging.NOTSET) with NamedTemporaryFile() as fh: with mock.patch('wakatime.offlinequeue.Queue._get_db_file') as mock_db_file: mock_db_file.return_value = fh.name entities = [ 'emptyfile.txt', 'twolinefile.txt', 'python.py', 'go.go', ] with TemporaryDirectory() as tempdir: for entity in entities: shutil.copy(os.path.join('tests/samples/codefiles', entity), os.path.join(tempdir, entity)) now = u(int(time.time())) key = str(uuid.uuid4()) args = ['--file', os.path.join(tempdir, entities[0]), '--key', key, '--config', 'tests/samples/configs/good_config.cfg', '--time', now, '--extra-heartbeats'] response = CustomResponse() response.response_code = 202 response.response_text = '{"responses": [[null,201], [null,500], [null,201], [null, 500]]}' self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response with mock.patch('wakatime.main.sys.stdin') as mock_stdin: heartbeats = json.dumps([{ 'timestamp': now, 'entity': os.path.join(tempdir, entity), 'entity_type': 'file', 'is_write': False, } for entity in entities[1:]]) mock_stdin.readline.return_value = heartbeats with mock.patch('wakatime.offlinequeue.Queue.pop') as mock_pop: mock_pop.return_value = None retval = execute(args) self.assertEquals(retval, SUCCESS) self.assertNothingPrinted() heartbeat = { 'entity': os.path.realpath(os.path.join(tempdir, entities[0])), 'language': ANY, 'lines': ANY, 'project': ANY, 'time': ANY, 'type': 'file', 'is_write': ANY, 'user_agent': ANY, 'dependencies': ANY, } extra_heartbeats = [{ 'entity': os.path.realpath(os.path.join(tempdir, entity)), 'language': ANY, 'lines': ANY, 'project': ANY, 'branch': ANY, 'time': ANY, 'is_write': ANY, 'type': 'file', 'dependencies': ANY, 'user_agent': ANY, } for entity in entities[1:]] self.assertHeartbeatSent(heartbeat, extra_heartbeats=extra_heartbeats) self.assertSessionCacheSaved() queue = Queue(None, None) self.assertEquals(queue._get_db_file(), fh.name) saved_heartbeats = queue.pop_many() self.assertNothingPrinted() self.assertNothingLogged(logs) # make sure only heartbeats with error code responses were saved self.assertEquals(len(saved_heartbeats), 2) self.assertEquals(saved_heartbeats[0].entity, os.path.realpath(os.path.join(tempdir, entities[1]))) self.assertEquals(saved_heartbeats[1].entity, os.path.realpath(os.path.join(tempdir, entities[3])))
def test_heartbeats_sent_not_saved_from_bulk_response(self, logs): logging.disable(logging.NOTSET) with NamedTemporaryFile() as fh: with mock.patch('wakatime.offlinequeue.Queue._get_db_file') as mock_db_file: mock_db_file.return_value = fh.name entities = [ 'emptyfile.txt', 'twolinefile.txt', 'python.py', 'go.go', ] with TemporaryDirectory() as tempdir: for entity in entities: shutil.copy(os.path.join('tests/samples/codefiles', entity), os.path.join(tempdir, entity)) now = u(int(time.time())) key = str(uuid.uuid4()) args = ['--file', os.path.join(tempdir, entities[0]), '--key', key, '--config', 'tests/samples/configs/good_config.cfg', '--time', now, '--extra-heartbeats'] response = CustomResponse() response.response_code = 202 response.response_text = '{"responses": [[null,201], [null,500], [null,201], [null, 500]]}' self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response with mock.patch('wakatime.main.sys.stdin') as mock_stdin: heartbeats = json.dumps([{ 'timestamp': now, 'entity': os.path.join(tempdir, entity), 'entity_type': 'file', 'is_write': False, } for entity in entities[1:]]) mock_stdin.readline.return_value = heartbeats with mock.patch('wakatime.offlinequeue.Queue.pop') as mock_pop: mock_pop.return_value = None retval = execute(args) self.assertEquals(retval, SUCCESS) self.assertNothingPrinted() heartbeat = { 'entity': os.path.realpath(os.path.join(tempdir, entities[0])), 'language': ANY, 'lines': ANY, 'project': ANY, 'time': ANY, 'type': 'file', 'is_write': ANY, 'user_agent': ANY, 'dependencies': ANY, } extra_heartbeats = [{ 'entity': os.path.realpath(os.path.join(tempdir, entity)), 'language': ANY, 'lines': ANY, 'project': ANY, 'branch': ANY, 'time': ANY, 'is_write': ANY, 'type': 'file', 'dependencies': ANY, 'user_agent': ANY, } for entity in entities[1:]] self.assertHeartbeatSent(heartbeat, extra_heartbeats=extra_heartbeats) self.assertSessionCacheSaved() queue = Queue(None, None) self.assertPathsEqual(queue._get_db_file(), fh.name) saved_heartbeats = next(queue.pop_many()) self.assertNothingPrinted() self.assertNothingLogged(logs) # make sure only heartbeats with error code responses were saved self.assertEquals(sum(1 for x in saved_heartbeats), 2) self.assertPathsEqual(saved_heartbeats[0].entity, os.path.realpath(os.path.join(tempdir, entities[1]))) self.assertPathsEqual(saved_heartbeats[1].entity, os.path.realpath(os.path.join(tempdir, entities[3])))
def test_offline_heartbeats_sent_after_partial_success_from_bulk_response( self, logs): logging.disable(logging.NOTSET) with utils.NamedTemporaryFile() as fh: with utils.mock.patch('wakatime.offlinequeue.Queue._get_db_file' ) as mock_db_file: mock_db_file.return_value = fh.name entities = [ 'emptyfile.txt', 'twolinefile.txt', 'python.py', 'go.go', ] with utils.TemporaryDirectory() as tempdir: for entity in entities: shutil.copy( os.path.join('tests/samples/codefiles', entity), os.path.join(tempdir, entity)) now = u(int(time.time())) key = str(uuid.uuid4()) args = [ '--file', os.path.join(tempdir, entities[0]), '--key', key, '--config', 'tests/samples/configs/good_config.cfg', '--time', now, '--extra-heartbeats' ] class CustomResponse(Response): @property def status_code(self): return 202 @status_code.setter def status_code(self, value): pass @property def text(self): return '[[{"id":1},201], [{"error":"error 2"},500], [{"id":3},201], [{"error":4},500]]' response = CustomResponse() self.patched[ 'wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response with utils.mock.patch( 'wakatime.main.sys.stdin') as mock_stdin: heartbeats = json.dumps([{ 'timestamp': now, 'entity': os.path.join(tempdir, entity), 'entity_type': 'file', 'is_write': False, } for entity in entities[1:]]) mock_stdin.readline.return_value = heartbeats retval = execute(args) self.assertEquals(retval, SUCCESS) self.assertNothingPrinted() queue = Queue(None, None) self.assertEquals(queue._get_db_file(), fh.name) saved_heartbeats = queue.pop_many() self.assertNothingPrinted() self.assertNothingLogged(logs) # make sure all offline heartbeats were sent, so queue should only have 1 heartbeat left from the second 500 response self.assertEquals(len(saved_heartbeats), 1)