-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d69d2fc
commit 342d8a3
Showing
13 changed files
with
129 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import contextvars | ||
import threading | ||
|
||
from griptape.utils.decorators import copy_contextvars | ||
|
||
|
||
class TestDecorators: | ||
def test_copy_contextvars_decorator(self): | ||
context_var = contextvars.ContextVar("context_var") | ||
context_var.set("test") | ||
|
||
def undecorated_function(vals: list) -> None: | ||
vals.append(context_var.get()) | ||
|
||
@copy_contextvars | ||
def decorated_function(vals: list) -> None: | ||
vals.append(context_var.get()) | ||
|
||
return_values = [] | ||
thread = threading.Thread(target=decorated_function, args=(return_values,)) | ||
thread.start() | ||
thread.join() | ||
|
||
assert return_values == ["test"] | ||
|
||
return_values = [] | ||
thread = threading.Thread(target=undecorated_function, args=(return_values,)) | ||
thread.start() | ||
thread.join() | ||
|
||
assert return_values == [] | ||
|
||
def test_copy_contextvars_direct(self): | ||
context_var = contextvars.ContextVar("context_var") | ||
context_var.set("test") | ||
|
||
def function(vals: list) -> None: | ||
vals.append(context_var.get()) | ||
|
||
decoratored_function = copy_contextvars(function) | ||
|
||
return_values = [] | ||
thread = threading.Thread(target=decoratored_function, args=(return_values,)) | ||
thread.start() | ||
thread.join() | ||
|
||
assert return_values == ["test"] | ||
|
||
return_values = [] | ||
thread = threading.Thread(target=function, args=(return_values,)) | ||
thread.start() | ||
thread.join() | ||
|
||
assert return_values == [] |