public void An_Outgoing_Connection_Must_Write_A_CompoundWrite_To_The_Network_And_Produce_Correct_ACKs() { new EstablishedConnectionTest(this).Run(x => { var writer = CreateTestProbe(); //TODO: Fix bug when empty write is used var compoundWrite = Tcp.CompoundWrite.Create( Tcp.Write.Create(ByteString.FromString("test1"), new Ack(1)), Tcp.Write.Create(ByteString.FromString("test2")), //Tcp.Write.Create(ByteString.Empty, new Ack(3)), Tcp.Write.Create(ByteString.FromString("test4"), new Ack(4))); var buffer = ByteBuffer.Allocate(100); x.ServerSideChannel.Read(buffer).ShouldBe(0); writer.Send(x.ConnectionActor, compoundWrite); x.PullFromServerSide(remaining: 15, into: buffer, remainingRetries: 1000); buffer.Flip(); ByteString.Create(buffer).DecodeString(Encoding.UTF8).ShouldBe("test1test2test4"); writer.ExpectMsg(new Ack(1)); //writer.ExpectMsg(new Ack(3)); writer.ExpectMsg(new Ack(4)); }); }
public void Tcp_listen_stream_must_shut_down_properly_even_if_some_accepted_connection_Flows_have_not_been_subscribed_to() { this.AssertAllStagesStopped(() => { var serverAddress = TestUtils.TemporaryServerAddress(); var firstClientConnected = new TaskCompletionSource <NotUsed>(); var takeTwoAndDropSecond = Flow.Create <Tcp.IncomingConnection>().Select(c => { firstClientConnected.TrySetResult(NotUsed.Instance); return(c); }).Grouped(2).Take(1).Select(e => e.First()); Sys.TcpStream() .Bind(serverAddress.Address.ToString(), serverAddress.Port) .Via(takeTwoAndDropSecond) .RunForeach(c => c.Flow.Join(Flow.Create <ByteString>()).Run(Materializer), Materializer); var folder = Source.From(Enumerable.Range(0, 100).Select(_ => ByteString.Create(new byte[] { 0 }))) .Via(Sys.TcpStream().OutgoingConnection(serverAddress)) .Aggregate(0, (i, s) => i + s.Count) .ToMaterialized(Sink.First <int>(), Keep.Right); var total = folder.Run(Materializer); firstClientConnected.Task.Wait(TimeSpan.FromSeconds(2)).Should().BeTrue(); var rejected = folder.Run(Materializer); total.Wait(TimeSpan.FromSeconds(10)).Should().BeTrue(); total.Result.Should().Be(100); rejected.Wait(TimeSpan.FromSeconds(5)).Should().BeTrue(); rejected.Exception.Flatten().InnerExceptions.Any(e => e is StreamTcpException).Should().BeTrue(); }, Materializer); }
private Tuple <int, ByteString> ReadN(Stream s, int n) { var buf = new byte[n]; var r = s.Read(buf, 0, n); return(new Tuple <int, ByteString>(r, ByteString.Create(buf, 0, r))); }
public void An_Outgoing_Connection_Must_Send_Big_Buffers_To_Network_Correctly() { new EstablishedConnectionTest(this).Run(x => { var bufferSize = 512 * 1024; var random = new Random(0); var testBytes = new byte[bufferSize]; random.NextBytes(testBytes); var testData = ByteString.Create(testBytes, 0, testBytes.Length); var writer = CreateTestProbe(); var write = Tcp.Write.Create(testData, Ack.Instance); var buffer = ByteBuffer.Allocate(bufferSize); x.ServerSideChannel.Read(buffer).ShouldBe(0); writer.Send(x.ConnectionActor, write); x.PullFromServerSide(remaining: bufferSize, into: buffer, remainingRetries: 1000); buffer.Flip(); //TODO: Buffer.limit //Assert.Equal(buffer, buffer.Limit()); ByteString.Create(buffer).ShouldBe(testData); }); }
private ByteString RandomByteString(int size) { var a = new byte[size]; ThreadLocalRandom.Current.NextBytes(a); return(ByteString.Create(a)); }
private void SendRequest(RequestBase s) { var sync = _currentConnection.GetNextSync(); var message = s.WithSync(sync).GetBytes(); _currentConnection.Socket.Tell(Tcp.Write.Create(ByteString.Create(message), new CommandAck(Sender, sync))); }
public void Outgoing_TCP_stream_must_shut_everything_down_if_client_signals_error_after_remote_has_closed_write() { this.AssertAllStagesStopped(() => { var testData = ByteString.Create(new byte[] { 1, 2, 3, 4, 5 }); var server = new Server(this); var tcpWriteProbe = new TcpWriteProbe(this); var tcpReadProbe = new TcpReadProbe(this); Source.FromPublisher(tcpWriteProbe.PublisherProbe) .Via(Sys.TcpStream().OutgoingConnection(server.Address)) .To(Sink.FromSubscriber(tcpReadProbe.SubscriberProbe)) .Run(Materializer); var serverConnection = server.WaitAccept(); // Server can still write serverConnection.Write(testData); tcpReadProbe.Read(5).ShouldBeEquivalentTo(testData); // Close remote side write serverConnection.ConfirmedClose(); tcpReadProbe.SubscriberProbe.ExpectComplete(); // Client can still write tcpWriteProbe.Write(testData); serverConnection.Read(5); serverConnection.WaitRead().ShouldBeEquivalentTo(testData); tcpWriteProbe.TcpWriteSubscription.Value.SendError(new IllegalStateException("test")); serverConnection.ExpectClosed(c => c.IsErrorClosed); serverConnection.ExpectTerminated(); }, Materializer); }
public void An_Outgoing_Connection_Must_Write_Data_To_Network_And_Acknowledge() { new EstablishedConnectionTest(this).Run(x => { var writer = CreateTestProbe(); var ackedWrite = Tcp.Write.Create(ByteString.FromString("testdata"), Ack.Instance); var buffer = ByteBuffer.Allocate(100); Assert.Equal(0, x.ServerSideChannel.Read(buffer)); writer.Send(x.ConnectionActor, ackedWrite); writer.ExpectMsg(Ack.Instance); x.PullFromServerSide(remaining: 8, into: buffer, remainingRetries: 1000); buffer.Flip(); //TODO: Buffer.limit //Assert.Equal(8, buffer.Limit); var unackedWrite = Tcp.Write.Create(ByteString.FromString("morestuff!"), Tcp.NoAck.Instance); buffer.Clear(); Assert.Equal(0, x.ServerSideChannel.Read(buffer)); writer.Send(x.ConnectionActor, unackedWrite); writer.ExpectNoMsg(TimeSpan.FromMilliseconds(500)); x.PullFromServerSide(remaining: 10, into: buffer, remainingRetries: 1000); buffer.Flip(); Assert.Equal("morestuff!", ByteString.Create(buffer).DecodeString(Encoding.UTF8)); }); }
public void Outgoing_TCP_stream_must_work_when_client_closes_read_then_server_closes_write_then_client_closes_write() { this.AssertAllStagesStopped(() => { var testData = ByteString.Create(new byte[] { 1, 2, 3, 4, 5 }); var server = new Server(this); var tcpWriteProbe = new TcpWriteProbe(this); var tcpReadProbe = new TcpReadProbe(this); Source.FromPublisher(tcpWriteProbe.PublisherProbe) .Via(Sys.TcpStream().OutgoingConnection(server.Address)) .To(Sink.FromSubscriber(tcpReadProbe.SubscriberProbe)) .Run(Materializer); var serverConnection = server.WaitAccept(); // Server can still write serverConnection.Write(testData); tcpReadProbe.Read(5).ShouldBeEquivalentTo(testData); // Close client side read tcpReadProbe.TcpReadSubscription.Value.Cancel(); // Client can still write tcpWriteProbe.Write(testData); serverConnection.Read(5); serverConnection.WaitRead().ShouldBeEquivalentTo(testData); serverConnection.ConfirmedClose(); // Close clint side write tcpWriteProbe.Close(); serverConnection.ExpectClosed(Akka.IO.Tcp.ConfirmedClosed.Instance); serverConnection.ExpectTerminated(); }, Materializer); }
public void Tcp_listen_stream_must_work_with_a_chain_of_echoes() { var serverAddress = TestUtils.TemporaryServerAddress(); var t = Sys.TcpStream() .Bind(serverAddress.Address.ToString(), serverAddress.Port) .ToMaterialized(EchoHandler(), Keep.Both) .Run(Materializer); var bindingFuture = t.Item1; var echoServerFinish = t.Item2; // make sure that the server has bound to the socket bindingFuture.Wait(100).Should().BeTrue(); var binding = bindingFuture.Result; var echoConnection = Sys.TcpStream().OutgoingConnection(serverAddress); var testInput = Enumerable.Range(0, 255).Select(i => ByteString.Create(new[] { Convert.ToByte(i) })).ToList(); var expectedOutput = testInput.Aggregate(ByteString.Empty, (agg, b) => agg.Concat(b)); var resultFuture = Source.From(testInput) .Via(echoConnection) // The echoConnection is reusable .Via(echoConnection) .Via(echoConnection) .Via(echoConnection) .RunAggregate(ByteString.Empty, (agg, b) => agg.Concat(b), Materializer); resultFuture.Wait(TimeSpan.FromSeconds(3)).Should().BeTrue(); resultFuture.Result.ShouldBeEquivalentTo(expectedOutput); binding.Unbind().Wait(TimeSpan.FromSeconds(3)).Should().BeTrue(); echoServerFinish.Wait(TimeSpan.FromSeconds(1)).Should().BeTrue(); }
private static ByteString Encode(ByteString payload, int fieldOffset, int fieldLength, ByteOrder byteOrder) { var h = new ByteStringBuilder().PutInt(payload.Count, byteOrder).Result(); var header = byteOrder == ByteOrder.LittleEndian ? h.Take(fieldLength) : h.Drop(4 - fieldLength); return(ByteString.Create(new byte[fieldOffset]) + header + payload); }
private static ByteString RandomByteString(int size) { var a = new byte[size]; new Random().NextBytes(a); return(ByteString.Create(a)); }
public void A_concatenated_bytestring_must_return_negative_one_when_an_element_was_not_found() { var b = ByteString.Create(new byte[] { 1 }) + ByteString.Create(new byte[] { 2 }); int offset = b.IndexOf(3); Assert.Equal(-1, offset); }
public void A_concatenated_bytestring_must_return_correct_index_of_elements_in_string() { var b = ByteString.Create(new byte[] { 1 }) + ByteString.Create(new byte[] { 2 }); int offset = b.IndexOf(2); Assert.Equal(1, offset); }
public void An_Outgoing_Connection_Must_Write_Data_After_Not_Acknowledged_Data() { new EstablishedConnectionTest(this).Run(x => { var writer = CreateTestProbe(); writer.Send(x.ConnectionActor, Tcp.Write.Create(ByteString.Create(new byte[] { 42 }, 0, 1), Tcp.NoAck.Instance)); writer.ExpectNoMsg(TimeSpan.FromMilliseconds(500)); }); }
static ByteOrderMark() { ByteString ZeroZero = ByteString.Create(new byte[] { 0x00, 0x00 }); UTF16_BE = ByteString.Create(new byte[] { 0xfe, 0xff }); UTF16_LE = ByteString.Create(new byte[] { 0xff, 0xfe }); UTF32_BE = ZeroZero + UTF16_BE; UTF32_LE = UTF16_LE + ZeroZero; UTF8 = ByteString.Create(new byte[] { 0xEF, 0xBB, 0xBF }); None = ByteString.Empty; }
public void InputStreamSink_should_return_less_than_was_expected_when_data_source_has_provided_some_but_not_enough_data() { this.AssertAllStagesStopped(() => { var inputStream = Source.Single(_byteString).RunWith(StreamConverters.AsInputStream(), _materializer); var arr = new byte[_byteString.Count + 1]; inputStream.Read(arr, 0, arr.Length).Should().Be(arr.Length - 1); inputStream.Close(); ByteString.Create(arr).ShouldBeEquivalentTo(_byteString.Concat(ByteString.Create(new byte[] { 0 }))); }, _materializer); }
private static ByteString GenerateByteString(int length) { var random = new Random(); var bytes = Enumerable.Range(0, 255) .Select(_ => random.Next(0, 255)) .Take(length) .Select(Convert.ToByte) .ToArray(); return(ByteString.Create(bytes)); }
private void ChitChat(TestSetup.ConnectionDetail actors, int rounds = 100) { var testData = ByteString.Create(new[] { (byte)0 }); Enumerable.Range(1, rounds).ForEach(_ => { actors.ClientHandler.Send(actors.ClientConnection, Tcp.Write.Create(testData)); actors.ServerHandler.ExpectMsg <Tcp.Received>(x => x.Data.Count == 1 && x.Data.Head == 0); actors.ServerHandler.Send(actors.ServerConnection, Tcp.Write.Create(testData)); actors.ClientHandler.ExpectMsg <Tcp.Received>(x => x.Data.Count == 1 && x.Data.Head == 0); }); }
public void The_TCP_transport_implementation_should_support_waiting_for_writes_with_backpressure() { new TestSetup(this).Run(x => { x.BindOptions = new[] { new Inet.SO.SendBufferSize(1024) }; x.ConnectOptions = new[] { new Inet.SO.SendBufferSize(1024) }; var actors = x.EstablishNewClientConnection(); actors.ServerHandler.Send(actors.ServerConnection, Tcp.Write.Create(ByteString.Create(new byte[100000]), Ack.Instance)); actors.ServerHandler.ExpectMsg(Ack.Instance); x.ExpectReceivedData(actors.ClientHandler, 100000); }); }
public OutputStreamSourceSpec(ITestOutputHelper helper) : base(Utils.UnboundedMailboxConfig, helper) { Sys.Settings.InjectTopLevelFallback(ActorMaterializer.DefaultConfig()); var settings = ActorMaterializerSettings.Create(Sys).WithDispatcher("akka.actor.default-dispatcher"); _materializer = Sys.Materializer(settings); _bytesArray = new[] { Convert.ToByte(new Random().Next(256)), Convert.ToByte(new Random().Next(256)), Convert.ToByte(new Random().Next(256)) }; _byteString = ByteString.Create(_bytesArray); }
public void Outgoing_TCP_stream_must_be_able_to_write_a_sequence_of_ByteStrings() { var server = new Server(this); var testInput = Enumerable.Range(0, 256).Select(i => ByteString.Create(new[] { Convert.ToByte(i) })); var expectedOutput = ByteString.Create(Enumerable.Range(0, 256).Select(Convert.ToByte).ToArray()); Source.From(testInput) .Via(Sys.TcpStream().OutgoingConnection(server.Address)) .To(Sink.Ignore <ByteString>()) .Run(Materializer); var serverConnection = server.WaitAccept(); serverConnection.Read(256); serverConnection.WaitRead().ShouldBeEquivalentTo(expectedOutput); }
public void Outgoing_TCP_stream_must_work_when_client_closes_read_then_client_closes_write() { this.AssertAllStagesStopped(() => { var testData = ByteString.Create(new byte[] { 1, 2, 3, 4, 5 }); var server = new Server(this); var tcpWriteProbe = new TcpWriteProbe(this); var tcpReadProbe = new TcpReadProbe(this); Source.FromPublisher(tcpWriteProbe.PublisherProbe) .Via(Sys.TcpStream().OutgoingConnection(server.Address)) .To(Sink.FromSubscriber(tcpReadProbe.SubscriberProbe)) .Run(Materializer); var serverConnection = server.WaitAccept(); // Server can still write serverConnection.Write(testData); tcpReadProbe.Read(5).ShouldBeEquivalentTo(testData); // Close client side read tcpReadProbe.TcpReadSubscription.Value.Cancel(); // Client can still write tcpWriteProbe.Write(testData); serverConnection.Read(5); serverConnection.WaitRead().ShouldBeEquivalentTo(testData); // Close client side write tcpWriteProbe.Close(); // Need a write on the server side to detect the close event AwaitAssert(() => { serverConnection.Write(testData); serverConnection.ExpectClosed(c => c.IsErrorClosed, TimeSpan.FromMilliseconds(500)); }, TimeSpan.FromSeconds(5)); serverConnection.ExpectTerminated(); }, Materializer); }
public void Outgoing_TCP_stream_must_materialize_correctly_when_used_in_multiple_flows() { var testData = ByteString.Create(new byte[] { 1, 2, 3, 4, 5 }); var server = new Server(this); var tcpWriteProbe1 = new TcpWriteProbe(this); var tcpReadProbe1 = new TcpReadProbe(this); var tcpWriteProbe2 = new TcpWriteProbe(this); var tcpReadProbe2 = new TcpReadProbe(this); var outgoingConnection = new Tcp().CreateExtension(Sys as ExtendedActorSystem).OutgoingConnection(server.Address); var conn1F = Source.FromPublisher(tcpWriteProbe1.PublisherProbe) .ViaMaterialized(outgoingConnection, Keep.Both) .To(Sink.FromSubscriber(tcpReadProbe1.SubscriberProbe)) .Run(Materializer).Item2; var serverConnection1 = server.WaitAccept(); var conn2F = Source.FromPublisher(tcpWriteProbe2.PublisherProbe) .ViaMaterialized(outgoingConnection, Keep.Both) .To(Sink.FromSubscriber(tcpReadProbe2.SubscriberProbe)) .Run(Materializer).Item2; var serverConnection2 = server.WaitAccept(); ValidateServerClientCommunication(testData, serverConnection1, tcpReadProbe1, tcpWriteProbe1); ValidateServerClientCommunication(testData, serverConnection2, tcpReadProbe2, tcpWriteProbe2); conn1F.Wait(TimeSpan.FromSeconds(1)).Should().BeTrue(); conn2F.Wait(TimeSpan.FromSeconds(1)).Should().BeTrue(); var conn1 = conn1F.Result; var conn2 = conn2F.Result; // Since we have already communicated over the connections we can have short timeouts for the tasks ((IPEndPoint)conn1.RemoteAddress).Port.Should().Be(((IPEndPoint)server.Address).Port); ((IPEndPoint)conn2.RemoteAddress).Port.Should().Be(((IPEndPoint)server.Address).Port); ((IPEndPoint)conn1.LocalAddress).Port.Should().NotBe(((IPEndPoint)conn2.LocalAddress).Port); tcpWriteProbe1.Close(); tcpReadProbe1.Close(); server.Close(); }
public void Tcp_listen_stream_must_not_shut_down_connections_after_the_connection_stream_cacelled() { this.AssertAllStagesStopped(() => { var serverAddress = TestUtils.TemporaryServerAddress(); Sys.TcpStream() .Bind(serverAddress.Address.ToString(), serverAddress.Port) .Take(1).RunForeach(c => { Thread.Sleep(1000); c.Flow.Join(Flow.Create <ByteString>()).Run(Materializer); }, Materializer); var total = Source.From( Enumerable.Range(0, 1000).Select(_ => ByteString.Create(new byte[] { 0 }))) .Via(Sys.TcpStream().OutgoingConnection(serverAddress)) .RunAggregate(0, (i, s) => i + s.Count, Materializer); total.Wait(TimeSpan.FromSeconds(3)).Should().BeTrue(); total.Result.Should().Be(1000); }, Materializer); }
public void Outgoing_TCP_stream_must_be_able_to_read_a_sequence_of_ByteStrings() { var server = new Server(this); var testInput = Enumerable.Range(0, 255).Select(i => ByteString.Create(new[] { Convert.ToByte(i) })); var expectedOutput = ByteString.Create(Enumerable.Range(0, 255).Select(Convert.ToByte).ToArray()); var idle = new TcpWriteProbe(this); //Just register an idle upstream var resultFuture = Source.FromPublisher(idle.PublisherProbe) .Via(Sys.TcpStream().OutgoingConnection(server.Address)) .RunAggregate(ByteString.Empty, (acc, input) => acc + input, Materializer); var serverConnection = server.WaitAccept(); foreach (var input in testInput) { serverConnection.Write(input); } serverConnection.ConfirmedClose(); resultFuture.Wait(TimeSpan.FromSeconds(3)).Should().BeTrue(); resultFuture.Result.ShouldBeEquivalentTo(expectedOutput); }
public void Outgoing_TCP_stream_must_work_in_the_happy_case() { this.AssertAllStagesStopped(() => { var testData = ByteString.Create(new byte[] { 1, 2, 3, 4, 5 }); var server = new Server(this); var tcpReadProbe = new TcpReadProbe(this); var tcpWriteProbe = new TcpWriteProbe(this); Source.FromPublisher(tcpWriteProbe.PublisherProbe) .Via(Sys.TcpStream().OutgoingConnection(server.Address)) .To(Sink.FromSubscriber(tcpReadProbe.SubscriberProbe)) .Run(Materializer); var serverConnection = server.WaitAccept(); ValidateServerClientCommunication(testData, serverConnection, tcpReadProbe, tcpWriteProbe); tcpWriteProbe.Close(); tcpReadProbe.Close(); server.Close(); }, Materializer); }
public void Outgoing_TCP_stream_must_Echo_should_work_even_if_server_is_in_full_close_mode() { var serverAddress = TestUtils.TemporaryServerAddress(); var task = Sys.TcpStream() .Bind(serverAddress.Address.ToString(), serverAddress.Port, halfClose: false) .ToMaterialized( Sink.ForEach <Tcp.IncomingConnection>(conn => conn.Flow.Join(Flow.Create <ByteString>())), Keep.Left) .Run(Materializer); task.Wait(TimeSpan.FromSeconds(3)).Should().BeTrue(); var binding = task.Result; var result = Source.From(Enumerable.Repeat(0, 1000) .Select(i => ByteString.Create(new[] { Convert.ToByte(i) }))) .Via(Sys.TcpStream().OutgoingConnection(serverAddress)) .RunAggregate(0, (i, s) => i + s.Count, Materializer); result.Wait(TimeSpan.FromSeconds(3)).Should().BeTrue(); result.Result.Should().Be(1000); binding.Unbind(); }
public Tcp.Write WriteCmd(Tcp.Event ack) { return(Tcp.Write.Create(ByteString.Create(new byte[TestSize]), ack)); }
public override void Write(byte[] buffer, int offset, int count) => SendData(ByteString.Create(buffer, offset, count));