11# Copyright 2016, Yahoo Inc.
22# Licensed under the terms of the Apache License, Version 2.0. See the LICENSE file associated with the project for terms.
3+ import functools as fnt
4+ import itertools as itt
35import logging
46
57import pytest
6- import itertools as itt
78
8- from graphkit import base
9+ from graphkit import base , network , operation
910
1011
1112def test_jetsam_without_failure (caplog ):
@@ -75,13 +76,17 @@ def test_jetsam_dummy_locals(caplog):
7576 assert "Supressed error" not in caplog .text
7677
7778
78- def _jetsamed_fn ():
79+ def _scream (* args , ** kwargs ):
80+ raise Exception ("ABC" )
81+
82+
83+ def _jetsamed_fn (* args , ** kwargs ):
7984 b = 1
8085 with base .jetsam (locals (), a = "a" , b = "b" ):
8186 try :
8287 a = 1
8388 b = 2
84- raise Exception ( "ABC" , a , b )
89+ _scream ( )
8590 finally :
8691 locals ()
8792
@@ -97,9 +102,9 @@ def test_jetsam_nested():
97102 def inner ():
98103 with base .jetsam (locals (), fn = "fn" ):
99104 try :
100-
105+ a = 0
101106 fn = "inner"
102- raise Exception ( "ABC" )
107+ _jetsamed_fn ( )
103108 finally :
104109 locals ()
105110
@@ -108,11 +113,87 @@ def outer():
108113 try :
109114
110115 fn = "outer"
116+ b = 0
111117 inner ()
112118 finally :
113119 locals ()
114120
115121 with pytest .raises (Exception , match = "ABC" ) as excinfo :
116122 outer ()
117123
118- assert excinfo .value .graphkit_jetsam == {"fn" : "inner" }
124+ assert excinfo .value .graphkit_jetsam == {"fn" : "inner" , "a" : 1 , "b" : 2 }
125+
126+
127+ def screaming_dumy_op ():
128+ # No jetsam, in particular, to check sites.
129+ class Op :
130+ _compute = _scream
131+
132+ return Op ()
133+
134+
135+ @pytest .mark .parametrize (
136+ "acallable, expected_jetsam" ,
137+ [
138+ # NO old-stuff Operation(fn=_jetsamed_fn, name="test", needs="['a']", provides=[]),
139+ (
140+ fnt .partial (
141+ operation (name = "test" , needs = ["a" ], provides = ["b" ])(_scream )._compute ,
142+ named_inputs = {"a" : 1 },
143+ ),
144+ "outputs provides results operation args" .split (),
145+ ),
146+ (
147+ fnt .partial (
148+ network .ExecutionPlan (* ([None ] * 7 ))._call_operation ,
149+ op = screaming_dumy_op (),
150+ solution = {},
151+ ),
152+ ["plan" ],
153+ ),
154+ # Not easy to test Network calling a screaming func (see next TC).
155+ ],
156+ )
157+ def test_jetsam_sites_screaming_func (acallable , expected_jetsam ):
158+ # Check jetsams when the underlying function fails.
159+ with pytest .raises (Exception , match = "ABC" ) as excinfo :
160+ acallable ()
161+
162+ ex = excinfo .value
163+ assert ex .graphkit_jetsam .keys () == set (expected_jetsam )
164+
165+ @pytest .mark .parametrize (
166+ "acallable, expected_jetsam" ,
167+ [
168+ # NO old-stuff Operation(fn=_jetsamed_fn, name="test", needs="['a']", provides=[]),
169+ (
170+ fnt .partial (
171+ operation (name = "test" , needs = ["a" ], provides = ["b" ])(_scream )._compute ,
172+ named_inputs = None ,
173+ ),
174+ "outputs provides results operation args" .split (),
175+ ),
176+ (
177+ fnt .partial (
178+ network .ExecutionPlan (* ([None ] * 7 ))._call_operation ,
179+ op = None ,
180+ solution = {},
181+ ),
182+ ["plan" ],
183+ ),
184+ (
185+ fnt .partial (
186+ network .Network ().compute , named_inputs = None , outputs = None
187+ ),
188+ "network plan solution outputs" .split (),
189+ ),
190+ ],
191+ )
192+ def test_jetsam_sites_scream (acallable , expected_jetsam ):
193+ # Check jetsams when the site fails.
194+ with pytest .raises (Exception ) as excinfo :
195+ acallable ()
196+
197+ ex = excinfo .value
198+ assert ex .graphkit_jetsam .keys () == set (expected_jetsam )
199+
0 commit comments