this repo has no description
1#!/usr/bin/env python3
2# Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com)
3
4# These tests cannot be on contextvars as they need access to Pyro-specific
5# internal functions.
6
7import _contextvars
8import unittest
9
10from test_support import pyro_only
11
12
13class CopyContextTests(unittest.TestCase):
14 @pyro_only
15 def test_produces_context_with_same_data_as_current_context(self):
16 empty_ctx = _contextvars.Context()
17
18 def f_with_empty_ctx():
19 var1 = _contextvars.ContextVar("foo")
20 var1.set(1)
21 var2 = _contextvars.ContextVar("bar")
22 var2.set(2)
23 ctx_copy = _contextvars.copy_context()
24 self.assertEqual(
25 list(ctx_copy.items()), list(_contextvars._thread_context().items())
26 )
27
28 empty_ctx.run(f_with_empty_ctx)
29
30 @pyro_only
31 def test_produces_new_context(self):
32 empty_ctx = _contextvars.Context()
33
34 def f_with_empty_ctx():
35 ctx_copy = _contextvars.copy_context()
36 self.assertIsNot(_contextvars._thread_context(), ctx_copy)
37
38 empty_ctx.run(f_with_empty_ctx)
39
40
41if __name__ == "__main__":
42 unittest.main()