11import unittest
2+ from time import sleep
23
34from executorlib import SingleNodeExecutor
45from executorlib .standalone .serialize import cloudpickle_register
@@ -12,6 +13,15 @@ def resource_dict(resource_dict):
1213 return resource_dict
1314
1415
16+ def get_worker_id (executorlib_worker_id ):
17+ sleep (0.1 )
18+ return executorlib_worker_id
19+
20+
21+ def init_function ():
22+ return {"a" : 1 , "b" : 2 }
23+
24+
1525class TestExecutorBackend (unittest .TestCase ):
1626 def test_meta_executor_serial_with_dependencies (self ):
1727 with SingleNodeExecutor (
@@ -75,3 +85,58 @@ def test_errors(self):
7585 block_allocation = True ,
7686 ) as exe :
7787 exe .submit (resource_dict , resource_dict = {})
88+
89+
90+ class TestWorkerID (unittest .TestCase ):
91+ def test_block_allocation_True (self ):
92+ with SingleNodeExecutor (
93+ max_cores = 1 ,
94+ block_allocation = True ,
95+ ) as exe :
96+ worker_id = exe .submit (get_worker_id , resource_dict = {}).result ()
97+ self .assertEqual (worker_id , 0 )
98+
99+ def test_block_allocation_True_two_workers (self ):
100+ with SingleNodeExecutor (
101+ max_cores = 2 ,
102+ block_allocation = True ,
103+ ) as exe :
104+ f1_worker_id = exe .submit (get_worker_id , resource_dict = {})
105+ f2_worker_id = exe .submit (get_worker_id , resource_dict = {})
106+ self .assertEqual (sum ([f1_worker_id .result (), f2_worker_id .result ()]), 1 )
107+
108+ def test_init_function (self ):
109+ with SingleNodeExecutor (
110+ max_cores = 1 ,
111+ block_allocation = True ,
112+ init_function = init_function ,
113+ ) as exe :
114+ worker_id = exe .submit (get_worker_id , resource_dict = {}).result ()
115+ self .assertEqual (worker_id , 0 )
116+
117+ def test_init_function_two_workers (self ):
118+ with SingleNodeExecutor (
119+ max_cores = 2 ,
120+ block_allocation = True ,
121+ init_function = init_function ,
122+ ) as exe :
123+ f1_worker_id = exe .submit (get_worker_id , resource_dict = {})
124+ f2_worker_id = exe .submit (get_worker_id , resource_dict = {})
125+ self .assertEqual (sum ([f1_worker_id .result (), f2_worker_id .result ()]), 1 )
126+
127+ def test_block_allocation_False (self ):
128+ with SingleNodeExecutor (
129+ max_cores = 1 ,
130+ block_allocation = False ,
131+ ) as exe :
132+ worker_id = exe .submit (get_worker_id , resource_dict = {}).result ()
133+ self .assertEqual (worker_id , 0 )
134+
135+ def test_block_allocation_False_two_workers (self ):
136+ with SingleNodeExecutor (
137+ max_cores = 2 ,
138+ block_allocation = False ,
139+ ) as exe :
140+ f1_worker_id = exe .submit (get_worker_id , resource_dict = {})
141+ f2_worker_id = exe .submit (get_worker_id , resource_dict = {})
142+ self .assertEqual (sum ([f1_worker_id .result (), f2_worker_id .result ()]), 0 )
0 commit comments