public void TestAppInboundChannel() { var consumer = new MockChannelReaderConsumer(); var accessSafely = consumer.AfterCompleting(2); _appReader.OpenFor(consumer); var buffer = new MemoryStream(1024); buffer.SetLength(1024); var message1 = AppMessage + 1; var rawMessage1 = RawMessage.From(0, 0, message1); _appChannel.Write(rawMessage1.AsStream(buffer)); ProbeUntilConsumed(() => accessSafely.ReadFromNow <int>("count") < 1, _appReader); Assert.Equal(1, accessSafely.ReadFromNow <int>("count")); Assert.Equal(message1, consumer.Messages.First()); var message2 = AppMessage + 2; var rawMessage2 = RawMessage.From(0, 0, message2); _appChannel.Write(rawMessage2.AsStream(buffer)); ProbeUntilConsumed(() => accessSafely.ReadFrom <int>("count") < 2, _appReader); Assert.Equal(2, accessSafely.ReadFrom <int>("count")); Assert.Equal(message2, consumer.Messages.Last()); }
//========================================= // Startable //========================================= public override void Start() { if (IsStopped) { return; } Logger.Debug($"Inbound stream listening: for '{_reader.Name}'"); try { _reader.OpenFor(this); } catch (Exception e) { _reader.Close(); Logger.Error("OpenFor failed", e); throw new InvalidOperationException(e.Message, e); } _cancellable = Stage.Scheduler.Schedule(SelfAs <IScheduled <object?> >(), null, TimeSpan.FromMilliseconds(1000), TimeSpan.FromMilliseconds(_probeInterval)); }
public void TestChannelWriter() { var consumer = new MockChannelReaderConsumer("consume"); var consumeCount = 0; var accessSafely = AccessSafely.Immediately() .WritingWith <int>("consume", (value) => consumeCount += value) .ReadingWith("consume", () => consumeCount); consumer.UntilConsume = accessSafely; _channelReader.OpenFor(consumer); var buffer = new MemoryStream(1024); buffer.SetLength(1024); var message1 = TestMessage + 1; var rawMessage1 = RawMessage.From(0, 0, message1); _channelWriter.Write(rawMessage1, buffer); ProbeUntilConsumed(() => accessSafely.ReadFrom <int>("consume") < 1, _channelReader); Assert.Equal(1, consumer.UntilConsume.ReadFrom <int>("consume")); Assert.Equal(message1, consumer.Messages.First()); var message2 = TestMessage + 2; var rawMessage2 = RawMessage.From(0, 0, message2); _channelWriter.Write(rawMessage2, buffer); ProbeUntilConsumed(() => accessSafely.ReadFrom <int>("consume") < 2, _channelReader); Assert.Equal(2, consumer.UntilConsume.ReadFrom <int>("consume")); Assert.Equal(message2, consumer.Messages.Last()); }