|
| 1 | +/* Copyright 2019-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 | + |
| 16 | +using System; |
| 17 | +using System.Linq; |
| 18 | +using System.Threading; |
| 19 | +using System.Threading.Tasks; |
| 20 | +using MongoDB.Bson; |
| 21 | +using MongoDB.Driver.Core.Clusters; |
| 22 | +using MongoDB.Driver.Core.Configuration; |
| 23 | +using MongoDB.Driver.Core.Events; |
| 24 | +using MongoDB.Driver.Core.Misc; |
| 25 | +using MongoDB.Driver.Core.Servers; |
| 26 | +using MongoDB.Driver.Core.TestHelpers.XunitExtensions; |
| 27 | +using Xunit; |
| 28 | + |
| 29 | +namespace MongoDB.Driver.Tests.Jira |
| 30 | +{ |
| 31 | + public class CSharp2564Tests |
| 32 | + { |
| 33 | + [SkippableFact] |
| 34 | + public async Task Misbehaved_async_method_should_not_deadlock_server_selection() |
| 35 | + { |
| 36 | + RequireServer.Check().ClusterType(ClusterType.ReplicaSet).VersionGreaterThanOrEqualTo("3.6"); |
| 37 | + |
| 38 | + // note: the code below deadlocks because await StartSessionAsync continues when UpdateClusterDescription in Cluster called TrySetResult after finding the secondary |
| 39 | + // but then the sync call to RunCommand blocks waiting for a primary and the call to TrySetResult never returns |
| 40 | + // which in turn prevents SDAM from unwinding back to process the next queued heartbeat event so the primary is never found |
| 41 | + |
| 42 | + var primary = CoreTestConfiguration.Cluster.Description.Servers.Where(s => s.Type == ServerType.ReplicaSetPrimary).Single(); |
| 43 | + void clusterConfigurator(ClusterBuilder builder) |
| 44 | + { |
| 45 | + builder.Subscribe((ServerHeartbeatSucceededEvent heartbeatEvent) => |
| 46 | + { |
| 47 | + // ensure that the primary heartbeat is the last to be processed by introducing a small artificial delay |
| 48 | + if (EndPointHelper.Equals(heartbeatEvent.ConnectionId.ServerId.EndPoint, primary.EndPoint)) |
| 49 | + { |
| 50 | + Thread.Sleep(TimeSpan.FromSeconds(1)); |
| 51 | + } |
| 52 | + }); |
| 53 | + } |
| 54 | + |
| 55 | + using (var client = DriverTestConfiguration.CreateDisposableClient(clusterConfigurator)) |
| 56 | + { |
| 57 | + using (var session = await client.StartSessionAsync().ConfigureAwait(false)) |
| 58 | + { |
| 59 | + var adminDatabase = client.GetDatabase("admin"); |
| 60 | + adminDatabase.RunCommand<BsonDocument>(session, "{ ping : 1 }"); // this async method is misbehaving by calling a blocking sync method |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | +} |
0 commit comments