class Server: def __init__(self, queue_name, bucket_name): self.queue = SQS(queue_name) self.bucket_name = bucket_name self.bucket = S3(bucket_name) def __enter__(self): return self def __exit__(self, exc_type, exc_val, exc_tb): self.bucket.__exit__(exc_type, exc_val, exc_tb) self.queue.__exit__(exc_type, exc_val, exc_tb) # Main "entry" point for server. Starts the infinite loop for message processing. def run(self): print("Receiving and handling messages") while True: self.receive_and_handle() # Function that receives a message and starts a new process to handle the request def receive_and_handle(self): try: msg = self.queue.recv() if msg is None: time.sleep(0.005) else: print("Got message: \t{}".format(msg)) Process(target=handle_json_request, args=(msg["Body"], self.bucket_name)).start() except BaseException as e: print("Ignoring exception {}\n".format(e))
def test_response_error(self): res = Responses.encode_fail("Test error") res_q = SQS(self.cli.cli_q_name) res_q.send(res, {}) with self.assertRaises(Exception): self.cli.wait_response()
def setUp(self): global g_count self.count = g_count g_count += 1 self.req_q = SQS("{}-{}".format(test_req_q, self.count)) self.in_b = S3("{}-{}".format(test_in_b, self.count)) self.out_b = S3("{}-{}".format(test_out_b, self.count)) self.cli = Client("{}-{}".format(test_res_q, self.count), "{}-{}".format(test_req_q, self.count), "{}-{}".format(test_in_b, self.count))
def send_file(self, name, really): my_queue_name = my_queue_name_prefix + str(uuid.uuid4()) with SQS(my_queue_name) as my_queue: remote_file_name = my_queue_name + "-normal-file.txt" if really: s3 = S3(args.bucket) s3.upload(local_file=name, remote_file=remote_file_name) sqs = SQS(args.queue) sqs.send(msg=Requests.encode_file(remote_file_name, my_queue_name), attrs={}) count = 0 msg = {} while count < 100: msg = my_queue.recv() if msg is None: time.sleep(0.005) count += 1 else: break else: self.fail("No response from the server") res = Responses.decode(msg["Body"]) if really: self.assertTrue("error" not in res) self.assertTrue("s3url" in res) parts = res["s3url"].split() self.assertEqual(len(parts), 2) bucket = parts[0] name = parts[1] res_s3 = S3(bucket) local_file = "{}-downloaded.txt".format(name) res_s3.download(local_file=local_file, remote_file=name) data = "" with open(local_file, "r") as f: data = f.read() decoded = json.loads(data) os.remove(local_file) return decoded else: self.assertTrue("error" in res) self.assertTrue("s3url" not in res)
def respond_success(queue_name, local_file): bucket_name = "titos-{}".format(str(uuid.uuid4())) bucket = S3(bucket_name) bucket.upload(local_file=local_file, remote_file="a.txt") os.remove(local_file) SQS(queue_name).send(msg=Responses.encode_success( "{} a.txt".format(bucket_name)), attrs={}) Process(target=delayed_delete, args=(bucket_name, )).start()
def test_response(self): #self.out_b.upload("lorem.txt", "a.txt") res = Responses.encode_success("{} a.txt".format(self.out_b.name)) res_q = SQS(self.cli.cli_q_name) res_q.send(res, {}) res = self.cli.wait_response()
class TestClientCase(unittest.TestCase): #standard setUp function to create instances of resources # that test cases will use def setUp(self): global g_count self.count = g_count g_count += 1 self.req_q = SQS("{}-{}".format(test_req_q, self.count)) self.in_b = S3("{}-{}".format(test_in_b, self.count)) self.out_b = S3("{}-{}".format(test_out_b, self.count)) self.cli = Client("{}-{}".format(test_res_q, self.count), "{}-{}".format(test_req_q, self.count), "{}-{}".format(test_in_b, self.count)) #standard tearDown function to clean up the resources created in setUp def tearDown(self): exc_type = None exc_val = None exc_tb = None self.cli.__exit__(exc_type, exc_val, exc_tb) self.req_q.__exit__(exc_type, exc_val, exc_tb) self.in_b.__exit__(exc_type, exc_val, exc_tb) self.out_b.__exit__(exc_type, exc_val, exc_tb) #helper function for receiving a message from the request queue def recv_req(self): count = 0 while count < 20: msg = self.req_q.recv() if msg is None: count += 1 continue return Requests.decode(msg["Body"]) else: self.fail("Did not receive request") #Test case which tests whether or not a file request was completed successfully #tests the @Client @request method def test_file_req(self): self.cli.request(is_file=True, words=None, input_file=test_in_file) req = self.recv_req() for key in ["url", "words", "queue_name"]: if key not in req: self.fail("Invalid request") self.in_b.download(req["url"], req["url"]) #Test case which tests whether or not a direct request was completed successfully #tests the @Client @request method def test_direct_req(self): self.cli.request(is_file=False, words=None, input_file=test_in_file) req = self.recv_req() for key in ["text", "words", "queue_name"]: if key not in req: self.fail("Invalid request") #Tests a successful response reception #tests the Client <wait_response> method def test_response(self): #self.out_b.upload("lorem.txt", "a.txt") res = Responses.encode_success("{} a.txt".format(self.out_b.name)) res_q = SQS(self.cli.cli_q_name) res_q.send(res, {}) res = self.cli.wait_response() #Tests a successful error response reception #tests the @Client @wait_response method def test_response_error(self): res = Responses.encode_fail("Test error") res_q = SQS(self.cli.cli_q_name) res_q.send(res, {}) with self.assertRaises(Exception): self.cli.wait_response() #Tests an unsuccessful response reception #tests the @Client @wait_response method def test_no_response(self): with self.assertRaises(Exception): self.cli.wait_response() #Tests a successful reception of the output file #test the @Client @get_file method def test_get_file(self): self.out_b.upload("{}".format(test_in_file), "a.txt") self.cli.get_file("{} {}".format(self.out_b.name, "a.txt")) #Tests an unsuccessful reception of the output file #tests the @Client @get_file method def test_get_no_file(self): with self.assertRaises(Exception): self.cli.get_file("{} {}".format(self.out_b.name, "a.txt")) def xtest_run(self): #TODO: implement? other methods all tested, redundant #"Mock" server run #New Process client.run pass
def send_error(queue_name): try: sqs = SQS(queue_name) sqs.send(msg=Responses.encode_fail("Shit hit the fan"), attrs={}) except BaseException as e: print("Well, that went really wrong: {}".format(e))
def __init__(self, queue_name, bucket_name): self.queue = SQS(queue_name) self.bucket_name = bucket_name self.bucket = S3(bucket_name)