Exemple #1
0
        /// <summary>
        /// <para>Adds a job to collection.</para>
        /// </summary>
        /// <exception cref="ArgumentNullException">if parameter is null</exception>
        /// <exception cref="ArgumentException">
        /// If job.Status is not valid <see cref="JobStatus"/> value OR
        /// If a job with given queueId has already been added to the collection.
        /// </exception>
        /// <param name="job">job to add</param>
        public void AddJob(ITradeActivityItem job)
        {
            //Validate
            Helper.ValidateNotNull(job, "job");
            Helper.ValidateJobStatus(job.Status, "job.Status");

            lock (syncRoot)
            {
                //Add to jobsByQueueID
                //NOTE - Must check by queueId and not by job
                if (jobsByQueueID.ContainsKey(job.QueueID))
                {
                    throw new ArgumentException("Job with queueId: " + job.QueueID + " has already been added.", "job");
                }
                jobsByQueueID[job.QueueID] = job;

                //Add to jobsByStatus
                AddTradeActivityItemToIndex <int>(jobsByStatus, (int)job.Status, job);

                //Add to jobsByServiceId
                AddTradeActivityItemToIndex <long>(jobsByServiceId, job.ServiceID, job);

                //Add to jobsByStatusAndServiceID
                JobInfo jobInfo = new JobInfo(job.ServiceID, job.Status);
                AddTradeActivityItemToIndex <JobInfo>(jobsByStatusAndServiceID, jobInfo, job);

                //Subsribes to job.StatusChanged event
                job.StatusChanged += new EventHandler <StatusEventArgs>(HandleStatusChange);
            }
        }
Exemple #2
0
 public void TearDown()
 {
     jsc = null;
     ta2 = null;
     ta1 = null;
     ta3 = null;
 }
Exemple #3
0
        public void TestRemoveJob()
        {
            //Add ta1 and ta2
            TestAddJob2();

            //Remove ta1
            ITradeActivityItem item = jsm.RemoveJob(2);

            Assert.IsTrue(object.ReferenceEquals(item, ta1), "The removed instance must be same as ta1");
            //Check if item was removed from all indices correctly.
            Assert.IsNull(jsm.GetJobByQueueID(2), "Wrong RemoveJob implementation.");
            Assert.AreEqual(1, jsm.GetJobsByServiceID(3).Count, "Wrong RemoveJob implementation.");
            Assert.AreEqual(1, jsm.GetJobsByStatus(JobStatus.Queued).Count, "Wrong RemoveJob implementation.");
            Assert.AreEqual(
                1, jsm.GetJobsByStatusAndServiceID(JobStatus.Queued, 3).Count, "Wrong RemoveJob implementation.");

            //Try to Remove ta1 again
            item = jsm.RemoveJob(2);
            Assert.IsNull(item, "Must return null if job with given queueId is not present.");

            //Remove ta2
            item = jsm.RemoveJob(3);
            Assert.IsTrue(object.ReferenceEquals(item, ta2), "The removed instance must be same as ta1");
            //Check if item was removed from all indices correctly.
            Assert.IsNull(jsm.GetJobByQueueID(3), "Wrong RemoveJob implementation.");
            Assert.AreEqual(0, jsm.GetJobsByServiceID(3).Count, "Wrong RemoveJob implementation.");
            Assert.AreEqual(0, jsm.GetJobsByStatus(JobStatus.Queued).Count, "Wrong RemoveJob implementation.");
            Assert.AreEqual(
                0, jsm.GetJobsByStatusAndServiceID(JobStatus.Queued, 3).Count, "Wrong RemoveJob implementation.");

            //Try to Remove ta2 again
            item = jsm.RemoveJob(3);
            Assert.IsNull(item, "Must return null if job with given queueId is not present.");
        }
Exemple #4
0
        public void SetUp()
        {
            jsc = new JobSortedCollection(new DefaultPrioritizer());

            ta1 = new MockTradeActivityItem("AS", DateTime.Today, 1, 2, 3, JobStatus.Queued);
            ta2 = new MockTradeActivityItem("AS", DateTime.Today.AddDays(-2), 1, 2, 3, JobStatus.Queued);
            ta3 = new MockTradeActivityItem("AS", DateTime.Today.AddDays(-1), 1, 2, 3, JobStatus.Queued);
        }
Exemple #5
0
        public void TestCopyToFail4()
        {
            //Add a few items
            jsc.Add(ta1);
            jsc.Add(ta2);
            jsc.Add(ta3);

            ITradeActivityItem[] array = new ITradeActivityItem[6];
            jsc.CopyTo(array, 4);
        }
Exemple #6
0
        /// <summary>
        /// Compares 2 jobs only on the basis of their creation dates.
        /// </summary>
        /// <param name="job1">The first job to compare</param>
        /// <param name="job2">The second job to compare</param>
        /// <returns>
        /// Greater than 0 if job1 has CreationDate greater than job2.
        /// Less than 0 if job1 has CreationDate lesser than job2.
        /// 0 otherwise.
        /// </returns>
        public int Compare(ITradeActivityItem job1, ITradeActivityItem job2)
        {
            //Compares 2 jobs only on the basis of their creation dates.
            if (job1.CreationDate != job2.CreationDate)
            {
                return(job1.CreationDate > job2.CreationDate ? 1 : -1);
            }

            return(0);
        }
