private ValueApiModel CreateDeploymentStorageData(int idx) { // Arrange var deviceGroupId = "dvcGroupId"; var deviceGroupName = "dvcGroupName"; var deviceGroupQuery = "dvcGroupQuery"; var packageName = "packageName"; var deploymentName = "deployment"; var priority = 10; var userid = "testUser"; var deployment = new DeploymentServiceModel() { Name = deploymentName + idx, DeviceGroupId = deviceGroupId, DeviceGroupName = deviceGroupName, DeviceGroupQuery = deviceGroupQuery, PackageContent = TestEdgePackageJson, PackageName = packageName, Priority = priority, CreatedBy = userid, CreatedDateTimeUtc = DateTime.UtcNow, CreatedDateTime = DateTime.UtcNow, DeploymentMetrics = new DeploymentMetricsServiceModel() { DeviceStatuses = new Dictionary <string, DeploymentStatus> { { "device1", DeploymentStatus.Pending } } }, }; var jsonValue = JsonConvert.SerializeObject(deployment); ValueApiModel value = new ValueApiModel() { Key = idx.ToString(), Data = jsonValue }; return(value); }
private void ThereIsAnEnabledSimulationInTheStorage() { var simulation = new SimulationModel { Id = SIMULATION_ID, Created = DateTimeOffset.UtcNow.Subtract(TimeSpan.FromDays(10)), Modified = DateTimeOffset.UtcNow.Subtract(TimeSpan.FromDays(10)), ETag = "ETag0", Enabled = true }; var list = new ValueListApiModel(); var value = new ValueApiModel { Key = SIMULATION_ID, Data = JsonConvert.SerializeObject(simulation), ETag = simulation.ETag }; list.Items.Add(value); this.mockStorageAdapterClient.Setup(x => x.GetAllAsync(STORAGE_COLLECTION)).ReturnsAsync(list); }
public async Task <bool?> TryLockAsync() { if (this.lastETag != null) { throw new ResourceOutOfDateException("Lock has already been acquired"); } ValueApiModel model = null; try { model = await this.client.GetAsync(this.collectionId, this.key); } catch (ResourceNotFoundException) { // No need to log here since this exception is being logged on DeviceProperties.cs } if (!this.testLockFunc(model)) { return(false); } this.lastValue = model == null ? new T() : JsonConvert.DeserializeObject <T>(model.Data); this.setLockFlagAction(this.lastValue, true); try { this.lastETag = await this.UpdateValueAsync(this.lastValue, model?.ETag); } catch (ConflictingResourceException) { return(null); } return(true); }
#pragma warning restore SA1122 public async Task PackageTagsCanBeRemoved(string tag, int expectedTagCount) { // Arrange var value = new ValueApiModel { Key = this.packageId, Data = JsonConvert.SerializeObject(new PackageServiceModel { Tags = this.tags.ToList() }) }; this.mockClient.Setup(m => m.GetAsync(Storage.PackagesCollectionId, this.packageId)).ReturnsAsync(value); this.mockClient.Setup(m => m.UpdateAsync(Storage.PackagesCollectionId, this.packageId, It.IsAny <string>(), It.IsAny <string>())) .ReturnsAsync((string c, string id, string v, string e) => { return(new ValueApiModel { Key = id, Data = v }); }); // Act var result = await this.storage.RemovePackageTagAsync(this.packageId, tag); // Assert Assert.Equal(expectedTagCount, result.Tags.Count); }
/// <summary> /// A function to decide whether or not cache needs to be rebuilt based on force flag and existing /// cache's validity /// </summary> /// <param name="force">A boolean flag to decide if cache needs to be rebuilt.</param> /// <param name="valueApiModel">An existing valueApiModel to check whether or not cache /// has expired</param> private bool ShouldCacheRebuild(bool force, ValueApiModel valueApiModel) { if (force) { this.log.Info("Cache will be rebuilt due to the force flag", () => { }); return(true); } if (valueApiModel == null) { this.log.Info("Cache will be rebuilt since no cache was found", () => { }); return(true); } DevicePropertyServiceModel cacheValue = new DevicePropertyServiceModel(); DateTimeOffset timstamp = new DateTimeOffset(); try { cacheValue = JsonConvert.DeserializeObject <DevicePropertyServiceModel>(valueApiModel.Data); timstamp = DateTimeOffset.Parse(valueApiModel.Metadata["$modified"]); } catch { this.log.Info("DeviceProperties will be rebuilt because the last one is broken.", () => { }); return(true); } if (cacheValue.Rebuilding) { if (timstamp.AddSeconds(this.rebuildTimeout) < DateTimeOffset.UtcNow) { this.log.Debug("Cache will be rebuilt because last rebuilding had timedout", () => { }); return(true); } else { this.log.Debug ("Cache rebuilding skipped because it is being rebuilt by other instance", () => { }); return(false); } } else { if (cacheValue.IsNullOrEmpty()) { this.log.Info("Cache will be rebuilt since it is empty", () => { }); return(true); } if (timstamp.AddSeconds(this.ttl) < DateTimeOffset.UtcNow) { this.log.Info("Cache will be rebuilt because it has expired", () => { }); return(true); } else { this.log.Debug("Cache rebuilding skipped because it has not expired", () => { }); return(false); } } }
/// <summary> /// Update Cache when devices are modified/created /// </summary> public async Task <DevicePropertyServiceModel> UpdateListAsync( DevicePropertyServiceModel deviceProperties) { // To simplify code, use empty set to replace null set deviceProperties.Tags = deviceProperties.Tags ?? new HashSet <string>(); deviceProperties.Reported = deviceProperties.Reported ?? new HashSet <string>(); string etag = null; while (true) { ValueApiModel model = null; try { model = await this.storageClient.GetAsync(CACHE_COLLECTION_ID, CACHE_KEY); } catch (ResourceNotFoundException) { this.log.Info($"Cache updating: cache {CACHE_COLLECTION_ID}:{CACHE_KEY} was not found", () => { }); } if (model != null) { DevicePropertyServiceModel devicePropertiesFromStorage; try { devicePropertiesFromStorage = JsonConvert. DeserializeObject <DevicePropertyServiceModel>(model.Data); } catch { devicePropertiesFromStorage = new DevicePropertyServiceModel(); } devicePropertiesFromStorage.Tags = devicePropertiesFromStorage.Tags ?? new HashSet <string>(); devicePropertiesFromStorage.Reported = devicePropertiesFromStorage.Reported ?? new HashSet <string>(); deviceProperties.Tags.UnionWith(devicePropertiesFromStorage.Tags); deviceProperties.Reported.UnionWith(devicePropertiesFromStorage.Reported); etag = model.ETag; // If the new set of deviceProperties are already there in cache, return if (deviceProperties.Tags.Count == devicePropertiesFromStorage.Tags.Count && deviceProperties.Reported.Count == devicePropertiesFromStorage.Reported.Count) { return(deviceProperties); } } var value = JsonConvert.SerializeObject(deviceProperties); try { var response = await this.storageClient.UpdateAsync( CACHE_COLLECTION_ID, CACHE_KEY, value, etag); return(JsonConvert.DeserializeObject <DevicePropertyServiceModel>(response.Data)); } catch (ConflictingResourceException) { this.log.Info("Cache updating: failed due to conflict. Retry soon", () => { }); } catch (Exception e) { this.log.Info("Cache updating: failed", () => e); throw new Exception("Cache updating: failed"); } } }
private TwinServiceModel CreateTwinServiceModel(ValueApiModel input) { TwinServiceModel output = JsonConvert.DeserializeObject <TwinServiceModel>(input.Data); return(output); }