#NEWLINE# def test_listen( self , stream ) : #NEWLINE# #INDENT# io_in = io . StringIO ( ) #NEWLINE# io_out = io . StringIO ( ) #NEWLINE# io_err = io . StringIO ( ) #NEWLINE# lines = [ '[{"id": 1, "color": "blue", "length": 1.0}]\n' , '[{"id": 1, "color": "{\\"a\\": 1, \\"b\\": 2}", "length": 1.0}]\n' , '[{"id": 1, "color": "blue", "length": 1.0}, {"id": 2, "color": "{\\"a\\": 1, \\"b\\": 2}", "length": 1.0}]\n' ] #NEWLINE# for line in lines : #NEWLINE# #INDENT# io_in . write ( line ) #NEWLINE# #UNINDENT# io_in . write ( 'EXIT\n' ) #NEWLINE# io_in . seek ( 0 ) #NEWLINE# predict = self . predict_service . predict #NEWLINE# predict . side_effect = ( lambda model , samples , **params : np . array ( ( [ { #NEWLINE# #INDENT# 'result' : 1 , #NEWLINE# #UNINDENT# } ] * len ( samples ) ) ) ) #NEWLINE# stream_thread = Thread ( target = stream . listen ( io_in , io_out , io_err ) ) #NEWLINE# stream_thread . start ( ) #NEWLINE# stream_thread . join ( ) #NEWLINE# io_out . seek ( 0 ) #NEWLINE# io_err . seek ( 0 ) #NEWLINE# assert ( len ( io_err . read ( ) ) == 0 ) #NEWLINE# assert ( io_out . read ( ) == ( ( '[{"result":1}]\n' * 2 ) + '[{"result":1},{"result":1}]\n' ) ) #NEWLINE# assert ( predict . call_count == 3 ) #NEWLINE# assert ( predict . call_args_list [ 0 ] [ 0 ] [ 1 ] == np . array ( [ { #NEWLINE# #INDENT# 'id' : 1 , #NEWLINE# 'color' : 'blue' , #NEWLINE# 'length' : 1.0 , #NEWLINE# #UNINDENT# } ] ) ) #NEWLINE# assert ( predict . call_args_list [ 1 ] [ 0 ] [ 1 ] == np . array ( [ { #NEWLINE# #INDENT# 'id' : 1 , #NEWLINE# 'color' : '{"a": 1, "b": 2}' , #NEWLINE# 'length' : 1.0 , #NEWLINE# #UNINDENT# } ] ) ) #NEWLINE# assert ( predict . call_args_list [ 2 ] [ 0 ] [ 1 ] == np . array ( [ { #NEWLINE# #INDENT# 'id' : 1 , #NEWLINE# 'color' : 'blue' , #NEWLINE# 'length' : 1.0 , #NEWLINE# #UNINDENT# } , { #NEWLINE# #INDENT# 'id' : 2 , #NEWLINE# 'color' : '{"a": 1, "b": 2}' , #NEWLINE# 'length' : 1.0 , #NEWLINE# #UNINDENT# } ] ) ) . all ( ) #NEWLINE# assert ( ujson . loads ( predict . call_args_list [ 1 ] [ 0 ] [ 1 ] [ 0 ] [ 'color' ] ) == { #NEWLINE# #INDENT# 'a' : 1 , #NEWLINE# 'b' : 2 , #NEWLINE# #UNINDENT# } )