<s> """<STR_LIT>""" <EOL> __author__ = '<STR_LIT>' <EOL> import logging <EOL> import random <EOL> import struct <EOL> import sys <EOL> import unittest <EOL> from functools import partial <EOL> from tornado import httpclient , options , web , testing <EOL> from tornado . stack_context import StackContext <EOL> from viewfinder . backend . base import handler , util <EOL> from viewfinder . backend . base . testing import async_test , BaseTestCase <EOL> from viewfinder . backend . db . local_client import LocalClient <EOL> from viewfinder . backend . db . db_client import DBClient , DBKey , DBKeySchema <EOL> class _DatastoreHandler ( web . RequestHandler ) : <EOL> """<STR_LIT>""" <EOL> @ handler . asynchronous ( datastore = True ) <EOL> def get ( self ) : <EOL> def _OnGet ( result ) : <EOL> self . write ( '<STR_LIT:%s>' % result . attributes [ '<STR_LIT:v>' ] ) <EOL> self . finish ( ) <EOL> self . _client . GetItem ( table = '<STR_LIT:test>' , <EOL> key = DBKey ( hash_key = self . get_argument ( '<STR_LIT:k>' ) , range_key = None ) , <EOL> attributes = [ '<STR_LIT:v>' ] , callback = _OnGet ) <EOL> @ handler . asynchronous ( datastore = True ) <EOL> def post ( self ) : <EOL> def _OnPut ( result ) : <EOL> self . write ( '<STR_LIT>' ) <EOL> self . finish ( ) <EOL> self . _client . PutItem ( table = '<STR_LIT:test>' , <EOL> key = DBKey ( hash_key = self . get_argument ( '<STR_LIT:k>' ) , range_key = None ) , <EOL> attributes = { '<STR_LIT:v>' : self . get_argument ( '<STR_LIT:v>' ) } , callback = _OnPut ) <EOL> class HandlerTestCase ( BaseTestCase , testing . AsyncHTTPTestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> super ( HandlerTestCase , self ) . setUp ( ) <EOL> self . http_client = httpclient . AsyncHTTPClient ( <EOL> io_loop = self . io_loop , max_clients = <NUM_LIT:100> , force_instance = True ) <EOL> options . options . localdb_dir = '<STR_LIT>' <EOL> DBClient . SetInstance ( LocalClient ( None ) ) <EOL> DBClient . Instance ( ) . CreateTable ( <EOL> table = '<STR_LIT:test>' , hash_key_schema = DBKeySchema ( name = '<STR_LIT:k>' , value_type = '<STR_LIT:S>' ) , <EOL> range_key_schema = None , read_units = <NUM_LIT:10> , write_units = <NUM_LIT:5> , callback = None ) <EOL> def get_app ( self ) : <EOL> """<STR_LIT>""" <EOL> return web . Application ( [ ( r"<STR_LIT>" , _DatastoreHandler ) ] ) <EOL> @ async_test <EOL> def testDatastore ( self ) : <EOL> """<STR_LIT>""" <EOL> values = self . _CreateRandomValues ( num_values = <NUM_LIT:100> ) <EOL> def _InsertDone ( ) : <EOL> self . _RetrieveValues ( values , self . stop ) <EOL> self . _InsertValues ( values , _InsertDone ) <EOL> def _CreateRandomValues ( self , num_values = <NUM_LIT:100> ) : <EOL> """<STR_LIT>""" <EOL> return [ int ( random . uniform ( <NUM_LIT:0> , <NUM_LIT:1> << <NUM_LIT:20> ) ) for i in xrange ( num_values ) ] <EOL> def _InsertValues ( self , values , callback ) : <EOL> """<STR_LIT>""" <EOL> def _VerifyResponse ( cb , resp ) : <EOL> self . assertEqual ( resp . body , '<STR_LIT>' ) <EOL> cb ( ) <EOL> with util . Barrier ( callback ) as b : <EOL> for val in values : <EOL> self . http_client . fetch ( <EOL> httpclient . HTTPRequest ( self . get_url ( '<STR_LIT>' ) , method = '<STR_LIT:POST>' , <EOL> body = '<STR_LIT>' % ( val , val ) ) , <EOL> callback = partial ( _VerifyResponse , b . Callback ( ) ) ) <EOL> def _RetrieveValues ( self , values , callback ) : <EOL> """<STR_LIT>""" <EOL> def _VerifyResponse ( val , cb , resp ) : <EOL> self . assertEqual ( resp . body , repr ( val ) ) <EOL> cb ( ) <EOL> with util . Barrier ( callback ) as b : <EOL> for val in values : <EOL> self . http_client . fetch ( <EOL> httpclient . HTTPRequest ( self . get_url ( '<STR_LIT>' % val ) , method = '<STR_LIT:GET>' ) , <EOL> callback = partial ( _VerifyResponse , val , b . Callback ( ) ) ) </s>