Exemple #7
0
        public void SetUp()
        {
            //Create some jobs
            job1 = new MockTradeActivityItem("MTM", DateTime.Today, 1, 2, 3, JobStatus.Queued);
            job2 = new MockTradeActivityItem("MTM", DateTime.Today, 1, 3, 3, JobStatus.Queued);

            //Create the list of jobs
            list = new List <ITradeActivityItem>();
            list.Add(job1);
            list.Add(job2);
        }
Exemple #8
0
        /// <summary>
        /// <para>Compares two <see cref="ITradeActivityItem"/> instances.</para>
        /// <para>
        /// The comparer uses following criteria -
        ///  Null or not-null (Non-null job is greater than null)
        ///  ServiceID (greater job has greater value)
        ///  Status as integer value (greater job has greater value)
        ///  Priority  (greater job has greater value)
        ///  AccountingTreatment ( 'MTM' is greater than 'ACC' )
        ///  CreationDate (greater job has greater value)
        /// If these parameters are not equal it returns result of comparison.
        /// If they are equal next criteria is compared. If all criteria are equal the method returns 0.
        /// </para>
        /// </summary>
        /// <param name="job">job to compare</param>
        /// <param name="otherJob">other job to campare</param>
        /// <returns>
        /// negative number if  job is less than otherJob,
        /// zero if they are equal,
        /// positive number if job is greater than otherJob
        /// </returns>
        public int Compare(ITradeActivityItem job, ITradeActivityItem otherJob)
        {
            //Compare based on null.
            if ((job == null) && (otherJob == null))
            {
                return(EQUAL);
            }
            if ((job != null) && (otherJob == null))
            {
                return(GREATER);
            }
            if ((job == null) && (otherJob != null))
            {
                return(LESS);
            }

            //Compare based on ServiceID
            if (job.ServiceID != otherJob.ServiceID)
            {
                return(job.ServiceID > otherJob.ServiceID ? GREATER : LESS);
            }

            //Compare based on Status
            if (job.Status != otherJob.Status)
            {
                return(job.Status > otherJob.Status ? GREATER : LESS);
            }

            //Compare based on JobPriority
            if (job.JobPriority != otherJob.JobPriority)
            {
                return(job.JobPriority > otherJob.JobPriority ? GREATER : LESS);
            }

            //Any other value of AccountingTreatment except for "MTM" can be taken as "ACC"
            string accountingTreatment1 = MTM.Equals(job.AccountingTreatment) ? MTM : ACC;
            string accountingTreatment2 = MTM.Equals(otherJob.AccountingTreatment) ? MTM : ACC;

            //Compare based on AccountingTreatment
            if (!accountingTreatment1.Equals(accountingTreatment2))
            {
                return(accountingTreatment1.CompareTo(accountingTreatment2));
            }

            //Compare based on CreationDate
            if (job.CreationDate != otherJob.CreationDate)
            {
                return(job.CreationDate > otherJob.CreationDate ? GREATER : LESS);
            }

            return(EQUAL);
        }
Exemple #9
0
            /// <summary>
            /// <para>Compares two <see cref="ITradeActivityItem"/> instances</para>
            /// <para>Compares jobs using prioritizer. If they are same, then compares them as per their QueueIds.
            /// </para>
            /// </summary>
            /// <param name="job">job to compare</param>
            /// <param name="otherJob">other job to compare</param>
            /// <returns>
            /// negative number if job is less than otherJob,
            /// zero if they are equal,
            /// positive number if job is greater than otherJob
            /// </returns>
            public int Compare(ITradeActivityItem job, ITradeActivityItem otherJob)
            {
                //Use prioritizer to compare first
                int comp = prioritizer.Compare(job, otherJob);

                if (comp != 0)
                {
                    return(comp);
                }

                //Comapre the queueIds
                return(job.QueueID.CompareTo(otherJob.QueueID));
            }
Exemple #10
0
        public void TestCopyTo()
        {
            //Add a few items
            jsc.Add(ta1);
            jsc.Add(ta2);
            jsc.Add(ta3);

            ITradeActivityItem[] array = new ITradeActivityItem[6];
            jsc.CopyTo(array, 2);
            Assert.IsNull(array[0], "Copying must be from index 2.");
            Assert.IsNull(array[1], "Copying must be from index 2.");

            //Note the order is ta2, ta3, ta1
            Assert.AreEqual(ta2, array[2], "Incorrect CopyTo implementation.");
            Assert.AreEqual(ta3, array[3], "Incorrect CopyTo implementation.");
            Assert.AreEqual(ta1, array[4], "Incorrect CopyTo implementation.");

            Assert.IsNull(array[5], "Copying must be from index 2.");
        }
