-
Notifications
You must be signed in to change notification settings - Fork 445
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
Ported Bandwidth/GigaChannel tests to modern TestBase #7546
Conversation
1c50c98
to
8a0edea
Compare
src/tribler/core/components/bandwidth_accounting/tests/test_community.py
Outdated
Show resolved
Hide resolved
src/tribler/core/components/bandwidth_accounting/tests/test_community.py
Outdated
Show resolved
Hide resolved
src/tribler/core/components/bandwidth_accounting/tests/test_community.py
Outdated
Show resolved
Hide resolved
self.add_node_to_experiment(self.create_node()) | ||
with self.assertReceivedBy(3, [SelectResponsePayload], message_filter=[SelectResponsePayload]): | ||
self.overlay(ID2).send_introduction_request(self.peer(3)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could've bound ID4 = 3
here to make the tests more consistent. However, considering this ID is only used in two places, I opted not to.
src/tribler/core/components/bandwidth_accounting/tests/test_community.py
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks nice!
When testing, I caught a single failure of test_gigachannel_search
but could not reproduce it again. The failure was caused by the fact that the search returned just [U_CHANNEL]
instead of [U_CHANNEL, U_TORRENT]
and that led to an assertion failure. I think this random test failure is not related to the current changes in this PR.
@dataclass | ||
class ChannelKey(Mapping): | ||
channel_pk: bytes | ||
origin_id: int | ||
|
||
# The following methods allow for use as a Mapping (i.e., the "**key"-syntax) | ||
def __iter__(self): | ||
return iter(asdict(self)) | ||
|
||
def __getitem__(self, item): | ||
return getattr(self, item) | ||
|
||
def __len__(self): | ||
return len(fields(self)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like it is possible to use a typed NamedTuple
here instead of dataclass.
@dataclass | |
class ChannelKey(Mapping): | |
channel_pk: bytes | |
origin_id: int | |
# The following methods allow for use as a Mapping (i.e., the "**key"-syntax) | |
def __iter__(self): | |
return iter(asdict(self)) | |
def __getitem__(self, item): | |
return getattr(self, item) | |
def __len__(self): | |
return len(fields(self)) | |
class ChannelKey(NamedTuple): | |
channel_pk: bytes | |
origin_id: int |
Then the iteration of items will be available out of the box. With this change, mapping.add(peer, *key.values())
becomes mapping.add(peer, *key)
, and mds.get_entries(**key)
becomes mds.get_entries(**key._asdict())
. Despite underscore, _asdict
is an official public method for namedtuple
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
src/tribler/core/components/bandwidth_accounting/tests/test_community.py
Outdated
Show resolved
Hide resolved
0ed29d2
to
5b0eaac
Compare
@kozlovsky I squashed my commits and shortened the ID assignment. Could you rereview the two changed lines? |
I've been told nobody uses the advanced
TestBase
functionality because there is no example code to copy-paste from in the Tribler source. So, I modernized the tests for theBandwidthCommunity
and theGigaChannelCommunity
.In passing, I've also changed up some test code that has test nodes be able to observe each other's private keys (by being sent each other's
my_peer
). Secondarily, it seems to me thattest_remote_select_channel_contents_happy_eyeballs
had a 50/50 chance of succeeding when not fixing random numbers, which I also changed into a 100% success guarantee.The
TestGigaChannelUnits
could probably be cleaned up even further by extracting string literals, etc. However, that is not the goal of this PR. The goal of this PR is only to showcase advancedTestBase
functionality.