public void can_add_epochs_to_cache() { Assert.That(_cache.Count == 0); //add fist epoch to empty cache _epochManager.AddEpochToCache(_epochs[3]); Assert.That(_cache.Count == 4); Assert.That(_cache.First.Value.EpochNumber == _epochs[0].EpochNumber); Assert.That(_cache.Last.Value.EpochNumber == _epochs[3].EpochNumber); //add new last epoch _epochManager.AddEpochToCache(_epochs[4]); Assert.That(_cache.Count == 5); Assert.That(_cache.First.Value.EpochNumber == _epochs[0].EpochNumber); Assert.That(_cache.Last.Value.EpochNumber == _epochs[4].EpochNumber); //idempotent add _epochManager.AddEpochToCache(_epochs[1]); _epochManager.AddEpochToCache(_epochs[2]); _epochManager.AddEpochToCache(_epochs[3]); Assert.That(_cache.Count == 5); Assert.That(_cache.First.Value.EpochNumber == _epochs[0].EpochNumber); Assert.That(_cache.Last.Value.EpochNumber == _epochs[4].EpochNumber); //add new skip 1 last epoch _epochManager.AddEpochToCache(_epochs[6]); Assert.That(_cache.Count == 7); Assert.That(_cache.First.Value.EpochNumber == _epochs[0].EpochNumber); Assert.That(_cache.Last.Value.EpochNumber == _epochs[6].EpochNumber); //add new skip 5 last epoch _epochManager.AddEpochToCache(_epochs[11]); Assert.That(_cache.Count == 10); Assert.That(_cache.First.Value.EpochNumber == _epochs[2].EpochNumber); Assert.That(_cache.Last.Value.EpochNumber == _epochs[11].EpochNumber); //add last rolls cache _epochManager.AddEpochToCache(_epochs[12]); Assert.That(_cache.Count == 10); Assert.That(_cache.First.Value.EpochNumber == _epochs[3].EpochNumber); Assert.That(_cache.Last.Value.EpochNumber == _epochs[12].EpochNumber); //add epoch before cache _epochManager.AddEpochToCache(_epochs[1]); Assert.That(_cache.Count == 10); Assert.That(_cache.First.Value.EpochNumber == _epochs[3].EpochNumber); Assert.That(_cache.Last.Value.EpochNumber == _epochs[12].EpochNumber); //add idempotent first epoch _epochManager.AddEpochToCache(_epochs[2]); Assert.That(_cache.Count == 10); Assert.That(_cache.First.Value.EpochNumber == _epochs[3].EpochNumber); Assert.That(_cache.Last.Value.EpochNumber == _epochs[12].EpochNumber); //add idempotent last epoch _epochManager.AddEpochToCache(_epochs[12]); Assert.That(_cache.Count == 10); Assert.That(_cache.First.Value.EpochNumber == _epochs[3].EpochNumber); Assert.That(_cache.Last.Value.EpochNumber == _epochs[12].EpochNumber); //add disjunct skip epoch _epochManager.AddEpochToCache(_epochs[24]); Assert.That(_cache.Count == 10); Assert.That(_cache.First.Value.EpochNumber == _epochs[15].EpochNumber); Assert.That(_cache.Last.Value.EpochNumber == _epochs[24].EpochNumber); //cannot get epoch ahead of last cached on master var nextEpoch = _epochManager.GetEpochAfter(_epochs[24].EpochNumber, false); Assert.Null(nextEpoch); Assert.That(_cache.Count == 10); Assert.That(_cache.First.Value.EpochNumber == _epochs[15].EpochNumber); Assert.That(_cache.Last.Value.EpochNumber == _epochs[24].EpochNumber); //cannot get epoch ahead of cache on master nextEpoch = _epochManager.GetEpochAfter(_epochs[25].EpochNumber, false); Assert.Null(nextEpoch); Assert.That(_cache.Count == 10); Assert.That(_cache.First.Value.EpochNumber == _epochs[15].EpochNumber); Assert.That(_cache.Last.Value.EpochNumber == _epochs[24].EpochNumber); //can get next in cache nextEpoch = _epochManager.GetEpochAfter(_epochs[20].EpochNumber, false); Assert.That(nextEpoch.EpochPosition == _epochs[21].EpochPosition); Assert.That(_cache.Count == 10); Assert.That(_cache.First.Value.EpochNumber == _epochs[15].EpochNumber); Assert.That(_cache.Last.Value.EpochNumber == _epochs[24].EpochNumber); //can get next from first nextEpoch = _epochManager.GetEpochAfter(_epochs[15].EpochNumber, false); Assert.That(nextEpoch.EpochPosition == _epochs[16].EpochPosition); Assert.That(_cache.Count == 10); Assert.That(_cache.First.Value.EpochNumber == _epochs[15].EpochNumber); Assert.That(_cache.Last.Value.EpochNumber == _epochs[24].EpochNumber); //can get next epoch from just before cache nextEpoch = _epochManager.GetEpochAfter(_epochs[14].EpochNumber, false); Assert.That(nextEpoch.EpochPosition == _epochs[15].EpochPosition); Assert.That(_cache.Count == 10); Assert.That(_cache.First.Value.EpochNumber == _epochs[15].EpochNumber); Assert.That(_cache.Last.Value.EpochNumber == _epochs[24].EpochNumber); //can get next epoch from before cache nextEpoch = _epochManager.GetEpochAfter(_epochs[10].EpochNumber, false); Assert.That(nextEpoch.EpochPosition == _epochs[11].EpochPosition); Assert.That(_cache.Count == 10); Assert.That(_cache.First.Value.EpochNumber == _epochs[15].EpochNumber); Assert.That(_cache.Last.Value.EpochNumber == _epochs[24].EpochNumber); //can get next epoch from 0 epoch nextEpoch = _epochManager.GetEpochAfter(_epochs[0].EpochNumber, false); Assert.That(nextEpoch.EpochPosition == _epochs[1].EpochPosition); Assert.That(_cache.Count == 10); Assert.That(_cache.First.Value.EpochNumber == _epochs[15].EpochNumber); Assert.That(_cache.Last.Value.EpochNumber == _epochs[24].EpochNumber); //can add last epoch in log _epochManager.AddEpochToCache(_epochs[29]); Assert.That(_cache.Count == 10); Assert.That(_cache.First.Value.EpochNumber == _epochs[20].EpochNumber); Assert.That(_cache.Last.Value.EpochNumber == _epochs[29].EpochNumber); }
public void can_add_epochs_to_cache() { Assert.That(_cache.Count == 0); //add fist epoch to empty cache _epochManager.AddEpochToCache(_epochs[3]); Assert.That(_cache.Count == 4); Assert.That(_cache.First.Value.EpochNumber == _epochs[0].EpochNumber); Assert.That(_cache.Last.Value.EpochNumber == _epochs[3].EpochNumber); //add new last epoch _epochManager.AddEpochToCache(_epochs[4]); Assert.That(_cache.Count == 5); Assert.That(_cache.First.Value.EpochNumber == _epochs[0].EpochNumber); Assert.That(_cache.Last.Value.EpochNumber == _epochs[4].EpochNumber); //idempotent add _epochManager.AddEpochToCache(_epochs[1]); _epochManager.AddEpochToCache(_epochs[2]); _epochManager.AddEpochToCache(_epochs[3]); Assert.That(_cache.Count == 5); Assert.That(_cache.First.Value.EpochNumber == _epochs[0].EpochNumber); Assert.That(_cache.Last.Value.EpochNumber == _epochs[4].EpochNumber); //add new skip 1 last epoch _epochManager.AddEpochToCache(_epochs[6]); Assert.That(_cache.Count == 7); Assert.That(_cache.First.Value.EpochNumber == _epochs[0].EpochNumber); Assert.That(_cache.Last.Value.EpochNumber == _epochs[6].EpochNumber); //add new skip 5 last epoch _epochManager.AddEpochToCache(_epochs[11]); Assert.That(_cache.Count == 10); Assert.That(_cache.First.Value.EpochNumber == _epochs[2].EpochNumber); Assert.That(_cache.Last.Value.EpochNumber == _epochs[11].EpochNumber); //add last rolls cache _epochManager.AddEpochToCache(_epochs[12]); Assert.That(_cache.Count == 10); Assert.That(_cache.First.Value.EpochNumber == _epochs[3].EpochNumber); Assert.That(_cache.Last.Value.EpochNumber == _epochs[12].EpochNumber); //add epoch before cache _epochManager.AddEpochToCache(_epochs[1]); Assert.That(_cache.Count == 10); Assert.That(_cache.First.Value.EpochNumber == _epochs[3].EpochNumber); Assert.That(_cache.Last.Value.EpochNumber == _epochs[12].EpochNumber); //add idempotent first epoch _epochManager.AddEpochToCache(_epochs[2]); Assert.That(_cache.Count == 10); Assert.That(_cache.First.Value.EpochNumber == _epochs[3].EpochNumber); Assert.That(_cache.Last.Value.EpochNumber == _epochs[12].EpochNumber); //add idempotent last epoch _epochManager.AddEpochToCache(_epochs[12]); Assert.That(_cache.Count == 10); Assert.That(_cache.First.Value.EpochNumber == _epochs[3].EpochNumber); Assert.That(_cache.Last.Value.EpochNumber == _epochs[12].EpochNumber); //add disjunct skip epoch _epochManager.AddEpochToCache(_epochs[24]); Assert.That(_cache.Count == 10); Assert.That(_cache.First.Value.EpochNumber == _epochs[15].EpochNumber); Assert.That(_cache.Last.Value.EpochNumber == _epochs[24].EpochNumber); //cannot get epoch ahead of last cached on master var nextEpoch = _epochManager.GetEpochAfter(_epochs[24].EpochNumber, false); Assert.Null(nextEpoch); Assert.That(_cache.Count == 10); Assert.That(_cache.First.Value.EpochNumber == _epochs[15].EpochNumber); Assert.That(_cache.Last.Value.EpochNumber == _epochs[24].EpochNumber); //cannot get epoch ahead of cache on master nextEpoch = _epochManager.GetEpochAfter(_epochs[25].EpochNumber, false); Assert.Null(nextEpoch); Assert.That(_cache.Count == 10); Assert.That(_cache.First.Value.EpochNumber == _epochs[15].EpochNumber); Assert.That(_cache.Last.Value.EpochNumber == _epochs[24].EpochNumber); //can get next in cache nextEpoch = _epochManager.GetEpochAfter(_epochs[20].EpochNumber, false); Assert.That(nextEpoch.EpochPosition == _epochs[21].EpochPosition); Assert.That(_cache.Count == 10); Assert.That(_cache.First.Value.EpochNumber == _epochs[15].EpochNumber); Assert.That(_cache.Last.Value.EpochNumber == _epochs[24].EpochNumber); //can get next from first nextEpoch = _epochManager.GetEpochAfter(_epochs[15].EpochNumber, false); Assert.That(nextEpoch.EpochPosition == _epochs[16].EpochPosition); Assert.That(_cache.Count == 10); Assert.That(_cache.First.Value.EpochNumber == _epochs[15].EpochNumber); Assert.That(_cache.Last.Value.EpochNumber == _epochs[24].EpochNumber); //can get next epoch from just before cache nextEpoch = _epochManager.GetEpochAfter(_epochs[14].EpochNumber, false); Assert.That(nextEpoch.EpochPosition == _epochs[15].EpochPosition); Assert.That(_cache.Count == 10); Assert.That(_cache.First.Value.EpochNumber == _epochs[15].EpochNumber); Assert.That(_cache.Last.Value.EpochNumber == _epochs[24].EpochNumber); //can get next epoch from before cache nextEpoch = _epochManager.GetEpochAfter(_epochs[10].EpochNumber, false); Assert.That(nextEpoch.EpochPosition == _epochs[11].EpochPosition); Assert.That(_cache.Count == 10); Assert.That(_cache.First.Value.EpochNumber == _epochs[15].EpochNumber); Assert.That(_cache.Last.Value.EpochNumber == _epochs[24].EpochNumber); //can get next epoch from 0 epoch nextEpoch = _epochManager.GetEpochAfter(_epochs[0].EpochNumber, false); Assert.That(nextEpoch.EpochPosition == _epochs[1].EpochPosition); Assert.That(_cache.Count == 10); Assert.That(_cache.First.Value.EpochNumber == _epochs[15].EpochNumber); Assert.That(_cache.Last.Value.EpochNumber == _epochs[24].EpochNumber); //can add last epoch in log _epochManager.AddEpochToCache(_epochs[29]); Assert.That(_cache.Count == 10); Assert.That(_cache.First.Value.EpochNumber == _epochs[20].EpochNumber); Assert.That(_cache.Last.Value.EpochNumber == _epochs[29].EpochNumber); // can write an epoch with epoch information (even though previous epochs // dont have epoch information) _epochManager.WriteNewEpoch(GetNextEpoch()); _epochManager.WriteNewEpoch(GetNextEpoch()); var epochsWritten = _published.OfType <SystemMessage.EpochWritten>().ToArray(); Assert.AreEqual(2, epochsWritten.Length); for (int i = 0; i < epochsWritten.Length; i++) { _reader.Reposition(epochsWritten[i].Epoch.EpochPosition); _reader.TryReadNext(); // read epoch IPrepareLogRecord <TStreamId> epochInfo; while (true) { var result = _reader.TryReadNext(); Assert.True(result.Success); if (result.LogRecord is IPrepareLogRecord <TStreamId> prepare) { epochInfo = prepare; break; } } var expectedStreamId = LogFormatHelper <TLogFormat, TStreamId> .Choose <TStreamId>( SystemStreams.EpochInformationStream, LogV3SystemStreams.EpochInformationStreamNumber); var expectedEventType = LogFormatHelper <TLogFormat, TStreamId> .Choose <TStreamId>( SystemEventTypes.EpochInformation, LogV3SystemEventTypes.EpochInformationNumber); Assert.AreEqual(expectedStreamId, epochInfo.EventStreamId); Assert.AreEqual(expectedEventType, epochInfo.EventType); Assert.AreEqual(i - 1, epochInfo.ExpectedVersion); Assert.AreEqual(_instanceId, epochInfo.Data.ParseJson <EpochDto>().LeaderInstanceId); } }