public void PostEvents() {
            var container = AppBuilder.CreateContainer();
            using (WebApp.Start(Settings.Current.BaseURL, app => AppBuilder.BuildWithContainer(app, container, false))) {
                EnsureSampleData(container);
                
                var events = new List<Event> { new Event { Message = "Testing" } };
                var configuration = GetClient().Configuration;
                var serializer = new DefaultJsonSerializer();

                var client = new DefaultSubmissionClient();
                var response = client.PostEvents(events, configuration, serializer);
                Assert.True(response.Success, response.Message);
                Assert.Null(response.Message);
            }
        }
        public void PostUserDescription() {
            var container = AppBuilder.CreateContainer();
            using (WebApp.Start(Settings.Current.BaseURL, app => AppBuilder.BuildWithContainer(app, container, false))) {
                var repository = container.GetInstance<IEventRepository>();
                repository.RemoveAll();

                const string referenceId = "fda94ff32921425ebb08b73df1d1d34c";
                const string badReferenceId = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz";

                var statsCounter = container.GetInstance<IAppStatsClient>() as InMemoryAppStatsClient;
                Assert.NotNull(statsCounter);

                EnsureSampleData(container);

                var events = new List<Event> { new Event { Message = "Testing", ReferenceId = referenceId } };
                var configuration = GetClient().Configuration;
                var serializer = new DefaultJsonSerializer();

                var client = new DefaultSubmissionClient();
                var description = new UserDescription { EmailAddress = "*****@*****.**", Description = "Some description." };
                statsCounter.WaitForCounter(StatNames.EventsUserDescriptionErrors, work: () => {
                    var response = client.PostUserDescription(referenceId, description, configuration, serializer);
                    Assert.True(response.Success, response.Message);
                    Assert.Null(response.Message);
                });

                statsCounter.WaitForCounter(StatNames.EventsUserDescriptionProcessed, work: () => {
                    var response = client.PostEvents(events, configuration, serializer);
                    Assert.True(response.Success, response.Message);
                    Assert.Null(response.Message);
                });

                container.GetInstance<IElasticClient>().Refresh();
                var ev = repository.GetByReferenceId("537650f3b77efe23a47914f4", referenceId).FirstOrDefault();
                Assert.NotNull(ev);
                Assert.NotNull(ev.GetUserDescription());
                Assert.Equal(description.ToJson(), ev.GetUserDescription().ToJson());

                Assert.Equal(2, statsCounter.GetCount(StatNames.EventsUserDescriptionErrors));
                statsCounter.WaitForCounter(StatNames.EventsUserDescriptionErrors, work: () => {
                    var response = client.PostUserDescription(badReferenceId, description, configuration, serializer);
                    Assert.True(response.Success, response.Message);
                    Assert.Null(response.Message);
                });

                Assert.Equal(2, statsCounter.GetCount(StatNames.EventsUserDescriptionErrors));
            }
        }
        public void GetSettings() {
            var container = AppBuilder.CreateContainer();
            using (WebApp.Start(Settings.Current.BaseURL, app => AppBuilder.BuildWithContainer(app, container, false))) {
                EnsureSampleData(container);
                
                var configuration = GetClient().Configuration;
                var serializer = new DefaultJsonSerializer();

                var client = new DefaultSubmissionClient();
                var response = client.GetSettings(configuration, serializer);
                Assert.True(response.Success, response.Message);
                Assert.NotEqual(-1, response.SettingsVersion);
                Assert.NotNull(response.Settings);
                Assert.Null(response.Message);
            }
        }
        public void CanManageQueue() {
            Reset();

            IFileStorage storage = GetStorage();
            const string queueName = "test";

            IJsonSerializer serializer = new DefaultJsonSerializer();
            var ev = new Event { Type = Event.KnownTypes.Log, Message = "test" };
            storage.Enqueue(queueName, ev, serializer);
            storage.SaveFile("test.txt", "test");
            Assert.True(storage.GetFileList().Any(f => f.Path.StartsWith(queueName + "\\q\\") && f.Path.EndsWith("0.json")));
            Assert.Equal(2, storage.GetFileList().Count());

            Assert.True(storage.GetQueueFiles(queueName).All(f => f.Path.EndsWith("0.json")));
            Assert.Equal(1, storage.GetQueueFiles(queueName).Count());

            storage.DeleteFile("test.txt");
            Assert.Equal(1, storage.GetFileList().Count());

            Assert.True(storage.LockFile(storage.GetFileList().FirstOrDefault()));
            Assert.True(storage.GetQueueFiles(queueName).All(f => f.Path.EndsWith("0.json.x")));
            Assert.True(storage.ReleaseFile(storage.GetFileList().FirstOrDefault()));

            var batch = storage.GetEventBatch(queueName, serializer);
            Assert.Equal(1, batch.Count);

            Assert.True(storage.GetFileList().All(f => f.Path.StartsWith(queueName + "\\q\\") && f.Path.EndsWith("1.json.x")));
            Assert.Equal(1, storage.GetFileList().Count());

            Assert.Equal(0, storage.GetQueueFiles(queueName).Count());
            Assert.Equal(0, storage.GetEventBatch(queueName, serializer).Count());

            Assert.False(storage.LockFile(storage.GetFileList().FirstOrDefault()));

            storage.ReleaseBatch(batch);
            Assert.True(storage.GetFileList().All(f => f.Path.StartsWith(queueName + "\\q\\") && f.Path.EndsWith("1.json")));
            Assert.Equal(1, storage.GetFileList().Count());
            Assert.Equal(1, storage.GetQueueFiles(queueName).Count());

            var file = storage.GetFileList().FirstOrDefault();
            storage.IncrementAttempts(file);
            Assert.True(storage.GetFileList().All(f => f.Path.StartsWith(queueName + "\\q\\") && f.Path.EndsWith("2.json")));
            storage.IncrementAttempts(file);
            Assert.True(storage.GetFileList().All(f => f.Path.StartsWith(queueName + "\\q\\") && f.Path.EndsWith("3.json")));

            Assert.True(storage.LockFile(file));
            Assert.NotNull(file);
            Assert.True(storage.GetFileList().All(f => f.Path.StartsWith(queueName + "\\q\\") && f.Path.EndsWith("3.json.x")));
            Thread.Sleep(TimeSpan.FromMilliseconds(1));
            storage.ReleaseOldLocks(queueName, TimeSpan.Zero);
            Assert.True(storage.GetFileList().All(f => f.Path.StartsWith(queueName + "\\q\\") && f.Path.EndsWith("3.json")));

            batch = storage.GetEventBatch(queueName, serializer);
            Assert.Equal(1, batch.Count);
            Assert.True(storage.GetFileList().All(f => f.Path.StartsWith(queueName + "\\q\\") && f.Path.EndsWith("4.json.x")));
            storage.DeleteBatch(batch);
            Assert.Equal(0, storage.GetQueueFiles(queueName).Count());

            ev = new Event { Type = Event.KnownTypes.Log, Message = "test" };
            storage.Enqueue(queueName, ev, serializer);
            file = storage.GetFileList().FirstOrDefault();
            Assert.NotNull(file);
            Thread.Sleep(TimeSpan.FromMilliseconds(1));
            storage.DeleteOldQueueFiles(queueName, TimeSpan.Zero);
            Assert.Equal(0, storage.GetQueueFiles(queueName).Count());
        }
        public void CanConcurrentlyManageFiles() {
            Reset();

            IFileStorage storage = GetStorage();
            IJsonSerializer serializer = new DefaultJsonSerializer();
            const string queueName = "test";

            Parallel.For(0, 25, i => {
                var ev = new Event {
                    Type = Event.KnownTypes.Log,
                    Message = "test" + i
                };
                storage.Enqueue(queueName, ev, serializer);
            });
            Assert.Equal(25, storage.GetFileList().Count());
            var working = new ConcurrentDictionary<string, object>();

            Parallel.For(0, 50, i => {
                var fileBatch = storage.GetEventBatch(queueName, serializer, 2);
                foreach (var f in fileBatch) {
                    if (working.ContainsKey(f.Item1.Path))
                        Debug.WriteLine(f.Item1.Path);
                    Assert.False(working.ContainsKey(f.Item1.Path));
                    working.TryAdd(f.Item1.Path, null);
                }

                if (RandomHelper.GetBool()) {
                    object o;
                    foreach (var f in fileBatch)
                        working.TryRemove(f.Item1.Path, out o);
                    storage.ReleaseBatch(fileBatch);
                } else {
                    storage.DeleteBatch(fileBatch);
                }
            });
            Assert.Equal(25, working.Count + storage.GetQueueFiles(queueName).Count);
        }