Skip to content

Commit 69cebf7

Browse files
committed
Add test for disposing subscription
1 parent fb86fa4 commit 69cebf7

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/Components/Components/test/Lifetime/ComponentApplicationLifetimeTest.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,37 @@ public async Task PersistStateAsync_FiresCallbacksInParallel()
125125
Assert.Equal(new[] { 1, 2, 3, 4 }, sequence);
126126
}
127127

128+
[Fact]
129+
public async Task PersistStateAsync_CallbacksAreRemovedWhenSubscriptionsAreDisposed()
130+
{
131+
// Arrange
132+
var state = new Dictionary<string, ReadOnlySequence<byte>>();
133+
var store = new TestStore(state);
134+
var lifetime = new ComponentStatePersistenceManager(NullLogger<ComponentStatePersistenceManager>.Instance);
135+
var renderer = new TestRenderer();
136+
137+
var sequence = new List<int> { };
138+
139+
var tcs = new TaskCompletionSource();
140+
var tcs2 = new TaskCompletionSource();
141+
142+
var subscription1 = lifetime.State.RegisterOnPersisting(async () => { sequence.Add(1); await tcs.Task; sequence.Add(3); });
143+
var subscription2 = lifetime.State.RegisterOnPersisting(async () => { sequence.Add(2); await tcs2.Task; sequence.Add(4); });
144+
145+
// Act
146+
subscription1.Dispose();
147+
subscription2.Dispose();
148+
149+
var persistTask = lifetime.PersistStateAsync(store, renderer);
150+
tcs.SetResult();
151+
tcs2.SetResult();
152+
153+
await persistTask;
154+
155+
// Assert
156+
Assert.Empty(sequence);
157+
}
158+
128159
[Fact]
129160
public async Task PersistStateAsync_ContinuesInvokingPauseCallbacksDuringPersistIfACallbackThrows()
130161
{

0 commit comments

Comments
 (0)