PageRenderTime 51ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/MongoDB.Driver.Core.Tests/MongoNodeIsRecoveringExceptionTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 99 lines | 72 code | 13 blank | 14 comment | 0 complexity | 2eb8aef805978d6e8740b3fd58d31b23 MD5 | raw file
Possible License(s): Apache-2.0
  1. /* Copyright 2013-present MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. using System.IO;
  17. using System.Net;
  18. #if NET452
  19. using System.Runtime.Serialization.Formatters.Binary;
  20. #endif
  21. using FluentAssertions;
  22. using MongoDB.Bson;
  23. using MongoDB.Driver.Core.Clusters;
  24. using MongoDB.Driver.Core.Connections;
  25. using MongoDB.Driver.Core.Servers;
  26. using Xunit;
  27. namespace MongoDB.Driver
  28. {
  29. public class MongoNodeIsRecoveringExceptionTests
  30. {
  31. private readonly ConnectionId _connectionId = new ConnectionId(new ServerId(new ClusterId(1), new DnsEndPoint("localhost", 27017)), 2).WithServerValue(3);
  32. private readonly BsonDocument _command = new BsonDocument("command", 1);
  33. private readonly BsonDocument _serverResult = new BsonDocument("result", 1);
  34. [Fact]
  35. public void constructor_should_initialize_subject()
  36. {
  37. var result = new MongoNodeIsRecoveringException(_connectionId, _command, _serverResult);
  38. result.ConnectionId.Should().BeSameAs(_connectionId);
  39. result.InnerException.Should().BeNull();
  40. result.Message.Should().Be("Server returned node is recovering error (code = -1).");
  41. result.Command.Should().BeSameAs(_command);
  42. result.Result.Should().BeSameAs(_serverResult);
  43. }
  44. [Fact]
  45. public void constructor_should_initialize_subject_when_result_contains_code()
  46. {
  47. var serverResult = BsonDocument.Parse("{ ok : 0, code : 1234 }");
  48. var result = new MongoNodeIsRecoveringException(_connectionId, _command, serverResult);
  49. result.ConnectionId.Should().BeSameAs(_connectionId);
  50. result.InnerException.Should().BeNull();
  51. result.Message.Should().Be("Server returned node is recovering error (code = 1234).");
  52. result.Command.Should().BeSameAs(_command);
  53. result.Result.Should().BeSameAs(serverResult);
  54. }
  55. [Fact]
  56. public void constructor_should_initialize_subject_when_result_contains_code_and_codeName()
  57. {
  58. var serverResult = BsonDocument.Parse("{ ok : 0, code : 1234, codeName : 'some name' }");
  59. var result = new MongoNodeIsRecoveringException(_connectionId, _command, serverResult);
  60. result.ConnectionId.Should().BeSameAs(_connectionId);
  61. result.InnerException.Should().BeNull();
  62. result.Message.Should().Be("Server returned node is recovering error (code = 1234, codeName = \"some name\").");
  63. result.Command.Should().BeSameAs(_command);
  64. result.Result.Should().BeSameAs(serverResult);
  65. }
  66. #if NET452
  67. [Fact]
  68. public void Serialization_should_work()
  69. {
  70. var subject = new MongoNodeIsRecoveringException(_connectionId, _command, _serverResult);
  71. var formatter = new BinaryFormatter();
  72. using (var stream = new MemoryStream())
  73. {
  74. formatter.Serialize(stream, subject);
  75. stream.Position = 0;
  76. var rehydrated = (MongoNodeIsRecoveringException)formatter.Deserialize(stream);
  77. rehydrated.ConnectionId.Should().Be(subject.ConnectionId);
  78. rehydrated.InnerException.Should().BeNull();
  79. rehydrated.Message.Should().Be(subject.Message);
  80. rehydrated.Command.Should().Be(subject.Command);
  81. rehydrated.Result.Should().Be(subject.Result);
  82. }
  83. }
  84. #endif
  85. }
  86. }