13
13
* limitations under the License.
14
14
*/
15
15
16
+ using System ;
16
17
using System . IO ;
17
18
using System . Text ;
19
+ using System . Threading ;
18
20
using MongoDB . Driver ;
19
21
using NUnit . Framework ;
20
22
@@ -23,6 +25,7 @@ namespace MongoDB.DriverUnitTests.Jira.CSharp269
23
25
[ TestFixture ]
24
26
public class CSharp269Tests
25
27
{
28
+ private MongoServer _server ;
26
29
private MongoDatabase _database ;
27
30
28
31
[ TestFixtureSetUp ]
@@ -31,8 +34,8 @@ public void TestFixtureSetup()
31
34
var clientSettings = Configuration . TestClient . Settings . Clone ( ) ;
32
35
clientSettings . ReadPreference = ReadPreference . SecondaryPreferred ;
33
36
var client = new MongoClient ( clientSettings ) ; // ReadPreference=SecondaryPreferred
34
- var server = client . GetServer ( ) ;
35
- _database = server . GetDatabase ( Configuration . TestDatabase . Name ) ;
37
+ _server = client . GetServer ( ) ;
38
+ _database = _server . GetDatabase ( Configuration . TestDatabase . Name ) ;
36
39
_database . GridFS . Files . Drop ( ) ;
37
40
_database . GridFS . Chunks . Drop ( ) ;
38
41
}
@@ -47,12 +50,27 @@ public void TestUploadAndDownload()
47
50
_database . GridFS . Upload ( stream , "HelloWorld.txt" ) ;
48
51
}
49
52
50
- using ( var stream = new MemoryStream ( ) )
53
+ // use RequestStart so that if we are running this test against a replica set we will bind to a specific secondary
54
+ using ( _server . RequestStart ( _database , ReadPreference . SecondaryPreferred ) )
51
55
{
52
- _database . GridFS . Download ( stream , "HelloWorld.txt" ) ;
53
- var downloadedBytes = stream . ToArray ( ) ;
54
- var downloadedText = Encoding . UTF8 . GetString ( downloadedBytes ) ;
55
- Assert . AreEqual ( "HelloWorld" , downloadedText ) ;
56
+ // wait for the GridFS file to be replicated before trying to Download it
57
+ var timeoutAt = DateTime . UtcNow . AddSeconds ( 30 ) ;
58
+ while ( ! _database . GridFS . Exists ( "HelloWorld.txt" ) )
59
+ {
60
+ if ( DateTime . UtcNow >= timeoutAt )
61
+ {
62
+ throw new TimeoutException ( "HelloWorld.txt failed to propagate to secondary" ) ;
63
+ }
64
+ Thread . Sleep ( TimeSpan . FromMilliseconds ( 1 ) ) ;
65
+ }
66
+
67
+ using ( var stream = new MemoryStream ( ) )
68
+ {
69
+ _database . GridFS . Download ( stream , "HelloWorld.txt" ) ;
70
+ var downloadedBytes = stream . ToArray ( ) ;
71
+ var downloadedText = Encoding . UTF8 . GetString ( downloadedBytes ) ;
72
+ Assert . AreEqual ( "HelloWorld" , downloadedText ) ;
73
+ }
56
74
}
57
75
}
58
76
}
0 commit comments