Ejemplo n.º 1
0
        public async Task Issue_4580_EmptyByteStringCausesPipeToBeClosed()
        {
            var serverPipe = new NamedPipeServerStream("unique-pipe-name", PipeDirection.Out, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous, 20, 20);
            var serverPipeConnectionTask = serverPipe.WaitForConnectionAsync();

            var clientPipe = new NamedPipeClientStream(".", "unique-pipe-name", PipeDirection.In, PipeOptions.Asynchronous);
            clientPipe.Connect();

            await serverPipeConnectionTask;
            var cnt = 0;

            var writeToStreamTask = Source.From(Enumerable.Range(0, 100))
                .Select(i => ByteString.FromString(i.ToString()))
                .Select(bs => cnt++ == 10 ? ByteString.Empty : bs) // ByteString.Empty.ToArray() failed in original bug
                .ToMaterialized(StreamConverters.FromOutputStream(() => serverPipe), Keep.Right)
                .Run(Materializer);

            var result = new List<string>();
            var readFromStreamTask = StreamConverters.FromInputStream(() => clientPipe, 1)
                .RunForeach(bs => result.Add(bs.ToString(Encoding.ASCII)), Materializer);

            await Task.WhenAll(writeToStreamTask, readFromStreamTask);

            var expected = Enumerable.Range(0, 100)
                .SelectMany(i => i == 10 ? Array.Empty<string>() : i.ToString().Select(c => c.ToString()));
            expected.SequenceEqual(result).ShouldBeTrue();
        }
Ejemplo n.º 2
0
 public override IPublisher <ByteString> CreatePublisher(long elements)
 {
     return(StreamConverters.FromInputStream(() => new InputStream())
            .WithAttributes(ActorAttributes.CreateDispatcher("akka.test.stream-dispatcher"))
            .Take(elements)
            .RunWith(Sink.AsPublisher <ByteString>(false), Materializer));
 }
Ejemplo n.º 3
0
        public void OutputStreamSource_must_not_block_flushes_when_buffer_is_empty()
        {
            this.AssertAllStagesStopped(() =>
            {
                var t = StreamConverters.AsOutputStream()
                        .ToMaterialized(this.SinkProbe <ByteString>(), Keep.Both)
                        .Run(_materializer);
                var outputStream = t.Item1;
                var probe        = t.Item2;
                var s            = probe.ExpectSubscription();

                outputStream.Write(_bytesArray, 0, _byteString.Count);
                var f = Task.Run(() =>
                {
                    outputStream.Flush();
                    return(NotUsed.Instance);
                });
                s.Request(1);
                ExpectSuccess(f, NotUsed.Instance);
                probe.ExpectNext(_byteString);

                var f2 = Task.Run(() =>
                {
                    outputStream.Flush();
                    return(NotUsed.Instance);
                });
                ExpectSuccess(f2, NotUsed.Instance);

                outputStream.Dispose();
                probe.ExpectComplete();
            }, _materializer);
        }
Ejemplo n.º 4
0
        public void OutputStreamSource_must_block_writes_when_buffer_is_full()
        {
            this.AssertAllStagesStopped(() =>
            {
                var t = StreamConverters.AsOutputStream()
                        .WithAttributes(Attributes.CreateInputBuffer(16, 16))
                        .ToMaterialized(this.SinkProbe <ByteString>(), Keep.Both)
                        .Run(_materializer);
                var outputStream = t.Item1;
                var probe        = t.Item2;
                var s            = probe.ExpectSubscription();

                for (var i = 1; i <= 16; i++)
                {
                    outputStream.Write(_bytesArray, 0, _byteString.Count);
                }

                //blocked call
                var f = Task.Run(() =>
                {
                    outputStream.Write(_bytesArray, 0, _byteString.Count);
                    return(NotUsed.Instance);
                });
                ExpectTimeout(f, Timeout);
                probe.ExpectNoMsg(TimeSpan.MinValue);

                s.Request(17);
                ExpectSuccess(f, NotUsed.Instance);
                probe.ExpectNextN(Enumerable.Repeat(_byteString, 17).ToList());

                outputStream.Dispose();
                probe.ExpectComplete();
            }, _materializer);
        }
