예제 #1
0
        private async Task ProcessQueueMessage(CloudQueueMessage msg)
        {
            Trace.TraceInformation("Processing queue message {0}", msg);

            var workPackage = JsonConvert.DeserializeObject <PrimeNumberWorkPackage>(msg.AsString);

            if (workPackage.WorkType == WorkType.NewWork)
            {
                _numItems     = workPackage.NumEntries;
                _numProcessed = 0;
                this._primesQueue.DeleteMessage(msg);
                return;
            }

            var numberToTest = workPackage.Number;
            var isPrime      = await Task.Run(() => PrimeSolver.IsPrime(numberToTest));

            var primeNumberCandidate = new PrimeNumberCandidate(numberToTest)
            {
                IsPrime = isPrime
            };
            await _repository.Add(primeNumberCandidate);

            _numProcessed++;
            await CommunicateResult(primeNumberCandidate);

            //var percent = (int)Math.Round((double)(100 * _numProcessed) / _numItems);
            await CommunicateProgress();

            // Remove message from queue.
            this._primesQueue.DeleteMessage(msg);
        }
 public static PrimeCandidateViewModel FromEntity(PrimeNumberCandidate item)
 {
     return(new PrimeCandidateViewModel
     {
         IsPrime = item.IsPrime,
         Number = item.Number
     });
 }
        public void GetIsPrimeNumberTest_PrimeNumber()
        {
            // Arrange
            var pn = new PrimeNumberCandidate(149);
            // Act
            var rc = pn.GetIsPrimeNumber();

            // Assert
            Assert.IsTrue(rc);
        }
        public void GetIsPrimeNumberTestNotPrimeNumber()
        {
            // Arrange
            var pn = new PrimeNumberCandidate(9);
            // Act
            var rc = pn.GetIsPrimeNumber();

            // Assert
            Assert.IsFalse(rc);
        }
        public void PrimeNumberCandidateTest()
        {
            // Arrange
            var pn = new PrimeNumberCandidate(143);
            // Act
            var rc = pn == null;

            // Assert
            Assert.IsFalse(rc);
        }
예제 #6
0
        /// <summary>
        /// Send result to client endpoint
        /// </summary>
        /// <param name="primeCandidate"></param>
        /// <remarks>Thanks to http://www.jerriepelser.com/blog/communicate-from-azure-webjob-with-signalr for the sample</remarks>
        /// <returns></returns>
        private async Task CommunicateResult(PrimeNumberCandidate primeCandidate)
        {
            if (!primeCandidate.IsPrime.HasValue || !primeCandidate.IsPrime.Value)
            {
                return;
            }

            var queryString = $"?number={primeCandidate.Number}&isPrime={primeCandidate.IsPrime}";
            //var request = CloudConfigurationManager.GetSetting("ProgressNotificationEndpoint");
            var request = BaseUrl + "/PrimeNumberCandidate/ResultNotification" + queryString;
            await _httpClient.GetAsync(request);
        }
        public void DisposeTest()
        {
            // Arrange
            var isOk = true;
            var pn   = new PrimeNumberCandidate(149);

            // Act
            try
            {
                // Act
                pn.Dispose();
            }
            catch
            {
                isOk = false;
            }
            // Assert
            Assert.IsTrue(isOk);
        }