コード例 #1
0
        public void EqualsFalseNullPayload()
        {
            var v1 = ContentHashList.Random();
            var v2 = ContentHashList.Random();

            Assert.False(v1.Equals(v2));
        }
コード例 #2
0
        public void EqualsFalseMismatchPayload()
        {
            var v1 = new ContentHashList(ContentHashArray, new byte[] { 0, 1 });
            var v2 = new ContentHashList(ContentHashArray, new byte[] { 0 });

            Assert.False(v1.Equals(v2));
        }
コード例 #3
0
        public void EqualsObjectTrue()
        {
            var v1 = new ContentHashList(ContentHashArray);
            var v2 = new ContentHashList(ContentHashArray) as object;

            Assert.True(v1.Equals(v2));
        }
コード例 #4
0
        public void EqualsObjectFalse()
        {
            var v1 = new ContentHashList(ContentHashArray);
            var v2 = new object();

            Assert.False(v1.Equals(v2));
        }
コード例 #5
0
        public void ConstructWithPayload()
        {
            var contentHashList = new ContentHashList(ContentHashArray, Payload);

            Assert.True(contentHashList.HasPayload);
            Assert.NotNull(contentHashList.Payload);
        }
コード例 #6
0
        public void ConstructWithoutPayload()
        {
            var contentHashList = new ContentHashList(ContentHashArray);

            Assert.False(contentHashList.HasPayload);
            Assert.Null(contentHashList.Payload);
        }
コード例 #7
0
        public void GetHashCodeSameWhenEqual()
        {
            var v1 = new ContentHashList(ContentHashArray);
            var v2 = new ContentHashList(ContentHashArray);

            Assert.Equal(v1.GetHashCode(), v2.GetHashCode());
        }
コード例 #8
0
        public void EqualsFalseNullRightContentHashList()
        {
            var v1 = new GetContentHashListResult(new ContentHashListWithDeterminism(ContentHashList.Random(), CacheDeterminism.None));
            var v2 = new GetContentHashListResult(new ContentHashListWithDeterminism(null, CacheDeterminism.None));

            Assert.False(v1.Equals(v2));
        }
コード例 #9
0
        private async Task BloatDbAsync(Context context, IMemoizationSession session)
        {
            uint dummyFingerprintsToAdd = 40; // generates a ~52KB DB file
            var  addBlock = new ActionBlock <int>(
                async _ =>
            {
                var strongFingerprint = StrongFingerprint.Random();
                var contentHashListWithDeterminism = new ContentHashListWithDeterminism(
                    ContentHashList.Random(), Determinism[DeterminismNone]);

                var result = await session.AddOrGetContentHashListAsync(
                    context, strongFingerprint, contentHashListWithDeterminism, Token);
                Assert.True(result.Succeeded);
            },
                new ExecutionDataflowBlockOptions {
                MaxDegreeOfParallelism = System.Environment.ProcessorCount
            });

            while (--dummyFingerprintsToAdd > 0)
            {
                await addBlock.SendAsync(0);
            }

            addBlock.Complete();
            await addBlock.Completion;
        }
コード例 #10
0
        public void EqualsFalseWithPayload()
        {
            var v1 = new ContentHashList(ContentHashArray, new byte[] { 1, 2, 3 });
            var v2 = new ContentHashList(ContentHashArray, new byte[] { 1, 1, 3 });

            Assert.False(v1.Equals(v2));
        }