Ejemplo n.º 5
0
        public void OutputStreamSource_must_block_flush_call_until_send_all_buffer_to_downstream()
        {
            this.AssertAllStagesStopped(() =>
            {
                var t = StreamConverters.AsOutputStream()
                        .ToMaterialized(this.SinkProbe <ByteString>(), Keep.Both)
                        .Run(_materializer);
                var outputStream = t.Item1;
                var probe        = t.Item2;
                var s            = probe.ExpectSubscription();

                outputStream.Write(_bytesArray, 0, _bytesArray.Length);
                var f = Task.Run(() =>
                {
                    outputStream.Flush();
                    return(NotUsed.Instance);
                });

                ExpectTimeout(f, Timeout);
                probe.ExpectNoMsg(TimeSpan.MinValue);

                s.Request(1);
                ExpectSuccess(f, NotUsed.Instance);
                probe.ExpectNext(_byteString);

                outputStream.Dispose();
                probe.ExpectComplete();
            }, _materializer);
        }
Ejemplo n.º 6
0
            public static GraphBuilder CreateParsingGraphBuilder(Stream stream, Func <AkkaIO.ByteString, ModuleLogMessageData> parserFunc)
            {
                var source = StreamConverters.FromInputStream(() => stream);
                var graph  = source
                             .Via(FramingFlow)
                             .Select(parserFunc)
                             .MapMaterializedValue(_ => AkkaNet.NotUsed.Instance);

                return(new GraphBuilder(graph));
            }
Ejemplo n.º 7
0
        public void InputStreamSink_should_throw_from_inputstream_read_if_terminated_abruptly()
        {
            var materializer = ActorMaterializer.Create(Sys);
            var probe        = this.CreatePublisherProbe <ByteString>();
            var inputStream  = Source.FromPublisher(probe).RunWith(StreamConverters.AsInputStream(), materializer);

            materializer.Shutdown();

            inputStream.Invoking(i => i.ReadByte()).Should().Throw <AbruptTerminationException>();
        }
Ejemplo n.º 8
0
        public void OutputStreamSink_must_complete_materialized_value_with_the_error()
        {
            this.AssertAllStagesStopped(() =>
            {
                var completion = Source.Failed <ByteString>(new Exception("Boom!"))
                                 .RunWith(StreamConverters.FromOutputStream(() => new OutputStream()), _materializer);

                AssertThrows <AbruptIOTerminationException>(completion.Wait);
            }, _materializer);
        }
Ejemplo n.º 9
0
        public void OutputStreamSink_must_close_underlying_stream_when_error_received()
        {
            this.AssertAllStagesStopped(() =>
            {
                var p = CreateTestProbe();
                Source.Failed <ByteString>(new Exception("Boom!"))
                .RunWith(StreamConverters.FromOutputStream(() => new CloseOutputStream(p)), _materializer);

                p.ExpectMsg("closed");
            }, _materializer);
        }
Ejemplo n.º 10
0
        public void InputStreamSource_must_read_bytes_from_InputStream()
        {
            this.AssertAllStagesStopped(() =>
            {
                var f = StreamConverters.FromInputStream(() => new ListInputStream(new[] { "a", "b", "c" }))
                        .RunWith(Sink.First <ByteString>(), _materializer);

                f.Wait(TimeSpan.FromSeconds(3)).Should().BeTrue();
                f.Result.ShouldBeEquivalentTo(ByteString.FromString("abc"));
            }, _materializer);
        }
Ejemplo n.º 11
0
        public void OutputStreamSink_must_close_underlying_stream_when_completion_received()
        {
            this.AssertAllStagesStopped(() =>
            {
                var p = CreateTestProbe();
                Source.Empty <ByteString>()
                .RunWith(StreamConverters.FromOutputStream(() => new CompletionOutputStream(p)), _materializer);

                p.ExpectMsg("closed");
            }, _materializer);
        }
Ejemplo n.º 12
0
 public void InputStreamSink_should_fail_to_materialize_with_zero_sized_input_buffer()
 {
     Action a = () => Source.Single(_byteString).RunWith(StreamConverters.AsInputStream(Timeout).WithAttributes(Attributes.CreateInputBuffer(0, 0)), _materializer);
     a.ShouldThrow<ArgumentException>();
     /*
     With Source.single we test the code path in which the sink
     itself throws an exception when being materialized. If
     Source.empty is used, the same exception is thrown by
     Materializer.
     */
 }
