<s> import random <EOL> import unittest <EOL> import time <EOL> import threading <EOL> try : <EOL> import BaseHTTPServer as HTTPServer <EOL> except ImportError : <EOL> import http . server as HTTPServer <EOL> import sys , os , os . path <EOL> rootDirectory = os . path . join ( os . path . dirname ( os . path . realpath ( __file__ ) ) , '<STR_LIT:..>' , '<STR_LIT:..>' ) <EOL> if rootDirectory not in sys . path : <EOL> sys . path . append ( rootDirectory ) <EOL> from applicationinsights import channel <EOL> class TestSenderBase ( unittest . TestCase ) : <EOL> def test_construct ( self ) : <EOL> actual = channel . SenderBase ( '<STR_LIT>' ) <EOL> self . assertIsNotNone ( actual ) <EOL> self . assertEqual ( '<STR_LIT>' , actual . service_endpoint_uri ) <EOL> self . assertIsNone ( actual . queue ) <EOL> self . assertEqual ( <NUM_LIT:100> , actual . send_buffer_size ) <EOL> def test_service_endpoint_uri_works_as_expected ( self ) : <EOL> actual = channel . SenderBase ( '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT>' , actual . service_endpoint_uri ) <EOL> actual . service_endpoint_uri = '<STR_LIT:foo>' <EOL> self . assertEqual ( '<STR_LIT:foo>' , actual . service_endpoint_uri ) <EOL> def test_queue_works_as_expected ( self ) : <EOL> actual = channel . SenderBase ( '<STR_LIT>' ) <EOL> self . assertIsNone ( actual . queue ) <EOL> expected = object ( ) <EOL> actual . queue = expected <EOL> self . assertEqual ( expected , actual . queue ) <EOL> def test_send_buffer_size_works_as_expected ( self ) : <EOL> actual = channel . SenderBase ( '<STR_LIT>' ) <EOL> self . assertEqual ( <NUM_LIT:100> , actual . send_buffer_size ) <EOL> actual . send_buffer_size = <NUM_LIT> <EOL> self . assertEqual ( <NUM_LIT> , actual . send_buffer_size ) <EOL> actual . send_buffer_size = - <NUM_LIT:1> <EOL> self . assertEqual ( <NUM_LIT:1> , actual . send_buffer_size ) <EOL> def test_send_works_as_expected ( self ) : <EOL> port = random . randint ( <NUM_LIT> , <NUM_LIT> ) <EOL> actual = channel . SenderBase ( "<STR_LIT>" + str ( port ) + "<STR_LIT>" ) <EOL> actual . queue = channel . QueueBase ( None ) <EOL> MockHTTPRequestHandler . ExpectedContent = "<STR_LIT>" <EOL> MockHTTPRequestHandler . TestCase = self <EOL> thread = WorkerThread ( actual ) <EOL> thread . start ( ) <EOL> runHttpHandlerOnce ( handler = MockHTTPRequestHandler , port = port ) <EOL> thread . join ( ) <EOL> if "<STR_LIT>" in dir ( self ) : <EOL> self . fail ( self . failed ) <EOL> self . assertEqual ( None , actual . queue . get ( ) ) <EOL> class WorkerThread ( threading . Thread ) : <EOL> def __init__ ( self , sender ) : <EOL> threading . Thread . __init__ ( self ) <EOL> self . sender = sender <EOL> def run ( self ) : <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> self . sender . send ( [ MockSerializable ( <NUM_LIT> ) , MockSerializable ( <NUM_LIT> ) ] ) <EOL> class MockSerializable ( object ) : <EOL> def __init__ ( self , data ) : <EOL> self . _data = data <EOL> def write ( self ) : <EOL> return self . _data <EOL> class MockHTTPRequestHandler ( HTTPServer . BaseHTTPRequestHandler ) : <EOL> ExpectedContent = None <EOL> TestCase = None <EOL> def do_POST ( self ) : <EOL> contentLength = int ( self . headers [ '<STR_LIT>' ] ) <EOL> content = self . rfile . read ( contentLength ) <EOL> response = "<STR_LIT>" <EOL> if isinstance ( content , bytes ) : <EOL> content = content . decode ( "<STR_LIT:utf-8>" ) <EOL> response = b"<STR_LIT>" <EOL> if "<STR_LIT:POST>" != self . command : <EOL> MockHTTPRequestHandler . TestCase . failed = '<STR_LIT>' <EOL> if "<STR_LIT>" != self . headers [ "<STR_LIT:Content-Type>" ] : <EOL> MockHTTPRequestHandler . TestCase . failed = '<STR_LIT>' <EOL> if MockHTTPRequestHandler . ExpectedContent != content : <EOL> MockHTTPRequestHandler . TestCase . failed = '<STR_LIT:">' + MockHTTPRequestHandler . ExpectedContent + '<STR_LIT>' <EOL> self . send_response ( <NUM_LIT:200> ) <EOL> self . send_header ( "<STR_LIT:Content-Type>" , "<STR_LIT:application/json>" ) <EOL> self . send_header ( "<STR_LIT>" , "<STR_LIT:0>" ) <EOL> self . end_headers ( ) <EOL> self . wfile . write ( response ) <EOL> def runHttpHandlerOnce ( server = HTTPServer . HTTPServer , handler = HTTPServer . BaseHTTPRequestHandler , port = <NUM_LIT> ) : <EOL> serverAddress = ( '<STR_LIT>' , port ) <EOL> httpd = server ( serverAddress , handler ) <EOL> httpd . handle_request ( ) </s>