コード例 #11
0
        public async Task UpgradeFromBeforeSerializedDeterminism(int oldDeterminism, int shouldBecomeDeterminism)
        {
            var context           = new Context(Logger);
            var strongFingerprint = StrongFingerprint.Random();
            var contentHashListWithDeterminism = new ContentHashListWithDeterminism(
                ContentHashList.Random(), Determinism[oldDeterminism]);

            using (var testDirectory = new DisposableDirectory(FileSystem))
            {
                await RunTestAsync(context, testDirectory, async (store, session) =>
                {
                    var result = await session.AddOrGetContentHashListAsync(
                        context, strongFingerprint, contentHashListWithDeterminism, Token);
                    Assert.True(result.Succeeded);

                    await((TestSQLiteMemoizationStore)store).DeleteColumnAsync("ContentHashLists", "SerializedDeterminism");
                });

                await RunTestAsync(context, testDirectory, async (store, session) =>
                {
                    var result = await session.GetContentHashListAsync(context, strongFingerprint, Token);
                    Assert.Equal(
                        new GetContentHashListResult(new ContentHashListWithDeterminism(
                                                         contentHashListWithDeterminism.ContentHashList, Determinism[shouldBecomeDeterminism])), result);
                });
            }
        }
コード例 #12
0
        public async Task FalseOnNullContentSession()
        {
            var context         = new Context(Logger);
            var contentHashList = new ContentHashList(new ContentHash[] {});

            (await((IContentSession)null).EnsureContentIsAvailableAsync(context, Name, contentHashList, Token)).Should().BeFalse();
        }
コード例 #13
0
 public void ContentHashListWithDeterminismRoundTrip(int contentHashCount, int determinism)
 {
     ContentHashListWithDeterminism contentHashListWithDeterminism = new ContentHashListWithDeterminism(
         ContentHashList.Random(HashingType, contentHashCount),
         m_memoizationDeterminism[determinism]);
     ContentHashListWithDeterminism roundTrip = contentHashListWithDeterminism.FromMemoization().ToMemoization();
 }
コード例 #14
0
ファイル: GrpcDataConverter.cs プロジェクト: kittinap/kunnjae
        /// <nodoc />
        public static ContentHashListWithDeterminism FromGrpc(this ContentHashListWithDeterminismData input)
        {
            ContentHashList  contentHashList = input.ContentHashList.FromGrpc();
            CacheDeterminism determinism     = FromGrpc(input.CacheDeterminism);

            return(new ContentHashListWithDeterminism(contentHashList, determinism));
        }
コード例 #15
0
        public void GetHashCodeNotSameWhenNotEqual()
        {
            var v1 = ContentHashList.Random();
            var v2 = ContentHashList.Random();

            Assert.NotEqual(v1.GetHashCode(), v2.GetHashCode());
        }
コード例 #16
0
        public void EqualsFalseCodeMismatch()
        {
            var v1 = new GetContentHashListResult(new ContentHashListWithDeterminism(ContentHashList.Random(), CacheDeterminism.None));
            var v2 = new GetContentHashListResult("error");

            Assert.False(v1.Equals(v2));
        }
コード例 #17
0
        public void EqualsFalseNullOtherPayload()
        {
            var v1 = new ContentHashList(ContentHashArray, new byte[] { 0 });
            var v2 = new ContentHashList(ContentHashArray);

            Assert.False(v1.Equals(v2));
        }
コード例 #18
0
        private CommandPool <ContentHashListWithDeterminism> CreateGetContentHashListCommandPool()
        {
            return(new CommandPool <ContentHashListWithDeterminism>(
                       Connection,
#pragma warning disable SA1118 // Parameter must not span multiple lines
                       "SELECT Payload, Determinism, SerializedDeterminism, ContentHashList FROM ContentHashLists" +
                       "  WHERE WeakFingerprint=@weakFingerprint" +
                       "    AND SelectorContentHash=@selectorContentHash" +
                       "    AND SelectorOutput=@selectorOutput",
#pragma warning restore SA1118 // Parameter must not span multiple lines
                       async command =>
            {
                await Task.Yield();
                using (SQLiteDataReader reader = command.ExecuteReader())
                {
                    if (await reader.ReadAsync())
                    {
                        var payloadObject = reader["Payload"];
                        var contentHashList = ContentHashList.Deserialize(
                            (string)reader["ContentHashList"], payloadObject == DBNull.Value ? null : (byte[])payloadObject);

                        var serializedDeterminismObject = reader["SerializedDeterminism"];
                        var determinism = ReadDeterminism(
                            serializedDeterminismObject == DBNull.Value ? null : (byte[])serializedDeterminismObject,
                            (byte[])reader["Determinism"]);

                        Contract.Assert(!reader.Read());
                        return new ContentHashListWithDeterminism(contentHashList, determinism);
                    }

                    return new ContentHashListWithDeterminism(null, CacheDeterminism.None);
                }
            }
                       ));
        }
