public void TestSourceCaching() { var src = new CachedResolver(new MultiResolver(ZipSource.CreateValidationSource(), new WebResolver())); Stopwatch sw1 = new Stopwatch(); // Ensure looking up a failed endpoint repeatedly does not cost much time sw1.Start(); src.ResolveByUri("http://some.none.existant.address.nl/fhir/StructureDefinition/bla"); sw1.Stop(); var sw2 = new Stopwatch(); sw2.Start(); src.ResolveByUri("http://some.none.existant.address.nl/fhir/StructureDefinition/bla"); sw2.Stop(); Debug.WriteLine("sw2 {0}, sw1 {1}", sw2.ElapsedMilliseconds, sw1.ElapsedMilliseconds); Assert.IsTrue(sw2.ElapsedMilliseconds <= sw1.ElapsedMilliseconds && sw2.ElapsedMilliseconds < 100); // Now try an existing artifact sw1.Restart(); src.ResolveByUri("http://hl7.org/fhir/ValueSet/v2-0292"); sw1.Stop(); sw2.Restart(); src.ResolveByUri("http://hl7.org/fhir/ValueSet/v2-0292"); sw2.Stop(); Assert.IsTrue(sw2.ElapsedMilliseconds < sw1.ElapsedMilliseconds && sw2.ElapsedMilliseconds < 100); }
public void SaveStructureDefinition(StructureDefinition sd) { // Ensure the folder exists Uri t = new Uri(sd.Url); string profileOutputDirectory = Path.Combine(_outputProfilePath, t.Host); if (!Directory.Exists(profileOutputDirectory)) { Directory.CreateDirectory(profileOutputDirectory); } // Non extensions will just go in the root of the output if (sd.BaseDefinition != "http://hl7.org/fhir/StructureDefinition/Extension") { profileOutputDirectory = _outputProfilePath; } // Now output the file System.IO.File.WriteAllText($"{profileOutputDirectory}/StructureDefinition-{sd.Id}.xml", new FhirXmlSerializer(new SerializerSettings() { AppendNewLine = true, Pretty = true }).SerializeToString(sd)); // And add it to our resolver sourceSD.InvalidateByCanonicalUri(sd.Url); ds.Refresh(); // And check that it comes back... var instSD = sourceSD.ResolveByUri(sd.Url) as StructureDefinition; if (instSD == null) { Console.WriteLine($"Was not able to resolve the newly created extension"); } }
public void TestCacheInvalidation() { var src = new CachedResolver(new MultiResolver(ZipSource.CreateValidationSource())); CachedResolver.LoadResourceEventArgs eventArgs = null; CachedResolver.LoadResourceEventHandler handler = (sender, args) => { eventArgs = args; }; src.Load += handler; // Verify that the Load event is fired on the initial load const string resourceUri = "http://hl7.org/fhir/ValueSet/v2-0292"; var resource = src.ResolveByUri(resourceUri); Assert.IsNotNull(eventArgs); Assert.AreEqual(resourceUri, eventArgs.Url); Assert.AreEqual(resource, eventArgs.Resource); // Verify that the Load event is not fired on subsequent load eventArgs = null; resource = src.ResolveByUri(resourceUri); Assert.IsNull(eventArgs); // Verify that we can remove the cache entry var result = src.InvalidateByUri(resourceUri); Assert.IsTrue(result); // Verify that the cache entry has been removed result = src.InvalidateByUri(resourceUri); Assert.IsFalse(result); // Verify that the Load event is fired again on the next load var resource2 = src.ResolveByUri(resourceUri); Assert.IsNotNull(eventArgs); Assert.AreEqual(resourceUri, eventArgs.Url); Assert.AreEqual(resource2, eventArgs.Resource); // Verify that the cache returned a new instance with exact same value Assert.AreNotEqual(resource2.GetHashCode(), resource.GetHashCode()); Assert.IsTrue(resource.IsExactly(resource2)); }