public void MaxSize() { var thing = new EventThing(); var context = Factory.Create(thing, new ThingOption { MaxEventSize = 2 }); thing.ThingContext = context; context.Actions.Should().BeEmpty(); context.Properties.Should().BeEmpty(); context.Events.Should().NotBeEmpty(); context.Events.Should().HaveCount(1); context.Events.Should().ContainKeys(nameof(EventThing.Event)); var values = new List <T>(3); for (var i = 0; i < 3; i++) { values.Add(Fixture.Create <T>()); thing.Invoke(values[i]); } context.Events[nameof(EventThing.Event)].Count.Should().Be(2); context.Events[nameof(EventThing.Event)].TryDequeue(out var value).Should().BeTrue(); value.Data.Should().Be(values[1]); context.Events[nameof(EventThing.Event)].TryDequeue(out value).Should().BeTrue(); value.Data.Should().Be(values[2]); context.Events[nameof(EventThing.Event)].TryDequeue(out value).Should().BeFalse(); }
public ActionResult SubThat([FromBody] EventThing yeah) { yeah.Id = 12; Events.Append(yeah); return(Content("yeah")); }
public void EventAddRemoveAnonymousFunction() { var x = new EventThing(); x.MyEvent += (object sender, EventArgs args) => { HandlerToRemove(sender, args); }; x.MyEvent -= (object sender, EventArgs args) => { HandlerToRemove(sender, args); }; x.OnMyEvent(); }
public void EventAddRemoveMethod() { var x = new EventThing(); x.MyEvent += HandlerToRemove; x.MyEvent -= HandlerToRemove; x.OnMyEvent(); }
public void EventAddRemoveLambda() { var x = new EventThing(); x.MyEvent += (sender, args) => HandlerToRemove(sender, args); x.MyEvent -= (sender, args) => HandlerToRemove(sender, args); x.OnMyEvent(); }
public void EventAddRemoveMethodRef() { var x = new EventThing(); EventHandler handlerToRemove = HandlerToRemove; x.MyEvent += handlerToRemove; x.MyEvent -= handlerToRemove; x.OnMyEvent(); }
public void TestEventDispose() { using (EventThing thing = new EventThing()) { DoStuff(thing); Assert.AreEqual(EventSubscriber.Count, 1); } GC.Collect(); GC.WaitForPendingFinalizers(); Assert.AreEqual(EventSubscriber.Count, 0, "Dispose should null the Event"); }
public void EventAddRemoveLocalFunction() { var x = new EventThing(); void handler(object sender, EventArgs args) { HandlerToRemove(sender, args); } x.MyEvent += handler; x.MyEvent -= handler; x.OnMyEvent(); }
public void TestEventNotNull() { EventThing thing = new EventThing(); DoStuff(thing); Assert.AreEqual(EventSubscriber.Count, 1); GC.Collect(); GC.WaitForPendingFinalizers(); Assert.AreEqual(EventSubscriber.Count, 1, "Event sub list should still hold a subscriber reference"); thing.OnMyEvent(); }
public void TestEventNull() { EventThing thing = new EventThing(); DoStuff(thing); Assert.AreEqual(EventSubscriber.Count, 1); thing.SetEventNull(); // Should clear out the list, removing all refs to subscribers GC.Collect(); GC.WaitForPendingFinalizers(); Assert.AreEqual(EventSubscriber.Count, 0, "Setting event to null should have cleared sub list"); thing.OnMyEvent(); }
public void CreateWithEvent() { var thing = new EventThing(); var option = new ThingOption(); var context = _factory.Create(thing, option); context.Should().NotBeNull(); _event .Received(1) .Add(Arg.Any <EventInfo>(), Arg.Any <ThingEventAttribute>()); _response .Received(1) .Add(Arg.Any <EventInfo>(), Arg.Any <ThingEventAttribute>()); _event .Received(1) .Build(); _property .Received(1) .Build(); _action .Received(1) .Build(); _response .Received(1) .Build(); _property .DidNotReceive() .Add(Arg.Any <PropertyInfo>(), Arg.Any <JsonSchema>()); _action .DidNotReceive() .Add(Arg.Any <MethodInfo>(), Arg.Any <ThingActionAttribute>()); _action .DidNotReceive() .Add(Arg.Any <ParameterInfo>(), Arg.Any <JsonSchema>()); }
public void NoObjectInitializersWithEvents() { bool firstHappened = false; bool secondHappened = false; var x = new EventThing(); // With Events, we required two statements to initialize x.MyEvent += (sender, args) => { firstHappened = true; }; // And still do multiple handlers, like this: x.MyEvent += (sender, args) => { secondHappened = true; }; x.OnMyEvent(); Assert.True(firstHappened); Assert.True(secondHappened); }
public void EmitEvent() { var thing = new EventThing(); var context = Factory.Create(thing, new ThingOption()); thing.ThingContext = context; context.Actions.Should().BeEmpty(); context.Properties.Should().BeEmpty(); context.Events.Should().NotBeEmpty(); context.Events.Should().HaveCount(1); context.Events.Should().ContainKeys(nameof(EventThing.Event)); var @eventCounter = 0; var value = Fixture.Create <T>(); thing.Event += OnEvent; thing.Invoke(value); thing.Invoke(null); eventCounter.Should().Be(2); context.Events[nameof(EventThing.Event)].Count.Should().Be(eventCounter); context.Events[nameof(EventThing.Event)].TryDequeue(out var @event).Should().BeTrue(); @event.Data.Should().Be(value); context.Events[nameof(EventThing.Event)].TryDequeue(out @event).Should().BeTrue(); @event.Data.Should().Be(null); context.Events[nameof(EventThing.Event)].TryDequeue(out @event).Should().BeFalse(); void OnEvent(object sender, T e) { sender.Should().NotBeNull(); sender.Should().Be(thing); e?.Should().Be(value); eventCounter++; } }
static void DoStuff(EventThing thing) { new EventSubscriber(thing); thing.OnMyEvent(); }
public EventBuilderTest() { _builder = new EventBuilder(); _thing = new EventThing(); }
public void BuildWithEvent() { var thing = new EventThing(); _builder .SetThing(thing) .SetThingOption(_option); Visit(thing.GetType()); var response = _builder.Build(); response.Should().NotBeNull(); var message = JsonSerializer.Serialize(response, response.GetType(), _option.ToJsonSerializerOptions()); FluentAssertions.Json.JsonAssertionExtensions.Should(JToken.Parse(message)) .BeEquivalentTo(JToken.Parse(@" { ""events"": { ""int"": { ""type"": ""integer"", ""links"": [ { ""href"": ""/things/event-thing/events/int"", ""rel"": ""event"" } ] }, ""test"": { ""title"": ""Bar"", ""description"": ""Foo"", ""unit"": ""milli"", ""type"": ""string"", ""links"": [ { ""href"": ""/things/event-thing/events/test"", ""rel"": ""event"" } ] } }, ""@context"": ""https://iot.mozilla.org/schemas"", ""security"": ""nosec_sc"", ""securityDefinitions"": { ""nosec_sc"": { ""scheme"": ""nosec"" } }, ""links"": [{ ""rel"": ""properties"", ""href"": ""/things/event-thing/properties"" },{ ""rel"": ""actions"", ""href"": ""/things/event-thing/actions"" },{ ""rel"": ""events"", ""href"": ""/things/event-thing/events"" }] } ")); void Visit(Type thingType) { var events = thingType.GetEvents(BindingFlags.Public | BindingFlags.Instance); foreach (var @event in events) { var args = @event.EventHandlerType !.GetGenericArguments(); if (args.Length > 1) { continue; } if ((args.Length == 0 && @event.EventHandlerType != typeof(EventHandler)) || (args.Length == 1 && @event.EventHandlerType != typeof(EventHandler <>).MakeGenericType(args[0]))) { continue; } _builder.Add(@event, @event.GetCustomAttribute <ThingEventAttribute>()); } } }