Exemple #11
0
        /// <summary>
        /// Adds an <see cref="ITradeActivityItem"/> instance (job) to the given dictionary.
        /// If the key to be found is not found in the dictionary,
        /// then a new entry (<see cref="JobSortedCollection"/> instance) is added to the dictionary and the job is
        /// added to the new <see cref="JobSortedCollection"/> instance.
        /// </summary>
        /// <typeparam name="T">The type of key of the dictionary.</typeparam>
        /// <param name="index">The dictionary in which to add.</param>
        /// <param name="keyToFind">The key which is supposed to be found in the dictionary.</param>
        /// <param name="jobToAdd">The job that needs to be added to the dictionary.</param>
        private static void AddTradeActivityItemToIndex <T>(IDictionary <T, JobSortedCollection> index, T keyToFind,
                                                            ITradeActivityItem jobToAdd)
        {
            //Check if the key is already present in the index
            JobSortedCollection coll;

            index.TryGetValue(keyToFind, out coll);

            if (coll == null)
            {
                //Create new JobSortedCollection
                coll = new JobSortedCollection(new DefaultPrioritizer());

                //Add the new JobSortedCollection to the dictionary
                index[keyToFind] = coll;
            }

            //Add the job to the created or found JobSortedCollection
            coll.Add(jobToAdd);
        }
Exemple #12
0
        /// <summary>
        /// <para>
        /// Event Handler for Job Status Changed event.
        /// The event is raised by the SetStatus methods of <see cref="ITradeActivityItem"/> and is handled
        /// here to maintain the internal indices of the <see cref="JobStatusManager"/>.
        /// </para>
        /// </summary>
        /// <param name="sender">event source</param>
        /// <param name="e">event data</param>
        /// <exception cref="ArgumentNullException">If any parameter is null.</exception>
        /// <exception cref="ArgumentException">
        /// If sender is not of type <see cref="ITradeActivityItem"/>.
        /// </exception>
        protected virtual void HandleStatusChange(object sender, StatusEventArgs e)
        {
            //Validate
            Helper.ValidateNotNull(sender, "sender");
            Helper.ValidateNotNull(e, "e");
            if (!(sender is ITradeActivityItem))
            {
                throw new ArgumentException("sender must be of type ITradeActivityItem.", "sender");
            }

            ITradeActivityItem job = sender as ITradeActivityItem;

            lock (syncRoot)
            {
                //Remove job from status index. Note that removing from other indices is useless.
                if (jobsByStatus.ContainsKey((int)e.OldStatus))
                {
                    jobsByStatus[(int)e.OldStatus].Remove(job);
                }

                //Add job to status index with the new status. Note that adding to other indices is useless.
                AddTradeActivityItemToIndex <int>(jobsByStatus, (int)e.NewStatus, job);
            }
        }
Exemple #13
0
        public void TestDemo()
        {
            //Base operations

            // create JobStatusManager
            JobStatusManager jobManager = new JobStatusManager();

            // add job
            jobManager.AddJob(job1);

            // Remove job
            jobManager.RemoveJob(job1.QueueID);

            // add a list of jobs
            jobManager.AddJobs(list);

            // Remove a list of jobs
            jobManager.RemoveJobs(list);


            //Get data for various criteria

            // get concrete job (QueueID is unique job identifier)
            ITradeActivityItem item1 = jobManager.GetJobByQueueID(2);

            // Get count of jobs with given status
            int jobsCount1 = jobManager.GetJobCountByStatus(JobStatus.Queued);

            // get jobs with given status
            ReadOnlyCollection <ITradeActivityItem> jobs2 = jobManager.GetJobsByStatus(JobStatus.Queued);

            // get jobs for given service
            ReadOnlyCollection <ITradeActivityItem> jobs3 = jobManager.GetJobsByServiceID(3);

            // Get count of jobs with given status for given service
            int jobCount2 = jobManager.GetJobCountByStatusServiceID(JobStatus.Queued, 3);

            // get jobs for given service and with given status
            ReadOnlyCollection <ITradeActivityItem> jobs4 = jobManager.GetJobsByStatusAndServiceID(JobStatus.Queued, 3);

            // get jobs for given service and with JobStatus.Queued status
            ReadOnlyCollection <ITradeActivityItem> jobs5 = jobManager.GetJobsForDispatch(3);

            // we can get exclusive access to JobStatusManager to execute not thread safe operations
            lock (jobManager.SyncRoot)
            {
                foreach (ITradeActivityItem job in jobs4)
                {
                    Console.WriteLine(job.QueueID);
                }
            }


            //Monitor jobs for given criteria
            //The application can get read only wrapper for given condition once and uses it many times.
            //The wrapper provides read only access to collection of jobs for given criteria.

            // get jobs for dispatch once
            ReadOnlyCollection <ITradeActivityItem> jobs = jobManager.GetJobsForDispatch(3);

            // call by timer for example
            foreach (ITradeActivityItem job in jobs)
            {
                // process job
            }
        }
Exemple #14
0
 public void TearDown()
 {
     job1 = null;
     job2 = null;
     list = null;
 }