Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Fix bug in StateFilter.return_expanded() and add some tests. (#12016)
Browse files Browse the repository at this point in the history
  • Loading branch information
reivilibre authored Feb 18, 2022
1 parent 31a298f commit eb609c6
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog.d/12016.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix bug in `StateFilter.return_expanded()` and add some tests.
8 changes: 7 additions & 1 deletion synapse/storage/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,16 @@ def return_expanded(self) -> "StateFilter":
if get_all_members:
# We want to return everything.
return StateFilter.all()
else:
elif EventTypes.Member in self.types:
# We want to return all non-members, but only particular
# memberships
return StateFilter(
types=frozendict({EventTypes.Member: self.types[EventTypes.Member]}),
include_others=True,
)
else:
# We want to return all non-members
return _ALL_NON_MEMBER_STATE_FILTER

def make_sql_filter_clause(self) -> Tuple[str, List[str]]:
"""Converts the filter to an SQL clause.
Expand Down Expand Up @@ -528,6 +531,9 @@ def approx_difference(self, other: "StateFilter") -> "StateFilter":


_ALL_STATE_FILTER = StateFilter(types=frozendict(), include_others=True)
_ALL_NON_MEMBER_STATE_FILTER = StateFilter(
types=frozendict({EventTypes.Member: frozenset()}), include_others=True
)
_NONE_STATE_FILTER = StateFilter(types=frozendict(), include_others=False)


Expand Down
109 changes: 109 additions & 0 deletions tests/storage/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -992,3 +992,112 @@ def test_state_filter_difference_simple_cases(self):
StateFilter.none(),
StateFilter.all(),
)


class StateFilterTestCase(TestCase):
def test_return_expanded(self):
"""
Tests the behaviour of the return_expanded() function that expands
StateFilters to include more state types (for the sake of cache hit rate).
"""

self.assertEqual(StateFilter.all().return_expanded(), StateFilter.all())

self.assertEqual(StateFilter.none().return_expanded(), StateFilter.none())

# Concrete-only state filters stay the same
# (Case: mixed filter)
self.assertEqual(
StateFilter.freeze(
{
EventTypes.Member: {"@wombat:test", "@alicia:test"},
"some.other.state.type": {""},
},
include_others=False,
).return_expanded(),
StateFilter.freeze(
{
EventTypes.Member: {"@wombat:test", "@alicia:test"},
"some.other.state.type": {""},
},
include_others=False,
),
)

# Concrete-only state filters stay the same
# (Case: non-member-only filter)
self.assertEqual(
StateFilter.freeze(
{"some.other.state.type": {""}}, include_others=False
).return_expanded(),
StateFilter.freeze({"some.other.state.type": {""}}, include_others=False),
)

# Concrete-only state filters stay the same
# (Case: member-only filter)
self.assertEqual(
StateFilter.freeze(
{
EventTypes.Member: {"@wombat:test", "@alicia:test"},
},
include_others=False,
).return_expanded(),
StateFilter.freeze(
{
EventTypes.Member: {"@wombat:test", "@alicia:test"},
},
include_others=False,
),
)

# Wildcard member-only state filters stay the same
self.assertEqual(
StateFilter.freeze(
{EventTypes.Member: None},
include_others=False,
).return_expanded(),
StateFilter.freeze(
{EventTypes.Member: None},
include_others=False,
),
)

# If there is a wildcard in the non-member portion of the filter,
# it's expanded to include ALL non-member events.
# (Case: mixed filter)
self.assertEqual(
StateFilter.freeze(
{
EventTypes.Member: {"@wombat:test", "@alicia:test"},
"some.other.state.type": None,
},
include_others=False,
).return_expanded(),
StateFilter.freeze(
{EventTypes.Member: {"@wombat:test", "@alicia:test"}},
include_others=True,
),
)

# If there is a wildcard in the non-member portion of the filter,
# it's expanded to include ALL non-member events.
# (Case: non-member-only filter)
self.assertEqual(
StateFilter.freeze(
{
"some.other.state.type": None,
},
include_others=False,
).return_expanded(),
StateFilter.freeze({EventTypes.Member: set()}, include_others=True),
)
self.assertEqual(
StateFilter.freeze(
{
"some.other.state.type": None,
"yet.another.state.type": {"wombat"},
},
include_others=False,
).return_expanded(),
StateFilter.freeze({EventTypes.Member: set()}, include_others=True),
)

0 comments on commit eb609c6

Please # to comment.