Ejemplo n.º 13
0
        public void Setup(BenchmarkContext context)
        {
            _actorSystem  = ActorSystem.Create("FileSourcesBenchmark");
            _materializer = _actorSystem.Materializer();

            _file = CreateFile();

            _fileChannelSource     = FileIO.FromFile(_file, BufferSize);
            _fileInputStreamSource = StreamConverters.FromInputStream(() => File.OpenRead(_file.FullName), BufferSize);
            _ioSourceLinesIterator = Source.FromEnumerator(() => File.ReadLines(_file.FullName).Select(ByteString.FromString).GetEnumerator());
        }
Ejemplo n.º 14
0
 public void InputStreamSink_should_read_bytes_from_input_stream()
 {
     this.AssertAllStagesStopped(() =>
     {
         var inputStream = Source.Single(_byteString).RunWith(StreamConverters.AsInputStream(), _materializer);
         var result      = ReadN(inputStream, _byteString.Count);
         inputStream.Close();
         result.Item1.Should().Be(_byteString.Count);
         result.Item2.Should().BeEquivalentTo(_byteString);
     }, _materializer);
 }
Ejemplo n.º 15
0
            public static IRunnableGraph <Task <IImmutableList <string> > > BuildMaterializedGraph(Stream stream)
            {
                var source  = StreamConverters.FromInputStream(() => stream);
                var seqSink = Sink.Seq <string>();
                IRunnableGraph <Task <IImmutableList <string> > > graph = source
                                                                          .Via(FramingFlow)
                                                                          .Select(b => b.Slice(8))
                                                                          .Select(b => b.ToString(Encoding.UTF8))
                                                                          .ToMaterialized(seqSink, Keep.Right);

                return(graph);
            }
Ejemplo n.º 16
0
        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);
        }
Ejemplo n.º 17
0
        public void InputStreamSink_should_work_when_more_bytes_pulled_from_input_stream_than_available()
        {
            this.AssertAllStagesStopped(() =>
            {
                var inputStream = Source.Single(_byteString).RunWith(StreamConverters.AsInputStream(), _materializer);

                var r = ReadN(inputStream, _byteString.Count * 2);
                r.Item1.Should().Be(_byteString.Count);
                r.Item2.ShouldBeEquivalentTo(_byteString);

                inputStream.Close();
            }, _materializer);
        }
Ejemplo n.º 18
0
        public void InputStreamSink_should_throw_exception_when_call_read_With_wrong_parameters()
        {
            this.AssertAllStagesStopped(() =>
            {
                var inputStream = Source.Single(_byteString).RunWith(StreamConverters.AsInputStream(), _materializer);
                var buf         = new byte[3];

                Action(() => inputStream.Read(buf, -1, 2)).ShouldThrow <ArgumentException>();
                Action(() => inputStream.Read(buf, 0, 5)).ShouldThrow <ArgumentException>();
                Action(() => inputStream.Read(new byte[0], 0, 1)).ShouldThrow <ArgumentException>();
                Action(() => inputStream.Read(buf, 0, 0)).ShouldThrow <ArgumentException>();
            }, _materializer);
        }
Ejemplo n.º 19
0
        public void InputStreamSource_must_emit_as_soon_as_read()
        {
            this.AssertAllStagesStopped(() =>
            {
                var latch = new TestLatch(1);
                var probe = StreamConverters.FromInputStream(() => new EmittedInputStream(latch), chunkSize: 1)
                            .RunWith(this.SinkProbe <ByteString>(), _materializer);

                probe.Request(4);
                probe.ExpectNext(ByteString.FromString("M"));
                latch.CountDown();
                probe.ExpectComplete();
            }, _materializer);
        }
Ejemplo n.º 20
0
        public void InputStreamSink_should_return_minus_1_when_read_after_stream_is_completed()
        {
            this.AssertAllStagesStopped(() =>
            {
                var inputStream = Source.Single(_byteString).RunWith(StreamConverters.AsInputStream(), _materializer);

                var r = ReadN(inputStream, _byteString.Count);
                r.Item1.Should().Be(_byteString.Count);
                r.Item2.ShouldBeEquivalentTo(_byteString);

                inputStream.ReadByte().Should().Be(-1);
                inputStream.Close();
            }, _materializer);
        }
