Skip to content

Commit f5c299d

Browse files
committed
CSHARP-1488: Properly implement Flush and FlushAsync for GridFS streams.
1 parent c44f347 commit f5c299d

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

src/MongoDB.Driver.GridFS.Tests/GridFSDownloadStreamBaseTests.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using System.IO;
1919
using System.Linq;
2020
using System.Text;
21+
using System.Threading;
2122
using System.Threading.Tasks;
2223
using FluentAssertions;
2324
using MongoDB.Bson;
@@ -71,6 +72,28 @@ public void CopyTo_should_copy_stream(
7172
}
7273
}
7374

75+
[Test]
76+
public void Flush_should_throw(
77+
[Values(false, true)] bool async)
78+
{
79+
var bucket = CreateBucket(128);
80+
var content = CreateContent();
81+
var id = CreateGridFSFile(bucket, content);
82+
var subject = bucket.OpenDownloadStream(id);
83+
84+
Action action;
85+
if (async)
86+
{
87+
action = () => subject.FlushAsync(CancellationToken.None).GetAwaiter().GetResult(); ;
88+
}
89+
else
90+
{
91+
action = () => subject.Flush();
92+
}
93+
94+
action.ShouldThrow<NotSupportedException>();
95+
}
96+
7497
// private methods
7598
private IGridFSBucket CreateBucket(int chunkSize)
7699
{
@@ -81,7 +104,7 @@ private IGridFSBucket CreateBucket(int chunkSize)
81104
return new GridFSBucket(database, bucketOptions);
82105
}
83106

84-
private byte[] CreateContent(int contentSize)
107+
private byte[] CreateContent(int contentSize = 0)
85108
{
86109
return Enumerable.Range(0, contentSize).Select(i => (byte)i).ToArray();
87110
}

src/MongoDB.Driver.GridFS.Tests/GridFSUploadStreamTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
using System;
1717
using System.IO;
18+
using System.Threading;
1819
using FluentAssertions;
1920
using MongoDB.Driver.Tests;
2021
using NUnit.Framework;
@@ -48,6 +49,26 @@ public void CopyTo_should_throw(
4849
}
4950
}
5051

52+
[Test]
53+
public void Flush_should_not_throw(
54+
[Values(false, true)] bool async)
55+
{
56+
var bucket = CreateBucket();
57+
var subject = bucket.OpenUploadStream("Filename");
58+
59+
Action action;
60+
if (async)
61+
{
62+
action = () => subject.FlushAsync(CancellationToken.None).GetAwaiter().GetResult(); ;
63+
}
64+
else
65+
{
66+
action = () => subject.Flush();
67+
}
68+
69+
action.ShouldNotThrow();
70+
}
71+
5172
// private methods
5273
private IGridFSBucket CreateBucket()
5374
{

src/MongoDB.Driver.GridFS/GridFSForwardOnlyUploadStream.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ namespace MongoDB.Driver.GridFS
2727
{
2828
internal class GridFSForwardOnlyUploadStream : GridFSUploadStream
2929
{
30+
#region static
31+
// private static fields
32+
private static readonly Task __completedTask = Task.FromResult(true);
33+
#endregion
34+
3035
// fields
3136
private bool _aborted;
3237
private readonly List<string> _aliases;
@@ -179,12 +184,13 @@ public override void Close(CancellationToken cancellationToken)
179184

180185
public override void Flush()
181186
{
182-
throw new NotSupportedException();
187+
// do nothing
183188
}
184189

185190
public override Task FlushAsync(CancellationToken cancellationToken)
186191
{
187-
throw new NotSupportedException();
192+
// do nothing
193+
return __completedTask;
188194
}
189195

190196
public override int Read(byte[] buffer, int offset, int count)

0 commit comments

Comments
 (0)