コード例 #1
0
        private void ExecuteLockingStrategyTest(ReadWritePriority priority, IDictionary <int, ResourceTaskInfo> tasks)
        {
            using (var resourceTasks = new ResourceTasks(priority))
            {
                foreach (var taskInfo in tasks)
                {
                    switch (taskInfo.Value.Operation)
                    {
                    case ResourceTaskOperation.Read:
                        resourceTasks.AddReadTask(taskInfo.Key, taskInfo.Value.StartDelay, taskInfo.Value.TaskTime);
                        break;

                    case ResourceTaskOperation.Write:
                        resourceTasks.AddWriteTask(taskInfo.Key, taskInfo.Value.StartDelay, taskInfo.Value.TaskTime);
                        break;

                    default:
                        throw new NotSupportedException($"Unknown operation: {taskInfo.Value.Operation}");
                    }
                }

                resourceTasks.Invoke();

                foreach (var lockTime in resourceTasks.LockTimes)
                {
                    System.Console.WriteLine($"TaskId: {lockTime.Key}, LockTime: {lockTime.Value}");
                }

                foreach (var lockTime in resourceTasks.LockTimes)
                {
                    var expectedMinTime = tasks[lockTime.Key].ExpectedMinTime;
                    var expectedMaxTime = tasks[lockTime.Key].ExpectedMaxTime;

                    Assert.IsTrue
                    (
                        lockTime.Value >= expectedMinTime && lockTime.Value < expectedMaxTime,
                        $"TaskId: {lockTime.Key}. Expected [{expectedMinTime}..{expectedMaxTime}] lock time but actual time is {lockTime.Value} ms."
                    );
                }
            }
        }
コード例 #2
0
 public ResourceTasks(ReadWritePriority priority)
 {
     _resource = new CommonResource(priority);
 }
コード例 #3
0
 public CommonResource(ReadWritePriority priority)
 {
     _lockObject = new PriorityReaderWriterLock(priority);
 }
コード例 #4
0
 public PriorityReaderWriterLock(ReadWritePriority priority)
 {
     _lockingStrategy = Disposable.Wrap(Strategies[priority].Invoke());
 }