Skip to content

Commit

Permalink
Refactor lock definition
Browse files Browse the repository at this point in the history
  • Loading branch information
ocelotl committed Oct 31, 2022
1 parent fda116f commit 064bb2b
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,20 @@
# limitations under the License.

from abc import ABC, abstractmethod
from threading import Lock


class Mapping(ABC):

# pylint: disable=no-member
def __new__(cls, scale: int):

if not hasattr(cls, "_mappings"):
cls._mappings = {}

if not hasattr(cls, "_mappings_lock"):
cls._mappings_lock = Lock()

with cls._mappings_lock:
if scale not in cls._mappings:
cls._mappings[scale] = super().__new__(cls)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@

class ExponentMapping(Mapping):

_mappings = {}
_mappings_lock = Lock()

_min_scale = -10
_max_scale = 0

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Copyright The 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.

from unittest import TestCase
from math import inf

from opentelemetry.sdk.metrics._internal.exponential_histogram.mapping import (
Mapping,
)


class TestMapping(TestCase):

def test_lock(self):

class Child0(Mapping):
def _get_max_scale(self) -> int:
return inf

def _get_min_scale(self) -> int:
return -inf

def map_to_index(self, value: float) -> int:
pass

def get_lower_boundary(self, index: int) -> float:
pass

class Child1(Mapping):
def _get_max_scale(self) -> int:
return inf

def _get_min_scale(self) -> int:
return -inf

def map_to_index(self, value: float) -> int:
pass

def get_lower_boundary(self, index: int) -> float:
pass

child_0 = Child0(0)
child_1 = Child1(1)

self.assertIsNot(child_0._mappings, child_1._mappings)
self.assertIsNot(child_0._mappings_lock, child_1._mappings_lock)

0 comments on commit 064bb2b

Please sign in to comment.