def setUp(self): self.user_token = api.post_user({ 'username': '******' }).json()['token'] self.other_user_token = api.post_user({ 'username': '******' }).json()['token'] contact_init_token = api.get_contact_init_token( self.user_token).json()['contact_init_token'] get_pin_response = api.get_contact_pin(contact_init_token, self.other_user_token).json() self.contact_token = api.post_contact_pin( get_pin_response['pin']).json()['contact_token'] self.socket = Socket() def connect(): self.socket.introduce_yourself(self.user_token) self.socket.on_connect(connect) self.other_socket = Socket() def connect(): self.other_socket.introduce_yourself(self.other_user_token) self.other_socket.on_connect(connect)
def __init__(self, ip, port, workers, cache_size, max_conn): # Create the socket self.server_socket = Socket(ip, port, max_conn) self.ip = ip self.port = port self.num_workers = workers self.cache_size = cache_size
class FileServer(object): def __init__(self, ip, port, workers, cache_size, max_conn): # Create the socket self.server_socket = Socket(ip, port, max_conn) self.ip = ip self.port = port self.num_workers = workers self.cache_size = cache_size def run(self): dispatchers = [] logger.init('fs-server') log_queue = Queue() req_queue = Queue() res_queues = [Queue() for i in range(self.num_workers)] # Create pool of dispatchers for i in range(self.num_workers): d = Dispatcher(i, self.server_socket, req_queue, res_queues[i], log_queue) dispatchers.append(d) d.start() # Set the ignore flag in main process # for SIGINT signal signal.signal(signal.SIGINT, signal.SIG_IGN) # Close server connection self.server_socket.close() # Create Cache and FS controller r = RequestExec(req_queue, res_queues, self.cache_size) r.start() # Create 'logger' process lp = Process(target=logger.log_worker, args=(log_queue, )) lp.start() # Wait for dispatchers to finish for j in range(self.num_workers): dispatchers[j].join() # Tell the 'RequestExec' to finish req_queue.put(None) r.join() # Tell the 'logger' to finish log_queue.put(None) lp.join() print('File System server finished')
class Server(object): def __init__(self, ip, port, workers, cache_size, num_fs, url_fs, max_conn): # Create the socket self.server_socket = Socket(ip, port, max_conn) self.ip = ip self.port = port self.num_workers = workers self.cache_size = cache_size self.num_fs = num_fs self.url_fs = url_fs def run(self): workers = [] log_queue = Queue() cache = ConcurrentCache(self.cache_size, Manager()) # Create pool of workers for i in range(self.num_workers): w = Worker(i, self.server_socket, log_queue, cache, self.url_fs, self.num_fs) workers.append(w) w.start() # Set the ignore flag in main process # for SIGINT signal signal.signal(signal.SIGINT, signal.SIG_IGN) # Close server connection self.server_socket.close() logger.init('http-server') logging = Process(target=logger.log_worker, args=(log_queue, )) logging.start() # Wait to workers to finish for j in range(self.num_workers): workers[j].join() # Tell the logger to finish log_queue.put(None) logging.join() print('Server finished')
def test_wrong_token_causes_incorrect_token_event(self): socket = Socket() error = {} def connect(): socket.introduce_yourself('dummy token') socket.on_connect(connect) def on_incorrect_token(data): error['message'] = data['message'] socket.disconnect() socket.on('incorrect_token', on_incorrect_token) socket.connect() self.assertEqual('Incorrect token', error['message'])
def test_can_connect(self): connection_established = {'success': False} socket = Socket() def connect(): connection_established['success'] = True socket.disconnect() socket.on_connect(connect) socket.connect() self.assertTrue(connection_established['success'])
def test_correct_token_causes_authorized_event(self): socket = Socket() result = {'event_emmited': False} token = api.post_user({ 'username': '******' }).json()['token'] def connect(): socket.introduce_yourself(token) socket.on_connect(connect) def on_authorized(data): result['event_emmited'] = True socket.disconnect() socket.on('authorized', on_authorized) socket.connect() self.assertTrue(result['event_emmited'])
class NewContactHandshakeTest(TestCase): def setUp(self): self.user_token = api.post_user({ 'username': '******' }).json()['token'] self.other_user_token = api.post_user({ 'username': '******' }).json()['token'] contact_init_token = api.get_contact_init_token( self.user_token).json()['contact_init_token'] get_pin_response = api.get_contact_pin(contact_init_token, self.other_user_token).json() self.contact_token = api.post_contact_pin( get_pin_response['pin']).json()['contact_token'] self.socket = Socket() def connect(): self.socket.introduce_yourself(self.user_token) self.socket.on_connect(connect) self.other_socket = Socket() def connect(): self.other_socket.introduce_yourself(self.other_user_token) self.other_socket.on_connect(connect) def test_sending_message_to_non_existing_contact(self): result = {} def on_authorized(data): self.socket.sio.send({ 'contact': 'incorrect', 'message': 'not important' }) self.socket.on_authorized(on_authorized) def on_incorrect_contact(data): result['data'] = data self.socket.disconnect() self.socket.on_incorrect_contact(on_incorrect_contact) self.socket.connect() self.assertEqual('Incorrect contact token', result['data']['error']) def test_sending_message_to_existing_contact(self): result = {} def on_authorized(data): self.socket.sio.send({ 'contact': self.contact_token, 'message': 'some message' }) self.socket.disconnect() self.socket.on_authorized(on_authorized) def on_message(data): result['data'] = data self.other_socket.disconnect() self.other_socket.on_message(on_message) self.socket.connect(block=False) self.other_socket.connect() self.assertEqual('some message', result['data']['message']) self.assertEqual(self.contact_token, result['data']['contact']) self.assertEqual('first_user', result['data']['username'])