Created
September 20, 2019 17:29
-
-
Save bonprosoft/25de2daeea4b507c9bda3301c848ab87 to your computer and use it in GitHub Desktop.
mock.patchの検証
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
import threading | |
import time | |
from unittest import mock | |
class Hoge: | |
b = 10 | |
a = Hoge() | |
def foo(): | |
print(f'[Foo]before-patch: {a.b}') | |
with mock.patch('__main__.a.b', 100): | |
print(f'[Foo]in-patch1: {a.b}') | |
time.sleep(1) | |
print(f'[Foo]in-patch2: {a.b}') | |
print(f'[Foo]after-patch: {a.b}') | |
def bar(): | |
for _ in range(10): | |
print(f'[Bar] {a.b}') | |
time.sleep(0.3) | |
t = threading.Thread(target=foo) | |
t.start() | |
bar() | |
t.join() |
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
[Foo]before-patch: 10 | |
[Bar] 10 | |
[Foo]in-patch1: 100 | |
[Bar] 100 | |
[Bar] 100 | |
[Bar] 100 | |
[Foo]in-patch2: 100 | |
[Foo]after-patch: 10 | |
[Bar] 10 | |
[Bar] 10 | |
[Bar] 10 | |
[Bar] 10 | |
[Bar] 10 | |
[Bar] 10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment