public ParalleLockTest()
 {
     _taskLock   = new TaskLock();
     _lockObject = new object();
     _count      = 0;
     _n          = 0;
 }
Пример #2
0
        public async Task GetLockTest4Async()
        {
            TaskLock lockQueue = new TaskLock(maxQueueSize: 1);

            var startTcs = new TaskCompletionSource <bool>();
            var stopTcs  = new TaskCompletionSource <bool>();

            _ = Task.Run(async() =>
            {
                using (await lockQueue.GetLock(CancellationToken.None))
                {
                    startTcs.TrySetResult(true);
                    await stopTcs.Task;
                }
            });

            try
            {
                await startTcs.Task;
                using (await lockQueue.GetLock(CancellationToken.None))
                {
                    Assert.Fail(); // We should not get here!
                }
            }
            catch (Exception ex)
            {
                ex.ShouldBeOfType <IndexOutOfRangeException>();
            }

            stopTcs.SetResult(true);
        }
Пример #3
0
        public async Task GetLockTest3Async()
        {
            TaskLock lockQueue = new TaskLock(maxQueueSize: 100);
            var      startTcs  = new TaskCompletionSource <bool>();
            var      stopTcs   = new TaskCompletionSource <bool>();

            _ = Task.Run(async() =>
            {
                using (await lockQueue.GetLock(CancellationToken.None))
                {
                    startTcs.TrySetResult(true);
                    await stopTcs.Task;
                }
            });

            try
            {
                await startTcs.Task;
                using (await lockQueue.GetLock(CancellationToken.None, TimeSpan.FromMilliseconds(1)))
                {
                    Assert.Fail(); // We should not get here!
                }
            }
            catch (Exception ex)
            {
                ex.ShouldBeOfType <TaskCanceledException>();
            }

            stopTcs.TrySetResult(true);
        }
        public bool LockOperation(ulong taskId, int state)
        {
            TaskLock tlock = null;

            if (!locks.TryGetValue(taskId, out tlock))
            {
                tlock = new TaskLock {
                    State = state, Time = DateTime.Now
                };

                if (!locks.TryAdd(taskId, tlock))
                {
                    if (state != TaskLock.WRITE_OPERATION_EXECUTED)
                    {
                        if (locks.TryGetValue(taskId, out tlock))
                        {
                            if (tlock.State != TaskLock.WRITE_OPERATION_EXECUTED)
                            {
                                return(TaskLock.LOCK_READER);
                            }
                        }
                    }
                    throw new Exception("Some WRITE operation currently is being executed.");
                }
                return(TaskLock.LOCK_HOLDER);
            }
            if (tlock.State != TaskLock.WRITE_OPERATION_EXECUTED && state != TaskLock.WRITE_OPERATION_EXECUTED)
            {
                return(TaskLock.LOCK_READER);
            }
            throw new Exception("Some WRITE operation currently is being executed.");
        }
        public void UnLockOperation(ulong taskId, bool lockHolder)
        {
            TaskLock tlock = null;

            if (lockHolder && locks.TryGetValue(taskId, out tlock) && tlock.State != TaskLock.NO_OPERATION_EXECUTED)
            {
                tlock.State = TaskLock.NO_OPERATION_EXECUTED;
            }
            else
            {
                throw new Exception(String.Format("State of the task {0} operations on the controller is in inconsistency", taskId));
            }
        }
        public async Task        LockedTimeout()
        {
            using (var x = new TaskLock()) {
                using (await x.Enter()) {
                    var start = DateTime.UtcNow;

                    try {
                        using (await x.Enter(2000)) {
                            Assert.Fail();
                        }
                    }
                    catch (TimeoutException) {
                        var time = (DateTime.UtcNow - start).Ticks;

                        Assert.IsTrue((TimeSpan.TicksPerMillisecond * 1900) <= time && time <= (TimeSpan.TicksPerMillisecond * 2100));
                    }
                }
            }
        }
Пример #7
0
        public async Task GetLockTestAsync()
        {
            TaskLock lockQueue = new TaskLock(maxQueueSize: 100);

            var task1 = Task.Run(async() =>
            {
                using (await lockQueue.GetLock(CancellationToken.None))
                {
                    await TaskDelay.TryDelay(2, CancellationToken.None);
                }
            });

            using (await lockQueue.GetLock(CancellationToken.None))
            {
                await TaskDelay.TryDelay(2, CancellationToken.None);
            }

            await task1;
        }
        public async Task        LockedCancellation()
        {
            using (var x = new TaskLock()) {
                using (await x.Enter()) {
                    var start = DateTime.UtcNow;

                    try {
                        using (var cts = new CancellationTokenSource(2000)) {
                            using (await x.Enter(cts.Token)) {
                                Assert.Fail();
                            }
                        }
                    }
                    catch (OperationCanceledException) {
                        var time = (DateTime.UtcNow - start).Ticks;
                        Assert.IsTrue((TimeSpan.TicksPerMillisecond * 1900) <= time && time <= (TimeSpan.TicksPerMillisecond * 2100));
                    }
                }
            }
        }
Пример #9
0
        public async Task GetLockTest2Async()
        {
            TaskLock lockQueue = new TaskLock(maxQueueSize: 100);

            var startTcs = new TaskCompletionSource <bool>();
            var stopTcs  = new TaskCompletionSource <bool>();

            var task1 = Task.Run(async() => {
                using (await lockQueue.GetLock(CancellationToken.None))
                {
                    startTcs.TrySetResult(true);
                    await stopTcs.Task;
                }
            });

            try
            {
                await startTcs.Task;

                using var preCts = new CancellationTokenSource();
                preCts.Cancel();
                using (await lockQueue.GetLock(preCts.Token)) {
                    Assert.Fail(); // We should not get here!
                }
            }
            catch (Exception ex)
            {
                ex.ShouldBeOfType <TaskCanceledException>();
            }

            stopTcs.TrySetResult(true);

            using (await lockQueue.GetLock(CancellationToken.None))
            {
            }

            await task1;
        }
Пример #10
0
        public async Task                    ConnectAsync()
        {
            // Init
            lock (this) {
                if (_state != ConnectionState.Closed)
                {
                    throw new SMPPException("SMPPConnection busy.");
                }

                _tcpClient = new TcpClient();
                _sendLock  = new TaskLock();
                _error     = null;
                _sequence  = 1;
            }

            Exception error = null;

            // Connect to SMPP server
            try {
                Stream stream;

                using (new System.Threading.Timer((object state) =>
                {
                    lock (this) {
                        if (_state == ConnectionState.Connecting || _state == ConnectionState.SslHandshake)
                        {
                            error = new TimeoutException("Timeout");
                            _tcpClient.Close();
                        }
                    }
                },
                                                  null, 15 * 1000, System.Threading.Timeout.Infinite))
                {
                    _setState(ConnectionState.Connecting);
                    await _tcpClient.ConnectAsync(Hostname, Port);

                    stream = _tcpClient.GetStream();

                    if (Tls)
                    {
                        _setState(ConnectionState.SslHandshake);
                        SslStream sslStream = new SslStream(stream, true);
                        await sslStream.AuthenticateAsClientAsync(Hostname);

                        stream = sslStream;
                    }
                }

                lock (this) {
                    if (error != null)
                    {
                        throw error;
                    }

                    _setState(ConnectionState.StreamConnected);
                    _stream = stream;
                }
            }
            catch (Exception err) {
                if (err is ObjectDisposedException || err is NullReferenceException)
                {
                    lock (this) {
                        if (error != null)
                        {
                            err = error;
                        }
                        else
                        if (_state == ConnectionState.Closed)
                        {
                            err = new SMPPException("Connect aborted by close.");
                        }
                    }
                }

                err = _setFailed(new SMPPException("Connect failed.", err));
                Close();
                throw err;
            }

            // Start comtask
            Task _ = _run();

            // Bind
            try {
                _setState(ConnectionState.Binding);
                SMPPMessage response = await _submitMessage(Bind);

                if (response.Status != CommandStatus.ESME_ROK)
                {
                    throw new SMPPException("Response from server " + response.Status + ".");
                }

                _setState(ConnectionState.Connected);
                _enquirePoll = EnquirePoll;
            }
            catch (Exception err) {
                throw _setFailed(new SMPPException("Bind failed", err));
            }
        }