コード例 #19
0
        public Task PreferSharedUrgencyHintIsRespected()
        {
            var context            = new Context(Logger);
            var weakFingerprint    = Fingerprint.Random();
            var selector1          = Selector.Random();
            var selector2          = Selector.Random();
            var strongFingerprint1 = new StrongFingerprint(weakFingerprint, selector1);
            var strongFingerprint2 = new StrongFingerprint(weakFingerprint, selector2);
            var contentHashListWithDeterminism1 = new ContentHashListWithDeterminism(ContentHashList.Random(), CacheDeterminism.None);
            var contentHashListWithDeterminism2 = new ContentHashListWithDeterminism(ContentHashList.Random(), CacheDeterminism.None);

            return(RunTestAsync(context, async session =>
            {
                await session.AddOrGetContentHashListAsync(context, strongFingerprint1, contentHashListWithDeterminism1, Token).ShouldBeSuccess();
                _clock.Increment();
                await session.AddOrGetContentHashListAsync(context, strongFingerprint2, contentHashListWithDeterminism2, Token).ShouldBeSuccess();
                _clock.Increment();

                var contentHashList = session.GetContentHashListAsync(context, strongFingerprint1, Token, UrgencyHint.PreferShared);

                List <GetSelectorResult> getSelectorResults = await session.GetSelectors(context, weakFingerprint, Token).ToListAsync(CancellationToken.None);
                Assert.Equal(2, getSelectorResults.Count);

                GetSelectorResult r1 = getSelectorResults[0];
                Assert.True(r1.Succeeded);
                Assert.True(r1.Selector == selector2);

                GetSelectorResult r2 = getSelectorResults[1];
                Assert.True(r2.Succeeded);
                Assert.True(r2.Selector == selector1);
            }));
        }
コード例 #20
0
        public Task AlwaysReplaceWhenPreviousContentMissing(int fromDeterminism, int toDeterminism)
        {
            var strongFingerprint = StrongFingerprint.Random();
            var contentHashList   = ContentHashList.Random();

            return(RunTest((context, contentLocationDatabase) =>
            {
                var addResult = contentLocationDatabase.AddOrGetContentHashList(
                    context, strongFingerprint, new ContentHashListWithDeterminism(contentHashList, Determinism[fromDeterminism]));
                Assert.Equal(Determinism[fromDeterminism].EffectiveGuid, addResult.ContentHashListWithDeterminism.Determinism.EffectiveGuid);

                // What we will do here is AddOrGet() a record that we already know is
                // there but with the determinism bit changed.
                addResult = contentLocationDatabase.AddOrGetContentHashList(
                    context, strongFingerprint, new ContentHashListWithDeterminism(contentHashList, Determinism[toDeterminism]))
                ;
                // We always expect the new determinism bit to take over
                Assert.Null(addResult.ContentHashListWithDeterminism.ContentHashList);
                Assert.Equal(Determinism[toDeterminism].EffectiveGuid, addResult.ContentHashListWithDeterminism.Determinism.EffectiveGuid);

                // Validate that it was actually updated in the DB
                var getResult = contentLocationDatabase.GetContentHashList(context, strongFingerprint);
                Assert.Equal(Determinism[toDeterminism].EffectiveGuid, getResult.ContentHashListWithDeterminism.Determinism.EffectiveGuid);
            }));
        }
