Esempio n. 1
0
        public async Task ServiceShouldRunMaxConcurrentCountTaskWhenExistInQueue()
        {
            CancellationTokenSource tokenSource  = new CancellationTokenSource();
            BackgroundQueue         queue        = new BackgroundQueue((ex) => { throw ex; }, 10, 10);;
            BackgroundQueueService  queueService = new BackgroundQueueService(queue);
            int highwaterMark = 0;

            for (int i = 0; i < 20; i++)
            {
                queue.Enqueue(async(ct) =>
                {
                    highwaterMark = Math.Max(queue.ConcurrentCount, highwaterMark);
                    await Task.Delay(5);
                });
            }

            var runningService = Task.Run(async() => await queueService.StartAsync(tokenSource.Token));

            while (queue.Count > 0)
            {
                await Task.Delay(20);
            }

            highwaterMark.Should().BeGreaterThan(1);
        }
        public void TestPushAndPopToBackgroundQueue()
        {
            var platformServicesFake = A.Fake <IPlatformServices>();

            Device.PlatformServices = platformServicesFake;

            DatabaseMigrator.Migrate(_connection, _migrationSkriptFolderPath);

            var testSpotGuid = Guid.NewGuid();
            var testLat      = 48.45;
            var testLng      = 13.9167;

            var result = BackgroundQueueService.PushWheaterRequestToBackgroundQueue(_connection, testSpotGuid, testLat, testLng).Result;

            Assert.True(result != Guid.Empty);

            var testSpotGuid2 = Guid.NewGuid();
            var testLat2      = 48.45;
            var testLng2      = 13.9167;

            var result2 = BackgroundQueueService.PushWheaterRequestToBackgroundQueue(_connection, testSpotGuid2, testLat2, testLng2).Result;

            Assert.True(result != Guid.Empty);


            var model = BackgroundQueueService.PopWheaterRequestFromBackgroundQueue(_connection).Result;

            Assert.Equal(model.ID_ElementReference.ToString(), testSpotGuid2.ToString());

            var model2 = BackgroundQueueService.PopWheaterRequestFromBackgroundQueue(_connection, false).Result;

            Assert.Equal(model2.ID_ElementReference.ToString(), testSpotGuid.ToString());
        }
        public void TestRemoveFromBackgroundQueue()
        {
            var platformServicesFake = A.Fake <IPlatformServices>();

            Device.PlatformServices = platformServicesFake;

            DatabaseMigrator.Migrate(_connection, _migrationSkriptFolderPath);

            var deleteAll = BackgroundQueueService.EmptyQueue(_connection).Result;

            Assert.True(deleteAll);

            var testSpotGuid = Guid.NewGuid();
            var testLat      = 48.45;
            var testLng      = 13.9167;

            var result = BackgroundQueueService.PushWheaterRequestToBackgroundQueue(_connection, testSpotGuid, testLat, testLng).Result;

            Assert.True(result != Guid.Empty);

            var model = BackgroundQueueService.PopWheaterRequestFromBackgroundQueue(_connection).Result;

            bool res = BackgroundQueueService.RemoveElementFromQueue(_connection, model).Result;

            Assert.True(res);

            model = BackgroundQueueService.PopWheaterRequestFromBackgroundQueue(_connection).Result;

            Assert.Null(model);
        }
        public void TestPushIntoBackgroundQueue()
        {
            var platformServicesFake = A.Fake <IPlatformServices>();

            Device.PlatformServices = platformServicesFake;

            DatabaseMigrator.Migrate(_connection, _migrationSkriptFolderPath);

            var testSpotGuid = Guid.NewGuid();
            var testLat      = 48.45;
            var testLng      = 13.9167;

            var result = BackgroundQueueService.PushWheaterRequestToBackgroundQueue(_connection, testSpotGuid, testLat, testLng).Result;

            Assert.True(result != Guid.Empty);
        }
        public async void TestRunBackgroundWorker()
        {
            var platformServicesFake = A.Fake <IPlatformServices>();

            Device.PlatformServices = platformServicesFake;

            DatabaseMigrator.Migrate(_connection, _migrationSkriptFolderPath);


            MessagingCenter.Subscribe <StartBackgroundWorkingServiceMessage>(this, MessageHelper.START_BACKGROUND_WORKING_SERVICE_MESSAGE, message =>
            {
                var msg = new DiagnosticMessage("Start Background Working Service");
                output.WriteLine("{0}", msg.Message);
            });

            MessagingCenter.Subscribe <StopBackgroundWorkingServiceMessage>(this, MessageHelper.STOP_BACKGROUND_WORKING_SERVICE_MESSAGE, message =>
            {
                var msg = new DiagnosticMessage("Stop Background Working Service");
                output.WriteLine("{0}", msg.Message);
            });

            MessagingCenter.Subscribe <ElementFinishedMessage>(this, MessageHelper.ELEMENT_FINISHED_MESSAGE, message =>
            {
                var msg = new DiagnosticMessage("Element processed");
                output.WriteLine("{0}", msg.Message);
            });

            var deleteAll = BackgroundQueueService.EmptyQueue(_connection).Result;

            Assert.True(deleteAll);

            var testSpotGuid = Guid.NewGuid();
            var testLat      = 48.45;
            var testLng      = 13.9167;

            var result = BackgroundQueueService.PushWheaterRequestToBackgroundQueue(_connection, testSpotGuid, testLat, testLng).Result;

            Assert.True(result != Guid.Empty);

            await BackgroundWorkerService.RunBackgroundWorkerService(_connection, new CancellationToken(false));

            int cnt = BackgroundQueueService.GetQueueElementCount(_connection).Result;

            Assert.Equal(0, cnt);
        }