Best Python code snippet using locust
test_greenlet.py
Source:test_greenlet.py
...6 def f():7 lst.append(1)8 greenlet.getcurrent().parent.switch()9 lst.append(3)10 g = greenlet(f)11 lst.append(0)12 g.switch()13 lst.append(2)14 g.switch()15 lst.append(4)16 assert lst == range(5)17 def test_parent(self):18 from greenlet import greenlet19 gmain = greenlet.getcurrent()20 assert gmain.parent is None21 g = greenlet(lambda: None)22 assert g.parent is gmain23 def test_pass_around(self):24 from greenlet import greenlet25 seen = []26 def f(x, y):27 seen.append((x, y))28 seen.append(greenlet.getcurrent().parent.switch())29 seen.append(greenlet.getcurrent().parent.switch(42))30 return 44, 'z'31 g = greenlet(f)32 seen.append(g.switch(40, 'x'))33 seen.append(g.switch(41, 'y'))34 seen.append(g.switch(43))35 #36 def f2():37 return 4538 g = greenlet(f2)39 seen.append(g.switch())40 #41 def f3():42 pass43 g = greenlet(f3)44 seen.append(g.switch())45 #46 assert seen == [(40, 'x'), (), (41, 'y'), 42, 43, (44, 'z'), 45, None]47 def test_exception_simple(self):48 from greenlet import greenlet49 #50 def fmain():51 raise ValueError52 #53 g1 = greenlet(fmain)54 raises(ValueError, g1.switch)55 def test_dead(self):56 from greenlet import greenlet57 #58 def fmain():59 assert g1 and not g1.dead60 #61 g1 = greenlet(fmain)62 assert not g1 and not g1.dead63 g1.switch()64 assert not g1 and g1.dead65 #66 gmain = greenlet.getcurrent()67 assert gmain and not gmain.dead68 def test_GreenletExit(self):69 from greenlet import greenlet, GreenletExit70 #71 def fmain(*args):72 raise GreenletExit(*args)73 #74 g1 = greenlet(fmain)75 res = g1.switch('foo', 'bar')76 assert isinstance(res, GreenletExit) and res.args == ('foo', 'bar')77 def test_throw_1(self):78 from greenlet import greenlet79 gmain = greenlet.getcurrent()80 #81 def f():82 try:83 gmain.switch()84 except ValueError:85 return "ok"86 #87 g = greenlet(f)88 g.switch()89 res = g.throw(ValueError)90 assert res == "ok"91 def test_throw_2(self):92 from greenlet import greenlet93 gmain = greenlet.getcurrent()94 #95 def f():96 gmain.throw(ValueError)97 #98 g = greenlet(f)99 raises(ValueError, g.switch)100 def test_throw_3(self):101 from greenlet import greenlet102 gmain = greenlet.getcurrent()103 raises(ValueError, gmain.throw, ValueError)104 def test_throw_4(self):105 from greenlet import greenlet106 gmain = greenlet.getcurrent()107 #108 def f1():109 g2.throw(ValueError)110 #111 def f2():112 try:113 gmain.switch()114 except ValueError:115 return "ok"116 #117 g1 = greenlet(f1)118 g2 = greenlet(f2)119 g2.switch()120 res = g1.switch()121 assert res == "ok"122 def test_throw_GreenletExit(self):123 from greenlet import greenlet124 gmain = greenlet.getcurrent()125 l = [0]126 #127 def func():128 l[0] += 1129 gmain.switch()130 l[0] += 1131 #132 g = greenlet(func)133 g.switch()134 assert l[0] == 1135 g.throw()136 assert l[0] == 1137 def test_throw_GreenletExit_result(self):138 from greenlet import greenlet139 gmain = greenlet.getcurrent()140 l = [0]141 #142 def func():143 l[0] += 1144 gmain.switch()145 l[0] += 1146 #147 g = greenlet(func)148 g.switch()149 assert l[0] == 1150 ge1 = greenlet.GreenletExit(1, 2, 3)151 ge2 = g.throw(ge1)152 assert l[0] == 1153 assert ge1 is ge2154 def test_nondefault_parent(self):155 from greenlet import greenlet156 #157 def f1():158 g2 = greenlet(f2)159 res = g2.switch()160 assert res == "from 2"161 return "from 1"162 #163 def f2():164 return "from 2"165 #166 g1 = greenlet(f1)167 res = g1.switch()168 assert res == "from 1"169 def test_change_parent(self):170 from greenlet import greenlet171 #172 def f1():173 res = g2.switch()174 assert res == "from 2"175 return "from 1"176 #177 def f2():178 return "from 2"179 #180 g1 = greenlet(f1)181 g2 = greenlet(f2)182 g2.parent = g1183 res = g1.switch()184 assert res == "from 1"185 def test_raises_through_parent_chain(self):186 from greenlet import greenlet187 #188 def f1():189 raises(IndexError, g2.switch)190 raise ValueError191 #192 def f2():193 raise IndexError194 #195 g1 = greenlet(f1)196 g2 = greenlet(f2)197 g2.parent = g1198 raises(ValueError, g1.switch)199 def test_switch_to_dead_1(self):200 from greenlet import greenlet201 #202 def f1():203 return "ok"204 #205 g1 = greenlet(f1)206 res = g1.switch()207 assert res == "ok"208 res = g1.switch("goes to gmain instead")209 assert res == "goes to gmain instead"210 def test_switch_to_dead_2(self):211 from greenlet import greenlet212 #213 def f1():214 g2 = greenlet(f2)215 return g2.switch()216 #217 def f2():218 return "ok"219 #220 g1 = greenlet(f1)221 res = g1.switch()222 assert res == "ok"223 res = g1.switch("goes to gmain instead")224 assert res == "goes to gmain instead"225 def test_switch_to_dead_3(self):226 from greenlet import greenlet227 gmain = greenlet.getcurrent()228 #229 def f1():230 res = g2.switch()231 assert res == "ok"232 res = gmain.switch("next step")233 assert res == "goes to f1 instead"234 return "all ok"235 #236 def f2():237 return "ok"238 #239 g1 = greenlet(f1)240 g2 = greenlet(f2)241 g2.parent = g1242 res = g1.switch()243 assert res == "next step"244 res = g2.switch("goes to f1 instead")245 assert res == "all ok"246 def test_throw_in_not_started_yet(self):247 from greenlet import greenlet248 #249 def f1():250 never_reached251 #252 g1 = greenlet(f1)253 raises(ValueError, g1.throw, ValueError)254 assert g1.dead255 def test_exc_info_save_restore(self):256 # sys.exc_info save/restore behaviour is wrong on CPython's greenlet257 from greenlet import greenlet258 import sys259 def f():260 try:261 raise ValueError('fun')262 except:263 exc_info = sys.exc_info()264 greenlet(h).switch()265 assert exc_info == sys.exc_info()266 def h():267 assert sys.exc_info() == (None, None, None)268 greenlet(f).switch()269 def test_exc_info_save_restore2(self):270 import sys271 from greenlet import greenlet272 result = []273 def f():274 result.append(sys.exc_info())275 g = greenlet(f)276 try:277 1 / 0278 except ZeroDivisionError:279 g.switch()280 assert result == [(None, None, None)]281 def test_gr_frame(self):282 from greenlet import greenlet283 import sys284 def f2():285 assert g.gr_frame is None286 gmain.switch()287 assert g.gr_frame is None288 def f1():289 assert gmain.gr_frame is gmain_frame290 assert g.gr_frame is None291 f2()292 assert g.gr_frame is None293 gmain = greenlet.getcurrent()294 assert gmain.gr_frame is None295 gmain_frame = sys._getframe()296 g = greenlet(f1)297 assert g.gr_frame is None298 g.switch()299 assert g.gr_frame.f_code.co_name == 'f2'300 g.switch()301 assert g.gr_frame is None302 def test_override_nonzero(self):303 from greenlet import greenlet304 class G(greenlet):305 def __nonzero__(self):306 raise ValueError307 g = G(lambda: 42)308 x = g.switch()309 assert x == 42310 def test_kwargs_to_f(self):311 import greenlet312 seen = []313 def f(*args, **kwds):314 seen.append([args, kwds])315 g = greenlet.greenlet(f)316 g.switch(1, 2, x=3, y=4)317 assert seen == [[(1, 2), {'x': 3, 'y': 4}]]318 def test_kwargs_to_switch(self):319 import greenlet320 main = greenlet.getcurrent()321 assert main.switch() == ()322 assert main.switch(5) == 5323 assert main.switch(5, 6) == (5, 6)324 #325 assert main.switch(x=5) == {'x': 5}326 assert main.switch(x=5, y=6) == {'x': 5, 'y': 6}327 assert main.switch(3, x=5) == ((3,), {'x': 5})328 assert main.switch(3, x=5, y=6) == ((3,), {'x': 5, 'y': 6})329 assert main.switch(2, 3, x=6) == ((2, 3), {'x': 6})330 def test_throw_GreenletExit_not_started(self):331 import greenlet332 def f():333 never_executed334 g = greenlet.greenlet(f)335 e = greenlet.GreenletExit()336 x = g.throw(e)337 assert x is e338 def test_throw_GreenletExit_already_finished(self):339 import greenlet340 def f():341 pass342 g = greenlet.greenlet(f)343 g.switch()344 e = greenlet.GreenletExit()345 x = g.throw(e)346 assert x is e347 def test_throw_exception_already_finished(self):348 import greenlet349 def f():350 pass351 g = greenlet.greenlet(f)352 g.switch()353 seen = []354 class MyException(Exception):355 def __init__(self):356 seen.append(1)357 try:358 g.throw(MyException)359 except MyException:360 pass361 else:362 raise AssertionError("no exception??")...
greenlet.py
Source:greenlet.py
...19 return _tls.current20# ____________________________________________________________21# The 'greenlet' class22_continulet = _continuation.continulet23class greenlet(_continulet):24 getcurrent = staticmethod(getcurrent)25 error = error26 GreenletExit = GreenletExit27 __main = False28 __started = False29 def __new__(cls, *args, **kwds):30 self = _continulet.__new__(cls)31 self.parent = getcurrent()32 return self33 def __init__(self, run=None, parent=None):34 if run is not None:35 self.run = run36 if parent is not None:37 self.parent = parent...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!