Skip to content

Commit f6fec6c

Browse files
authored
Fix unstable mpsc buffer test (#410)
1 parent c37109c commit f6fec6c

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

BitFaster.Caching.UnitTests/Buffers/MpscBoundedBufferTests.cs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,19 @@ public async Task WhileBufferIsFilledItemsCanBeTaken()
180180

181181
var fill = Threaded.Run(4, () =>
182182
{
183+
var spin = new SpinWait();
183184
int count = 0;
184185
while (count < 256)
185186
{
186-
if (buffer.TryAdd("hello") == BufferStatus.Success)
187-
{
188-
count++;
187+
while (true)
188+
{
189+
if (buffer.TryAdd("hello") == BufferStatus.Success)
190+
{
191+
break;
192+
}
193+
spin.SpinOnce();
189194
}
195+
count++;
190196
}
191197
});
192198

@@ -210,13 +216,19 @@ public async Task WhileBufferIsFilledBufferCanBeDrained()
210216

211217
var fill = Threaded.Run(4, () =>
212218
{
219+
var spin = new SpinWait();
213220
int count = 0;
214221
while (count < 256)
215-
{
216-
if (buffer.TryAdd("hello") == BufferStatus.Success)
217-
{
218-
count++;
222+
{
223+
while (true)
224+
{
225+
if (buffer.TryAdd("hello") == BufferStatus.Success)
226+
{
227+
break;
228+
}
229+
spin.SpinOnce();
219230
}
231+
count++;
220232
}
221233
});
222234

0 commit comments

Comments
 (0)