<s> from base64 import b64encode <EOL> from geventhttpclient import HTTPClient , URL <EOL> from multiprocessing . pool import ThreadPool <EOL> import datetime <EOL> import os <EOL> import redis <EOL> import subprocess <EOL> import tempfile <EOL> import time <EOL> def get_async ( ) : <EOL> pool = ThreadPool ( processes = <NUM_LIT:1> ) <EOL> async_result = pool . apply_async ( get ) <EOL> return async_result <EOL> def get ( path = "<STR_LIT>" ) : <EOL> url = URL ( "<STR_LIT>" % ( os . getenv ( "<STR_LIT>" , '<STR_LIT:localhost>' ) , path ) ) <EOL> http = HTTPClient . from_url ( url ) <EOL> response = http . get ( path ) <EOL> return response <EOL> def run_broadcaster ( frames_path , envs = { } ) : <EOL> env = os . environ . copy ( ) <EOL> env [ "<STR_LIT>" ] = frames_path <EOL> env . update ( envs ) <EOL> p = subprocess . Popen ( [ "<STR_LIT>" , "<STR_LIT>" ] , env = env ) <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> return p <EOL> def create_image ( tempdir , channel , content ) : <EOL> image_dir = os . path . join ( tempdir , channel ) <EOL> if not os . path . exists ( image_dir ) : <EOL> os . mkdir ( image_dir ) <EOL> image = tempfile . NamedTemporaryFile ( dir = image_dir , delete = False ) <EOL> image . write ( content ) <EOL> image . close ( ) <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> return image <EOL> def test_when_file_is_created_its_contents_are_posted ( ) : <EOL> tempdir = tempfile . mkdtemp ( ) <EOL> p = run_broadcaster ( tempdir ) <EOL> r = get_async ( ) <EOL> create_image ( tempdir , "<STR_LIT>" , "<STR_LIT>" ) <EOL> p . terminate ( ) <EOL> buffer_size = <NUM_LIT> <EOL> assert "<STR_LIT>" in r . get ( timeout = <NUM_LIT:2> ) . read ( buffer_size ) <EOL> def test_file_is_deleted_after_post ( ) : <EOL> tempdir = tempfile . mkdtemp ( ) <EOL> p = run_broadcaster ( tempdir ) <EOL> image = create_image ( tempdir , "<STR_LIT>" , "<STR_LIT>" ) <EOL> p . terminate ( ) <EOL> assert os . path . exists ( image . name ) is False <EOL> def test_contents_are_encoded ( ) : <EOL> tempdir = tempfile . mkdtemp ( ) <EOL> p = run_broadcaster ( tempdir , envs = { "<STR_LIT>" : "<STR_LIT:1>" } ) <EOL> r = get_async ( ) <EOL> create_image ( tempdir , "<STR_LIT>" , "<STR_LIT>" ) <EOL> p . terminate ( ) <EOL> buffer_size = <NUM_LIT> <EOL> data = r . get ( timeout = <NUM_LIT:2> ) . read ( buffer_size ) <EOL> assert "<STR_LIT>" not in data <EOL> assert b64encode ( "<STR_LIT>" ) in data <EOL> def test_image_is_posted_to_redis_with_utctimestamp ( ) : <EOL> tempdir = tempfile . mkdtemp ( ) <EOL> r = redis . StrictRedis ( host = os . getenv ( "<STR_LIT>" , '<STR_LIT:localhost>' ) , port = int ( os . getenv ( "<STR_LIT>" , <NUM_LIT> ) ) , db = <NUM_LIT:0> ) <EOL> r . delete ( "<STR_LIT>" ) <EOL> now = int ( time . mktime ( datetime . datetime . utcnow ( ) . timetuple ( ) ) ) <EOL> p = run_broadcaster ( tempdir , envs = { "<STR_LIT>" : "<STR_LIT:1>" } ) <EOL> create_image ( tempdir , "<STR_LIT>" , "<STR_LIT>" ) <EOL> p . terminate ( ) <EOL> keys = r . zrangebyscore ( "<STR_LIT>" , min = str ( now - <NUM_LIT:1> ) , max = str ( now + <NUM_LIT:1> ) , start = <NUM_LIT:0> , num = <NUM_LIT:1> ) <EOL> assert len ( keys ) == <NUM_LIT:1> <EOL> assert r . get ( keys [ <NUM_LIT:0> ] ) == "<STR_LIT>" <EOL> def test_image_can_be_get_from_nginx ( ) : <EOL> tempdir = tempfile . mkdtemp ( ) <EOL> r = redis . StrictRedis ( host = os . getenv ( "<STR_LIT>" , '<STR_LIT:localhost>' ) , port = int ( os . getenv ( "<STR_LIT>" , <NUM_LIT> ) ) , db = <NUM_LIT:0> ) <EOL> r . delete ( "<STR_LIT>" ) <EOL> now = time . time ( ) <EOL> p = run_broadcaster ( tempdir , envs = { "<STR_LIT>" : "<STR_LIT:1>" } ) <EOL> create_image ( tempdir , "<STR_LIT>" , "<STR_LIT>" ) <EOL> create_image ( tempdir , "<STR_LIT>" , "<STR_LIT>" ) <EOL> p . terminate ( ) <EOL> assert get ( "<STR_LIT>" % ( now - <NUM_LIT:1> ) ) . read ( ) == "<STR_LIT>" <EOL> assert get ( "<STR_LIT>" ) . read ( ) == "<STR_LIT>" <EOL> def test_image_can_be_get_from_nginx_with_utctimestamp ( ) : <EOL> tempdir = tempfile . mkdtemp ( ) <EOL> r = redis . StrictRedis ( host = os . getenv ( "<STR_LIT>" , '<STR_LIT:localhost>' ) , port = int ( os . getenv ( "<STR_LIT>" , <NUM_LIT> ) ) , db = <NUM_LIT:0> ) <EOL> r . delete ( "<STR_LIT>" ) <EOL> utcnow = int ( time . mktime ( datetime . datetime . utcnow ( ) . timetuple ( ) ) ) <EOL> p = run_broadcaster ( tempdir , envs = { "<STR_LIT>" : "<STR_LIT:1>" } ) <EOL> create_image ( tempdir , "<STR_LIT>" , "<STR_LIT>" ) <EOL> create_image ( tempdir , "<STR_LIT>" , "<STR_LIT>" ) <EOL> p . terminate ( ) <EOL> assert get ( "<STR_LIT>" % ( utcnow - <NUM_LIT:1> ) ) . read ( ) == "<STR_LIT>" <EOL> assert get ( "<STR_LIT>" ) . read ( ) == "<STR_LIT>" </s>
