@@ -137,6 +137,51 @@ public void GIVEN_subscription_wildcard_WHEN_remove_topic_THEN_no_matches() {
137
137
assertEquals (0 , trie .size ());
138
138
}
139
139
140
+ @ Test
141
+ public void GIVEN_subscriptions_with_wildcards_WHEN_remove_topics_THEN_clean_up_trie () {
142
+ assertEquals (0 , trie .size ());
143
+ SubscriptionCallback cb1 = generateSubscriptionCallback ();
144
+ SubscriptionCallback cb2 = generateSubscriptionCallback ();
145
+ SubscriptionCallback cb3 = generateSubscriptionCallback ();
146
+ String topic = "foo/+/bar/#" ;
147
+ trie .add ("bar" , cb1 );
148
+ trie .add (topic , cb1 );
149
+ trie .add (topic , cb2 );
150
+ // Topic is not registered with the callback, mark it as removed when requested to remove
151
+ assertThat ("remove topic" , trie .remove (topic , cb3 ), is (false ));
152
+ assertThat (trie .get (topic ), containsInAnyOrder (cb1 , cb2 ));
153
+
154
+ trie .add ("foo/#" , cb3 );
155
+ trie .add ("foo/+" , cb2 );
156
+
157
+ assertThat (trie .get (topic ), containsInAnyOrder (cb1 , cb2 , cb3 ));
158
+ assertEquals (5 , trie .size ());
159
+
160
+ assertThat ("remove topic" , trie .remove ("foo/+" , cb2 ), is (true ));
161
+ assertEquals (4 , trie .size ());
162
+ assertThat (trie .get ("foo/+" ), containsInAnyOrder (cb3 )); // foo/+ still matches with foo/#
163
+ assertThat (trie .get (topic ), containsInAnyOrder (cb1 , cb2 , cb3 )); // foo/+/bar/# still exists
164
+
165
+ assertThat ("remove topic" , trie .remove ("foo/#" , cb3 ), is (true ));
166
+ assertFalse (trie .containsKey ("foo/#" ));
167
+ assertThat (trie .get ("foo/#" ), is (empty ()));
168
+ assertThat (trie .get ("foo/+" ), is (empty ())); // foo/+ doesn't match with any existing topic
169
+ assertThat (trie .get (topic ), containsInAnyOrder (cb1 , cb2 )); // foo/+/bar/# still exists
170
+ assertEquals (3 , trie .size ());
171
+
172
+ assertThat ("remove topic" , trie .remove (topic , cb1 ), is (true ));
173
+ assertThat (trie .get (topic ), contains (cb2 ));
174
+ assertEquals (2 , trie .size ());
175
+ assertTrue (trie .containsKey ("foo/+" ));
176
+ assertTrue (trie .containsKey ("foo/+/bar/#" ));
177
+
178
+ assertThat ("remove topic" , trie .remove (topic , cb2 ), is (true ));
179
+ assertThat (trie .get (topic ), is (empty ()));
180
+ assertEquals (1 , trie .size ());
181
+ assertFalse (trie .containsKey ("foo/+" ));
182
+ assertFalse (trie .containsKey ("foo/+/bar/#" ));
183
+ }
184
+
140
185
@ Test
141
186
void GIVEN_topics_WHEN_isWildcard_THEN_returns_whether_it_uses_wildcard () {
142
187
assertTrue (SubscriptionTrie .isWildcard ("+" ));
0 commit comments