public void when_cannot_connect_to_database_then_on_completed_should_not_stall_or_throw()
        {
            const string validNotExisting = @"Data Source=(localdb)\v11.0; AttachDBFilename='|DataDirectory|\DoesNotExist.mdf';Initial Catalog=SemanticLoggingTests;Integrated Security=True";

            using (var sink = new SqlDatabaseSink("test", validNotExisting, "tableName", Buffering.DefaultBufferingInterval, Buffering.DefaultBufferingCount, Buffering.DefaultMaxBufferSize, TimeSpan.FromSeconds(20)))
            using (var collectErrorsListener = new MockEventListener())
            {
                collectErrorsListener.EnableEvents(SemanticLoggingEventSource.Log, EventLevel.Error, Keywords.All);

                sink.OnNext(new EventRecord());
                Assert.IsTrue(Task.Run(() => sink.OnCompleted()).Wait(TimeSpan.FromSeconds(5)));

                Assert.IsTrue(collectErrorsListener.WrittenEntries.Any(x => x.EventId == 101));
            }
        }
        public void when_cannot_connect_to_database_then_flush_should_finish_faulted()
        {
            const string ValidNotExisting = @"Data Source=(localdb)\v11.0; AttachDBFilename='|DataDirectory|\DoesNotExist.mdf';Initial Catalog=SemanticLoggingTests;Integrated Security=True";

            using (var sink = new SqlDatabaseSink("test", ValidNotExisting, "tableName", Buffering.DefaultBufferingInterval, Buffering.DefaultBufferingCount, Buffering.DefaultMaxBufferSize, TimeSpan.FromSeconds(20)))
            using (var collectErrorsListener = new MockEventListener())
            {
                collectErrorsListener.EnableEvents(SemanticLoggingEventSource.Log, EventLevel.Error, Keywords.All);

                sink.OnNext(EventEntryTestHelper.Create());
                try
                {
                    Assert.IsTrue(sink.FlushAsync().Wait(TimeSpan.FromSeconds(5)));
                    Assert.Fail("Exception should be thrown.");
                }
                catch (AggregateException ex)
                {
                    Assert.IsInstanceOfType(ex.InnerException, typeof(FlushFailedException));
                }

                Assert.IsTrue(collectErrorsListener.WrittenEntries.Any(x => x.EventId == 101));
            }
        }
        // Ignoring because the storage client library considers inexistant account as transient so it retries and the exponential back-off could take time
        public void when_cannot_connect_to_storage_account_then_flush_should_finish_faulted()
        {
            const string ValidNotExisting = "DefaultEndpointsProtocol=https;AccountName=InexistantDoesntReallyMatter;AccountKey=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa==";

            using (var sink = new WindowsAzureTableSink("instanceName", ValidNotExisting, "Table", TimeSpan.FromSeconds(1), 5000, TimeSpan.FromSeconds(20)))
            using (var collectErrorsListener = new MockEventListener())
            {
                collectErrorsListener.EnableEvents(SemanticLoggingEventSource.Log, EventLevel.Error, Keywords.All);

                sink.OnNext(new CloudEventEntry(EventEntryTestHelper.Create()));
                try
                {
                    Assert.IsTrue(sink.FlushAsync().Wait(TimeSpan.FromSeconds(15)));
                    Assert.Fail("Exception should be thrown.");
                }
                catch (AggregateException ex)
                {
                    Assert.IsInstanceOfType(ex.InnerException, typeof(FlushFailedException));
                }

                Assert.IsTrue(collectErrorsListener.WrittenEntries.Any(x => x.EventId == 500));
            }
        }
        public void when_cannot_connect_to_database_then_flush_should_finish_faulted()
        {
            const string ValidNotExisting = @"Data Source=(localdb)\v11.0; AttachDBFilename='|DataDirectory|\DoesNotExist.mdf';Initial Catalog=SemanticLoggingTests;Integrated Security=True";

            using (var sink = new SqlDatabaseSink("test", ValidNotExisting, "tableName", Buffering.DefaultBufferingInterval, Buffering.DefaultBufferingCount, Buffering.DefaultMaxBufferSize, TimeSpan.FromSeconds(20)))
            using (var collectErrorsListener = new MockEventListener())
            {
                collectErrorsListener.EnableEvents(SemanticLoggingEventSource.Log, EventLevel.Error, Keywords.All);
                sink.OnNext(EventEntryTestHelper.Create());

                //ERH 6.26.2015: there is volatility around the delay when under load; the longer the delay the more likely the test will pass.
                //               This probably indicates some kind of missed case or a bad assumption about the underlying implementation
                const int waitTimeInSeconds = 10;

                try
                {
                    Assert.IsTrue(sink.FlushAsync().Wait(TimeSpan.FromSeconds(waitTimeInSeconds)));
                    Assert.Fail("Exception should be thrown.");
                }
                catch (AggregateException ex)
                {
                    Console.WriteLine(ex.GetType());
                    Assert.IsNotNull(ex.InnerException, "ex.InnerException != null");
                    Console.WriteLine(ex.InnerException.GetType());
                    Assert.IsInstanceOfType(ex.InnerException, typeof(FlushFailedException),
                        "exception should be of type FlushFailedException");
                }

                Assert.IsTrue(
                    collectErrorsListener.WrittenEntries.Any(
                        x => x.EventId == 101
                        ),
                    "should have entries for eventid 101"
                    );
            }
        }
 protected override void Given()
 {
     base.Given();
     this.sink = new SqlDatabaseSink(InstanceName, this.GetSqlConnectionString(), SqlDatabaseLog.DefaultTableName, Buffering.DefaultBufferingInterval, NumberOfEntries, Buffering.DefaultMaxBufferSize, Timeout.InfiniteTimeSpan);
     this.collectErrorsListener = new MockEventListener();
     this.collectErrorsListener.EnableEvents(SemanticLoggingEventSource.Log, EventLevel.Error, Keywords.All);
 }
        // Ignoring because the storage client library considers inexistant account as transient so it retries and the exponential back-off could take time
        public void when_cannot_connect_to_storage_account_then_on_completed_should_not_stall_or_throw()
        {
            const string ValidNotExisting = "DefaultEndpointsProtocol=https;AccountName=InexistantDoesntReallyMatter;AccountKey=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa==";

            using (var sink = new WindowsAzureTableSink("instanceName", ValidNotExisting, "Table", TimeSpan.FromSeconds(1), 5000, TimeSpan.FromSeconds(20)))
            using (var collectErrorsListener = new MockEventListener())
            {
                collectErrorsListener.EnableEvents(SemanticLoggingEventSource.Log, EventLevel.Error, Keywords.All);

                sink.OnNext(new CloudEventEntry(EventEntryTestHelper.Create()));
                Assert.IsTrue(Task.Run(() => sink.OnCompleted()).Wait(TimeSpan.FromSeconds(15)));

                Assert.IsTrue(collectErrorsListener.WrittenEntries.Any(x => x.EventId == 500));
            }
        }
        protected override void Given()
        {
            this.collectErrorsListener = new MockEventListener();
            this.collectErrorsListener.EnableEvents(SemanticLoggingEventSource.Log, EventLevel.Informational, SemanticLoggingEventSource.Keywords.Sink);

            this.sink = new TestableWindowsAzureTableSink("TestName", maxBufferSize: BufferSize);
            sink.WaitHandle.Reset();
        }