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);
        }
예제 #2
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");
        }
예제 #3
0
        public void OutputStreamSource_must_use_dedicated_default_blocking_io_dispatcher_by_default()
        {
            this.AssertAllStagesStopped(() =>
            {
                var sys          = ActorSystem.Create("dispatcher-testing", Utils.UnboundedMailboxConfig);
                var materializer = sys.Materializer();

                try
                {
                    StreamConverters.AsOutputStream().RunWith(this.SinkProbe <ByteString>(), materializer);
                    ((ActorMaterializerImpl)materializer).Supervisor.Tell(StreamSupervisor.GetChildren.Instance,
                                                                          TestActor);
                    var actorRef = ExpectMsg <StreamSupervisor.Children>()
                                   .Refs.First(c => c.Path.ToString().Contains("outputStreamSource"));
                    Utils.AssertDispatcher(actorRef, "akka.stream.default-blocking-io-dispatcher");
                }
                finally
                {
                    Shutdown(sys);
                }
            }, _materializer);
        }
예제 #4
0
        public void OutputStreamSource_must_correctly_complete_the_stage_after_close()
        {
            // actually this was a race, so it only happened in at least one of 20 runs

            const int bufferSize = 4;

            var t = StreamConverters.AsOutputStream(Timeout)
                    .AddAttributes(Attributes.CreateInputBuffer(bufferSize, bufferSize))
                    .ToMaterialized(this.SinkProbe <ByteString>(), Keep.Both)
                    .Run(_materializer);
            var outputStream = t.Item1;
            var probe        = t.Item2;

            // fill the buffer up
            Enumerable.Range(1, bufferSize - 1).ForEach(i => outputStream.WriteByte((byte)i));

            Task.Run(() => outputStream.Dispose());

            // here is the race, has the elements reached the stage buffer yet?
            Thread.Sleep(500);
            probe.Request(bufferSize - 1);
            probe.ExpectNextN(bufferSize - 1);
            probe.ExpectComplete();
        }