Ejemplo n.º 21
0
        public void InputStreamSink_should_read_next_byte_as_an_int_from_InputStream()
        {
            this.AssertAllStagesStopped(() =>
            {
                var bytes       = ByteString.CopyFrom(new byte[] { 0, 100, 200, 255 });
                var inputStream = Source.Single(bytes).RunWith(StreamConverters.AsInputStream(), _materializer);

                Enumerable.Range(1, 5)
                .Select(_ => inputStream.ReadByte())
                .Should().BeEquivalentTo(new[] { 0, 100, 200, 255, -1 });

                inputStream.Dispose();
            }, _materializer);
        }
Ejemplo n.º 22
0
        public void OutputStreamSource_must_fail_to_materialize_with_zero_sized_input_buffer()
        {
            new Action(
                () =>
                StreamConverters.AsOutputStream(Timeout)
                .WithAttributes(Attributes.CreateInputBuffer(0, 0))
                .RunWith(Sink.First <ByteString>(), _materializer)).ShouldThrow <ArgumentException>();

            /*
             * With Sink.First we test the code path in which the source
             * itself throws an exception when being materialized. If
             * Sink.Ignore is used, the same exception is thrown by
             * Materializer.
             */
        }
Ejemplo n.º 23
0
        public async Task <IReadOnlyList <string> > GetText(Stream stream)
        {
            Preconditions.CheckNotNull(stream, nameof(stream));
            var source  = StreamConverters.FromInputStream(() => stream);
            var seqSink = Sink.Seq <string>();
            IRunnableGraph <Task <IImmutableList <string> > > graph = source
                                                                      .Via(FramingFlow)
                                                                      .Select(b => b.Slice(8))
                                                                      .Select(b => b.ToString(Encoding.UTF8))
                                                                      .ToMaterialized(seqSink, Keep.Right);

            IImmutableList <string> result = await graph.Run(this.materializer);

            return(result);
        }
Ejemplo n.º 24
0
        public async Task <IReadOnlyList <ModuleLogMessage> > GetMessages(Stream stream, string moduleId)
        {
            Preconditions.CheckNotNull(stream, nameof(stream));
            Preconditions.CheckNonWhiteSpace(moduleId, nameof(moduleId));

            var source  = StreamConverters.FromInputStream(() => stream);
            var seqSink = Sink.Seq <ModuleLogMessage>();
            IRunnableGraph <Task <IImmutableList <ModuleLogMessage> > > graph = source
                                                                                .Via(FramingFlow)
                                                                                .Select(b => this.logMessageParser.Parse(b, moduleId))
                                                                                .ToMaterialized(seqSink, Keep.Right);

            IImmutableList <ModuleLogMessage> result = await graph.Run(this.materializer);

            return(result);
        }
Ejemplo n.º 25
0
        public void OutputStreamSource_must_throw_error_when_writer_after_stream_is_closed()
        {
            this.AssertAllStagesStopped(() =>
            {
                var t = StreamConverters.AsOutputStream()
                        .ToMaterialized(this.SinkProbe <ByteString>(), Keep.Both)
                        .Run(_materializer);
                var outputStream = t.Item1;
                var probe        = t.Item2;

                probe.ExpectSubscription();
                outputStream.Dispose();
                probe.ExpectComplete();

                outputStream.Invoking(s => s.Write(_bytesArray, 0, _byteString.Count)).ShouldThrow <IOException>();
            }, _materializer);
        }
Ejemplo n.º 26
0
        public void InputStreamSink_should_throw_error_when_reactive_stream_is_closed()
        {
            this.AssertAllStagesStopped(() =>
            {
                var t = this.SourceProbe <ByteString>()
                        .ToMaterialized(StreamConverters.AsInputStream(), Keep.Both)
                        .Run(_materializer);
                var probe       = t.Item1;
                var inputStream = t.Item2;

                probe.SendNext(_byteString);
                inputStream.Close();
                probe.ExpectCancellation();

                Action block = () => inputStream.Read(new byte[1], 0, 1);
                block.ShouldThrow <IOException>();
            }, _materializer);
        }
