-
Notifications
You must be signed in to change notification settings - Fork 648
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding attach/detach methods as per spec (#429)
This change updates the Context API with the following: - removes the remove_value method - removes the set_current method - adds attach and detach methods Fixes #420 Co-authored-by: Chris Kleinknecht <[email protected]>
- Loading branch information
Showing
12 changed files
with
226 additions
and
174 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# Copyright 2020, OpenTelemetry Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import unittest | ||
from logging import ERROR | ||
|
||
from opentelemetry import context | ||
|
||
|
||
def do_work() -> None: | ||
context.attach(context.set_value("say", "bar")) | ||
|
||
|
||
class ContextTestCases: | ||
class BaseTest(unittest.TestCase): | ||
def setUp(self) -> None: | ||
self.previous_context = context.get_current() | ||
|
||
def tearDown(self) -> None: | ||
context.attach(self.previous_context) | ||
|
||
def test_context(self): | ||
self.assertIsNone(context.get_value("say")) | ||
empty = context.get_current() | ||
second = context.set_value("say", "foo") | ||
|
||
self.assertEqual(context.get_value("say", context=second), "foo") | ||
|
||
do_work() | ||
self.assertEqual(context.get_value("say"), "bar") | ||
third = context.get_current() | ||
|
||
self.assertIsNone(context.get_value("say", context=empty)) | ||
self.assertEqual(context.get_value("say", context=second), "foo") | ||
self.assertEqual(context.get_value("say", context=third), "bar") | ||
|
||
def test_set_value(self): | ||
first = context.set_value("a", "yyy") | ||
second = context.set_value("a", "zzz") | ||
third = context.set_value("a", "---", first) | ||
self.assertEqual("yyy", context.get_value("a", context=first)) | ||
self.assertEqual("zzz", context.get_value("a", context=second)) | ||
self.assertEqual("---", context.get_value("a", context=third)) | ||
self.assertEqual(None, context.get_value("a")) | ||
|
||
def test_attach(self): | ||
context.attach(context.set_value("a", "yyy")) | ||
|
||
token = context.attach(context.set_value("a", "zzz")) | ||
self.assertEqual("zzz", context.get_value("a")) | ||
|
||
context.detach(token) | ||
self.assertEqual("yyy", context.get_value("a")) | ||
|
||
with self.assertLogs(level=ERROR): | ||
context.detach("some garbage") | ||
|
||
def test_detach_out_of_order(self): | ||
t1 = context.attach(context.set_value("c", 1)) | ||
self.assertEqual(context.get_current(), {"c": 1}) | ||
t2 = context.attach(context.set_value("c", 2)) | ||
self.assertEqual(context.get_current(), {"c": 2}) | ||
context.detach(t1) | ||
self.assertEqual(context.get_current(), {}) | ||
context.detach(t2) | ||
self.assertEqual(context.get_current(), {"c": 1}) |
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
Oops, something went wrong.