def finish_request_and_check(self, expect_failure=False): """Executes the set of RPCs requested since start_request() was called and checks to see if the response is successful and matches the expected Session state. Outputs of each RPC are also compared with the expected outputs. """ if self.rpcs is None: raise RuntimeError("tried to finish a request before starting a request") # like the real thing, call save() at the end of a request self.save() logger.info(trim_output_str('Running request: rpcs=%s' % self.rpcs)) self.app.set_client(self) resp = self.app.post('/', dict(rpcs=b64encode(pickle.dumps(self.rpcs)), api_statuses=b64encode(pickle.dumps(self.api_statuses)))) assert resp.status[:3] == '200', 'did not get code 200 back: %s' % resp remote_outputs, remote_ss = pickle.loads(b64decode(resp.body)) if self.ok_if_in_db_remotely and remote_ss.in_db: self.ss.in_db = remote_ss.in_db if self.ok_if_in_mc_remotely and remote_ss.in_mc: self.ss.in_mc = remote_ss.in_mc if self.ss.sid == ANY_SID: self.ss.sid = remote_ss.sid # expire the memcache if it is past the expir time (this doesn't handle # expiration mid-usage, but the test cases workaround that) has_expired_now = self._get_expiration() <= int(time.time()) if has_expired_now: self.ss.in_mc = False if expect_failure: assert remote_ss.in_db is False, "failure expected: data should not be in the datastore" assert remote_ss.in_mc is False, "failure expected: data should not be in memcache" self.api_statuses = self.outputs = self.rpcs = None logger.info('Request completed (expected failure and got it)') return assert self.ss == remote_ss, 'mismatch b/w local and remote states:\n\tlocal: %s\n\tremote: %s' % (self.ss, remote_ss) assert len(remote_outputs)==len(self.outputs), 'internal test error: number outputs should be the same' assert len(remote_outputs)==len(self.rpcs), 'internal test error: number outputs should be the same as the number of RPCs' for i in xrange(len(remote_outputs)): l, r = self.outputs[i], remote_outputs[i] assert l==r, 'output for rpc #%d (%s) does not match:\n\tlocal: %s\n\tremote: %s' % (i, self.rpcs[i], l, r) logger.info('state (local and remote): %s' % trim_output_str(str(self.ss))) # extra checks we sometimes need to do if self.check_expir: expir_remote = int(remote_ss.sid.split('_')[0]) assert self.check_expir==expir_remote, "remote expiration %s does match the expected expiration %s" % (expir_remote, self.check_expir) self.check_expir = None if self.check_sid_is_not: assert self.check_sid_is_not != remote_ss.sid, 'remote sid should not be %s' % remote_ss.sid self.__check_cookies() self.api_statuses = self.outputs = self.rpcs = None logger.info('Request completed')
def session_method(f): """Decorator which returns a function which calls the original function, records its output, and adds the function+args to the list of calls to be duplicated on the test web server too. """ def stub(*args, **kwargs): myself = args[0] if myself.rpcs is None: raise RuntimeError( "you must start a request before you can call a session method" ) rpc = (f.__name__, args[1:], kwargs) myself.rpcs.append(rpc) try: output = f(*args, **kwargs) caught_exception = None except Exception, e: output = '%s-%s' % (type(e), e) caught_exception = e myself.outputs.append(output) logger.info( trim_output_str('rpc enqueud: %s(%s, %s)' % (f.__name__, args[1:], kwargs))) if caught_exception: raise caught_exception else: return output
def session_method(f): """Decorator which returns a function which calls the original function, records its output, and adds the function+args to the list of calls to be duplicated on the test web server too. """ def stub(*args, **kwargs): myself = args[0] if myself.rpcs is None: raise RuntimeError("you must start a request before you can call a session method") rpc = (f.__name__, args[1:], kwargs) myself.rpcs.append(rpc) try: output = f(*args, **kwargs) caught_exception = None except Exception, e: output = '%s-%s' % (type(e), e) caught_exception = e myself.outputs.append(output) logger.info(trim_output_str('rpc enqueud: %s(%s, %s)' % (f.__name__,args[1:],kwargs))) if caught_exception: raise caught_exception else: return output
def finish_request_and_check(self, expect_failure=False): """Executes the set of RPCs requested since start_request() was called and checks to see if the response is successful and matches the expected Session state. Outputs of each RPC are also compared with the expected outputs. """ if self.rpcs is None: raise RuntimeError( "tried to finish a request before starting a request") # like the real thing, call save() at the end of a request self.save() logger.info(trim_output_str('Running request: rpcs=%s' % self.rpcs)) self.app.set_client(self) resp = self.app.post( '/', dict(rpcs=b64encode(pickle.dumps(self.rpcs)), api_statuses=b64encode(pickle.dumps(self.api_statuses)))) assert resp.status[:3] == '200', 'did not get code 200 back: %s' % resp remote_outputs, remote_ss = pickle.loads(b64decode(resp.body)) if self.ok_if_in_db_remotely and remote_ss.in_db: self.ss.in_db = remote_ss.in_db if self.ok_if_in_mc_remotely and remote_ss.in_mc: self.ss.in_mc = remote_ss.in_mc if self.ss.sid == ANY_SID: self.ss.sid = remote_ss.sid # expire the memcache if it is past the expir time (this doesn't handle # expiration mid-usage, but the test cases workaround that) has_expired_now = self._get_expiration() <= int(time.time()) if has_expired_now: self.ss.in_mc = False if expect_failure: assert remote_ss.in_db is False, "failure expected: data should not be in the datastore" assert remote_ss.in_mc is False, "failure expected: data should not be in memcache" self.api_statuses = self.outputs = self.rpcs = None logger.info('Request completed (expected failure and got it)') return assert self.ss == remote_ss, 'mismatch b/w local and remote states:\n\tlocal: %s\n\tremote: %s' % ( self.ss, remote_ss) assert len(remote_outputs) == len( self.outputs ), 'internal test error: number outputs should be the same' assert len(remote_outputs) == len( self.rpcs ), 'internal test error: number outputs should be the same as the number of RPCs' for i in xrange(len(remote_outputs)): l, r = self.outputs[i], remote_outputs[i] assert l == r, 'output for rpc #%d (%s) does not match:\n\tlocal: %s\n\tremote: %s' % ( i, self.rpcs[i], l, r) logger.info('state (local and remote): %s' % trim_output_str(str(self.ss))) # extra checks we sometimes need to do if self.check_expir: expir_remote = int(remote_ss.sid.split('_')[0]) assert self.check_expir == expir_remote, "remote expiration %s does match the expected expiration %s" % ( expir_remote, self.check_expir) self.check_expir = None if self.check_sid_is_not: assert self.check_sid_is_not != remote_ss.sid, 'remote sid should not be %s' % remote_ss.sid self.__check_cookies() self.api_statuses = self.outputs = self.rpcs = None logger.info('Request completed')