Exemple #1
0
        public async Task Test_FileDataProvider_LoopAsync()
        {
            var loop = new Loop()
            {
                Id = 1
            };

            loop.Inner = loop;

            var fdp = new FileDataProvider()
            {
                DirectoryPath   = _directory,
                FilenameBuilder = x => x.EventType
            };

            Configuration.DataProvider = fdp;
            var guid = "x" + Guid.NewGuid().ToString();
            await AuditScope.CreateAndSaveAsync(guid, loop);

            var ev = await fdp.GetEventAsync(Path.Combine(_directory, guid));

            Assert.IsNotNull(ev);
            Assert.AreEqual(guid, ev.EventType);
            Assert.AreEqual(2, ev.CustomFields.Count);
            Assert.AreEqual(1, ev.CustomFields["Id"]);
            Assert.AreEqual(JObject.Parse("{\"Id\": 1}").ToString(), ev.CustomFields["Inner"].ToString());
        }
Exemple #2
0
        public async Task Test_StartAndSave_Async()
        {
            var provider = new Mock <AuditDataProvider>();

            provider.Setup(p => p.Serialize(It.IsAny <string>())).CallBase();

            var eventType = "event type";
            await AuditScope.CreateAndSaveAsync(eventType, new { ExtraField = "extra value" });

            await AuditScope.CreateAndSaveAsync(eventType, new { Extra1 = new { SubExtra1 = "test1" }, Extra2 = "test2" }, provider.Object);

            provider.Verify(p => p.InsertEventAsync(It.IsAny <AuditEvent>()), Times.Once);
            provider.Verify(p => p.ReplaceEvent(It.IsAny <object>(), It.IsAny <AuditEvent>()), Times.Never);
            provider.Verify(p => p.ReplaceEventAsync(It.IsAny <object>(), It.IsAny <AuditEvent>()), Times.Never);
        }
Exemple #3
0
        public async Task Test_ScopeActionsStress_Async()
        {
            int counter  = 0;
            int counter2 = 0;
            int counter3 = 0;
            int MAX      = 200;

            Audit.Core.Configuration.Setup()
            .UseDynamicProvider(_ => _.OnInsert(ev =>
            {
                System.Threading.Interlocked.Increment(ref counter);
            }))
            .WithCreationPolicy(EventCreationPolicy.InsertOnEnd)
            .WithAction(_ => _.OnEventSaving(ev =>
            {
                System.Threading.Interlocked.Increment(ref counter2);
            }))
            .WithAction(_ => _.OnScopeCreated(ev =>
            {
                System.Threading.Interlocked.Increment(ref counter3);
            }));

            var tasks = new List <Task>();

            for (int i = 0; i < MAX; i++)
            {
                tasks.Add(Task.Factory.StartNew(async() =>
                {
                    await AuditScope.CreateAndSaveAsync("LoginSuccess", new { username = "******", id = i });
                    Audit.Core.Configuration.AddCustomAction(ActionType.OnEventSaving, ev =>
                    {
                        //do nothing, just bother
                        var d = ev.Event.Duration * 1234567;
                    });
                    await AuditScope.CreateAndSaveAsync("LoginFailed", new { username = "******", id = i * -1 });
                }));
            }
            await Task.WhenAll(tasks.ToArray());

            await Task.Delay(1000);

            Assert.AreEqual(MAX * 2, counter);
            Assert.AreEqual(MAX * 2, counter2);
            Assert.AreEqual(MAX * 2, counter3);
        }
Exemple #4
0
        public async Task Test_FileDataProvider_ErrorAsync()
        {
            var loop = new Loop()
            {
                Id = 1
            };

            loop.Inner = loop;

            var fdp = new FileDataProvider()
            {
                DirectoryPath   = _directory,
                FilenameBuilder = x => x.EventType
            };

            Configuration.Setup().UseFileLogProvider(_ => _
                                                     .Directory(_directory)
                                                     .FilenameBuilder(x => x.EventType)
                                                     .JsonSettings(new JsonSerializerSettings()
            {
                ReferenceLoopHandling = ReferenceLoopHandling.Error
            }));

            var guid = "x" + Guid.NewGuid().ToString();

            try
            {
                await AuditScope.CreateAndSaveAsync(guid, loop);

                Assert.Fail("Should not get here. JsonSettings not respected?");
            }
            catch (JsonSerializationException ex)
            {
                Assert.IsTrue(ex.Message.ToLower().Contains("loop detected"));
            }
        }