|
| 1 | +part of 'channels_provider_test.dart'; |
| 2 | + |
| 3 | +void _testWithClock(String name, void Function(FakeAsync) body) { |
| 4 | + test(name, () => fakeAsync(body)); |
| 5 | +} |
| 6 | + |
| 7 | +void _terminalSubscriptionTests() { |
| 8 | + for (final beforeReady in [true, false]) { |
| 9 | + _testWithClock( |
| 10 | + 'persistent terminal rejection ${beforeReady ? 'before' : 'after'} ' |
| 11 | + 'readiness cannot amplify refreshes or retries', |
| 12 | + (clock) { |
| 13 | + final session = _TerminalRelaySession(beforeReady: beforeReady); |
| 14 | + final logs = <String>[]; |
| 15 | + final originalDebugPrint = debugPrint; |
| 16 | + debugPrint = (message, {wrapWidth}) { |
| 17 | + if (message != null) logs.add(message); |
| 18 | + }; |
| 19 | + final container = _buildContainer(session: session); |
| 20 | + container.read(channelsProvider); |
| 21 | + try { |
| 22 | + clock.flushMicrotasks(); |
| 23 | + // The finite snapshot is usable even before live readiness settles. |
| 24 | + expect( |
| 25 | + container.read(channelsProvider).requireValue.single.id, |
| 26 | + _channelA, |
| 27 | + ); |
| 28 | + expect(session.totalSubscribeCount, 1); |
| 29 | + final initialMemberships = session.membershipRequestCount; |
| 30 | + clock.elapse(const Duration(milliseconds: 2)); |
| 31 | + final historyAfterClose = session.historyFilters.length; |
| 32 | + final batchesAfterClose = session.queryBatches.length; |
| 33 | + final logsAfterClose = logs.length; |
| 34 | + |
| 35 | + // Virtual time permits many immediate retries on the broken path. |
| 36 | + for (var i = 0; i < 20; i++) { |
| 37 | + clock.elapse(const Duration(milliseconds: 10)); |
| 38 | + } |
| 39 | + expect(session.rejectionCount, 1); |
| 40 | + expect(session.membershipRequestCount, initialMemberships); |
| 41 | + expect(session.historyFilters.length, historyAfterClose); |
| 42 | + expect(session.queryBatches.length, batchesAfterClose); |
| 43 | + expect(session.totalSubscribeCount, 1); |
| 44 | + expect(logs.length, logsAfterClose); |
| 45 | + expect(clock.periodicTimerCount, 1); |
| 46 | + expect(clock.nonPeriodicTimerCount, 0); |
| 47 | + |
| 48 | + // Ordinary polling may refresh memberships, but never re-admits an |
| 49 | + // unchanged terminal filter, creates retry timers, or repeats logs. |
| 50 | + final pollStart = session.membershipRequestCount; |
| 51 | + for (var i = 0; i < 3; i++) { |
| 52 | + clock.elapse(const Duration(seconds: 60)); |
| 53 | + expect(session.totalSubscribeCount, 1); |
| 54 | + expect(session.activeSubscriptionCount, 0); |
| 55 | + expect(logs.length, logsAfterClose); |
| 56 | + expect(clock.periodicTimerCount, 1); |
| 57 | + expect(clock.nonPeriodicTimerCount, 0); |
| 58 | + expect( |
| 59 | + container.read(channelsProvider).requireValue.single.id, |
| 60 | + _channelA, |
| 61 | + ); |
| 62 | + } |
| 63 | + expect(session.membershipRequestCount - pollStart, 6); |
| 64 | + |
| 65 | + // A pull-to-refresh still isn't evidence that admission changed. |
| 66 | + unawaited(container.read(channelsProvider.notifier).refresh()); |
| 67 | + clock.flushMicrotasks(); |
| 68 | + clock.elapse(const Duration(milliseconds: 2)); |
| 69 | + expect(session.totalSubscribeCount, 1); |
| 70 | + expect(logs.length, logsAfterClose); |
| 71 | + } finally { |
| 72 | + container.dispose(); |
| 73 | + debugPrint = originalDebugPrint; |
| 74 | + } |
| 75 | + clock.elapse(const Duration(seconds: 60)); |
| 76 | + expect(clock.pendingTimers, isEmpty); |
| 77 | + expect(session.totalSubscribeCount, 1); |
| 78 | + }, |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + for (final beforeReady in [true, false]) { |
| 83 | + _testWithClock( |
| 84 | + 'disposal fences pending terminal closure ${beforeReady ? 'before' : 'after'} readiness', |
| 85 | + (clock) { |
| 86 | + final session = _TerminalRelaySession(beforeReady: beforeReady); |
| 87 | + final container = _buildContainer(session: session); |
| 88 | + container.read(channelsProvider); |
| 89 | + clock.flushMicrotasks(); |
| 90 | + final requests = session.membershipRequestCount; |
| 91 | + container.dispose(); |
| 92 | + clock.elapse(const Duration(minutes: 3)); |
| 93 | + expect(session.totalSubscribeCount, 1); |
| 94 | + expect(session.membershipRequestCount, requests); |
| 95 | + expect(session.activeSubscriptionCount, 0); |
| 96 | + expect(clock.pendingTimers, isEmpty); |
| 97 | + }, |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + _testWithClock('terminal rejection retries only after a new connection', ( |
| 102 | + clock, |
| 103 | + ) { |
| 104 | + final session = _TerminalRelaySession(); |
| 105 | + final container = _buildContainer(session: session); |
| 106 | + try { |
| 107 | + container.read(channelsProvider); |
| 108 | + clock.flushMicrotasks(); |
| 109 | + clock.elapse(const Duration(milliseconds: 2)); |
| 110 | + final oldClosed = session.closedCallbacks.single; |
| 111 | + for (var attempt = 2; attempt <= 4; attempt++) { |
| 112 | + session.setStatus(SessionStatus.disconnected); |
| 113 | + clock.flushMicrotasks(); |
| 114 | + session.setStatus(SessionStatus.connected); |
| 115 | + clock.flushMicrotasks(); |
| 116 | + clock.elapse(const Duration(milliseconds: 2)); |
| 117 | + clock.elapse(const Duration(milliseconds: 20)); |
| 118 | + expect(session.totalSubscribeCount, attempt); |
| 119 | + expect(session.rejectionCount, attempt); |
| 120 | + expect(session.activeSubscriptionCount, 0); |
| 121 | + expect( |
| 122 | + container.read(channelsProvider).requireValue.single.id, |
| 123 | + _channelA, |
| 124 | + ); |
| 125 | + } |
| 126 | + session.reject = false; |
| 127 | + session.setStatus(SessionStatus.disconnected); |
| 128 | + clock.flushMicrotasks(); |
| 129 | + session.setStatus(SessionStatus.connected); |
| 130 | + clock.flushMicrotasks(); |
| 131 | + expect(session.totalSubscribeCount, 5); |
| 132 | + expect(session.activeChannels, {_channelA}); |
| 133 | + oldClosed(); |
| 134 | + clock.flushMicrotasks(); |
| 135 | + expect(session.activeChannels, {_channelA}); |
| 136 | + expect(session.totalSubscribeCount, 5); |
| 137 | + } finally { |
| 138 | + container.dispose(); |
| 139 | + } |
| 140 | + clock.flushMicrotasks(); |
| 141 | + }); |
| 142 | + |
| 143 | + _testWithClock( |
| 144 | + 'changed membership admits a new filter, not a rejected old one', |
| 145 | + (clock) { |
| 146 | + final session = _TerminalRelaySession(); |
| 147 | + final container = _buildContainer(session: session); |
| 148 | + try { |
| 149 | + container.read(channelsProvider); |
| 150 | + clock.flushMicrotasks(); |
| 151 | + clock.elapse(const Duration(milliseconds: 2)); |
| 152 | + session.reject = false; |
| 153 | + session.memberships.add(_membership(_channelB, 'me')); |
| 154 | + session.metadata.add(_meta(id: _channelB, name: 'b')); |
| 155 | + unawaited(container.read(channelsProvider.notifier).refresh()); |
| 156 | + clock.flushMicrotasks(); |
| 157 | + expect(session.totalSubscribeCount, 2); |
| 158 | + expect(session.activeChannels, {_channelA, _channelB}); |
| 159 | + // Removing then rejoining is a meaningful transition even if it returns |
| 160 | + // to an earlier filter; obsolete quarantine keys must not leak forever. |
| 161 | + session.memberships.removeLast(); |
| 162 | + unawaited(container.read(channelsProvider.notifier).refresh()); |
| 163 | + clock.flushMicrotasks(); |
| 164 | + expect(session.totalSubscribeCount, 3); |
| 165 | + expect(session.activeChannels, {_channelA}); |
| 166 | + } finally { |
| 167 | + container.dispose(); |
| 168 | + } |
| 169 | + clock.flushMicrotasks(); |
| 170 | + }, |
| 171 | + ); |
| 172 | + |
| 173 | + for (final switchIdentity in [true, false]) { |
| 174 | + _testWithClock( |
| 175 | + 'terminal quarantine does not cross ${switchIdentity ? 'identity' : 'community'} scope', |
| 176 | + (clock) { |
| 177 | + final session = _TerminalRelaySession(); |
| 178 | + final container = _buildContainer(session: session); |
| 179 | + try { |
| 180 | + container.read(channelsProvider); |
| 181 | + clock.flushMicrotasks(); |
| 182 | + clock.elapse(const Duration(milliseconds: 2)); |
| 183 | + session.reject = false; |
| 184 | + if (switchIdentity) { |
| 185 | + session.memberships = [_membership(_channelA, _otherPk)]; |
| 186 | + container.read(_testPubkeyProvider.notifier).set(_otherPk); |
| 187 | + } else { |
| 188 | + container |
| 189 | + .read(relayConfigProvider.notifier) |
| 190 | + .update(baseUrl: 'https://new-community.example'); |
| 191 | + } |
| 192 | + container.read(channelsProvider); |
| 193 | + clock.flushMicrotasks(); |
| 194 | + expect(session.totalSubscribeCount, 2); |
| 195 | + expect(session.activeChannels, {_channelA}); |
| 196 | + } finally { |
| 197 | + container.dispose(); |
| 198 | + } |
| 199 | + clock.flushMicrotasks(); |
| 200 | + }, |
| 201 | + ); |
| 202 | + } |
| 203 | +} |
| 204 | + |
| 205 | +/// Reject every attempted subscription, including every replacement, as a |
| 206 | +/// terminal relay CLOSED. A timer models the wire turn and prevents a broken |
| 207 | +/// immediate retry loop from hanging the test's microtask drain. |
| 208 | +class _TerminalRelaySession extends _FakeRelaySession { |
| 209 | + _TerminalRelaySession({this.beforeReady = false}) |
| 210 | + : super( |
| 211 | + memberships: [_membership(_channelA, 'me')], |
| 212 | + metadata: [_meta(id: _channelA, name: 'a')], |
| 213 | + ); |
| 214 | + |
| 215 | + final bool beforeReady; |
| 216 | + bool reject = true; |
| 217 | + int rejectionCount = 0; |
| 218 | + final List<void Function()> closedCallbacks = []; |
| 219 | + |
| 220 | + @override |
| 221 | + Future<void Function()> subscribe( |
| 222 | + NostrFilter filter, |
| 223 | + void Function(NostrEvent) onEvent, { |
| 224 | + void Function(String message)? onClosed, |
| 225 | + }) async { |
| 226 | + final unsubscribe = await super.subscribe( |
| 227 | + filter, |
| 228 | + onEvent, |
| 229 | + onClosed: onClosed, |
| 230 | + ); |
| 231 | + if (!reject) return unsubscribe; |
| 232 | + final key = _subscriptions.keys.last; |
| 233 | + final closed = Completer<void>(); |
| 234 | + final timer = Timer(const Duration(milliseconds: 1), () { |
| 235 | + final subscription = _subscriptions.remove(key); |
| 236 | + if (subscription != null) { |
| 237 | + subscribeFilters.remove(subscription.$1); |
| 238 | + rejectionCount++; |
| 239 | + void notify() => onClosed?.call('restricted: persistent rejection'); |
| 240 | + closedCallbacks.add(notify); |
| 241 | + notify(); |
| 242 | + } |
| 243 | + closed.complete(); |
| 244 | + }); |
| 245 | + if (beforeReady) { |
| 246 | + await closed.future; |
| 247 | + throw StateError('restricted: persistent rejection'); |
| 248 | + } |
| 249 | + return () { |
| 250 | + timer.cancel(); |
| 251 | + unsubscribe(); |
| 252 | + }; |
| 253 | + } |
| 254 | +} |
0 commit comments