Ejemplo n.º 27
0
        public void OutputStreamSource_must_read_bytes_from_OutputStream()
        {
            this.AssertAllStagesStopped(() =>
            {
                var t = StreamConverters.AsOutputStream()
                        .ToMaterialized(this.SinkProbe <ByteString>(), Keep.Both)
                        .Run(_materializer);
                var outputStream = t.Item1;
                var probe        = t.Item2;
                var s            = probe.ExpectSubscription();

                outputStream.Write(_bytesArray, 0, _bytesArray.Length);
                s.Request(1);
                probe.ExpectNext(_byteString);
                outputStream.Dispose();
                probe.ExpectComplete();
            }, _materializer);
        }
Ejemplo n.º 28
0
        public void InputStreamSink_should_work_when_read_chunks_smaller_then_stream_chunks()
        {
            this.AssertAllStagesStopped(() =>
            {
                var bytes       = RandomByteString(10);
                var inputStream = Source.Single(bytes).RunWith(StreamConverters.AsInputStream(), _materializer);

                while (bytes.NonEmpty)
                {
                    var expected = bytes.Take(3);
                    bytes        = bytes.Drop(3);

                    var result = ReadN(inputStream, 3);
                    result.Item1.Should().Be(expected.Count);
                    result.Item2.ShouldBeEquivalentTo(expected);
                }

                inputStream.Close();
            }, _materializer);
        }
Ejemplo n.º 29
0
 public void InputStreamSink_should_use_dedicated_default_blocking_io_dispatcher_by_default()
 {
     this.AssertAllStagesStopped(() =>
     {
         var sys          = ActorSystem.Create("dispatcher-testing", Utils.UnboundedMailboxConfig);
         var materializer = ActorMaterializer.Create(sys);
         try
         {
             this.SourceProbe <ByteString>().RunWith(StreamConverters.AsInputStream(), materializer);
             (materializer as ActorMaterializerImpl).Supervisor.Tell(StreamSupervisor.GetChildren.Instance, TestActor);
             var children = ExpectMsg <StreamSupervisor.Children>().Refs;
             var actorRef = children.First(c => c.Path.ToString().Contains("inputStreamSink"));
             Utils.AssertDispatcher(actorRef, "akka.stream.default-blocking-io-dispatcher");
         }
         finally
         {
             Shutdown(sys);
         }
     }, _materializer);
 }
Ejemplo n.º 30
0
        public void OutputStreamSource_must_not_leave_blocked_threads()
        {
            var tuple =
                StreamConverters.AsOutputStream(Timeout)
                .ToMaterialized(this.SinkProbe <ByteString>(), Keep.Both)
                .Run(_materializer);
            var outputStream = tuple.Item1;
            var probe        = tuple.Item2;

            var sub = probe.ExpectSubscription();

            // triggers a blocking read on the queue
            // and then cancel the stage before we got anything
            sub.Request(1);
            sub.Cancel();

            //we need to make sure that the underling BlockingCollection isn't blocked after the stream has finished,
            //the jvm way isn't working so we need to use reflection and check the collection directly
            //def threadsBlocked =
            //ManagementFactory.getThreadMXBean.dumpAllThreads(true, true).toSeq
            //          .filter(t => t.getThreadName.startsWith("OutputStreamSourceSpec") &&
            //t.getLockName != null &&
            //t.getLockName.startsWith("java.util.concurrent.locks.AbstractQueuedSynchronizer"))
            //awaitAssert(threadsBlocked should === (Seq()), 3.seconds)

            var bindFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic |
                            BindingFlags.Static;
            var field = typeof(OutputStreamAdapter).GetField("_dataQueue", bindFlags);
            var blockingCollection = field.GetValue(outputStream) as BlockingCollection <ByteString>;

            //give the stage enough time to finish, otherwise it may take the hello message
            Thread.Sleep(1000);

            // if a take operation is pending inside the stage it will steal this one and the next take will not succeed
            blockingCollection.Add(ByteString.FromString("hello"));

            ByteString result;

            blockingCollection.TryTake(out result, TimeSpan.FromSeconds(3)).Should().BeTrue();
            result.ToString().Should().Be("hello");
        }