예제 #1
0
        public async Task CasEntriesReplacedOnMissingContent(int fromDeterminism, int toDeterminism, bool differentCasEntries)
        {
            string testName    = I($"ReplacedOnMissingContent{fromDeterminism}x{toDeterminism}{(differentCasEntries ? "Diff" : "Same")}");
            string testCacheId = MakeCacheId(testName);

            ICache cache = await CreateCacheAsync(testCacheId, strictMetadataCasCoupling : false);

            try
            {
                string        testSessionId = "Session1-" + testCacheId;
                ICacheSession session       = await CreateSessionAsync(cache, testSessionId);

                // We need at least 2 to make "differentCasEntries" work
                FullCacheRecord[] records =
                {
                    RandomHelpers.CreateRandomFullCacheRecord(session.CacheId, s_determinism[fromDeterminism]),
                    RandomHelpers.CreateRandomFullCacheRecord(session.CacheId, s_determinism[fromDeterminism])
                };
                foreach (var record in records)
                {
                    var addResult = await session.AddOrGetAsync(
                        record.StrongFingerprint.WeakFingerprint,
                        record.StrongFingerprint.CasElement,
                        record.StrongFingerprint.HashElement,
                        record.CasEntries).SuccessAsync();

                    XAssert.IsNull(addResult.Record);
                }

                await CloseSessionAsync(session, testSessionId);

                testSessionId = "Session2-" + testCacheId;
                session       = await CreateSessionAsync(cache, testSessionId);

                // What we will do here is AddOrGet() a record with the determinism bit changed.
                for (int i = 0; i < records.Length; i++)
                {
                    var record = records[i];

                    var getResult = await session.GetCacheEntryAsync(record.StrongFingerprint).SuccessAsync();

                    XAssert.AreEqual(record.CasEntries.Determinism.EffectiveGuid, getResult.Determinism.EffectiveGuid);

                    // This gets the CasEntries we want
                    var        recordsLength = records.Length;
                    CasEntries newEntries    = records[(i + (differentCasEntries ? 1 : 0)) % recordsLength].CasEntries;

                    // Validate that the entry for the record is what we expect
                    var entries = (await session.GetCacheEntryAsync(record.StrongFingerprint)).Success();
                    XAssert.AreEqual(s_determinism[fromDeterminism].EffectiveGuid, entries.Determinism.EffectiveGuid);

                    // Now make a new record
                    var newRecord = (await session.AddOrGetAsync(
                                         record.StrongFingerprint.WeakFingerprint,
                                         record.StrongFingerprint.CasElement,
                                         record.StrongFingerprint.HashElement,
                                         new CasEntries(newEntries, s_determinism[toDeterminism]))).Success();

                    // The new record should be null because the old value should have
                    // been replaced with the new value in all cases (due to missing content).
                    XAssert.IsNull(newRecord.Record);

                    // Now, we will try to get the same record from the cache to validate the replacement
                    entries = (await session.GetCacheEntryAsync(record.StrongFingerprint)).Success();
                    XAssert.AreEqual(newEntries, entries);
                    XAssert.AreEqual(s_determinism[toDeterminism].EffectiveGuid, entries.Determinism.EffectiveGuid);
                }

                await CloseSessionAsync(session, testSessionId);
            }
            finally
            {
                await ShutdownCacheAsync(cache, testCacheId);
            }
        }