<s> from __future__ import absolute_import , unicode_literals <EOL> from datetime import datetime <EOL> from itertools import count <EOL> from time import time <EOL> from celery import states <EOL> from celery . events import Event as _Event <EOL> from celery . events . state import State , Worker , Task <EOL> from celery . utils import gen_unique_id <EOL> from djcelery import celery <EOL> from djcelery import snapshot <EOL> from djcelery import models <EOL> from djcelery . utils import make_aware <EOL> from djcelery . tests . utils import unittest <EOL> from djcelery . compat import unicode <EOL> _next_id_gen = count ( <NUM_LIT:0> ) <EOL> _next_id = lambda : next ( _next_id_gen ) <EOL> _next_clock_gen = count ( <NUM_LIT:1> ) <EOL> _next_clock = lambda : next ( _next_clock_gen ) <EOL> def Event ( * args , ** kwargs ) : <EOL> kwargs . setdefault ( '<STR_LIT>' , _next_clock ( ) ) <EOL> kwargs . setdefault ( '<STR_LIT>' , time ( ) ) <EOL> return _Event ( * args , ** kwargs ) <EOL> def create_task ( worker , ** kwargs ) : <EOL> d = dict ( uuid = gen_unique_id ( ) , <EOL> name = '<STR_LIT>' . format ( _next_id ( ) ) , <EOL> worker = worker ) <EOL> return Task ( ** dict ( d , ** kwargs ) ) <EOL> class test_Camera ( unittest . TestCase ) : <EOL> Camera = snapshot . Camera <EOL> def setUp ( self ) : <EOL> self . state = State ( ) <EOL> self . cam = self . Camera ( self . state ) <EOL> def test_constructor ( self ) : <EOL> cam = self . Camera ( State ( ) ) <EOL> self . assertTrue ( cam . state ) <EOL> self . assertTrue ( cam . freq ) <EOL> self . assertTrue ( cam . cleanup_freq ) <EOL> self . assertTrue ( cam . logger ) <EOL> def test_get_heartbeat ( self ) : <EOL> worker = Worker ( hostname = '<STR_LIT>' ) <EOL> self . assertIsNone ( self . cam . get_heartbeat ( worker ) ) <EOL> t1 = time ( ) <EOL> t2 = time ( ) <EOL> t3 = time ( ) <EOL> for t in t1 , t2 , t3 : <EOL> worker . event ( '<STR_LIT>' , t , t , { } ) <EOL> self . state . workers [ worker . hostname ] = worker <EOL> self . assertEqual ( self . cam . get_heartbeat ( worker ) , <EOL> make_aware ( datetime . fromtimestamp ( t3 ) ) ) <EOL> def test_handle_worker ( self ) : <EOL> worker = Worker ( hostname = '<STR_LIT>' ) <EOL> worker . event ( '<STR_LIT>' , time ( ) , time ( ) , { } ) <EOL> self . cam . _last_worker_write . clear ( ) <EOL> m = self . cam . handle_worker ( ( worker . hostname , worker ) ) <EOL> self . assertTrue ( m ) <EOL> self . assertTrue ( m . hostname ) <EOL> self . assertTrue ( m . last_heartbeat ) <EOL> self . assertTrue ( m . is_alive ( ) ) <EOL> self . assertEqual ( unicode ( m ) , unicode ( m . hostname ) ) <EOL> self . assertTrue ( repr ( m ) ) <EOL> def test_handle_task_received ( self ) : <EOL> worker = Worker ( hostname = '<STR_LIT>' ) <EOL> worker . event ( '<STR_LIT>' , time ( ) , time ( ) , { } ) <EOL> self . cam . handle_worker ( ( worker . hostname , worker ) ) <EOL> task = create_task ( worker ) <EOL> task . event ( '<STR_LIT>' , time ( ) , time ( ) , { } ) <EOL> self . assertEqual ( task . state , '<STR_LIT>' ) <EOL> mt = self . cam . handle_task ( ( task . uuid , task ) ) <EOL> self . assertEqual ( mt . name , task . name ) <EOL> self . assertTrue ( unicode ( mt ) ) <EOL> self . assertTrue ( repr ( mt ) ) <EOL> mt . eta = celery . now ( ) <EOL> self . assertIn ( '<STR_LIT>' , unicode ( mt ) ) <EOL> self . assertIn ( mt , models . TaskState . objects . active ( ) ) <EOL> def test_handle_task ( self ) : <EOL> worker1 = Worker ( hostname = '<STR_LIT>' ) <EOL> worker1 . event ( '<STR_LIT>' , time ( ) , time ( ) , { } ) <EOL> mw = self . cam . handle_worker ( ( worker1 . hostname , worker1 ) ) <EOL> task1 = create_task ( worker1 ) <EOL> task1 . event ( '<STR_LIT>' , time ( ) , time ( ) , { } ) <EOL> mt = self . cam . handle_task ( ( task1 . uuid , task1 ) ) <EOL> self . assertEqual ( mt . worker , mw ) <EOL> worker2 = Worker ( hostname = None ) <EOL> task2 = create_task ( worker2 ) <EOL> task2 . event ( '<STR_LIT>' , time ( ) , time ( ) , { } ) <EOL> mt = self . cam . handle_task ( ( task2 . uuid , task2 ) ) <EOL> self . assertIsNone ( mt . worker ) <EOL> task1 . event ( '<STR_LIT>' , time ( ) , time ( ) , { '<STR_LIT:result>' : <NUM_LIT> } ) <EOL> self . assertEqual ( task1 . state , states . SUCCESS ) <EOL> self . assertEqual ( task1 . result , <NUM_LIT> ) <EOL> mt = self . cam . handle_task ( ( task1 . uuid , task1 ) ) <EOL> self . assertEqual ( mt . name , task1 . name ) <EOL> self . assertEqual ( mt . result , <NUM_LIT> ) <EOL> task3 = create_task ( worker1 , name = None ) <EOL> task3 . event ( '<STR_LIT>' , time ( ) , time ( ) , { } ) <EOL> mt = self . cam . handle_task ( ( task3 . uuid , task3 ) ) <EOL> self . assertIsNone ( mt ) <EOL> def assertExpires ( self , dec , expired , tasks = <NUM_LIT:10> ) : <EOL> worker = Worker ( hostname = '<STR_LIT>' ) <EOL> worker . event ( '<STR_LIT>' , time ( ) , time ( ) , { } ) <EOL> for total in range ( tasks ) : <EOL> task = create_task ( worker ) <EOL> task . event ( '<STR_LIT>' , time ( ) - dec , time ( ) - dec , { } ) <EOL> task . event ( '<STR_LIT>' , time ( ) - dec , time ( ) - dec , { '<STR_LIT:result>' : <NUM_LIT> } ) <EOL> self . assertTrue ( task . name ) <EOL> self . assertTrue ( self . cam . handle_task ( ( task . uuid , task ) ) ) <EOL> self . assertEqual ( self . cam . on_cleanup ( ) , expired ) <EOL> def test_on_cleanup_expires ( self , dec = <NUM_LIT> ) : <EOL> self . assertExpires ( dec , <NUM_LIT:10> ) <EOL> def test_on_cleanup_does_not_expire_new ( self , dec = <NUM_LIT:0> ) : <EOL> self . assertExpires ( dec , <NUM_LIT:0> ) <EOL> def test_on_shutter ( self ) : <EOL> state = self . state <EOL> cam = self . cam <EOL> ws = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> uus = [ gen_unique_id ( ) for i in range ( <NUM_LIT:50> ) ] <EOL> events = [ Event ( '<STR_LIT>' , hostname = ws [ <NUM_LIT:0> ] ) , <EOL> Event ( '<STR_LIT>' , hostname = ws [ <NUM_LIT:1> ] ) , <EOL> Event ( '<STR_LIT>' , hostname = ws [ <NUM_LIT:2> ] ) , <EOL> Event ( '<STR_LIT>' , <EOL> uuid = uus [ <NUM_LIT:0> ] , name = '<STR_LIT:A>' , hostname = ws [ <NUM_LIT:0> ] ) , <EOL> Event ( '<STR_LIT>' , <EOL> uuid = uus [ <NUM_LIT:0> ] , name = '<STR_LIT:A>' , hostname = ws [ <NUM_LIT:0> ] ) , <EOL> Event ( '<STR_LIT>' , <EOL> uuid = uus [ <NUM_LIT:1> ] , name = '<STR_LIT:B>' , hostname = ws [ <NUM_LIT:1> ] ) , <EOL> Event ( '<STR_LIT>' , <EOL> uuid = uus [ <NUM_LIT:2> ] , name = '<STR_LIT:C>' , hostname = ws [ <NUM_LIT:2> ] ) ] <EOL> for event in events : <EOL> event [ '<STR_LIT>' ] = time ( ) <EOL> state . event ( event ) <EOL> cam . on_shutter ( state ) <EOL> for host in ws : <EOL> worker = models . WorkerState . objects . get ( hostname = host ) <EOL> self . assertTrue ( worker . is_alive ( ) ) <EOL> t1 = models . TaskState . objects . get ( task_id = uus [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( t1 . state , '<STR_LIT>' ) <EOL> self . assertEqual ( t1 . name , '<STR_LIT:A>' ) <EOL> t2 = models . TaskState . objects . get ( task_id = uus [ <NUM_LIT:1> ] ) <EOL> self . assertEqual ( t2 . state , '<STR_LIT>' ) <EOL> t3 = models . TaskState . objects . get ( task_id = uus [ <NUM_LIT:2> ] ) <EOL> self . assertEqual ( t3 . state , '<STR_LIT>' ) <EOL> events = [ Event ( '<STR_LIT>' , <EOL> uuid = uus [ <NUM_LIT:0> ] , hostname = ws [ <NUM_LIT:0> ] , result = <NUM_LIT> ) , <EOL> Event ( '<STR_LIT>' , <EOL> uuid = uus [ <NUM_LIT:1> ] , exception = "<STR_LIT>" , <EOL> hostname = ws [ <NUM_LIT:1> ] ) , <EOL> Event ( '<STR_LIT>' , hostname = ws [ <NUM_LIT:0> ] ) ] <EOL> list ( map ( state . event , events ) ) <EOL> cam . _last_worker_write . clear ( ) <EOL> cam . on_shutter ( state ) <EOL> w1 = models . WorkerState . objects . get ( hostname = ws [ <NUM_LIT:0> ] ) <EOL> self . assertFalse ( w1 . is_alive ( ) ) <EOL> t1 = models . TaskState . objects . get ( task_id = uus [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( t1 . state , '<STR_LIT>' ) <EOL> self . assertEqual ( t1 . result , '<STR_LIT>' ) <EOL> self . assertEqual ( t1 . worker , w1 ) <EOL> t2 = models . TaskState . objects . get ( task_id = uus [ <NUM_LIT:1> ] ) <EOL> self . assertEqual ( t2 . state , '<STR_LIT>' ) <EOL> self . assertEqual ( t2 . result , "<STR_LIT>" ) <EOL> self . assertEqual ( t2 . worker . hostname , ws [ <NUM_LIT:1> ] ) <EOL> cam . on_shutter ( state ) </s>
