public async Task RunShouldNotUpdateSequenceNumberIfEndOfMessageIsReachedButSequenceNumberIsNull( byte[] bytes, int sequenceNumber, [Frozen, Substitute] Stream stream, [Frozen, Substitute] IChannel <GatewayMessageChunk> channel, [Frozen, Substitute] IGatewayUtilsFactory factory, [Frozen, Substitute] IGatewayService gateway, [Frozen, Substitute] ISerializer serializer, [Target] DefaultGatewayRxWorker worker ) { var message = new GatewayMessage { SequenceNumber = null }; serializer.Deserialize <GatewayMessage>(Any <Stream>(), Any <CancellationToken>()).Returns(message); channel.Read(Any <CancellationToken>()).Returns(new GatewayMessageChunk(bytes, bytes.Length, true)); gateway.SequenceNumber = sequenceNumber; await worker.Start(gateway); await worker.Run(); gateway.SequenceNumber.Should().Be(sequenceNumber); }
public async Task RunShouldRouteTheMessageIfEndOfMessageIsReached( byte[] bytes, uint interval, int sequenceNumber, [Frozen, Substitute] Stream stream, [Frozen, Substitute] IChannel <GatewayMessageChunk> channel, [Frozen, Substitute] IGatewayUtilsFactory factory, [Frozen, Substitute] IGatewayService gateway, [Frozen, Substitute] ISerializer serializer, [Frozen, Substitute] IEventRouter router, [Target] DefaultGatewayRxWorker worker ) { var @event = new HelloEvent { HeartbeatInterval = interval }; var message = new GatewayMessage { SequenceNumber = sequenceNumber, Data = @event }; var cancellationToken = new CancellationToken(false); serializer.Deserialize <GatewayMessage>(Any <Stream>(), Any <CancellationToken>()).Returns(message); channel.Read(Any <CancellationToken>()).Returns(new GatewayMessageChunk(bytes, bytes.Length, true)); await worker.Start(gateway); await worker.Run(cancellationToken); await router.Received().Route(Is(@event), Is(cancellationToken)); }
public async Task StartShouldStartTheTimer( [Frozen, Substitute] ITimer timer, [Frozen, Substitute] IGatewayService gateway, [Target] DefaultGatewayRxWorker rxWorker ) { await rxWorker.Start(gateway); await timer.Received().Start(); }
public async Task StartShouldCreateWorkerThread( [Frozen, Substitute] ITimerFactory timerFactory, [Frozen, Substitute] IGatewayService gateway, [Target] DefaultGatewayRxWorker rxWorker ) { var cancellationToken = new CancellationToken(false); await rxWorker.Start(gateway); timerFactory.Received().CreateTimer(Is((AsyncTimerCallback)rxWorker.Run), Is(0), Is("Gateway RX")); }
public async Task RunShouldNotTruncateTheStreamIfEndOfMessageIsNotReached( byte[] bytes, [Frozen, Substitute] Stream stream, [Frozen, Substitute] IChannel <GatewayMessageChunk> channel, [Frozen, Substitute] IGatewayUtilsFactory factory, [Frozen, Substitute] IGatewayService gateway, [Target] DefaultGatewayRxWorker worker ) { channel.Read(Any <CancellationToken>()).Returns(new GatewayMessageChunk(bytes, bytes.Length, false)); await worker.Start(gateway); await worker.Run(); stream.DidNotReceive().SetLength(0); }
public async Task RunShouldCreateAStream( byte[] bytes, [Frozen, Substitute] Stream stream, [Frozen, Substitute] IChannel <GatewayMessageChunk> channel, [Frozen, Substitute] IGatewayUtilsFactory factory, [Frozen, Substitute] IGatewayService gateway, [Target] DefaultGatewayRxWorker worker ) { channel.Read(Any <CancellationToken>()).Returns(new GatewayMessageChunk(bytes, 0, true)); await worker.Start(gateway); await worker.Run(); factory.Received().CreateStream(); }
public async Task SendShouldThrowIfTheOperationWasCanceled( string chunk, [Frozen, Substitute] IGatewayService gateway, [Target] DefaultGatewayRxWorker worker ) { var message = new GatewayMessageChunk { Bytes = Encoding.UTF8.GetBytes(chunk) }; var cancellationToken = new CancellationToken(false); await worker.Start(gateway); var operationCancellationToken = new CancellationToken(true); Func <Task> func = () => worker.Emit(message, operationCancellationToken); await func.Should().ThrowAsync <OperationCanceledException>(); }
public async Task RunShouldWriteTheBytesToTheStream( byte[] bytes, [Frozen, Substitute] Stream stream, [Frozen, Substitute] IChannel <GatewayMessageChunk> channel, [Frozen, Substitute] IGatewayUtilsFactory factory, [Frozen, Substitute] IGatewayService gateway, [Target] DefaultGatewayRxWorker worker ) { var cancellationToken = new CancellationToken(false); channel.Read(Any <CancellationToken>()).Returns(new GatewayMessageChunk(bytes, bytes.Length, true)); await worker.Start(gateway); await worker.Run(cancellationToken); await stream.Received().WriteAsync(Is <ReadOnlyMemory <byte> >(givenBytes => Encoding.UTF8.GetString(bytes) == Encoding.UTF8.GetString(bytes)), Is(cancellationToken)); }
public async Task RunShouldNotReadFromTheChannelIfWaitToReadReturnedFalse( byte[] bytes, [Frozen, Substitute] Stream stream, [Frozen, Substitute] IChannel <GatewayMessageChunk> channel, [Frozen, Substitute] IGatewayUtilsFactory factory, [Frozen, Substitute] IGatewayService gateway, [Target] DefaultGatewayRxWorker worker ) { var cancellationToken = new CancellationToken(false); channel.WaitToRead(Any <CancellationToken>()).Returns(false); await worker.Start(gateway); await worker.Run(cancellationToken); await channel.DidNotReceive().Read(Is(cancellationToken)); }
public async Task EmitShouldEmitTheMessageToTheChannel( int sequenceNumber, string chunk, IGatewayService gateway, [Frozen, Substitute] IChannel <GatewayMessageChunk> channel, [Target] DefaultGatewayRxWorker worker ) { var message = new GatewayMessageChunk { Bytes = Encoding.UTF8.GetBytes(chunk) }; var cancellationToken = new CancellationToken(false); await worker.Start(gateway); await worker.Emit(message, cancellationToken); await channel.Received().Write(Is(message), Is(cancellationToken)); }
public async Task StartShouldSetupGatewayToRestartOnUnexpectedStops( [Frozen, Substitute] ITimer timer, [Frozen, Substitute] IGatewayService gateway, [Target] DefaultGatewayRxWorker worker ) { var cancellationToken = new CancellationToken(false); var source = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); await worker.Start(gateway); timer.Received().StopOnException = Is(true); timer.Received().OnUnexpectedStop = Any <OnUnexpectedTimerStop>(); var arg = (from call in timer.ReceivedCalls() where call.GetMethodInfo().Name.Contains(nameof(timer.OnUnexpectedStop)) select(OnUnexpectedTimerStop) call.GetArguments()[0]).First(); await arg(); await gateway.Received().Restart(); }
public async Task RunShouldNotDeserializeTheMessageIfEndOfMessageIsNotReached( byte[] bytes, [Frozen, Substitute] Stream stream, [Frozen, Substitute] IChannel <GatewayMessageChunk> channel, [Frozen, Substitute] IGatewayUtilsFactory factory, [Frozen, Substitute] IGatewayService gateway, [Frozen, Substitute] ISerializer serializer, [Target] DefaultGatewayRxWorker worker ) { var cancellationToken = new CancellationToken(false); channel.Read(Any <CancellationToken>()).Returns(new GatewayMessageChunk(bytes, bytes.Length, false)); await worker.Start(gateway); await worker.Run(cancellationToken); await serializer.DidNotReceive().Deserialize <GatewayMessage>(Is(stream), Is(cancellationToken)); }
public async Task RunShouldNotRouteTheMessageIfEndOfMessageIsReachedButEventDataIsNull( byte[] bytes, uint interval, int sequenceNumber, [Frozen, Substitute] Stream stream, [Frozen, Substitute] IChannel <GatewayMessageChunk> channel, [Frozen, Substitute] IGatewayUtilsFactory factory, [Frozen, Substitute] IGatewayService gateway, [Frozen, Substitute] ISerializer serializer, [Frozen, Substitute] IEventRouter router, [Target] DefaultGatewayRxWorker worker ) { channel.Read(Any <CancellationToken>()).Returns(new GatewayMessageChunk(bytes, bytes.Length, true)); await worker.Start(gateway); await worker.Run(); await router.DidNotReceive().Route(Any <IGatewayEvent>(), Any <CancellationToken>()); }