forked from samus/mongodb-csharp
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Refactored ISerializationFactory to use GetBsonWriterSettings instead…
… of GetBsonDescriptor directly.
- Loading branch information
Showing
14 changed files
with
181 additions
and
174 deletions.
There are no files selected for viewing
124 changes: 64 additions & 60 deletions
124
source/MongoDB.Tests/IntegrationTests/Connections/TestConnection.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,72 @@ | ||
using System; | ||
using System.IO; | ||
using System.Text; | ||
using MongoDB.Bson; | ||
using MongoDB.Connections; | ||
using MongoDB.Protocol; | ||
using NUnit.Framework; | ||
|
||
namespace MongoDB.IntegrationTests.Connections | ||
{ | ||
[TestFixture()] | ||
public class TestConnection | ||
{ | ||
[Test] | ||
public void TestSendQueryMessage(){ | ||
//Connection conn = new Connection("10.141.153.2"); | ||
Connection conn = ConnectionFactory.GetConnection(string.Empty); | ||
conn.Open(); | ||
|
||
var qmsg = generateQueryMessage(); | ||
conn.SendTwoWayMessage(qmsg); | ||
|
||
conn.Close(); | ||
} | ||
|
||
[Test] | ||
public void TestReconnectOnce(){ | ||
Connection conn = ConnectionFactory.GetConnection(string.Empty); | ||
conn.Open(); | ||
|
||
WriteBadMessage(conn); | ||
try{ | ||
var qmsg = generateQueryMessage(); | ||
conn.SendTwoWayMessage(qmsg); | ||
|
||
}catch(IOException){ | ||
//Should be able to resend. | ||
Assert.IsTrue(conn.State == ConnectionState.Opened); | ||
var qmsg = generateQueryMessage(); | ||
ReplyMessage<Document> rmsg = conn.SendTwoWayMessage(qmsg); | ||
Assert.IsNotNull(rmsg); | ||
|
||
} | ||
} | ||
|
||
protected void WriteBadMessage(Connection conn){ | ||
//Write a bad message to the socket to force mongo to shut down our connection. | ||
BinaryWriter writer = new BinaryWriter(conn.GetStream()); | ||
System.Text.UTF8Encoding encoding=new System.Text.UTF8Encoding(); | ||
Byte[] msg = encoding.GetBytes("Goodbye MongoDB!"); | ||
writer.Write(16 + msg.Length + 1); | ||
writer.Write(1); | ||
writer.Write(1); | ||
writer.Write(1001); | ||
writer.Write(msg); | ||
writer.Write((byte)0); | ||
} | ||
|
||
protected QueryMessage generateQueryMessage(){ | ||
Document qdoc = new Document(); | ||
qdoc.Add("listDatabases", 1.0); | ||
namespace MongoDB.IntegrationTests.Connections | ||
{ | ||
[TestFixture] | ||
public class TestConnection | ||
{ | ||
protected void WriteBadMessage(Connection conn) | ||
{ | ||
//Write a bad message to the socket to force mongo to shut down our connection. | ||
var writer = new BinaryWriter(conn.GetStream()); | ||
var encoding = new UTF8Encoding(); | ||
var msg = encoding.GetBytes("Goodbye MongoDB!"); | ||
writer.Write(16 + msg.Length + 1); | ||
writer.Write(1); | ||
writer.Write(1); | ||
writer.Write(1001); | ||
writer.Write(msg); | ||
writer.Write((byte)0); | ||
} | ||
|
||
protected QueryMessage GenerateQueryMessage() | ||
{ | ||
var qdoc = new Document {{"listDatabases", 1.0}}; | ||
//QueryMessage qmsg = new QueryMessage(qdoc,"system.namespaces"); | ||
var qmsg = new QueryMessage(new BsonDocumentDescriptor(), qdoc, "admin.$cmd"); | ||
qmsg.NumberToReturn = -1; | ||
|
||
return qmsg; | ||
} | ||
} | ||
return new QueryMessage(new BsonWriterSettings(), qdoc, "admin.$cmd") | ||
{ | ||
NumberToReturn = -1 | ||
}; | ||
} | ||
|
||
[Test] | ||
public void TestReconnectOnce() | ||
{ | ||
var conn = ConnectionFactory.GetConnection(string.Empty); | ||
conn.Open(); | ||
|
||
WriteBadMessage(conn); | ||
try | ||
{ | ||
var qmsg = GenerateQueryMessage(); | ||
conn.SendTwoWayMessage(qmsg); | ||
} | ||
catch(IOException) | ||
{ | ||
//Should be able to resend. | ||
Assert.IsTrue(conn.State == ConnectionState.Opened); | ||
var qmsg = GenerateQueryMessage(); | ||
var rmsg = conn.SendTwoWayMessage(qmsg); | ||
Assert.IsNotNull(rmsg); | ||
} | ||
} | ||
|
||
[Test] | ||
public void TestSendQueryMessage() | ||
{ | ||
//Connection conn = new Connection("10.141.153.2"); | ||
var conn = ConnectionFactory.GetConnection(string.Empty); | ||
conn.Open(); | ||
|
||
var qmsg = GenerateQueryMessage(); | ||
conn.SendTwoWayMessage(qmsg); | ||
|
||
conn.Close(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.