<s> from __future__ import absolute_import <EOL> from nydus . db import create_cluster <EOL> from nydus . db . base import BaseCluster <EOL> from nydus . db . backends . redis import Redis <EOL> from nydus . db . promise import EventualCommand <EOL> from nydus . testutils import BaseTest , fixture <EOL> import mock <EOL> import redis as redis_ <EOL> class RedisPipelineTest ( BaseTest ) : <EOL> @ fixture <EOL> def cluster ( self ) : <EOL> return create_cluster ( { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { <EOL> <NUM_LIT:0> : { '<STR_LIT>' : <NUM_LIT:5> } , <EOL> <NUM_LIT:1> : { '<STR_LIT>' : <NUM_LIT:6> } , <EOL> <NUM_LIT:2> : { '<STR_LIT>' : <NUM_LIT:7> } , <EOL> <NUM_LIT:3> : { '<STR_LIT>' : <NUM_LIT:8> } , <EOL> <NUM_LIT:4> : { '<STR_LIT>' : <NUM_LIT:9> } , <EOL> } <EOL> } ) <EOL> def test_pipelined_map ( self ) : <EOL> chars = ( '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' , '<STR_LIT:d>' , '<STR_LIT:e>' , '<STR_LIT:f>' ) <EOL> with self . cluster . map ( ) as conn : <EOL> [ conn . set ( c , i ) for i , c in enumerate ( chars ) ] <EOL> res = [ conn . get ( c ) for c in chars ] <EOL> self . assertEqual ( range ( len ( chars ) ) , [ int ( r ) for r in res ] ) <EOL> def test_map_single_connection ( self ) : <EOL> with self . cluster . map ( ) as conn : <EOL> conn . set ( '<STR_LIT:a>' , '<STR_LIT:1>' ) <EOL> self . assertEquals ( self . cluster . get ( '<STR_LIT:a>' ) , '<STR_LIT:1>' ) <EOL> def test_no_proxy_without_call_on_map ( self ) : <EOL> with self . cluster . map ( ) as conn : <EOL> result = conn . incr <EOL> assert type ( result ) is EventualCommand <EOL> assert not result . was_called ( ) <EOL> class RedisTest ( BaseTest ) : <EOL> def setUp ( self ) : <EOL> self . redis = Redis ( num = <NUM_LIT:0> , db = <NUM_LIT:1> ) <EOL> self . redis . flushdb ( ) <EOL> def test_proxy ( self ) : <EOL> self . assertEquals ( self . redis . incr ( '<STR_LIT>' ) , <NUM_LIT:1> ) <EOL> def test_with_cluster ( self ) : <EOL> p = BaseCluster ( <EOL> backend = Redis , <EOL> hosts = { <NUM_LIT:0> : { '<STR_LIT>' : <NUM_LIT:1> } } , <EOL> ) <EOL> self . assertEquals ( p . incr ( '<STR_LIT>' ) , <NUM_LIT:1> ) <EOL> def test_provides_retryable_exceptions ( self ) : <EOL> self . assertEquals ( Redis . retryable_exceptions , frozenset ( [ redis_ . ConnectionError , redis_ . InvalidResponse ] ) ) <EOL> def test_provides_identifier ( self ) : <EOL> self . assertEquals ( self . redis . identifier , str ( self . redis . identifier ) ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_client_instantiates_with_kwargs ( self , RedisClient ) : <EOL> client = Redis ( num = <NUM_LIT:0> ) <EOL> client . connect ( ) <EOL> self . assertEquals ( RedisClient . call_count , <NUM_LIT:1> ) <EOL> RedisClient . assert_any_call ( host = '<STR_LIT:localhost>' , port = <NUM_LIT> , db = <NUM_LIT:0> , socket_timeout = None , <EOL> password = None , unix_socket_path = None ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_map_does_pipeline ( self , RedisClient ) : <EOL> redis = create_cluster ( { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { <EOL> <NUM_LIT:0> : { '<STR_LIT>' : <NUM_LIT:0> } , <EOL> <NUM_LIT:1> : { '<STR_LIT>' : <NUM_LIT:1> } , <EOL> } <EOL> } ) <EOL> with redis . map ( ) as conn : <EOL> conn . set ( '<STR_LIT:a>' , <NUM_LIT:0> ) <EOL> conn . set ( '<STR_LIT:d>' , <NUM_LIT:1> ) <EOL> self . assertFalse ( RedisClient ( ) . set . called ) <EOL> self . assertEquals ( RedisClient ( ) . pipeline . call_count , <NUM_LIT:2> ) <EOL> RedisClient ( ) . pipeline . assert_called_with ( ) <EOL> self . assertEquals ( RedisClient ( ) . pipeline ( ) . set . call_count , <NUM_LIT:2> ) <EOL> RedisClient ( ) . pipeline ( ) . set . assert_any_call ( '<STR_LIT:a>' , <NUM_LIT:0> ) <EOL> RedisClient ( ) . pipeline ( ) . set . assert_any_call ( '<STR_LIT:d>' , <NUM_LIT:1> ) <EOL> self . assertEquals ( RedisClient ( ) . pipeline ( ) . execute . call_count , <NUM_LIT:2> ) <EOL> RedisClient ( ) . pipeline ( ) . execute . assert_called_with ( ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_map_only_runs_on_required_nodes ( self , RedisClient ) : <EOL> redis = create_cluster ( { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { <EOL> <NUM_LIT:0> : { '<STR_LIT>' : <NUM_LIT:0> } , <EOL> <NUM_LIT:1> : { '<STR_LIT>' : <NUM_LIT:1> } , <EOL> } <EOL> } ) <EOL> with redis . map ( ) as conn : <EOL> conn . set ( '<STR_LIT:a>' , <NUM_LIT:0> ) <EOL> conn . set ( '<STR_LIT:b>' , <NUM_LIT:1> ) <EOL> self . assertFalse ( RedisClient ( ) . set . called ) <EOL> self . assertEquals ( RedisClient ( ) . pipeline . call_count , <NUM_LIT:1> ) <EOL> RedisClient ( ) . pipeline . assert_called_with ( ) <EOL> self . assertEquals ( RedisClient ( ) . pipeline ( ) . set . call_count , <NUM_LIT:2> ) <EOL> RedisClient ( ) . pipeline ( ) . set . assert_any_call ( '<STR_LIT:a>' , <NUM_LIT:0> ) <EOL> RedisClient ( ) . pipeline ( ) . set . assert_any_call ( '<STR_LIT:b>' , <NUM_LIT:1> ) <EOL> self . assertEquals ( RedisClient ( ) . pipeline ( ) . execute . call_count , <NUM_LIT:1> ) <EOL> RedisClient ( ) . pipeline ( ) . execute . assert_called_with ( ) <EOL> def test_normal_exceptions_dont_break_the_cluster ( self ) : <EOL> redis = create_cluster ( { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { <EOL> <NUM_LIT:0> : { '<STR_LIT>' : <NUM_LIT:0> } , <EOL> <NUM_LIT:1> : { '<STR_LIT>' : <NUM_LIT:1> } , <EOL> } <EOL> } ) <EOL> redis . set ( '<STR_LIT:a>' , <NUM_LIT:0> ) <EOL> with self . assertRaises ( redis_ . ResponseError ) : <EOL> redis . scard ( '<STR_LIT:a>' ) <EOL> redis . get ( '<STR_LIT:a>' ) <EOL> def test_custom_identifier_specified ( self ) : <EOL> cluster_config = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { <EOL> <NUM_LIT:0> : { '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT>' : '<STR_LIT>' } , <EOL> <NUM_LIT:1> : { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : '<STR_LIT>' } , <EOL> } , <EOL> } <EOL> redis = create_cluster ( cluster_config ) <EOL> for idx in cluster_config [ '<STR_LIT>' ] . keys ( ) : <EOL> self . assertEquals ( redis . hosts [ idx ] . identifier , <EOL> cluster_config [ '<STR_LIT>' ] [ idx ] [ '<STR_LIT>' ] ) </s>
