public void RunTransaction_isolationLevel()
        {
            var db1 = RootServiceProvider.GetService <IShardingDbAccessor>();
            var db2 = RootServiceProvider.CreateScope().ServiceProvider.GetService <IShardingDbAccessor>();

            db1.Insert(_newData);

            var  updateData = _newData.DeepClone();
            Task db2Task    = new Task(() =>
            {
                updateData.UserName = Guid.NewGuid().ToString();
                db2.Update(updateData);
            });

            var res = db1.RunTransaction(() =>
            {
                //db1读=>db2写(阻塞)=>db1读=>db1提交
                var db1Data_1 = db1.GetIShardingQueryable <Base_UnitTest>().Where(x => x.Id == _newData.Id).FirstOrDefault();

                db2Task.Start();

                var db1Data_2 = db1.GetIShardingQueryable <Base_UnitTest>().Where(x => x.Id == _newData.Id).FirstOrDefault();
                Assert.AreEqual(db1Data_1.ToJson(), db1Data_2.ToJson());
            });

            db2Task.Wait();
            var db1Data_3 = db1.GetIShardingQueryable <Base_UnitTest>().Where(x => x.Id == _newData.Id).FirstOrDefault();

            Assert.AreEqual(updateData.ToJson(), db1Data_3.ToJson());
        }
Exemple #2
0
        public void RootServiceProvider_will_create_log_on_failure()
        {
            var rsp =  new RootServiceProvider(new List <Type> {
                typeof(PServiceRegistration)
            }.GetEnumerator());

            using (var record = new LateBoundLogRecord()) {
                rsp.GetService(typeof(string));

                Assert.Equal(
                    "Problem executing service registration method `Carbonfrost.UnitTests.Core.Runtime.RootServiceProviderTests+PServiceRegistration.ErrorRegistrationMethod'",
                    record.LastFailure.Message
                    );
            }
        }
        protected MongoDbTestBase()
        {
            var services = CreateServiceCollection();

            services.AddLogging();
            services.AddMongoDbService();

            services.Configure <MongoDbConnectStringOption>(option => option.MongoDbServer = "mongodb://localhost:27017");

            services.AddSingleton <ITestMongoDbContext, TestMongoDbContext>();

            RegisterRepository(services);

            Services = services;

            RootServiceProvider = CreateServiceProvider(services);

            LoggerFactory = RootServiceProvider.GetService <ILoggerFactory>() !;

            LoggerFactory.AddProvider(new MongoDbLoggerProvider(RootServiceProvider.GetService <IMongoDbLogRepository>() !));
        }
Exemple #4
0
        public static void Begin(TestContext context)
        {
            ServiceCollection services = new ServiceCollection();

            services.AddEFCoreSharding(config =>
            {
                config.UseDatabase(Config.CONSTRING1, DatabaseType.SqlServer);
                config.UseDatabase <ISQLiteDb1>(Config.SQLITE1, DatabaseType.SQLite);
                config.UseDatabase <ISQLiteDb2>(Config.SQLITE2, DatabaseType.SQLite);
                config.UseDatabase <ICustomDbAccessor>(Config.CONSTRING1, DatabaseType.SqlServer);

                //分表配置
                //添加数据源
                config.AddDataSource(Config.CONSTRING1, ReadWriteType.Read | ReadWriteType.Write, DatabaseType.SqlServer);
                //设置分表规则
                config.SetHashModSharding <Base_UnitTest>(nameof(Base_UnitTest.Id), 3);
            });

            RootServiceProvider = services.BuildServiceProvider();
            ServiceScopeFactory = RootServiceProvider.GetService <IServiceScopeFactory>();
        }
Exemple #5
0
 public MongoDbTest() : base()
 {
     _cityRepository = RootServiceProvider.GetService <ICityRepository>() !;
     Logger          = NullLogger <MongoDbTest> .Instance;
 }
Exemple #6
0
        public void DistributedTransaction()
        {
            //失败事务
            var db1 = RootServiceProvider.GetService <ISQLiteDb1>();
            var db2 = RootServiceProvider.GetService <ISQLiteDb2>();

            db1.DeleteAll <Base_UnitTest>();
            db2.DeleteAll <Base_UnitTest>();
            Base_UnitTest data1 = new Base_UnitTest
            {
                Id       = Guid.NewGuid().ToString(),
                UserId   = "1",
                UserName = Guid.NewGuid().ToString()
            };
            Base_UnitTest data2 = new Base_UnitTest
            {
                Id       = data1.Id,
                UserId   = "1",
                UserName = Guid.NewGuid().ToString()
            };
            Base_UnitTest data3 = new Base_UnitTest
            {
                Id       = Guid.NewGuid().ToString(),
                UserId   = "2",
                UserName = Guid.NewGuid().ToString()
            };

            new Action(() =>
            {
                var transaction = DistributedTransactionFactory.GetDistributedTransaction();
                transaction.AddDbAccessor(db1, db2);
                var succcess = transaction.RunTransaction(() =>
                {
                    db1.ExecuteSql("insert into Base_UnitTest(Id,CreateTime) values('10',@CreateTime) ", ("@CreateTime", DateTime.Now));
                    db1.Insert(data1);
                    db1.Insert(data2);
                    db2.Insert(data1);
                    db2.Insert(data3);
                });
                Assert.IsFalse(succcess.Success);
                Assert.AreEqual(0, db1.GetIQueryable <Base_UnitTest>().Count());
                Assert.AreEqual(0, db2.GetIQueryable <Base_UnitTest>().Count());
            })();

            //成功事务
            new Action(() =>
            {
                var transaction = DistributedTransactionFactory.GetDistributedTransaction();
                transaction.AddDbAccessor(db1, db2);

                var succcess = transaction
                               .RunTransaction(() =>
                {
                    db1.ExecuteSql("insert into Base_UnitTest(Id,CreateTime) values('10',@CreateTime) ", ("@CreateTime", DateTime.Now));
                    db1.Insert(data1);
                    db1.Insert(data3);
                    db2.Insert(data1);
                    db2.Insert(data3);
                });
                int count1 = db1.GetIQueryable <Base_UnitTest>().Count();
                int count2 = db2.GetIQueryable <Base_UnitTest>().Count();
                Assert.IsTrue(succcess.Success);
                Assert.AreEqual(3, count1);
                Assert.AreEqual(2, count2);
            })();
        }