PageRenderTime 34ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/mongodb/mongo-csharp-driver
C# | 71 lines | 50 code | 7 blank | 14 comment | 0 complexity | bf1305e684da8c2386b18c09e42a62f1 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 MongoNotPrimaryExceptionTests
  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 subject = new MongoNotPrimaryException(_connectionId, _command, _serverResult);
  38. subject.ConnectionId.Should().BeSameAs(_connectionId);
  39. subject.InnerException.Should().BeNull();
  40. subject.Message.Should().Be("Server returned not master error.");
  41. subject.Command.Should().BeSameAs(_command);
  42. subject.Result.Should().Be(_serverResult);
  43. }
  44. #if NET452
  45. [Fact]
  46. public void Serialization_should_work()
  47. {
  48. var subject = new MongoNotPrimaryException(_connectionId, _command, _serverResult);
  49. var formatter = new BinaryFormatter();
  50. using (var stream = new MemoryStream())
  51. {
  52. formatter.Serialize(stream, subject);
  53. stream.Position = 0;
  54. var rehydrated = (MongoNotPrimaryException)formatter.Deserialize(stream);
  55. rehydrated.ConnectionId.Should().Be(subject.ConnectionId);
  56. rehydrated.InnerException.Should().BeNull();
  57. rehydrated.Message.Should().Be(subject.Message);
  58. rehydrated.Command.Should().Be(subject.Command);
  59. rehydrated.Result.Should().Be(subject.Result);
  60. }
  61. }
  62. #endif
  63. }
  64. }