コード例 #21
0
        public Task GetSelectorsGivesSelectorsInReverseLruOrderAfterGet()
        {
            var context            = new Context(Logger);
            var weakFingerprint    = Fingerprint.Random();
            var selector1          = Selector.Random();
            var selector2          = Selector.Random();
            var strongFingerprint1 = new StrongFingerprint(weakFingerprint, selector1);
            var strongFingerprint2 = new StrongFingerprint(weakFingerprint, selector2);
            var contentHashListWithDeterminism1 = new ContentHashListWithDeterminism(ContentHashList.Random(), CacheDeterminism.None);
            var contentHashListWithDeterminism2 = new ContentHashListWithDeterminism(ContentHashList.Random(), CacheDeterminism.None);

            return(RunTestAsync(context, async(store, session) =>
            {
                await session.AddOrGetContentHashListAsync(context, strongFingerprint1, contentHashListWithDeterminism1, Token).ShouldBeSuccess();
                _clock.Increment();
                await session.AddOrGetContentHashListAsync(context, strongFingerprint2, contentHashListWithDeterminism2, Token).ShouldBeSuccess();
                _clock.Increment();
                await session.GetContentHashListAsync(context, strongFingerprint1, Token).ShouldBeSuccess();
                _clock.Increment();

                List <GetSelectorResult> getSelectorResults = await session.GetSelectors(context, weakFingerprint, Token).ToListAsync();
                Assert.Equal(2, getSelectorResults.Count);

                GetSelectorResult r1 = getSelectorResults[0];
                Assert.True(r1.Succeeded);
                Assert.True(r1.Selector == selector1);

                GetSelectorResult r2 = getSelectorResults[1];
                Assert.True(r2.Succeeded);
                Assert.True(r2.Selector == selector2);
            }));
        }
コード例 #22
0
        public Task GetSelectorsGivesSelectors()
        {
            var weakFingerprint    = Fingerprint.Random();
            var selector1          = Selector.Random();
            var selector2          = Selector.Random();
            var strongFingerprint1 = new StrongFingerprint(weakFingerprint, selector1);
            var strongFingerprint2 = new StrongFingerprint(weakFingerprint, selector2);
            var contentHashListWithDeterminism1 = new ContentHashListWithDeterminism(ContentHashList.Random(), CacheDeterminism.None);
            var contentHashListWithDeterminism2 = new ContentHashListWithDeterminism(ContentHashList.Random(), CacheDeterminism.None);

            return(RunTest((context, contentLocationDatabase) =>
            {
                contentLocationDatabase.AddOrGetContentHashList(
                    context, strongFingerprint1, contentHashListWithDeterminism1).ShouldBeSuccess();
                contentLocationDatabase.AddOrGetContentHashList(
                    context, strongFingerprint2, contentHashListWithDeterminism2).ShouldBeSuccess();

                List <GetSelectorResult> getSelectorResults = contentLocationDatabase.GetSelectors(context, weakFingerprint).ToList();
                Assert.Equal(2, getSelectorResults.Count);

                GetSelectorResult r1 = getSelectorResults[0].ShouldBeSuccess();
                Assert.True(r1.Selector == selector1 || r1.Selector == selector2);

                GetSelectorResult r2 = getSelectorResults[1].ShouldBeSuccess();
                Assert.True(r2.Selector == selector1 || r2.Selector == selector2);
            }));
        }
コード例 #23
0
        public Task DeterminismUpgradeWhenPreviousContentExists(int fromDeterminism, int toDeterminism, bool shouldUpgrade)
        {
            var context           = new Context(Logger);
            var strongFingerprint = StrongFingerprint.Random();

            return(RunTestAsync(context, async session =>
            {
                var putResult = await((ICacheSession)session).PutRandomAsync(
                    context, ContentHashType, false, RandomContentByteCount, Token);
                var contentHashList = new ContentHashList(new[] { putResult.ContentHash });
                var addResult = await session.AddOrGetContentHashListAsync(
                    context, strongFingerprint, new ContentHashListWithDeterminism(contentHashList, Determinism[fromDeterminism]), Token).ShouldBeSuccess();
                Assert.Equal(Determinism[fromDeterminism].EffectiveGuid, addResult.ContentHashListWithDeterminism.Determinism.EffectiveGuid);

                // What we will do here is AddOrGet() a record that we already know is
                // there but with the determinism bit changed.
                addResult = await session.AddOrGetContentHashListAsync(
                    context, strongFingerprint, new ContentHashListWithDeterminism(contentHashList, Determinism[toDeterminism]), Token).ShouldBeSuccess();
                Assert.Null(addResult.ContentHashListWithDeterminism.ContentHashList);

                var getResult = await session.GetContentHashListAsync(context, strongFingerprint, Token).ShouldBeSuccess();
                Assert.Equal(
                    Determinism[shouldUpgrade ? toDeterminism : fromDeterminism].EffectiveGuid,
                    getResult.ContentHashListWithDeterminism.Determinism.EffectiveGuid);
            }));
        }
