1+ from auto_function_serving .ServerHandler import ServerHandler , AsyncServerHandler
2+ from multiprocessing import Pool
3+ import pickle
4+ import asyncio
5+ import time
6+
7+ import unittest
8+
9+ class alltests (unittest .TestCase ):
10+ def testeverything (self ):
11+ def servers_running ():
12+ return [ServerHandler .port_inuse (ServerHandler .ip_address , port ) for port in (1234 , 58604 , 52881 )]
13+ assert servers_running () == [False , False , False ], f"SERVERS ARE RUNNING, { servers_running ()} "
14+
15+ return_one = ServerHandler ("""
16+ def return_one():
17+ return 1
18+ """ ,"return_one" , port = 1234 , backend = 'multiprocessing' )
19+
20+ @ServerHandler .decorator
21+ def fact (n , somesting = "Default" ):
22+ if n < 0 :
23+ return somesting
24+ if n == 0 :
25+ return 1
26+ return n * fact (n - 1 )
27+
28+ @AsyncServerHandler .decorator
29+ def stringreverseextra (to_reverse , extra = "MORE THINGS" ):
30+ return to_reverse [::- 1 ] + extra
31+
32+ ports_used = (return_one .port , stringreverseextra .port , fact .port )
33+ assert ports_used == (1234 , 58604 , 52881 ), f"Wrong ports used, { str (ports_used )} "
34+
35+ assert servers_running () == [True , True , True ], f"SERVERS ARE NOT RUNNING, { servers_running ()} "
36+
37+ a ,b ,c = pickle .dumps (fact ), pickle .dumps (stringreverseextra ), pickle .dumps (return_one )
38+
39+ assert return_one () == 1 , "NEEDS TO RETURN ONE"
40+ assert asyncio .run (stringreverseextra ("CBA" , extra = "DEF" )) == "ABCDEF" , "Async or kwargs failed"
41+ with Pool (5 ) as p :
42+ assert p .map (fact , [- 1 , 2 , 3 , 4 , 5 ]) == ['Default' , 2 , 6 , 24 , 120 ]
43+ async_call = stringreverseextra ("CBA" , extra = "DEF" )
44+ assert str (type (async_call )) == "<class 'coroutine'>" ; "Is not a coroutine"
45+ assert asyncio .run (async_call ) == "ABCDEF" , "Async or kwargs failed"
46+
47+ print ("ALL FUNCTION CALLS WORKED" )
48+
49+ #DELETE
50+ return_one .__del__ ()
51+ stringreverseextra .__del__ ()
52+ fact .__del__ ()
53+
54+ start_time = time .time ()
55+ while servers_running () != [False , False , False ]:
56+ time .sleep (1 )
57+ print (servers_running ())
58+ assert time .time () - start_time < 20 , "Too much time to close a server"
59+
60+ assert servers_running () == [False , False , False ], f"SERVERS ARE RUNNING AFTER CLOSE COMMAND, { servers_running ()} "
61+
62+ print ("CLOSING SERVERS WORKED" )
63+
64+ return_one = pickle .loads (c )
65+ fact = pickle .loads (a )
66+ stringreverseextra = pickle .loads (b )
67+
68+ assert return_one () == 1 , "NEEDS TO RETURN ONE"
69+ assert asyncio .run (stringreverseextra ("CBA" , extra = "DEF" )) == "ABCDEF" , "Async or kwargs failed"
70+ with Pool (5 ) as p :
71+ assert p .map (fact , [- 1 , 2 , 3 , 4 , 5 ]) == ['Default' , 2 , 6 , 24 , 120 ]
72+ async_call = stringreverseextra ("CBA" , extra = "DEF" )
73+ assert str (type (async_call )) == "<class 'coroutine'>" ; "Is not a coroutine"
74+ assert asyncio .run (async_call ) == "ABCDEF" , "Async or kwargs failed"
75+
76+ print ("ALL FUNCTION CALLS WORKED" )
77+
78+ if __name__ == '__main__' :
79+ unittest .main ()
0 commit comments