Exemplo n.º 1
0
        public async Task WaitForChange()
        {
            const string newValue = "foo";
            string       key      = _fixture.GenerateCustomKey();

            await _fixture.UpdateMetadataAsync($"instance/attributes/{key}", "initial value");

            var task = Task.Run(async() =>
            {
                await Task.Delay(TimeSpan.FromSeconds(2));
                await _fixture.UpdateMetadataAsync($"instance/attributes/{key}", newValue);
            });

            // TODO: This pattern to get the original ETag seems messy. Maybe add something to get an ETag.

            // Snippet: WaitForChange
            MetadataClient client = MetadataClient.Create();

            // Get the original ETag so we know when the metadata changes.
            string lastETag = client.WaitForChange("instance/attributes", timeout: TimeSpan.FromMilliseconds(1)).ETag;

            WaitForChangeResult result;

            while (true)
            {
                // Wait in 30 second blocks until the value actually changes.
                result = client.WaitForChange("instance/attributes", lastETag, TimeSpan.FromSeconds(30));
                if (result.ETag != lastETag)
                {
                    JObject currentCustomMetadata = JObject.Parse(result.Content);
                    foreach (JProperty customMetadata in currentCustomMetadata.Properties())
                    {
                        Console.WriteLine(customMetadata.Name + " = " + customMetadata.Value);
                    }
                    break;
                }
            }
            // End snippet

            dynamic obj = JsonConvert.DeserializeObject(result.Content);

            Assert.Equal(newValue, obj[key].Value);
            await task;
        }
        public async Task WaitForChange()
        {
            const string newValue = "foo";
            string       key      = _fixture.GenerateCustomKey();

            await _fixture.UpdateMetadataAsync($"instance/attributes/{key}", "initial value");

            var task = Task.Run(async() =>
            {
                await Task.Delay(TimeSpan.FromSeconds(2));
                await _fixture.UpdateMetadataAsync($"instance/attributes/{key}", newValue);
            });

            // Snippet: WaitForChange
            MetadataClient client = MetadataClient.Create();

            // Get the original metadata and its ETag so we know when the metadata changes.
            MetadataResult initialResult = client.GetMetadata("instance/attributes");

            Console.WriteLine("Initial Metadata:");
            JObject initialMetadata = JObject.Parse(initialResult.Content);

            foreach (JProperty property in initialMetadata.Properties())
            {
                Console.WriteLine(property.Name + " = " + property.Value);
            }
            string lastETag = initialResult.ETag;

            MetadataResult changeResult;

            while (true)
            {
                // Wait in 30 second blocks until the value actually changes.
                changeResult = client.WaitForChange("instance/attributes", lastETag, TimeSpan.FromSeconds(30));
                if (changeResult.ETag != lastETag)
                {
                    Console.WriteLine("Changed Metadata:");
                    JObject currentMetadata = JObject.Parse(changeResult.Content);
                    foreach (JProperty property in currentMetadata.Properties())
                    {
                        Console.WriteLine(property.Name + " = " + property.Value);
                    }
                    break;
                }
            }
            // End snippet

            dynamic obj = JsonConvert.DeserializeObject(changeResult.Content);

            Assert.Equal(newValue, obj[key].Value);
            await task;
        }