コード例 #1
0
ファイル: ServiceProxyTest.cs プロジェクト: nlcamp/iotedge
        public async Task IteratorTest()
        {
            // Arrange
            IEnumerable <Device> devices1 = new[] { GetDevice("d1"), GetDevice("d2"), GetDevice("d3") };
            IEnumerable <Module> modules1 = null;
            string continuationToken1     = "/devices/d301/modules/%24edgeHub/devicesAndModulesInDeviceScope?deviceCount=10&continuationToken=cccccDDDDDRRRRRssssw&api-version=2018-08-30-preview";
            var    scopeResult1           = new ScopeResult(devices1, modules1, continuationToken1);

            IEnumerable <Device> devices2 = new[] { GetDevice("d4"), GetDevice("d5") };
            IEnumerable <Module> modules2 = new[] { GetModule("d10", "m1") };
            string continuationToken2     = "/devices/d301/modules/%24edgeHub/devicesAndModulesInDeviceScope?deviceCount=10&continuationToken=cccccbbbbRRRRRssssw&api-version=2018-08-30-preview";
            var    scopeResult2           = new ScopeResult(devices2, modules2, continuationToken2);

            IEnumerable <Device> devices3 = null;
            IEnumerable <Module> modules3 = new[] { GetModule("d11", "m1"), GetModule("d11", "m2"), GetModule("d12", "m2") };
            string continuationToken3     = null;
            var    scopeResult3           = new ScopeResult(devices3, modules3, continuationToken3);

            var deviceScopeApiResult = new Mock <IDeviceScopeApiClient>();

            deviceScopeApiResult.Setup(d => d.GetIdentitiesInScopeAsync())
            .ReturnsAsync(scopeResult1);
            deviceScopeApiResult.SetupSequence(d => d.GetNextAsync(It.IsAny <string>()))
            .ReturnsAsync(scopeResult2)
            .ReturnsAsync(scopeResult3);

            var deviceScopeApiClientProvider = new Mock <IDeviceScopeApiClientProvider>();

            deviceScopeApiClientProvider.Setup(p => p.CreateDeviceScopeClient()).Returns(deviceScopeApiResult.Object);

            IServiceProxy serviceProxy = new ServiceProxy(deviceScopeApiClientProvider.Object, false);

            // Act / Assert
            IServiceIdentitiesIterator iterator = serviceProxy.GetServiceIdentitiesIterator();

            Assert.NotNull(iterator);
            Assert.True(iterator.HasNext);

            IEnumerable <ServiceIdentity> serviceIdentities = await iterator.GetNext();

            Assert.NotNull(serviceIdentities);
            Assert.True(Compare(serviceIdentities, scopeResult1));
            Assert.True(iterator.HasNext);

            serviceIdentities = await iterator.GetNext();

            Assert.NotNull(serviceIdentities);
            Assert.True(Compare(serviceIdentities, scopeResult2));
            Assert.True(iterator.HasNext);

            serviceIdentities = await iterator.GetNext();

            Assert.NotNull(serviceIdentities);
            Assert.True(Compare(serviceIdentities, scopeResult3));
            Assert.False(iterator.HasNext);

            serviceIdentities = await iterator.GetNext();

            Assert.Empty(serviceIdentities);
        }
コード例 #2
0
        async Task RefreshCache()
        {
            while (true)
            {
                var currentCacheIds = new List <string>();

                try
                {
                    Events.StartingRefreshCycle();
                    IServiceIdentitiesIterator iterator = this.serviceProxy.GetServiceIdentitiesIterator();
                    while (iterator.HasNext)
                    {
                        IEnumerable <ServiceIdentity> batch = await iterator.GetNext();

                        foreach (ServiceIdentity serviceIdentity in batch)
                        {
                            try
                            {
                                await this.HandleNewServiceIdentity(serviceIdentity);

                                currentCacheIds.Add(serviceIdentity.Id);
                            }
                            catch (Exception e)
                            {
                                Events.ErrorProcessing(serviceIdentity, e);
                            }
                        }
                    }

                    // Diff and update
                    IList <string> allIds = await this.serviceIdentityHierarchy.GetAllIds();

                    IList <string> removedIds = allIds.Except(currentCacheIds).ToList();
                    await Task.WhenAll(removedIds.Select(id => this.HandleNoServiceIdentity(id)));
                }
                catch (Exception e)
                {
                    Events.ErrorInRefreshCycle(e);
                }

                Events.DoneRefreshCycle(this.periodicRefreshRate);
                this.ServiceIdentitiesUpdated?.Invoke(this, await this.serviceIdentityHierarchy.GetAllIds());

                lock (this.refreshCacheLock)
                {
                    // Send the completion signal first, then reset the
                    // refresh signal to signify that we're no longer
                    // doing any work on this thread
                    this.refreshCacheCompleteSignal.Set();
                    this.refreshCacheSignal.Reset();
                }

                await this.IsReady();
            }
        }
コード例 #3
0
        async Task RefreshCache()
        {
            while (true)
            {
                try
                {
                    Events.StartingRefreshCycle();
                    var currentCacheIds = new List <string>();
                    IServiceIdentitiesIterator iterator = this.serviceProxy.GetServiceIdentitiesIterator();
                    while (iterator.HasNext)
                    {
                        IEnumerable <ServiceIdentity> batch = await iterator.GetNext();

                        foreach (ServiceIdentity serviceIdentity in batch)
                        {
                            try
                            {
                                await this.HandleNewServiceIdentity(serviceIdentity);

                                currentCacheIds.Add(serviceIdentity.Id);
                            }
                            catch (Exception e)
                            {
                                Events.ErrorProcessing(serviceIdentity, e);
                            }
                        }
                    }

                    // Diff and update
                    List <string> removedIds = this.serviceIdentityCache
                                               .Where(kvp => kvp.Value.ServiceIdentity.HasValue)
                                               .Select(kvp => kvp.Key)
                                               .Except(currentCacheIds).ToList();
                    await Task.WhenAll(removedIds.Select(id => this.HandleNoServiceIdentity(id)));
                }
                catch (Exception e)
                {
                    Events.ErrorInRefreshCycle(e);
                }

                Events.DoneRefreshCycle(this.refreshRate);
                await this.IsReady();
            }
        }
コード例 #4
0
        async Task <bool> RefreshCacheInternal()
        {
            try
            {
                Events.StartingRefreshCycle();
                var currentCacheIds = new List <string>();
                IServiceIdentitiesIterator iterator = this.serviceProxy.GetServiceIdentitiesIterator();
                while (iterator.HasNext)
                {
                    IEnumerable <ServiceIdentity> batch = await iterator.GetNext();

                    foreach (ServiceIdentity serviceIdentity in batch)
                    {
                        try
                        {
                            await this.HandleNewServiceIdentity(serviceIdentity);

                            currentCacheIds.Add(serviceIdentity.Id);
                        }
                        catch (Exception e)
                        {
                            Events.ErrorProcessing(serviceIdentity, e);
                        }
                    }
                }

                // Diff and update
                IList <string> allIds = await this.serviceIdentityHierarchy.GetAllIds();

                IList <string> removedIds = allIds.Except(currentCacheIds).ToList();
                await Task.WhenAll(removedIds.Select(id => this.HandleNoServiceIdentity(id)));

                return(true);
            }
            catch (Exception e)
            {
                Events.ErrorInRefreshCycle(e);
                return(false);
            }
        }