Exemplo n.º 1
0
        public void substories_should_be_in_right_order()
        {
            List <string> _loggedStoryNames = new List <string>();

            var handlerRules = new Ruleset <IStory, IStoryHandler>();

            handlerRules.Rules.Add(new PredicateRule
                                   (
                                       _ => _.IsRoot(),
                                       _ => new ActionHandler("unittest", (story) => RegisterStoryName(story, _loggedStoryNames))
                                   )
                                   );
            Storytelling.Factory = new BasicStoryFactory(handlerRules);
            using (Storytelling.StartNew("parent"))
            {
                using (Storytelling.StartNew("child1")) { }
                using (Storytelling.StartNew("child2"))
                {
                    using (Storytelling.StartNew("child2/child1")) { }
                    using (Storytelling.StartNew("child2/child2")) { }
                }
                using (Storytelling.StartNew("child3")) { }
            }

            CollectionAssert.AreEqual(new List <string>
            {
                "parent",
                "child1",
                "child2",
                "child2/child1",
                "child2/child2",
                "child3",
            }, _loggedStoryNames);
        }
Exemplo n.º 2
0
        public void story_data_is_observed_during_invocation()
        {
            var data = new List <KeyValuePair <string, object> >()
            {
                new KeyValuePair <string, object>("bool_value", true),
                new KeyValuePair <string, object>("int_value", 123),
                new KeyValuePair <string, object>("string_value", "test!"),
            };

            var handlerRules = new Ruleset <IStory, IStoryHandler>()
            {
                Rules =
                {
                    new PredicateRule(
                        _ => true,                                                        // always run for story
                        _ => new ActionHandler(
                            "data_verification_handler",
                            (story) => Assert.AreEqual(0, story.Data.Count()),            // make sure OnStart() is invoked with zero data items.
                            (story) => Assert.IsTrue(data.SequenceEqual(story.Data)))     // make sure OnStop() is invoked with 3 data items.
                        ),
                },
            };

            Storytelling.StartNew("testStory", () =>
            {
                foreach (var kvp in data)
                {
                    Storytelling.Data[kvp.Key] = kvp.Value;
                }
            });
        }
Exemplo n.º 3
0
        public void story_handler_is_not_invoked_when_rule_condition_is_not_met()
        {
            var invokedBefore = false;
            var invokedAfter  = false;

            var handlerRules = new Ruleset <IStory, IStoryHandler>()
            {
                Rules =
                {
                    new PredicateRule(story =>
                    {
                        var userId = (string)story.Data["userId"];
                        return(userId != null && userId == "user13");
                    },
                                      _ => new ActionHandler("invocation_handler", s => invokedBefore = true, s => invokedAfter = true)),
                }
            };

            Storytelling.Factory = new BasicStoryFactory(handlerRules);

            Storytelling.StartNew("testStory", () =>
            {
                Storytelling.Data["userId"] = "user21";

                Assert.IsFalse(invokedBefore);
                Assert.IsFalse(invokedAfter);
            });
        }
        public override Task Invoke(IOwinContext context)
        {
            var request = context.Request;

            return(Storytelling.StartNewAsync("Request", async() =>
            {
                try
                {
                    Storytelling.Data["RequestUrl"] = request.Uri.ToString();
                    Storytelling.Data["RequestMethod"] = request.Method;
                    Storytelling.Data["UserIp"] = request.RemoteIpAddress;
                    Storytelling.Data["UserAgent"] = request.Headers.Get("User-Agent");
                    Storytelling.Data["Referer"] = request.Headers.Get("Referer");

                    await Next.Invoke(context);

                    Storytelling.Data["Response"] = context.Response.StatusCode;
                }
                catch (Exception e)
                {
                    var m = e.Message;
                    Storytelling.Error(m);
                    throw;
                }
            }));
        }
Exemplo n.º 5
0
        public async Task <string> GetRandomName()
        {
            Storytelling.Info("Getting random name");
            await Task.Delay(10);

            return(GetRandomItem(Adjectives) + " " + GetRandomItem(Animals));
        }
