Skip to content

fix bug in BoundedList for python 3.4 and add tests #199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions opentelemetry-sdk/src/opentelemetry/sdk/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def __len__(self):

def __iter__(self):
with self._lock:
return iter(self._dq.copy())
return iter(deque(self._dq))

def append(self, item):
with self._lock:
Expand All @@ -89,7 +89,7 @@ def from_seq(cls, maxlen, seq):


class BoundedDict(MutableMapping):
"""A dict with a fixed max capacity."""
"""An ordered dict with a fixed max capacity."""

def __init__(self, maxlen):
if not isinstance(maxlen, int):
Expand Down
162 changes: 162 additions & 0 deletions opentelemetry-sdk/tests/test_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
# Copyright 2019, 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 collections
import unittest

from opentelemetry.sdk.util import BoundedDict, BoundedList


class TestBoundedList(unittest.TestCase):
base = [52, 36, 53, 29, 54, 99, 56, 48, 22, 35, 21, 65, 10, 95, 42, 60]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just use list(range(n))?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not that happy having base[i] == i.


def test_negative_maxlen(self):
with self.assertRaises(ValueError):
BoundedList(-1)

def test_from_seq(self):
list_len = len(TestBoundedList.base)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not "self.base" here? I worry about calling out the class directly as it just increase the work when one wants to change the class name.

blist = BoundedList.from_seq(list_len, TestBoundedList.base)

self.assertEqual(len(blist), list_len)

for idx in range(list_len):
self.assertEqual(blist[idx], TestBoundedList.base[idx])

# sequence too big
with self.assertRaises(ValueError):
BoundedList.from_seq(list_len / 2, TestBoundedList.base)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also add an assertion that modifying base does not modify the BoundedList or vice versa


def test_append_no_drop(self):
# create empty list
list_len = len(TestBoundedList.base)
blist = BoundedList(list_len)
self.assertEqual(len(blist), 0)

# fill list
for idx in TestBoundedList.base:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for idx in TestBoundedList.base:
for item in TestBoundedList.base:

blist.append(idx)

self.assertEqual(len(blist), list_len)
self.assertEqual(blist.dropped, 0)

for idx in range(list_len):
self.assertEqual(blist[idx], TestBoundedList.base[idx])

# test __iter__ in BoundedList
idx = 0
for val in blist:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could use enumerate() here instead of manually incrementing.

self.assertEqual(val, TestBoundedList.base[idx])
idx += 1

def test_append_drop(self):
list_len = len(TestBoundedList.base)
# create full BoundedList
blist = BoundedList.from_seq(list_len, TestBoundedList.base)

# try to append more items
for idx in range(8):
# should drop the element without raising exceptions
blist.append(idx)

self.assertEqual(len(blist), list_len)
self.assertEqual(blist.dropped, 8)

def test_extend_no_drop(self):
# create empty list
list_len = len(TestBoundedList.base)
blist = BoundedList(list_len)
self.assertEqual(len(blist), 0)

# fill list
blist.extend(TestBoundedList.base)

self.assertEqual(len(blist), list_len)
self.assertEqual(blist.dropped, 0)

for idx in range(list_len):
self.assertEqual(blist[idx], TestBoundedList.base[idx])

# test __iter__ in BoundedList
idx = 0
for val in blist:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could use enumerate

self.assertEqual(val, TestBoundedList.base[idx])
idx += 1

def test_extend_drop(self):
list_len = len(TestBoundedList.base)
# create full BoundedList
blist = BoundedList.from_seq(list_len, TestBoundedList.base)
other_list = [13, 37, 51, 91]

# try to extend with more elements
blist.extend(other_list)

self.assertEqual(len(blist), list_len)
self.assertEqual(blist.dropped, len(other_list))


class TestBoundedDict(unittest.TestCase):
base = collections.OrderedDict(
[
("name", "Firulais"),
("age", 7),
("weight", 13),
("vaccinated", True),
]
)

def test_negative_maxlen(self):
with self.assertRaises(ValueError):
BoundedDict(-1)

def test_from_map(self):
dic_len = len(TestBoundedDict.base)
bdict = BoundedDict.from_map(dic_len, TestBoundedDict.base)

self.assertEqual(len(bdict), dic_len)

for key in TestBoundedDict.base:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also test that all keys are yielded by the iterator like asserting that len(tuple(bdict)) == len(bdict)). Same for BoundedList.

self.assertEqual(bdict[key], TestBoundedDict.base[key])

# map too big
with self.assertRaises(ValueError):
BoundedDict.from_map(dic_len / 2, TestBoundedDict.base)

def test_bounded_dict(self):
# create empty dict
dic_len = len(TestBoundedDict.base)
bdict = BoundedDict(dic_len)
self.assertEqual(len(bdict), 0)

# fill list
for key in TestBoundedDict.base:
bdict[key] = TestBoundedDict.base[key]

self.assertEqual(len(bdict), dic_len)
self.assertEqual(bdict.dropped, 0)

for key in TestBoundedDict.base:
self.assertEqual(bdict[key], TestBoundedDict.base[key])

# test __iter__ in BoundedList
for key in bdict:
self.assertEqual(bdict[key], TestBoundedDict.base[key])

# try to append more elements
for key in TestBoundedDict.base:
bdict["new-key" + key] = TestBoundedDict.base[key]

self.assertEqual(len(bdict), dic_len)
self.assertEqual(bdict.dropped, dic_len)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also assert that the new key is in the bounded dict (i.e., old elements are dropped instead of new ones).
Same for BoundedList.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good one, actually I was thinking that the new elements were dropped.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could this behavior be documented in the docstring as well?