|
| 1 | +# Copyright 2019, OpenTelemetry Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import unittest |
| 16 | + |
| 17 | +from opentelemetry import distributedcontext |
| 18 | + |
| 19 | + |
| 20 | +class TestEntryMetadata(unittest.TestCase): |
| 21 | + def test_entry_ttl_no_propagation(self): |
| 22 | + metadata = distributedcontext.EntryMetadata( |
| 23 | + distributedcontext.EntryMetadata.NO_PROPAGATION, |
| 24 | + ) |
| 25 | + self.assertEqual(metadata.entry_ttl, 0) |
| 26 | + |
| 27 | + def test_entry_ttl_unlimited_propagation(self): |
| 28 | + metadata = distributedcontext.EntryMetadata( |
| 29 | + distributedcontext.EntryMetadata.UNLIMITED_PROPAGATION, |
| 30 | + ) |
| 31 | + self.assertEqual(metadata.entry_ttl, -1) |
| 32 | + |
| 33 | + |
| 34 | +class TestEntryKey(unittest.TestCase): |
| 35 | + def test_create_too_long(self): |
| 36 | + with self.assertRaises(ValueError): |
| 37 | + distributedcontext.EntryKey.create("a" * 256) |
| 38 | + |
| 39 | + def test_create_invalid_character(self): |
| 40 | + with self.assertRaises(ValueError): |
| 41 | + distributedcontext.EntryKey.create("\x00") |
| 42 | + |
| 43 | + def test_create_valid(self): |
| 44 | + key = distributedcontext.EntryKey.create("ok") |
| 45 | + self.assertEqual(key, "ok") |
| 46 | + |
| 47 | + def test_key_new(self): |
| 48 | + key = distributedcontext.EntryKey("ok") |
| 49 | + self.assertEqual(key, "ok") |
| 50 | + |
| 51 | + |
| 52 | +class TestEntryValue(unittest.TestCase): |
| 53 | + def test_create_invalid_character(self): |
| 54 | + with self.assertRaises(ValueError): |
| 55 | + distributedcontext.EntryValue.create("\x00") |
| 56 | + |
| 57 | + def test_create_valid(self): |
| 58 | + key = distributedcontext.EntryValue.create("ok") |
| 59 | + self.assertEqual(key, "ok") |
| 60 | + |
| 61 | + def test_key_new(self): |
| 62 | + key = distributedcontext.EntryValue("ok") |
| 63 | + self.assertEqual(key, "ok") |
| 64 | + |
| 65 | + |
| 66 | +class TestDistributedContextManager(unittest.TestCase): |
| 67 | + def setUp(self): |
| 68 | + self.manager = distributedcontext.DistributedContextManager() |
| 69 | + |
| 70 | + def test_get_current_context(self): |
| 71 | + self.assertIsNone(self.manager.get_current_context()) |
| 72 | + |
| 73 | + def test_use_context(self): |
| 74 | + o = object() |
| 75 | + with self.manager.use_context(o) as output: |
| 76 | + self.assertIs(output, o) |
0 commit comments