Exemplo n.º 6
0
        public async Task <string> GetRandomString()
        {
            Storytelling.Info("Getting random string");

            await Task.Delay(10);

            return(Guid.NewGuid().ToString());
        }
Exemplo n.º 7
0
        public void story_name_is_not_chained_to_null_parent()
        {
            var handlerRules = new Ruleset <IStory, IStoryHandler>();

            Storytelling.StartNew("testStory", () =>
            {
                Assert.AreEqual("testStory", Storytelling.Current.Name);
            });
        }
Exemplo n.º 8
0
        public void story_exception_thrown_is_propagated()
        {
            var handlerRules = new Ruleset <IStory, IStoryHandler>();

            Storytelling.StartNew("testStory", () =>
            {
                throw new InvalidOperationException("oh oh");
            });
        }
Exemplo n.º 9
0
        public void story_name_is_chained_to_parent()
        {
            var handlerRules = new Ruleset <IStory, IStoryHandler>();

            Storytelling.StartNew("base", () =>
            {
                Storytelling.StartNew("child", () =>
                {
                    Assert.AreEqual("base/child", Storytelling.Current.Name);
                });
            });
        }
Exemplo n.º 10
0
        public Task <HttpResponseMessage> ProduceLogs()
        {
            return(Storytelling.Factory.StartNewAsync("ProduceLogs", async() =>
            {
                var str = await _fooService.GetRandomString();
                Storytelling.Debug("This is a debug message {0}", str);
                str = await _fooService.GetRandomString();
                Storytelling.Info("This is a information message {0}", str);
                str = await _fooService.GetRandomString();
                Storytelling.Warn("This is a warning message {0}", str);

                return Request.CreateResponse(HttpStatusCode.Created, "logs created");
            }));
        }
Exemplo n.º 11
0
        public Task <HttpResponseMessage> GetSomething()
        {
            return(Storytelling.Factory.StartNewAsync("GetSomething", async() =>
            {
                var name = await _fooService.GetRandomName();

                // Log to the story
                Storytelling.Info("Prepare something object");
                object something = new
                {
                    Name = name
                };

                // Add data to story
                Storytelling.Data["something"] = something;

                return Request.CreateResponse(something);
            }));
        }
        public override void OnException(HttpActionExecutedContext context)
        {
            Storytelling.Warn("Internal error - " + context.Exception);
            var message        = context.Exception.Message;
            var httpStatusCode = HttpStatusCode.InternalServerError;

            Storytelling.Data["responseMessage"] = message;

            var resp = new HttpResponseMessage()
            {
                Content = new StringContent(message)
            };

            resp.StatusCode = httpStatusCode;

            context.Response = resp;

            base.OnException(context);
        }
Exemplo n.º 13
0
        public void story_log_is_observed_during_invocation()
        {
            var log = new List <KeyValuePair <LogSeverity, string> >
            {
                new KeyValuePair <LogSeverity, string>(LogSeverity.Info, "test_info"),
                new KeyValuePair <LogSeverity, string>(LogSeverity.Warning, "test_warning"),
                new KeyValuePair <LogSeverity, string>(LogSeverity.Error, "test_error"),
            };

            var handlerRules = new Ruleset <IStory, IStoryHandler>()
            {
                Rules =
                {
                    new PredicateRule(
                        _ => true,                                                              // always run for story
                        _ => new ActionHandler(
                            "data_verification_handler",
                            (story) => Assert.AreEqual(0, story.Log.Count()),                   // make sure OnStart() is invoked with zero log items.
                            (story) => Assert.IsTrue(story.Log.All(
                                                         entry => log.Exists(
                                                             l => l.Key == entry.Severity &&
                                                             l.Value == entry.Text))))
                        ),
                },
            };

            Storytelling.Factory = new BasicStoryFactory(handlerRules);

            Storytelling.StartNew("testStory", () =>
            {
                foreach (var kvp in log)
                {
                    Storytelling.Current.Log.Add(kvp.Key, kvp.Value);
                }
            });
        }