コード例 #24
0
        public Task TestAddOrGetReturnsConflictingResult()
        {
            var context           = new Context(Logger);
            var strongFingerprint = StrongFingerprint.Random();

            return(RunTestAsync(context, async session =>
            {
                ContentHashList contentHashList = null;

                async Task <AddOrGetContentHashListResult> addNew()
                {
                    var putResult = await((ICacheSession)session).PutRandomAsync(
                        context, ContentHashType, false, RandomContentByteCount, Token);
                    contentHashList = new ContentHashList(new[] { putResult.ContentHash });
                    return await session.AddOrGetContentHashListAsync(
                        context, strongFingerprint, new ContentHashListWithDeterminism(contentHashList, CacheDeterminism.None), Token).ShouldBeSuccess();
                }

                var originalResult = await addNew();
                var originalContentHashList = contentHashList;
                Assert.Null(originalResult.ContentHashListWithDeterminism.ContentHashList);

                var conflictResult = await addNew();
                Assert.NotNull(conflictResult.ContentHashListWithDeterminism.ContentHashList);
                Assert.Equal(originalContentHashList, conflictResult.ContentHashListWithDeterminism.ContentHashList);

                var getResult = await session.GetContentHashListAsync(context, strongFingerprint, Token).ShouldBeSuccess();
                Assert.Equal(originalContentHashList, getResult.ContentHashListWithDeterminism.ContentHashList);
            }));
        }
コード例 #25
0
        public async Task TestGetOrAddStrongFingerprintAsyncExisting()
        {
            var strongFp = StrongFingerprint.Random();
            var hashList = new ContentHashListWithDeterminism(ContentHashList.Random(), CacheDeterminism.None);

            var initialData = new Dictionary <RedisKey, RedisValue>
            {
                { _redisSerializer.ToRedisKey(strongFp).Prepend(RedisNameSpace), _redisSerializer.ToRedisValue(hashList) },
            };

            using (var mockDb = new MockRedisDatabase(SystemClock.Instance, initialData))
            {
                await RunTest(
                    mockDb,
                    async (context, metadataCache, redisDb) =>
                {
                    var getContentHashListResult = await metadataCache.GetOrAddContentHashListAsync(
                        context,
                        strongFp,
                        fp =>
                    {
                        throw new InvalidOperationException(
                            "GetFunc not expected to be called since data is already present in the cache");
                    });

                    // Check result
                    Assert.True(getContentHashListResult.Succeeded);
                    Assert.Equal(hashList, getContentHashListResult.ContentHashListWithDeterminism);
                });
            }
        }
コード例 #26
0
        public void EqualsObjectFalse()
        {
            var v1 = new GetContentHashListResult(new ContentHashListWithDeterminism(ContentHashList.Random(), CacheDeterminism.None));
            var v2 = new object();

            Assert.False(v1.Equals(v2));
        }
コード例 #27
0
        public void ContentHashListWithDeterminismProperty()
        {
            var contentHashListWithDeterminism = new ContentHashListWithDeterminism(
                ContentHashList.Random(), CacheDeterminism.ViaCache(CacheDeterminism.NewCacheGuid(), CacheDeterminism.NeverExpires));

            Assert.Equal(contentHashListWithDeterminism, new GetContentHashListResult(contentHashListWithDeterminism).ContentHashListWithDeterminism);
        }
