public async Task CompositeSinkWriter_WithSuccessfulWriteInPrimarySink_ShouldNotCallSecondarySink()
        {
            var waitHandle  = new ManualResetEvent(false);
            var redisClient = new Mock <ILogSink>();

            redisClient.Setup(x => x.WriteAsync(It.IsAny <ILog>(), It.IsAny <byte[]>())).Returns(() =>
            {
                waitHandle.Set();
                return(Task.CompletedTask);
            });

            var firehoseClient = new Mock <ILogSink>();

            firehoseClient.Setup(x => x.WriteAsync(It.IsAny <ILog>(), It.IsAny <byte[]>())).Returns(() =>
            {
                waitHandle.Set();
                return(Task.CompletedTask);
            });

            var compositeLogWriter = new CompositeSink(JsonLogFormatter.Instance, new List <ILogSink> {
                firehoseClient.Object, redisClient.Object
            });

            var log = GetApiLog();
            await compositeLogWriter.WriteAsync(log, Format(log));

            waitHandle.WaitOne();

            firehoseClient.Verify(x => x.WriteAsync(It.IsAny <ILog>(), It.IsAny <byte[]>()), Times.Once);
            redisClient.Verify(x => x.WriteAsync(It.IsAny <ILog>(), It.IsAny <byte[]>()), Times.Never);
        }
        public async Task CompositeSinkWriter_WithUnSuccessfulWriteInPrimarySink_ShouldWriteToSecondarySink()
        {
            var waitHandle      = new ManualResetEvent(false);
            var redisClient     = new Mock <ILogSink>();
            var redisWriteCount = 0;

            redisClient.Setup(x => x.WriteAsync(It.IsAny <ILog>(), It.IsAny <byte[]>()))
            .Callback(() =>
            {
                if (redisWriteCount == 1)
                {
                    waitHandle.Set();
                }
                redisWriteCount++;
            })
            .Returns(() =>
            {
                return(Task.CompletedTask);
            });

            var firehoseClient = new Mock <ILogSink>();

            firehoseClient.Setup(x => x.WriteAsync(It.IsAny <ILog>(), It.IsAny <byte[]>())).Throws(new Exception());

            var compositeLogWriter = new CompositeSink(JsonLogFormatter.Instance, new List <ILogSink> {
                firehoseClient.Object, redisClient.Object
            });

            var log = GetApiLog();
            await compositeLogWriter.WriteAsync(log, Format(log));

            waitHandle.WaitOne();

            firehoseClient.Verify(x => x.WriteAsync(It.IsAny <ILog>(), It.IsAny <byte[]>()), Times.Once);
            redisClient.Verify(x => x.WriteAsync(It.IsAny <ILog>(), It.IsAny <byte[]>()), Times.Exactly(2));
        }
        public async Task CompositeSinkWriter_WithUnSuccessfullWriteInBothSink_ShouldThrowException()
        {
            var waitHandle  = new ManualResetEvent(false);
            var redisClient = new Mock <ILogSink>();

            redisClient.Setup(x => x.WriteAsync(It.IsAny <ILog>(), It.IsAny <byte[]>())).Callback(() => waitHandle.Set()).Throws(new Exception());

            var firehoseClient = new Mock <ILogSink>();

            firehoseClient.Setup(x => x.WriteAsync(It.IsAny <ILog>(), It.IsAny <byte[]>())).Throws(new Exception());

            var compositeLogWriter = new CompositeSink(JsonLogFormatter.Instance, new List <ILogSink> {
                firehoseClient.Object, redisClient.Object
            });

            var log = GetApiLog();

            await Assert.ThrowsAsync <AggregateException>(async() => await compositeLogWriter.WriteAsync(log, Format(log)));

            waitHandle.WaitOne();

            firehoseClient.Verify(x => x.WriteAsync(It.IsAny <ILog>(), It.IsAny <byte[]>()), Times.Once);
            redisClient.Verify(x => x.WriteAsync(It.IsAny <ILog>(), It.IsAny <byte[]>()), Times.Exactly(2));
        }
 public async Task CompositeSinkWriter_WithNullSink_ShouldReturnWithoutWritingLogs()
 {
     var compositeLogWriter = new CompositeSink(JsonLogFormatter.Instance, null);
     var log = GetApiLog();
     await compositeLogWriter.WriteAsync(log, Format(log));
 }