def test_remove_schedule_many_schedules(self): # Number of jobs and floor(requests) + ceiling(requests) are equal num_of_jobs = MANY num_of_schedules = num_of_jobs # Automatic flooring care of Python num_of_requests = num_of_jobs / 2 # Create a crontab with many jobs test_jobs = create_test_tab(num_of_jobs, user1) api.set_jobs(test_jobs, user1) jobs_list = api.get_jobs() test_workers = [] # Create one worker and add them to a list for bookkeeping test_workers.append(api.create_worker()) # Create a schedule for many jobs test_schedules = [] for schedule in range(num_of_schedules): test_schedules.append(api.Schedule(datetime.now(), test_jobs[schedule], test_workers[0])) api.add_schedules(test_schedules) # Kill the floor of half of the workers in the list for request in range(num_of_requests): api.remove_schedule(test_schedules.pop()) schedules_list = api.get_schedules(test_workers[0]) # Verify that the correct remaining schedules are still in the list check_schedule_fields(self, schedules_list, test_schedules) # Remove all remaining test schedules while test_schedules: api.remove_schedule(test_schedules.pop()) # Try to get schedules from an empty pool schedules_list = api.get_schedules(test_workers[0]) # Verify that the next worker does not exist self.assertFalse(len(schedules_list))
def test_remove_schedule_many_schedules(self): # Number of jobs and floor(requests) + ceiling(requests) are equal num_of_jobs = MANY num_of_schedules = num_of_jobs # Automatic flooring care of Python num_of_requests = num_of_jobs / 2 # Create a crontab with many jobs test_jobs = create_test_tab(num_of_jobs, user1) api.set_jobs(test_jobs, user1) jobs_list = api.get_jobs() test_workers = [] # Create one worker and add them to a list for bookkeeping test_workers.append(api.create_worker()) # Create a schedule for many jobs test_schedules = [] for schedule in range(num_of_schedules): test_schedules.append( api.Schedule(datetime.now(), test_jobs[schedule], test_workers[0])) api.add_schedules(test_schedules) # Kill the floor of half of the workers in the list for request in range(num_of_requests): api.remove_schedule(test_schedules.pop()) schedules_list = api.get_schedules(test_workers[0]) # Verify that the correct remaining schedules are still in the list check_schedule_fields(self, schedules_list, test_schedules) # Remove all remaining test schedules while test_schedules: api.remove_schedule(test_schedules.pop()) # Try to get schedules from an empty pool schedules_list = api.get_schedules(test_workers[0]) # Verify that the next worker does not exist self.assertFalse(len(schedules_list))
def test_remove_schedules_many_schedules_random_schedules(self): # Number of jobs and floor(requests) + ceiling(requests) are equal num_of_jobs = MANY num_of_schedules = num_of_jobs # Leave one schedule remaining for comparison at the end num_of_requests = num_of_jobs - 1 # Create a crontab with many jobs test_jobs = create_test_tab(num_of_jobs, user1) api.set_jobs(test_jobs, user1) jobs_list = api.get_jobs() test_workers = [] # Create one worker and add them to a list for bookkeeping test_workers.append(api.create_worker()) test_schedules = [] for schedule in range(num_of_schedules): test_schedules.append(api.Schedule(datetime.now(), test_jobs[schedule], test_workers[0])) api.add_schedules(test_schedules) # Kill number of requests random schedules in the list for request in range(num_of_requests): random_schedule = random.randrange(len(test_schedules)) api.remove_schedule(test_schedules.pop(random_schedule)) schedules_list = [] schedules_list = api.get_schedules(test_workers[0]) # Verify that the schedules list contains exactly one schedule self.assertEqual(len(schedules_list), 1) # Verify that the correct remaining schedule is still in the list check_schedule_fields(self, schedules_list, test_schedules)
def test_get_schedules_many_workers_many_schedules(self): num_of_jobs = MANY num_of_schedules = num_of_jobs num_of_workers = num_of_schedules num_of_workers = MANY test_jobs = create_test_tab(num_of_jobs, user1) api.set_jobs(test_jobs, user1) jobs_list = api.get_jobs() test_workers = [] # Create many workers and add them to a list for bookkeeping for worker in range(num_of_workers): test_workers.append(api.create_worker()) # Create a schedule belonging to each worker representing the only job test_schedules = [] for worker in test_workers: for job in jobs_list: test_schedules.append(api.Schedule(datetime.now(), job, worker)) api.add_schedules(test_schedules) # Aggregate each worker's schedules into a single list for comparison # with test schedules schedules_for_workers = [] for worker in test_workers: schedules_list = api.get_schedules(worker) schedules_for_workers.extend(schedules_list) # Verify that the information we get matches what was set check_schedule_fields(self, schedules_for_workers, test_schedules)
def test_remove_schedules_many_schedules_random_schedules(self): # Number of jobs and floor(requests) + ceiling(requests) are equal num_of_jobs = MANY num_of_schedules = num_of_jobs # Leave one schedule remaining for comparison at the end num_of_requests = num_of_jobs - 1 # Create a crontab with many jobs test_jobs = create_test_tab(num_of_jobs, user1) api.set_jobs(test_jobs, user1) jobs_list = api.get_jobs() test_workers = [] # Create one worker and add them to a list for bookkeeping test_workers.append(api.create_worker()) test_schedules = [] for schedule in range(num_of_schedules): test_schedules.append( api.Schedule(datetime.now(), test_jobs[schedule], test_workers[0])) api.add_schedules(test_schedules) # Kill number of requests random schedules in the list for request in range(num_of_requests): random_schedule = random.randrange(len(test_schedules)) api.remove_schedule(test_schedules.pop(random_schedule)) schedules_list = [] schedules_list = api.get_schedules(test_workers[0]) # Verify that the schedules list contains exactly one schedule self.assertEqual(len(schedules_list), 1) # Verify that the correct remaining schedule is still in the list check_schedule_fields(self, schedules_list, test_schedules)
def test_update_heartbeat_deleted_worker(self): test_workers = [] # Create a temporary worker in the test workers to_be_deleted = test_workers.append(api.create_worker()) # Destroy the only worker in the list api.destroy_worker(api.get_next_worker()) # Verify that we do not update nonexistent workers with self.assertRaises(ValueError): api.update_heartbeat(to_be_deleted)
def test_get_workers_one_worker(self): num_of_workers = ONE test_workers = [] # Create one worker and add them to a list for bookkeeping test_workers.append(api.create_worker()) workers_list = api.get_workers() # Verify that the workers list contains exactly one worker self.assertEqual(len(workers_list), num_of_workers) # Verify that the information we get is the same as what was set check_worker_fields(self, workers_list, test_workers)
def test_get_workers_many_workers(self): num_of_workers = MANY test_workers = [] for job in range(num_of_workers): # Create workers and add them to a list for bookkeeping test_workers.append(api.create_worker()) workers_list = api.get_workers() # Verify that the workers list contains the correct number of workers self.assertEqual(len(workers_list), num_of_workers) check_worker_fields(self, workers_list, test_workers)
def test_destroy_worker_one_worker(self): test_workers = [] # Create one worker and add it to a list for bookkeeping test_workers.append(api.create_worker()) # Kill the only worker in the list api.destroy_worker(test_workers[0]) # Try to get a worker from an empty pool next_worker = api.get_next_worker() # Verify that the next worker does not exist self.assertEqual(next_worker, None)
def test_create_worker_one_worker(self): checkpoint1 = datetime.now() test_workers = [] # Create one worker and add them to a list for bookkeeping test_workers.append(api.create_worker()) checkpoint2 = datetime.now() current = 1 # Verify that the worker has been created just now self.assertTrue(test_workers[0].heartbeat > checkpoint1 and test_workers[0].heartbeat < checkpoint2) current += 1
def test_create_worker_many_workers(self): checkpoint1 = datetime.now() num_of_workers = MANY test_workers = [] for job in range(num_of_workers): # Create many workers and add them to a list for bookkeeping test_workers.append(api.create_worker()) checkpoint2 = datetime.now() current = 1 for worker in test_workers: # Verify that the workers have been created just now self.assertTrue(worker.heartbeat > checkpoint1 and worker.heartbeat < checkpoint2) current += 1
def test_update_heartbeat_one_worker(self): test_workers = [] # Create one worker and it to a list for bookkeeping test_workers.append(api.create_worker()) # Get the original heartbeat of the only worker previous_heartbeats = [] previous_heartbeats.append(test_workers[0].heartbeat) # Old heartbeat is before checkpoint, new heartbeat should be after checkpoint1 = datetime.now() api.update_heartbeat(test_workers[0]) workers_list = [] workers_list = api.get_workers() checkpoint2 = datetime.now() # Verify that the heartbeat has been updated correctly and previous # heartbeat was not an unexpected value check_heartbeat_value(self, workers_list[0], previous_heartbeats, checkpoint1, checkpoint2, 0)
def test_get_next_worker_one_worker_many_requests(self): num_of_workers = ONE num_of_requests = MANY test_workers = [] # Create one worker and it to a list for bookkeeping test_workers.append(api.create_worker()) # Verify that the test workers contains exactly one worker self.assertEqual(len(test_workers), num_of_workers) workers_list = [] for request in range(num_of_requests): # Get workers and add them to a list for # comparison with list of test workers workers_list.append(api.get_next_worker()) # Verify that the workers list contains # as many workers as there were requests self.assertEqual(len(workers_list), num_of_requests) # Verify that the information we get is the same as what was set check_worker_fields(self, workers_list, test_workers)
def test_update_heartbeat_one_worker_many_requests(self): num_of_requests = MANY test_workers = [] # Create many workers and them to a list for bookkeeping test_workers.append(api.create_worker()) # Get the original heartbeat of the only worker previous_heartbeats = [] previous_heartbeats.append(test_workers[0].heartbeat) checkpoint1 = datetime.now() workers_list = [] for request in range(num_of_requests): api.update_heartbeat(test_workers[0]) workers_list = api.get_workers() checkpoint2 = datetime.now() # Verify that the heartbeat has been updated correctly and previous # heartbeat was not an unexpected value check_heartbeat_value(self, workers_list[0], previous_heartbeats, checkpoint1, checkpoint2, 0)
def test_get_schedules_one_worker_many_schedules(self): num_of_jobs = MANY num_of_schedules = num_of_jobs test_jobs = create_test_tab(num_of_jobs, user1) api.set_jobs(test_jobs, user1) jobs_list = api.get_jobs() test_workers = [] # Create one worker and add them to a list for bookkeeping test_workers.append(api.create_worker()) # Create a schedule for the only job test_schedules = [] for job in range(num_of_jobs): test_schedules.append(api.Schedule(datetime.now(), test_jobs[job], test_workers[0])) api.add_schedules(test_schedules) schedules_list = api.get_schedules(test_workers[0]) # Verify that the information we get matches what was set check_schedule_fields(self, schedules_list, test_schedules)
def test_add_schedules_one_job_one_schedule(self): num_of_jobs = ONE num_of_schedules = num_of_jobs # Create a crontab with one job test_jobs = create_test_tab(num_of_jobs, user1) api.set_jobs(test_jobs, user1) jobs_list = api.get_jobs() test_workers = [] # Create one worker and add them to a list for bookkeeping test_workers.append(api.create_worker()) # Create a schedule for the only job test_schedules = [] test_schedules.append( api.Schedule(datetime.now(), test_jobs[0], test_workers[0])) api.add_schedules(test_schedules) schedules_list = api.get_schedules(test_workers[0]) # Verify that the schedules list contains exactly one schedule self.assertEqual(len(schedules_list), num_of_schedules)
def test_get_schedules_one_worker_many_schedules(self): num_of_jobs = MANY num_of_schedules = num_of_jobs test_jobs = create_test_tab(num_of_jobs, user1) api.set_jobs(test_jobs, user1) jobs_list = api.get_jobs() test_workers = [] # Create one worker and add them to a list for bookkeeping test_workers.append(api.create_worker()) # Create a schedule for the only job test_schedules = [] for job in range(num_of_jobs): test_schedules.append( api.Schedule(datetime.now(), test_jobs[job], test_workers[0])) api.add_schedules(test_schedules) schedules_list = api.get_schedules(test_workers[0]) # Verify that the information we get matches what was set check_schedule_fields(self, schedules_list, test_schedules)
def test_add_schedules_one_job_one_schedule(self): num_of_jobs = ONE num_of_schedules = num_of_jobs # Create a crontab with one job test_jobs = create_test_tab(num_of_jobs, user1) api.set_jobs(test_jobs, user1) jobs_list = api.get_jobs() test_workers = [] # Create one worker and add them to a list for bookkeeping test_workers.append(api.create_worker()) # Create a schedule for the only job test_schedules = [] test_schedules.append(api.Schedule(datetime.now(), test_jobs[0], test_workers[0])) api.add_schedules(test_schedules) schedules_list = api.get_schedules(test_workers[0]) # Verify that the schedules list contains exactly one schedule self.assertEqual(len(schedules_list), num_of_schedules)
def test_remove_schedule_one_schedule(self): num_of_jobs = ONE num_of_schedules = num_of_jobs # Create a crontab with one job test_jobs = create_test_tab(num_of_jobs, user1) api.set_jobs(test_jobs, user1) jobs_list = api.get_jobs() test_workers = [] # Create one worker and add them to a list for bookkeeping test_workers.append(api.create_worker()) # Create a schedule for the only job test_schedules = [] test_schedules.append( api.Schedule(datetime.now(), test_jobs[0], test_workers[0])) api.add_schedules(test_schedules) # Kill the only schedule in the list api.remove_schedule(test_schedules[0]) schedules_list = api.get_schedules(test_workers[0]) # Verify that the schedules list is empty self.assertFalse(len(schedules_list))
def test_destroy_worker_many_workers_random_workers(self): # Number of workers and floor(requests) + ceiling(requests) are equal num_of_workers = MANY # Leave one worker remaining for comparison at the end num_of_requests = num_of_workers - 1 # Automatic flooring care of Python test_workers = [] for worker in range(num_of_workers): # Create many workers and add them to a list for bookkeeping test_workers.append(api.create_worker()) # Kill number of requests random worker in the list for request in range(num_of_requests): random_worker = random.randrange(len(test_workers)) api.destroy_worker(test_workers.pop(random_worker)) workers_list = [] workers_list = api.get_workers() # Verify that the workers list contains exactly one worker self.assertEqual(len(workers_list), 1) # Verify that the correct remaining worker is still in the list check_worker_fields(self, workers_list, test_workers)
def test_remove_schedule_one_schedule(self): num_of_jobs = ONE num_of_schedules = num_of_jobs # Create a crontab with one job test_jobs = create_test_tab(num_of_jobs, user1) api.set_jobs(test_jobs, user1) jobs_list = api.get_jobs() test_workers = [] # Create one worker and add them to a list for bookkeeping test_workers.append(api.create_worker()) # Create a schedule for the only job test_schedules = [] test_schedules.append(api.Schedule(datetime.now(), test_jobs[0], test_workers[0])) api.add_schedules(test_schedules) # Kill the only schedule in the list api.remove_schedule(test_schedules[0]) schedules_list = api.get_schedules(test_workers[0]) # Verify that the schedules list is empty self.assertFalse(len(schedules_list))
def test_get_next_worker_many_workers_more_requests(self): num_of_workers = MANY num_of_requests = MORE_THAN_MANY test_workers = [] for worker in range(num_of_workers): # Create many workers and add them to a list for bookkeeping test_workers.append(api.create_worker()) # Verify that the test workers contains # the correct number of workers self.assertEqual(len(test_workers), num_of_workers) workers_list = [] for request in range(num_of_requests): # Get more workers than available in the test workers # (popped workers are pushed to the end of the queue) # and add them to a list for comparison workers_list.append(api.get_next_worker()) # Verify that the workers list contains # as many workers as there were requests self.assertEqual(len(workers_list), num_of_requests) # Verify that the information we get is the same as what was set check_worker_fields(self, workers_list, test_workers)
def test_update_heartbeat_many_workers_random_worker(self): # Number of workers and floor(requests) + ceiling(requests) are equal num_of_workers = MANY test_workers = [] previous_heartbeats = [] # Create many workers and them to a list for bookkeeping for worker in range(num_of_workers): test_workers.append(api.create_worker()) # Get the original heartbeat of each worker checkpoint1 = datetime.now() for worker in test_workers: previous_heartbeats.append(worker.heartbeat) random_worker = random.randrange(num_of_workers) api.update_heartbeat(test_workers[random_worker]) workers_list = [] workers_list = api.get_workers() checkpoint2 = datetime.now() # Verify that the random worker's heartbeat has been updated correctly # and its previous heartbeat was not an unexpected value check_heartbeat_value(self, workers_list[random_worker], previous_heartbeats, checkpoint1, checkpoint2, random_worker)
def test_destroy_worker_many_workers(self): # Number of workers and floor(requests) + ceiling(requests) are equal num_of_workers = MANY # Automatic flooring care of Python num_of_requests = num_of_workers / 2 test_workers = [] for worker in range(num_of_workers): # Create many workers and add them to a list for bookkeeping test_workers.append(api.create_worker()) # Kill the floor of half of the workers in the list for request in range(num_of_requests): api.destroy_worker(test_workers.pop()) workers_list = [] workers_list = api.get_workers() # Verify that the correct workers are still in the list check_worker_fields(self, workers_list, test_workers) # Remove the rest of the test workers while test_workers: api.destroy_worker(test_workers.pop()) # Try to get a worker from an empty pool next_worker = api.get_next_worker() # Verify that the next worker does not exist self.assertEqual(next_worker, None)
def test_update_heartbeat_many_workers_many_requests(self): num_of_workers = MANY test_workers = [] previous_heartbeats = [] # Create many workers and them to a list for bookkeeping for worker in range(num_of_workers): test_workers.append(api.create_worker()) # Get the original heartbeat of each worker for worker in test_workers: previous_heartbeats.append(worker.heartbeat) checkpoint1 = datetime.now() for worker in test_workers: api.update_heartbeat(worker) workers_list = [] workers_list = api.get_workers() checkpoint2 = datetime.now() # Verify that the heartbeats have been updated correctly and previous # heartbeats were not an unexpected value current = 0 for worker in workers_list: check_heartbeat_value(self, worker, previous_heartbeats, checkpoint1, checkpoint2, current) current += 1
def test_destroy_worker_many_workers(self): # Number of workers and floor(requests) + ceiling(requests) are equal num_of_workers = MANY # Automatic flooring care of Python num_of_requests = num_of_workers/2 test_workers = [] for worker in range(num_of_workers): # Create many workers and add them to a list for bookkeeping test_workers.append(api.create_worker()) # Kill the floor of half of the workers in the list for request in range(num_of_requests): api.destroy_worker(test_workers.pop()) workers_list = [] workers_list = api.get_workers() # Verify that the correct workers are still in the list check_worker_fields(self, workers_list, test_workers) # Remove the rest of the test workers while test_workers: api.destroy_worker(test_workers.pop()) # Try to get a worker from an empty pool next_worker = api.get_next_worker() # Verify that the next worker does not exist self.assertEqual(next_worker, None)
import pwd import os import subprocess from datetime import datetime, timedelta from megacron import api SCHEDULES_UPDATE_INTERVAL = timedelta(seconds=10) HEARTBEAT_UPDATE_INTERVAL = timedelta(seconds=10) worker = api.create_worker() _schedules = [] _run_schedules_event = None def cleanup(): global worker api.destroy_worker(worker) def update_schedules(events): global worker global _schedules global _run_schedules_event _schedules = api.get_schedules(worker) # Cancel the existing event because _run_schedules will create a new one. if _run_schedules_event is not None: events.cancel(_run_schedules_event)
def init_worker(): global worker worker = api.create_worker()