File tree Expand file tree Collapse file tree 2 files changed +40
-0
lines changed
main/java/com/google/cloud/functions/invoker
test/java/com/google/cloud/functions/invoker Expand file tree Collapse file tree 2 files changed +40
-0
lines changed Original file line number Diff line number Diff line change @@ -48,12 +48,16 @@ public static HttpFunctionExecutor forClass(Class<?> functionClass) {
48
48
+ HttpFunction .class .getName ());
49
49
}
50
50
Class <? extends HttpFunction > httpFunctionClass = functionClass .asSubclass (HttpFunction .class );
51
+ ClassLoader oldContextLoader = Thread .currentThread ().getContextClassLoader ();
51
52
try {
53
+ Thread .currentThread ().setContextClassLoader (httpFunctionClass .getClassLoader ());
52
54
HttpFunction httpFunction = httpFunctionClass .getConstructor ().newInstance ();
53
55
return new HttpFunctionExecutor (httpFunction );
54
56
} catch (ReflectiveOperationException e ) {
55
57
throw new RuntimeException (
56
58
"Could not construct an instance of " + functionClass .getName () + ": " + e , e );
59
+ } finally {
60
+ Thread .currentThread ().setContextClassLoader (oldContextLoader );
57
61
}
58
62
}
59
63
Original file line number Diff line number Diff line change
1
+ package com .google .cloud .functions .invoker ;
2
+
3
+ import static com .google .common .truth .Truth .assertThat ;
4
+
5
+ import com .google .cloud .functions .HttpFunction ;
6
+ import com .google .cloud .functions .HttpRequest ;
7
+ import com .google .cloud .functions .HttpResponse ;
8
+ import org .junit .Test ;
9
+ import org .junit .runner .RunWith ;
10
+ import org .junit .runners .JUnit4 ;
11
+
12
+ @ RunWith (JUnit4 .class )
13
+ public class HttpFunctionExecutorTest {
14
+ private static ClassLoader customClassLoader =
15
+ new ClassLoader (ClassLoader .getSystemClassLoader ()) {};
16
+
17
+ public static class ClassLoaderVerifier implements HttpFunction {
18
+ public ClassLoaderVerifier () {
19
+ assertThat (Thread .currentThread ().getContextClassLoader ())
20
+ .isNotSameInstanceAs (customClassLoader );
21
+ }
22
+
23
+ @ Override
24
+ public void service (HttpRequest request , HttpResponse response ) throws Exception {
25
+ throw new UnsupportedOperationException ("Not implemented" );
26
+ }
27
+ }
28
+
29
+ @ Test
30
+ public void usesCorrectClassLoaderOverride () {
31
+ ClassLoader oldClassLoader = Thread .currentThread ().getContextClassLoader ();
32
+ Thread .currentThread ().setContextClassLoader (customClassLoader );
33
+ HttpFunctionExecutor .forClass (ClassLoaderVerifier .class );
34
+ Thread .currentThread ().setContextClassLoader (oldClassLoader );
35
+ }
36
+ }
You can’t perform that action at this time.
0 commit comments