<s> import threading <EOL> import time <EOL> from types import ListType <EOL> import unittest <EOL> from nose . plugins . attrib import attr <EOL> from aggregator import MetricsAggregator <EOL> from dogstatsd import Server <EOL> from jmxfetch import JMXFetch <EOL> from tests . checks . common import Fixtures <EOL> STATSD_PORT = <NUM_LIT> <EOL> class DummyReporter ( threading . Thread ) : <EOL> def __init__ ( self , metrics_aggregator ) : <EOL> threading . Thread . __init__ ( self ) <EOL> self . finished = threading . Event ( ) <EOL> self . metrics_aggregator = metrics_aggregator <EOL> self . interval = <NUM_LIT:10> <EOL> self . metrics = None <EOL> self . finished = False <EOL> self . start ( ) <EOL> def run ( self ) : <EOL> while not self . finished : <EOL> time . sleep ( self . interval ) <EOL> self . flush ( ) <EOL> def flush ( self ) : <EOL> metrics = self . metrics_aggregator . flush ( ) <EOL> if metrics : <EOL> self . metrics = metrics <EOL> @ attr ( requires = '<STR_LIT>' ) <EOL> class JMXTestCase ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> aggregator = MetricsAggregator ( "<STR_LIT>" ) <EOL> self . server = Server ( aggregator , "<STR_LIT:localhost>" , STATSD_PORT ) <EOL> self . reporter = DummyReporter ( aggregator ) <EOL> self . t1 = threading . Thread ( target = self . server . start ) <EOL> self . t1 . start ( ) <EOL> confd_path = Fixtures . directory ( ) <EOL> self . jmx_daemon = JMXFetch ( confd_path , { '<STR_LIT>' : STATSD_PORT } ) <EOL> self . t2 = threading . Thread ( target = self . jmx_daemon . run ) <EOL> self . t2 . start ( ) <EOL> def tearDown ( self ) : <EOL> self . server . stop ( ) <EOL> self . reporter . finished = True <EOL> self . jmx_daemon . terminate ( ) <EOL> def testTomcatMetrics ( self ) : <EOL> count = <NUM_LIT:0> <EOL> while self . reporter . metrics is None : <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> count += <NUM_LIT:1> <EOL> if count > <NUM_LIT> : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> metrics = self . reporter . metrics <EOL> self . assertTrue ( isinstance ( metrics , ListType ) ) <EOL> self . assertTrue ( len ( metrics ) > <NUM_LIT:8> , metrics ) <EOL> self . assertEquals ( len ( [ t for t in metrics if '<STR_LIT>' in t [ '<STR_LIT>' ] and t [ '<STR_LIT>' ] == "<STR_LIT>" ] ) , <NUM_LIT:1> , metrics ) <EOL> self . assertTrue ( len ( [ t for t in metrics if "<STR_LIT>" in t [ '<STR_LIT>' ] and '<STR_LIT>' in t [ '<STR_LIT>' ] ] ) > <NUM_LIT:4> , metrics ) <EOL> self . assertTrue ( len ( [ t for t in metrics if "<STR_LIT>" in t [ '<STR_LIT>' ] and '<STR_LIT>' in t [ '<STR_LIT>' ] ] ) > <NUM_LIT:4> , metrics ) </s>
