示例#1
0
        public void Fork_should_return_expected_result()
        {
            var coreServerSession = new CoreServerSession();
            var options           = new ClientSessionOptions();
            var coreSession       = new CoreSession(coreServerSession, options.ToCore());
            var coreSessionHandle = new CoreSessionHandle(coreSession);
            var subject           = CreateSubject(coreSession: coreSessionHandle);

            coreSessionHandle.ReferenceCount().Should().Be(1);

            var result = subject.Fork();

            result.Client.Should().BeSameAs(subject.Client);
            result.Options.Should().BeSameAs(subject.Options);
            result.WrappedCoreSession.Should().NotBeSameAs(subject.WrappedCoreSession);
            var coreSessionHandle1 = (CoreSessionHandle)subject.WrappedCoreSession;
            var coreSessionHandle2 = (CoreSessionHandle)result.WrappedCoreSession;

            coreSessionHandle2.Wrapped.Should().BeSameAs(coreSessionHandle1.Wrapped);
            coreSessionHandle.ReferenceCount().Should().Be(2);
        }
示例#2
0
        public void Fork_should_return_expected_result()
        {
            var cluster           = Mock.Of <ICluster>();
            var coreServerSession = new CoreServerSession();
            var options           = new ClientSessionOptions();

#pragma warning disable CS0618 // Type or member is obsolete
            var coreSession = new CoreSession(cluster, coreServerSession, options.ToCore());
#pragma warning restore CS0618 // Type or member is obsolete
            var coreSessionHandle = new CoreSessionHandle(coreSession);
            var subject           = CreateSubject(coreSession: coreSessionHandle);
            coreSessionHandle.ReferenceCount().Should().Be(1);

            var result = subject.Fork();

            result.Client.Should().BeSameAs(subject.Client);
            result.Options.Should().BeSameAs(subject.Options);
            result.WrappedCoreSession.Should().NotBeSameAs(subject.WrappedCoreSession);
            var coreSessionHandle1 = (CoreSessionHandle)subject.WrappedCoreSession;
            var coreSessionHandle2 = (CoreSessionHandle)result.WrappedCoreSession;
            coreSessionHandle2.Wrapped.Should().BeSameAs(coreSessionHandle1.Wrapped);
            coreSessionHandle.ReferenceCount().Should().Be(2);
        }
示例#3
0
 // NOTE(rkm 2020-03-10) We don't actually implement transaction rollback as we only need to be able to start with a fresh collection for each test
 public override IClientSessionHandle StartSession(ClientSessionOptions options = null, CancellationToken cancellationToken = new CancellationToken()) => MockSessionHandle.Object;
示例#4
0
 public Task <IClientSessionHandle> StartSessionAsync(ClientSessionOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     return(Task.FromResult(StartSession(options, cancellationToken)));
 }
示例#5
0
 public INMClientSessionHandle(IMongoClient client)
 {
     Client  = client;
     Options = new ClientSessionOptions {
     };
 }
示例#6
0
 public Task <IClientSessionHandle> StartSessionAsync(ClientSessionOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     return(wrapped.StartSessionAsync(options, cancellationToken));
 }
示例#7
0
        private IClientSessionHandle CreateSession(BsonDocument entity, Dictionary <string, DisposableMongoClient> clients)
        {
            IMongoClient         client  = null;
            ClientSessionOptions options = null;

            foreach (var element in entity)
            {
                switch (element.Name)
                {
                case "id":
                    // handled on higher level
                    break;

                case "client":
                    var clientId = element.Value.AsString;
                    client = clients[clientId];
                    break;

                case "sessionOptions":
                    options = new ClientSessionOptions();
                    foreach (var option in element.Value.AsBsonDocument)
                    {
                        switch (option.Name)
                        {
                        case "snapshot":
                            options.Snapshot = option.Value.ToBoolean();
                            break;

                        case "causalConsistency":
                            options.CausalConsistency = option.Value.ToBoolean();
                            break;

                        case "defaultTransactionOptions":
                            ReadConcern    readConcern    = null;
                            ReadPreference readPreference = null;
                            WriteConcern   writeConcern   = null;
                            foreach (var transactionOption in option.Value.AsBsonDocument)
                            {
                                switch (transactionOption.Name)
                                {
                                case "readConcern":
                                    readConcern = ReadConcern.FromBsonDocument(transactionOption.Value.AsBsonDocument);
                                    break;

                                case "readPreference":
                                    readPreference = ReadPreference.FromBsonDocument(transactionOption.Value.AsBsonDocument);
                                    break;

                                case "writeConcern":
                                    writeConcern = WriteConcern.FromBsonDocument(transactionOption.Value.AsBsonDocument);
                                    break;

                                default:
                                    throw new FormatException($"Invalid session transaction option: '{transactionOption.Name}'.");
                                }
                            }
                            options.DefaultTransactionOptions = new TransactionOptions(readConcern, readPreference, writeConcern);
                            break;

                        default:
                            throw new FormatException($"Invalid session option argument name: '{option.Name}'.");
                        }
                    }
                    break;

                default:
                    throw new FormatException($"Invalid session argument name: '{element.Name}'.");
                }
            }

            var session = client.StartSession(options);

            return(session);
        }
