55
66import urllib3
77import unittest
8+ import pytest
9+ import sys
810import requests
911
12+ from multiprocessing .pool import ThreadPool
13+ from time import sleep
14+
1015import tests .apps .flask_app
1116from ..helpers import testenv
1217from instana .singletons import agent , tracer
@@ -30,6 +35,43 @@ def test_vanilla_requests(self):
3035 spans = self .recorder .queued_spans ()
3136 self .assertEqual (1 , len (spans ))
3237
38+ @pytest .mark .skipif (sys .version_info [0 ] < 3 , reason = "ThreadPool works differently on python 2" )
39+ def test_parallel_requests (self ):
40+ http_pool_5 = urllib3 .PoolManager (num_pools = 5 )
41+
42+ def task (num ):
43+ r = http_pool_5 .request ('GET' , testenv ["wsgi_server" ] + '/' , fields = {'num' : num })
44+ return r
45+
46+ with ThreadPool (processes = 5 ) as executor :
47+ # iterate over results as they become available
48+ for result in executor .map (task , (1 , 2 , 3 , 4 , 5 )):
49+ self .assertEqual (result .status , 200 )
50+
51+ spans = self .recorder .queued_spans ()
52+ self .assertEqual (5 , len (spans ))
53+ nums = map (lambda s : s .data ['http' ]['params' ].split ('=' )[1 ], spans )
54+ self .assertEqual (set (nums ), set (('1' , '2' , '3' , '4' , '5' )))
55+
56+ def test_customers_setup_zd_26466 (self ):
57+ def make_request (u = None ):
58+ sleep (10 )
59+ x = requests .get (testenv ["wsgi_server" ] + '/' )
60+ sleep (10 )
61+ return x .status_code
62+
63+ status = make_request ()
64+ #print(f'request made outside threadpool, instana should instrument - status: {status}')
65+
66+ threadpool_size = 15
67+ pool = ThreadPool (processes = threadpool_size )
68+ res = pool .map (make_request , [u for u in range (threadpool_size )])
69+ #print(f'requests made within threadpool, instana does not instrument - statuses: {res}')
70+
71+ spans = self .recorder .queued_spans ()
72+ self .assertEqual (16 , len (spans ))
73+
74+
3375 def test_get_request (self ):
3476 with tracer .start_active_span ('test' ):
3577 r = self .http .request ('GET' , testenv ["wsgi_server" ] + '/' )
0 commit comments