public async void RunSnapshotMultiThreaded()
        {
            // Arrange
            var source    = new CachedResolver(new ZipSource("specification.zip"));
            var generator = new SnapshotGenerator(source);

            OperationOutcome GenerateSnapshot(StructureDefinition sd)
            {
                generator.Update(sd);
                System.Diagnostics.Debug.WriteLine(sd.HasSnapshot);
                return(generator.Outcome ?? new OperationOutcome());
            }

            var nrOfParrallelTasks = 100;
            var results            = new ConcurrentBag <OperationOutcome>();
            var buffer             = new BufferBlock <StructureDefinition>();
            var processor          = new ActionBlock <StructureDefinition>(sd =>
            {
                // Act
                var outcome = ProfilePreprocessor.GenerateSnapshots(new[] { sd }, GenerateSnapshot, "unittest");
                results.Add(outcome);
            }
                                                                           ,
                                                                           new ExecutionDataflowBlockOptions
            {
                MaxDegreeOfParallelism = 100
            });

            buffer.LinkTo(processor, new DataflowLinkOptions {
                PropagateCompletion = true
            });

            var patientSD = source.ResolveByCanonicalUri("http://hl7.org/fhir/StructureDefinition/Patient") as StructureDefinition;

            // Clear snapshots after initial load
            // This will force the validator to regenerate all snapshots
            if (patientSD.HasSnapshot)
            {
                patientSD.Snapshot = null;
            }

            for (int i = 0; i < nrOfParrallelTasks; i++)
            {
                buffer.Post(patientSD);
            }
            buffer.Complete();
            await processor.Completion;

            // Assert
            var nrOfSuccess = results.Count(r => r.Success);

            Assert.Equal(nrOfParrallelTasks, nrOfSuccess);
        }
Пример #2
0
        public StructureDefinition CreateProfileWithAllMinZero(string canonicalUri, string newBase)
        {
            var realSD = sourceSD.ResolveByCanonicalUri(canonicalUri) as StructureDefinition;
            var newSD  = realSD.DeepCopy() as StructureDefinition;
            Uri t      = new Uri(canonicalUri);

            newSD.Url       = $"{newBase}StructureDefinition/{t.Segments.Last()}";
            newSD.Text      = null;
            newSD.Version   = null;
            newSD.Publisher = _publisher;
            newSD.Status    = PublicationStatus.Draft;
            newSD.Contact   = null;
            newSD.Mapping.Clear();
            newSD.Extension.Clear();
            newSD.Snapshot       = null;
            newSD.BaseDefinition = canonicalUri;
            newSD.Derivation     = StructureDefinition.TypeDerivationRule.Constraint;
            foreach (var e in newSD.Differential.Element)
            {
                MinimizeElementCardinality(newSD, e);
            }
            return(newSD);
        }
Пример #3
0
        public void TestCacheLoadingStrategy()
        {
            const string resourceUri = "http://hl7.org/fhir/ValueSet/v2-0292";

            // Create empty in-memory resolver
            var mem   = new InMemoryProfileResolver();
            var cache = new CachedResolver(mem);

            // Load on demand should return null
            var resource = cache.ResolveByCanonicalUri(resourceUri);

            Assert.IsNull(resource);

            // Resolve core resource from ZIP and refresh in-memory resolver
            var zipSource = ZipSource.CreateValidationSource();
            var original  = zipSource.ResolveByUri(resourceUri) as ValueSet;

            Assert.IsNotNull(original);
            mem.Reload(original);

            // Load on demand should still return null
            resource = cache.ResolveByCanonicalUri(resourceUri);
            Assert.IsNull(resource);

            // Invalidate the cache, delete existing cache entry
            cache.InvalidateByCanonicalUri(resourceUri);

            // Load from cache should still return null
            resource = cache.ResolveByCanonicalUri(resourceUri, CachedResolverLoadingStrategy.LoadFromCache);
            Assert.IsNull(resource);

            // Load on demand should now resolve instance and update cache
            resource = cache.ResolveByCanonicalUri(resourceUri);
            Assert.IsNotNull(resource);
            Assert.AreEqual(original, resource);
            Assert.IsTrue(cache.IsCachedCanonicalUri(resourceUri));

            // Update in-memory resolver with new, modified instance (same url)
            var modified = (ValueSet)original.DeepCopy();

            modified.Name = "MODIFIED";
            mem.Reload(modified);

            // Load on demand should still return the original, unmodified instance from cache
            // As the cache is unaware that the internal source has changed
            resource = cache.ResolveByCanonicalUri(resourceUri);
            Assert.IsNotNull(resource);
            Assert.AreEqual(original, resource);

            // Forced load should update cache and return new, modified instance
            resource = cache.ResolveByCanonicalUri(resourceUri, CachedResolverLoadingStrategy.LoadFromSource);
            Assert.IsNotNull(resource);
            Assert.AreEqual(modified, resource);

            // Clear in-memory resolver; i.e. simulate delete file from disk
            mem.Clear();

            // Load on demand should still return the modified instance from cache
            // As the cache is unaware that the internal source has changed
            resource = cache.ResolveByCanonicalUri(resourceUri);
            Assert.IsNotNull(resource);
            Assert.AreEqual(modified, resource);
            Assert.IsTrue(cache.IsCachedCanonicalUri(resourceUri));

            // Forced load should update cache and now return null
            resource = cache.ResolveByCanonicalUri(resourceUri, CachedResolverLoadingStrategy.LoadFromSource);
            Assert.IsNull(resource);
            Assert.IsFalse(cache.IsCachedCanonicalUri(resourceUri));
        }