Example #1
0
 public void CanCreateLockTable()
 {
     Assert.DoesNotThrow(() =>
     {
         _lockTable = new LockTable(TimeSpan.FromSeconds(10), TimeSpan.FromMilliseconds(300));
     });
 }
Example #2
0
 public void CanCreateDefaultLockTable()
 {
     Assert.DoesNotThrow(() =>
     {
         _lockTable = new LockTable(null, null);
     });
 }
Example #3
0
        public void CanSetSharedLock()
        {
            _lockTable = new LockTable(TimeSpan.FromSeconds(10), TimeSpan.FromMilliseconds(300));

            Assert.DoesNotThrow(() =>
            {
                _lockTable.SharedLock(new Block(RandomFilename, 0));
            });
        }
        public void TableTest()
        {
            LockTable table = new LockTable(10);

            table.GetQueue("queue1");
            table.GetQueue("queue2");
            table.GetQueue("queue3");
            Assert.IsTrue(true);
        }
Example #5
0
        public void CanSetExclusiveLock()
        {
            _lockTable = new LockTable(TimeSpan.FromSeconds(10), TimeSpan.FromMilliseconds(300));

            Assert.DoesNotThrow(() =>
            {
                var block = new Block(RandomFilename, 0);
                _lockTable.ExclusiveLock(block);
            });
        }
Example #6
0
        public string publishInv(ApiPublish publish)
        {
            Company currentCom = ((EInvoiceContext)FXContext.Current).CurrentCompany;
            ILog    log        = LogManager.GetLogger(typeof(PublishController));

            if (!LockTable.Contains(String.Format("{0}${1}", publish.pattern, currentCom.id)))
            {
                object lockobj = new object();
                LockTable.Add(String.Format("{0}${1}", publish.pattern, currentCom.id), lockobj);
            }
            lock (LockTable[String.Format("{0}${1}", publish.pattern, currentCom.id)])
            {
                IDeliver         _DeliverService = currentCom.Config.ContainsKey("IDeliver") ? InvServiceFactory.GetDeliver(currentCom.Config["IDeliver"]) : null;
                IInvoiceService  IInvSrv         = InvServiceFactory.GetService(publish.pattern, currentCom.id);
                IList <IInvoice> lst             = IInvSrv.GetByID(currentCom.id, publish.invIDs).OrderBy(p => p.CreateDate).ToList();
                try
                {
                    if (lst.Count() <= 50)
                    {
                        Launcher.Instance.Launch(publish.pattern, publish.serial, lst.ToArray());
                    }
                    else
                    {
                        for (int i = 0; i < lst.Count() / 50; i++)
                        {
                            Launcher.Instance.Launch(publish.pattern, publish.serial, lst.Skip(i * 50).Take(50).ToArray());
                        }
                    }
                    try
                    {
                        if (_DeliverService != null)
                        {
                            _DeliverService.PrepareDeliver(lst.ToArray(), currentCom);
                        }
                    }
                    catch (Exception ex)
                    {
                        log.Error(ex);
                    }
                    return("OK");
                }
                catch (EInvoice.Core.Launching.NoFactory.OpenTranException ex)
                {
                    log.Error(ex);
                    return("ERR:14");
                }
                catch (Exception ex)
                {
                    log.Error(ex);
                    return("ERR:5 " + ex.Message);//loi phat hanh hoa don
                }
            }
        }
Example #7
0
        public void ThrowsExceptionIfSharedLockWasNotReceivedBecauseAlreadHadExclusive()
        {
            _lockTable = new LockTable(TimeSpan.FromMilliseconds(500), TimeSpan.FromMilliseconds(100));

            Assert.Throws <LockAbortException>(() =>
            {
                var block = new Block(RandomFilename, 0);

                _lockTable.ExclusiveLock(block);
                _lockTable.SharedLock(block);
            });
        }
Example #8
0
        public void CanHaveMultipleSharedLocks()
        {
            _lockTable = new LockTable(TimeSpan.FromSeconds(10), TimeSpan.FromMilliseconds(300));

            Assert.DoesNotThrow(() =>
            {
                var block = new Block(RandomFilename, 0);
                _lockTable.SharedLock(block);
                _lockTable.SharedLock(block);
                _lockTable.SharedLock(block);
                _lockTable.Unlock(block);
            });
        }
Example #9
0
        public void CannotWaitForExclusiveLockIfHaveExclusive()
        {
            _lockTable = new LockTable(TimeSpan.FromMilliseconds(500), TimeSpan.FromMilliseconds(100));

            Assert.Throws <LockAbortException>(() =>
            {
                var block = new Block(RandomFilename, 0);

                _lockTable.ExclusiveLock(block);
                _lockTable.ExclusiveLock(block);
                _lockTable.Unlock(block);
            });
        }
Example #10
0
        public void NotThrowsExceptionIfExclusiveLockWasNotReceivedBecauseAlreadHadShared()
        {
            _lockTable = new LockTable(TimeSpan.FromMilliseconds(500), TimeSpan.FromMilliseconds(100));

            //Assert.Throws<LockAbortException>(() =>
            Assert.DoesNotThrow(() =>
            {
                var block = new Block(RandomFilename, 0);

                // The only way to receive a exclusive lock is only after contesting an shared lock
                _lockTable.SharedLock(block);
                _lockTable.ExclusiveLock(block);
            });
        }
Example #11
0
        public void CanTakeExclusiveLockIfAlreadyTaken()
        {
            var lockTable = new LockTable(TimeSpan.FromMilliseconds(300), TimeSpan.FromMilliseconds(100));

            concurrencyManager = new ConcurrencyManager();

            var block = new Block(RandomFilename, 0);

            Assert.DoesNotThrow(() =>
            {
                concurrencyManager.ExclusiveLock(block);
                concurrencyManager.ExclusiveLock(block);
            });
        }
Example #12
0
        public void CanWaitForExclusiveLockIfHaveExclusive()
        {
            _lockTable = new LockTable(TimeSpan.FromMilliseconds(500), TimeSpan.FromMilliseconds(100));

            Assert.DoesNotThrow(() =>
            {
                var block = new Block(RandomFilename, 0);

                _lockTable.ExclusiveLock(block);

                Task.Run(async() =>
                {
                    await Task.Delay(500);
                    _lockTable.Unlock(block);
                });

                _lockTable.ExclusiveLock(block);
                _lockTable.Unlock(block);
            });
        }
Example #13
0
 public void Setup()
 {
     lockTable = new LockTable(null, null);
 }