示例#8
0
 public Task <IClientSessionHandle> StartSessionAsync(ClientSessionOptions options = null, CancellationToken cancellationToken = default)
 {
     return(((IMongoClient)Client).StartSessionAsync(options, cancellationToken));
 }
 public IClientSessionHandle StartSession(ClientSessionOptions options = null, CancellationToken cancellationToken = default(CancellationToken)) => _mongoClient.StartSession(options, cancellationToken);
示例#10
0
 public Task <IClientSessionHandle> StartSessionAsync(ClientSessionOptions options = null, CancellationToken cancellationToken = default)
 {
     return(Task.FromResult <IClientSessionHandle>(new INMClientSessionHandle(this)));
 }
示例#11
0
 /// <summary>
 /// Gets a transaction context/scope for the current instance's database
 /// </summary>
 /// <param name="options">Client session options (not required)</param>
 public Transaction Transaction(ClientSessionOptions options = null)
 {
     return(new Transaction(DatabaseName(), options));
 }
示例#12
0
 /// <summary>
 /// Starts a client session.
 /// </summary>
 /// <param name="options">The <see cref="ClientSessionOptions" />.</param>
 /// <param name="cancellationToken">The <see cref="CancellationToken" />.</param>
 /// <returns>The <see cref="IClientSessionHandle" />.</returns>
 public IClientSessionHandle StartSession(ClientSessionOptions options = default, CancellationToken cancellationToken = default) => MongoClient.StartSession(options, cancellationToken);
示例#13
0
 /// <summary>
 /// Instantiates and begins a transaction.
 /// </summary>
 /// <param name="database">The name of the database to use for this transaction. default db is used if not specified</param>
 /// <param name="options">Client session options for this transaction</param>
 /// <param name="modifiedBy">An optional ModifiedBy instance.
 /// When supplied, all save/update operations performed via this DBContext instance will set the value on entities that has a property of type ModifiedBy.
 /// You can inherit from the ModifiedBy class and add your own properties to it.
 /// Only one ModifiedBy property is allowed on a single entity type.</param>
 public Transaction(string database = default, ClientSessionOptions options = null, ModifiedBy modifiedBy = null)
 {
     Session = DB.Database(database).Client.StartSession(options);
     Session.StartTransaction();
     ModifiedBy = modifiedBy;
 }
示例#14
0
 public Task <IClientSessionHandle> initTransactionAsync(ClientSessionOptions options = null)
 {
     return(mService.client.StartSessionAsync(options));
 }
示例#15
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="options"></param>
 /// <param name="cancellationToken"></param>
 /// <returns></returns>
 public async Task <IClientSessionHandle> StartSessionAsync(ClientSessionOptions options = null, CancellationToken cancellationToken = default)
 {
     return(await client.StartSessionAsync(options, cancellationToken));
 }
示例#16
0
 /// <summary>
 /// Instantiates and begins a transaction.
 /// </summary>
 /// <param name="options">Client session options for this transaction</param>
 public Transaction(ClientSessionOptions options = null)
 {
     client  = DB.GetClient();
     Session = client.StartSession(options);
     Session.StartTransaction();
 }
        public void constructor_should_initialize_instance()
        {
            var result = new ClientSessionOptions();

            result.CausalConsistency.Should().NotHaveValue();
        }
示例#18
0
 public virtual Task <IClientSessionHandle> StartSessionAsync(ClientSessionOptions options = null, CancellationToken cancellationToken = new CancellationToken()) => throw new NotImplementedException();
 public Task <IClientSessionHandle> StartSessionAsync(ClientSessionOptions options = null, CancellationToken cancellationToken = default(CancellationToken)) => _mongoClient.StartSessionAsync(options, cancellationToken);
示例#20
0
 public static IClientSessionHandle Get(IMongoClient client, ClientSessionOptions options, double expiringInSeconds)
 {
     return(AsyncHelper.RunSync(() => GetAsync(client, options, expiringInSeconds)));
 }
示例#21
0
 /// <summary>
 /// Gets a transaction context/scope for a given database or the default database if not specified.
 /// </summary>
 /// <param name="database">The name of the database which this transaction is for (not required)</param>
 /// <param name="options">Client session options (not required)</param>
 public static Transaction Transaction(string database = default, ClientSessionOptions options = null)
 {
     return(new Transaction(database, options));
 }
示例#22
0
 /// <summary>
 /// Gets a transaction context/scope for a given entity type's database
 /// </summary>
 /// <typeparam name="T">The entity type to determine the database from for the transaction</typeparam>
 /// <param name="options">Client session options (not required)</param>
 public static Transaction Transaction <T>(ClientSessionOptions options = null) where T : IEntity
 {
     return(new Transaction(DatabaseName <T>(), options));
 }
示例#23
0
 public IClientSessionHandle StartSession(ClientSessionOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     return(wrapped.StartSession(options, cancellationToken));
 }
示例#24
0
 public IClientSessionHandle StartSession(ClientSessionOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     return(new FakeClientSessionHandle(this));
 }
示例#25
0
 /// <summary>
 /// Instantiates and begins a transaction.
 /// </summary>
 /// <param name="database">The name of the database to use for this transaction. default db is used if not specified</param>
 /// <param name="options">Client session options for this transaction</param>
 public Transaction(string database = default, ClientSessionOptions options = null)
 {
     client  = DB.Database(database).Client;
     Session = client.StartSession(options);
     Session.StartTransaction();
 }
示例#26
0
 public IClientSessionHandle StartSession(ClientSessionOptions options = null, CancellationToken cancellationToken = default)
 {
     throw new System.NotImplementedException();
 }