public async Task WhenDelegateIsTyped_Manual() { bool executed = false; Task changesHandler(ChangeFeedProcessorContext context, IReadOnlyCollection <dynamic> docs, Func <Task> checkpointAsync, CancellationToken token) { Assert.AreEqual(1, docs.Count); Assert.AreEqual("Test", docs.First().id.ToString()); executed = true; return(Task.CompletedTask); } ChangeFeedObserverFactoryCore <dynamic> changeFeedObserverFactoryCore = new ChangeFeedObserverFactoryCore <dynamic>(changesHandler, this.cosmosSerializerCore); ChangeFeedObserver changeFeedObserver = changeFeedObserverFactoryCore.CreateObserver(); Assert.IsNotNull(changeFeedObserver); ResponseMessage responseMessage = this.BuildResponseMessage(); ChangeFeedObserverContextCore context = new ChangeFeedObserverContextCore(this.leaseToken, responseMessage, Mock.Of <PartitionCheckpointer>()); await changeFeedObserver.ProcessChangesAsync(context, responseMessage.Content, CancellationToken.None); Assert.IsTrue(executed); }
public async Task WhenDelegateIsStream_Automatic() { ResponseMessage responseMessage = this.BuildResponseMessage(); bool executed = false; Task changesHandler(ChangeFeedProcessorContext context, Stream stream, CancellationToken token) { Assert.ReferenceEquals(responseMessage.Content, stream); executed = true; return(Task.CompletedTask); } ChangeFeedObserverFactoryCore changeFeedObserverFactoryCore = new ChangeFeedObserverFactoryCore(changesHandler); ChangeFeedObserver changeFeedObserver = changeFeedObserverFactoryCore.CreateObserver(); Assert.IsNotNull(changeFeedObserver); ChangeFeedObserverContextCore context = new ChangeFeedObserverContextCore(this.leaseToken, responseMessage, Mock.Of <PartitionCheckpointer>()); await changeFeedObserver.ProcessChangesAsync(context, responseMessage.Content, CancellationToken.None); Assert.IsTrue(executed); }
public void ValidateConstructor() { ResponseMessage responseMessage = new ResponseMessage(); ChangeFeedObserverContextCore observerContext = new ChangeFeedObserverContextCore(Guid.NewGuid().ToString(), feedResponse: responseMessage, Mock.Of <PartitionCheckpointer>()); ChangeFeedProcessorContextCore changeFeedProcessorContext = new ChangeFeedProcessorContextCore(observerContext); Exception exception = new Exception("randomMessage"); ChangeFeedProcessorUserException ex = new ChangeFeedProcessorUserException(exception, changeFeedProcessorContext); Assert.AreEqual(exception.Message, ex.InnerException.Message); Assert.AreEqual(exception, ex.InnerException); Assert.ReferenceEquals(changeFeedProcessorContext, ex.ChangeFeedProcessorContext); }
public override async Task RunAsync(CancellationToken shutdownToken) { var context = new ChangeFeedObserverContextCore <T>(this.lease.CurrentLeaseToken); await this.observer.OpenAsync(context).ConfigureAwait(false); this.processorCancellation = CancellationTokenSource.CreateLinkedTokenSource(shutdownToken); Task processorTask = this.processor.RunAsync(this.processorCancellation.Token); processorTask.ContinueWith(_ => this.renewerCancellation.Cancel()).LogException(); Task renewerTask = this.renewer.RunAsync(this.renewerCancellation.Token); renewerTask.ContinueWith(_ => this.processorCancellation.Cancel()).LogException(); ChangeFeedObserverCloseReason closeReason = shutdownToken.IsCancellationRequested ? ChangeFeedObserverCloseReason.Shutdown : ChangeFeedObserverCloseReason.Unknown; try { await Task.WhenAll(processorTask, renewerTask).ConfigureAwait(false); } catch (LeaseLostException) { closeReason = ChangeFeedObserverCloseReason.LeaseLost; throw; } catch (FeedSplitException) { closeReason = ChangeFeedObserverCloseReason.LeaseGone; throw; } catch (OperationCanceledException) when(shutdownToken.IsCancellationRequested) { closeReason = ChangeFeedObserverCloseReason.Shutdown; } catch (ObserverException) { closeReason = ChangeFeedObserverCloseReason.ObserverError; throw; } catch (Exception) when(processorTask.IsFaulted) { closeReason = ChangeFeedObserverCloseReason.Unknown; throw; } finally { await this.observer.CloseAsync(context, closeReason).ConfigureAwait(false); } }
public void ExposesResponseProperties() { string leaseToken = Guid.NewGuid().ToString(); ResponseMessage responseMessage = new ResponseMessage(HttpStatusCode.OK); responseMessage.Headers.RequestCharge = 10; ChangeFeedObserverContextCore changeFeedObserverContextCore = new ChangeFeedObserverContextCore(leaseToken, responseMessage, Mock.Of <PartitionCheckpointer>()); Assert.AreEqual(leaseToken, changeFeedObserverContextCore.LeaseToken); Assert.ReferenceEquals(responseMessage.Headers, changeFeedObserverContextCore.Headers); Assert.ReferenceEquals(responseMessage.Diagnostics, changeFeedObserverContextCore.Diagnostics); }
public void ValidateSerialization_AllFields() { ResponseMessage responseMessage = new ResponseMessage(); ChangeFeedObserverContextCore observerContext = new ChangeFeedObserverContextCore(Guid.NewGuid().ToString(), feedResponse: responseMessage, Mock.Of <PartitionCheckpointer>()); ChangeFeedProcessorContextCore changeFeedProcessorContext = new ChangeFeedProcessorContextCore(observerContext); Exception exception = new Exception("randomMessage"); ChangeFeedProcessorUserException originalException = new ChangeFeedProcessorUserException(exception, changeFeedProcessorContext); string json = JsonConvert.SerializeObject(originalException); ChangeFeedProcessorUserException deserializedException = JsonConvert.DeserializeObject <ChangeFeedProcessorUserException>(json); Assert.AreEqual(originalException.Message, deserializedException.Message); Assert.AreEqual(originalException.InnerException.Message, deserializedException.InnerException.Message); }
public ObserverExceptionWrappingChangeFeedObserverDecoratorTests() { this.observer = new Mock <ChangeFeedObserver>(); this.changeFeedObserverContext = new ChangeFeedObserverContextCore(Guid.NewGuid().ToString(), feedResponse: null, Mock.Of <PartitionCheckpointer>());; this.observerWrapper = new ObserverExceptionWrappingChangeFeedObserverDecorator(this.observer.Object); this.serializerCore = new CosmosSerializerCore(); MyDocument document = new MyDocument(); this.documents = new List <MyDocument> { document }; }
public async Task TryCheckpoint_OnSuccess() { string leaseToken = Guid.NewGuid().ToString(); string continuation = Guid.NewGuid().ToString(); ResponseMessage responseMessage = new ResponseMessage(HttpStatusCode.OK); responseMessage.Headers.ContinuationToken = continuation; Mock <PartitionCheckpointer> checkpointer = new Mock <PartitionCheckpointer>(); checkpointer.Setup(c => c.CheckpointPartitionAsync(It.Is <string>(s => s == continuation))).Returns(Task.CompletedTask); ChangeFeedObserverContextCore changeFeedObserverContextCore = new ChangeFeedObserverContextCore(leaseToken, responseMessage, checkpointer.Object); await changeFeedObserverContextCore.CheckpointAsync(); }
public async Task TryCheckpoint_OnLeaseLost() { string leaseToken = Guid.NewGuid().ToString(); string continuation = Guid.NewGuid().ToString(); ResponseMessage responseMessage = new ResponseMessage(HttpStatusCode.OK); responseMessage.Headers.ContinuationToken = continuation; Mock <PartitionCheckpointer> checkpointer = new Mock <PartitionCheckpointer>(); checkpointer.Setup(c => c.CheckpointPartitionAsync(It.Is <string>(s => s == continuation))).ThrowsAsync(new LeaseLostException()); ChangeFeedObserverContextCore changeFeedObserverContextCore = new ChangeFeedObserverContextCore(leaseToken, responseMessage, checkpointer.Object); CosmosException exception = await Assert.ThrowsExceptionAsync <CosmosException>(() => changeFeedObserverContextCore.CheckpointAsync()); Assert.AreEqual(HttpStatusCode.PreconditionFailed, exception.StatusCode); }
public async Task ProcessChanges_WhenCheckpointThrows_ShouldThrow() { CosmosException original = CosmosExceptionFactory.CreateThrottledException("throttled", new Headers()); Mock <PartitionCheckpointer> checkpointer = new Mock <PartitionCheckpointer>(); checkpointer.Setup(c => c.CheckpointPartitionAsync(It.IsAny <string>())).ThrowsAsync(original); ResponseMessage responseMessage = new ResponseMessage(); responseMessage.Headers.ContinuationToken = Guid.NewGuid().ToString(); ChangeFeedObserverContextCore observerContext = new ChangeFeedObserverContextCore(Guid.NewGuid().ToString(), feedResponse: responseMessage, checkpointer.Object); CosmosException caught = await Assert.ThrowsExceptionAsync <CosmosException>(() => this.sut.ProcessChangesAsync(observerContext, this.stream, CancellationToken.None)); Assert.AreEqual(original, caught); }
public async Task TryCheckpoint_OnUnknownException() { NotImplementedException cosmosException = new NotImplementedException(); string leaseToken = Guid.NewGuid().ToString(); string continuation = Guid.NewGuid().ToString(); ResponseMessage responseMessage = new ResponseMessage(HttpStatusCode.OK); responseMessage.Headers.ContinuationToken = continuation; Mock <PartitionCheckpointer> checkpointer = new Mock <PartitionCheckpointer>(); checkpointer.Setup(c => c.CheckpointPartitionAsync(It.Is <string>(s => s == continuation))).ThrowsAsync(cosmosException); ChangeFeedObserverContextCore changeFeedObserverContextCore = new ChangeFeedObserverContextCore(leaseToken, responseMessage, checkpointer.Object); NotImplementedException exception = await Assert.ThrowsExceptionAsync <NotImplementedException>(() => changeFeedObserverContextCore.CheckpointAsync()); Assert.ReferenceEquals(cosmosException, exception); }
public AutoCheckPointTests() { this.changeFeedObserver = Mock.Of <ChangeFeedObserver>(); this.partitionCheckpointer = new Mock <PartitionCheckpointer>(); this.partitionCheckpointer .Setup(checkPointer => checkPointer.CheckpointPartitionAsync(It.IsAny <string>())) .Returns(Task.CompletedTask); this.sut = new AutoCheckpointer(this.changeFeedObserver); this.stream = Mock.Of <Stream>(); ResponseMessage responseMessage = new ResponseMessage(); responseMessage.Headers.ContinuationToken = Guid.NewGuid().ToString(); this.observerContext = new ChangeFeedObserverContextCore(Guid.NewGuid().ToString(), feedResponse: responseMessage, this.partitionCheckpointer.Object); }
public async Task TryCheckpoint_OnCosmosException() { CosmosException cosmosException = CosmosExceptionFactory.CreateThrottledException("throttled", new Headers()); string leaseToken = Guid.NewGuid().ToString(); string continuation = Guid.NewGuid().ToString(); ResponseMessage responseMessage = new ResponseMessage(HttpStatusCode.OK); responseMessage.Headers.ContinuationToken = continuation; Mock <PartitionCheckpointer> checkpointer = new Mock <PartitionCheckpointer>(); checkpointer.Setup(c => c.CheckpointPartitionAsync(It.Is <string>(s => s == continuation))).ThrowsAsync(cosmosException); ChangeFeedObserverContextCore changeFeedObserverContextCore = new ChangeFeedObserverContextCore(leaseToken, responseMessage, checkpointer.Object); (bool isSuccess, Exception exception) = await changeFeedObserverContextCore.TryCheckpointAsync(); Assert.IsFalse(isSuccess); Assert.IsNotNull(exception); Assert.ReferenceEquals(cosmosException, exception); }
public void ValidateSerialization_AllFields() { ResponseMessage responseMessage = new ResponseMessage(); ChangeFeedObserverContextCore observerContext = new ChangeFeedObserverContextCore(Guid.NewGuid().ToString(), feedResponse: responseMessage, Mock.Of <PartitionCheckpointer>()); ChangeFeedProcessorContextCore changeFeedProcessorContext = new ChangeFeedProcessorContextCore(observerContext); Exception exception = new Exception("randomMessage"); ChangeFeedProcessorUserException originalException = new ChangeFeedProcessorUserException(exception, changeFeedProcessorContext); byte[] buffer = new byte[4096]; BinaryFormatter formatter = new BinaryFormatter(); MemoryStream stream1 = new MemoryStream(buffer); MemoryStream stream2 = new MemoryStream(buffer); formatter.Serialize(stream1, originalException); ChangeFeedProcessorUserException deserializedException = (ChangeFeedProcessorUserException)formatter.Deserialize(stream2); Assert.AreEqual(originalException.Message, deserializedException.Message); Assert.AreEqual(originalException.InnerException.Message, deserializedException.InnerException.Message); }