Skip to content
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

Merged
merged 1 commit into from
Jul 17, 2023

Conversation

qstokkink
Copy link
Contributor

@qstokkink qstokkink commented Jul 14, 2023

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 the BandwidthCommunity and the GigaChannelCommunity.

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 that test_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 advanced TestBase functionality.

@qstokkink qstokkink marked this pull request as ready for review July 14, 2023 19:38
@qstokkink qstokkink requested a review from a team as a code owner July 14, 2023 19:38
@qstokkink qstokkink requested review from kozlovsky and removed request for a team July 14, 2023 19:38
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))
Copy link
Contributor Author

@qstokkink qstokkink Jul 15, 2023

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.

Copy link
Collaborator

@kozlovsky kozlovsky left a 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.

Comment on lines +41 to +52
@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))
Copy link
Collaborator

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.

Suggested change
@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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestion: that was also my initial approach. The reason that I did not use NamedTuple, in the end, is that the context menu in de PyCharm IDE does not list _asdict:

contextmenu

If you absolutely insist on using NamedTuple, I'm also fine with using it though.

@qstokkink qstokkink marked this pull request as draft July 17, 2023 12:35
@qstokkink qstokkink marked this pull request as ready for review July 17, 2023 13:11
@qstokkink qstokkink requested a review from kozlovsky July 17, 2023 13:20
@qstokkink
Copy link
Contributor Author

@kozlovsky I squashed my commits and shortened the ID assignment. Could you rereview the two changed lines?

@qstokkink qstokkink merged commit 69c299e into Tribler:main Jul 17, 2023
28 checks passed
@qstokkink qstokkink deleted the upd_testbase_cases branch July 17, 2023 13:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants