public void ValidParamsCalledTwiceWithin1Minute_UsesCachedVersionOnSecondCall() { var readerCreator = mocks.DynamicMock<IDnaDataReaderCreator>(); var reader = mocks.DynamicMock<IDnaDataReader>(); var cacheManager = mocks.DynamicMock<ICacheManager>(); DateTime startSampleTime = DateTime.Now.AddMinutes(-1); int minutes = 1; // URL Default int totalActiveForums = 1; DateTime closeDate = DateTime.Now.AddDays(1.0); int count = 5; DateTime lastPostedDate = DateTime.Now.AddMinutes(-0.5); int siteId = 1; string urlName = "h2g2"; string title = "Test Title"; int totalPosts = 20; string url = "https://local.bbc.co.uk/dna/h2g2/comments"; StubDatabaseCall(readerCreator, reader, totalActiveForums, closeDate, count, lastPostedDate, siteId, urlName, title, totalPosts, url); CommentForumActivity cachedActivity = new CommentForumActivity(); cachedActivity.ClosingDate = new DateTimeHelper(closeDate); cachedActivity.Count = count; cachedActivity.LastPostedDate = new DateTimeHelper(lastPostedDate); cachedActivity.SiteId = siteId; cachedActivity.SiteName = urlName; cachedActivity.Title = title; cachedActivity.TotalPosts = totalPosts; cachedActivity.URL = url; CommentForumsActivityList cachedActivityList = new CommentForumsActivityList(); cachedActivityList.CommentForumsActivity = new List<CommentForumActivity>(); cachedActivityList.CommentForumsActivity.Add(cachedActivity); cachedActivityList.DateChecked = new DateTimeHelper(DateTime.Now); cachedActivityList.Minutes = minutes; cachedActivityList.StartDate = new DateTimeHelper(startSampleTime); cacheManager.Stub(x => x.GetData(Arg<string>.Is.Anything)).Return(null).Repeat.Once(); // Not cached first time cacheManager.Stub(x => x.GetData(Arg<string>.Is.Anything)).Return(cachedActivityList).Repeat.Once(); // Cached second time mocks.ReplayAll(); Comments comments = new Comments(null, readerCreator, cacheManager, null); CommentForumsActivityList activity = comments.GetCommentForumsActivity(minutes, startSampleTime.ToString()); VerifyActivityList(totalActiveForums, minutes, closeDate, startSampleTime, count, lastPostedDate, siteId, urlName, title, totalPosts, url, activity); activity = comments.GetCommentForumsActivity(minutes, startSampleTime.ToString()); VerifyActivityList(totalActiveForums, minutes, closeDate, startSampleTime, count, lastPostedDate, siteId, urlName, title, totalPosts, url, activity); cacheManager.AssertWasCalled(x => x.GetData(Arg<string>.Is.Anything), options => options.Repeat.Times(2)); cacheManager.AssertWasCalled(x => x.Add(Arg<string>.Is.Anything, Arg<object>.Is.Anything, Arg<CacheItemPriority>.Is.Anything, Arg<ICacheItemRefreshAction>.Is.Anything, Arg<ICacheItemExpiration>.Is.Anything), options => options.Repeat.Once()); }
/// <summary> /// Get the latest activity on all comment forums across all sites /// </summary> /// <param name="minutes">Activity over the last 'X' minutes. Will top out at 60 mins if greater</param> /// <param name="startDate">Activity since the given start date. Will truncate to 60 mins if greater</param> /// <returns>A list of activity on forums within the given time span</returns> public CommentForumsActivityList GetCommentForumsActivity(int minutes, string startDate) { if (minutes > 60) { minutes = 60; } else if (minutes < 1) { minutes = 1; } DateTime startDateTime = DateTime.Now.AddMinutes(-minutes); if (startDate.Length > 0 && DateTime.TryParse(startDate, out startDateTime)) { if (startDateTime < DateTime.Now.AddHours(-1)) { startDateTime = DateTime.Now.AddHours(-1); } minutes = (int)(DateTime.Now - startDateTime).TotalMinutes; } CommentForumsActivityList activityList = GetCachedActivitylist(minutes); if (activityList != null) { return activityList; } activityList = new CommentForumsActivityList(); activityList.CommentForumsActivity = new List<CommentForumActivity>(); activityList.DateChecked = new DateTimeHelper(DateTime.Now); activityList.StartDate = new DateTimeHelper(startDateTime); activityList.Minutes = minutes; using (IDnaDataReader reader = CreateReader("getcommentforumsactivity")) { reader.AddParameter("minutes", minutes); reader.AddParameter("startdate", startDateTime); reader.Execute(); while (reader.HasRows && reader.Read()) { CommentForumActivity activity = new CommentForumActivity(); activity.ClosingDate = new DateTimeHelper(reader.GetDateTime("ForumCloseDate")); activity.Count = reader.GetInt32("count"); activity.LastPostedDate = new DateTimeHelper(reader.GetDateTime("LastPostedDate")); activity.SiteId = reader.GetInt32("siteid"); activity.SiteName = reader.GetString("urlname"); activity.Title = reader.GetString("title"); activity.TotalPosts = reader.GetInt32("totalPosts"); activity.URL = reader.GetString("url"); activityList.CommentForumsActivity.Add(activity); } } CacheActivityList(activityList); return activityList; }