コード例 #28
0
        public async Task GetTriggersBackgroundSealingByEndOfSession()
        {
            var context        = new Context(Logger);
            var cacheNamespace = Guid.NewGuid().ToString();

            // Each of the caches created below must use the same testDirectory in order to share local content.
            // And they must not run concurrently because the local content store requires exclusive access.
            // This is just for testing purposes; in production, "local" (not in the backing store) content
            // will live in CASaaS which allows concurrent access and can find and serve content from peers.
            using (var testDirectory = new DisposableDirectory(FileSystem))
            {
                // Add a value and its content to a write-never store. This will not upload the content but the content will still be available locally.
                StrongFingerprint strongFingerprint = StrongFingerprint.Random();
                ContentHashList   contentHashList   = null;
                await RunTestAsync(
                    context,
                    async (writeNeverCache, writeNeverSession) =>
                {
                    strongFingerprint = await CreateRandomStrongFingerprintAsync(context, true, writeNeverSession);
                    var initialValue  = await CreateRandomContentHashListWithDeterminismAsync(context, true, writeNeverSession);
                    contentHashList   = initialValue.ContentHashList;

                    var addOrGetResult = await writeNeverSession.AddOrGetContentHashListAsync(
                        context, strongFingerprint, initialValue, Token).ConfigureAwait(false);
                    addOrGetResult.Succeeded.Should().BeTrue();
                    addOrGetResult.ContentHashListWithDeterminism.ContentHashList.Should().BeNull();
                },
                    () => CreateCache(testDirectory, cacheNamespace, FileSystem, Logger, BackingOption.WriteNever, ItemStorageOption));

                contentHashList.Should().NotBeNull();

                // Get the unbacked value to trigger a background sealing
                await RunTestAsync(
                    context,
                    async (writeBehindCache, writeBehindSession) =>
                {
                    var getResult =
                        await writeBehindSession.GetContentHashListAsync(context, strongFingerprint, Token).ConfigureAwait(false);
                    getResult.Succeeded.Should().BeTrue();
                    getResult.ContentHashListWithDeterminism.ContentHashList.Should().Be(contentHashList);
                    getResult.ContentHashListWithDeterminism.Determinism.IsDeterministic.Should().BeFalse();
                },
                    () => CreateCache(testDirectory, cacheNamespace, FileSystem, Logger, BackingOption.WriteBehind, ItemStorageOption));

                // Check that the previous run sealed the value as backed
                await RunTestAsync(
                    context,
                    async (writeBehindCache, writeBehindSession) =>
                {
                    var getResult =
                        await writeBehindSession.GetContentHashListAsync(context, strongFingerprint, Token).ConfigureAwait(false);
                    getResult.Succeeded.Should().BeTrue();
                    getResult.ContentHashListWithDeterminism.ContentHashList.Should().Be(contentHashList);
                    Assert.Equal(writeBehindCache.Id, getResult.ContentHashListWithDeterminism.Determinism.EffectiveGuid);
                    getResult.ContentHashListWithDeterminism.Determinism.IsDeterministic.Should().BeTrue();
                },
                    () => CreateCache(testDirectory, cacheNamespace, FileSystem, Logger, BackingOption.WriteBehind, ItemStorageOption));
            }
        }
コード例 #29
0
        public void EqualsTrue()
        {
            var contentHashListWithDeterminism = new ContentHashListWithDeterminism(ContentHashList.Random(), CacheDeterminism.None);
            var v1 = new GetContentHashListResult(contentHashListWithDeterminism);
            var v2 = new GetContentHashListResult(contentHashListWithDeterminism);

            Assert.True(v1.Equals(v2));
        }
コード例 #30
0
 public void ToStringSuccess()
 {
     Assert.Contains(
         "Success",
         new GetContentHashListResult(
             new ContentHashListWithDeterminism(ContentHashList.Random(), CacheDeterminism.None)).ToString(),
         StringComparison.OrdinalIgnoreCase);
 }