Exemple #1
0
        private static Tuple <IFilteredStream, Func <Action <string> > > InitForCatchingJsonEvents()
        {
            // arrange
            var fakeStreamResultGenerator = A.Fake <IStreamResultGenerator>();

            var container = new TweetinviContainer();

            container.BeforeRegistrationCompletes += (sender, args) =>
            {
                container.RegisterInstance(typeof(IStreamResultGenerator), fakeStreamResultGenerator);
            };
            container.Initialize();

            Action <string> jsonReceivedCallback = null;

            A.CallTo(() => fakeStreamResultGenerator.StartAsync(It.IsAny <Action <string> >(), It.IsAny <Func <ITwitterRequest> >()))
            .ReturnsLazily(callInfo =>
            {
                jsonReceivedCallback = callInfo.Arguments.Get <Action <string> >("onJsonReceivedCallback");
                return(Task.CompletedTask);
            });

            var client = new TwitterClient(A.Fake <ITwitterCredentials>(), new TwitterClientParameters
            {
                Container = container
            });

            var fs = client.Streams.CreateFilteredStream();

            return(new Tuple <IFilteredStream, Func <Action <string> > >(fs, () => jsonReceivedCallback));
        }
        public async Task GetRateLimits_FromTwitterApiOnly_GetsResultFromTwitterAccessor()
        {
            var expectedRateLimits = A.Fake <CredentialsRateLimitsDTO>();
            var twitterAccessor    = A.Fake <ITwitterAccessor>();

            var container = new TweetinviContainer();

            container.BeforeRegistrationCompletes += (sender, args) => { container.RegisterInstance(typeof(ITwitterAccessor), twitterAccessor); };
            container.Initialize();

            var fakeCredentials = new TwitterCredentials("consumerKey", "consumerSecret", "accessToken", "accessTokenSecret");
            var client          = new TwitterClient(fakeCredentials, new TwitterClientParameters
            {
                Container = container
            });

            A.CallTo(() => twitterAccessor.ExecuteRequest <CredentialsRateLimitsDTO>(It.IsAny <ITwitterRequest>()))
            .Returns(new TwitterResult <CredentialsRateLimitsDTO> {
                DataTransferObject = expectedRateLimits
            });

            // act
            var rateLimits = await client.RateLimits.GetRateLimits(RateLimitsSource.TwitterApiOnly);

            // arrange
            Assert.Same(rateLimits.CredentialsRateLimitsDTO, expectedRateLimits);
        }
Exemple #3
0
        public async Task RateLimitAwaiter()
        {
            if (!EndToEndTestConfig.ShouldRunEndToEndTests || !EndToEndTestConfig.ShouldRunRateLimitHungryTests)
            {
                return;
            }

            var taskDelayer = A.Fake <ITaskDelayer>();

            var container = new TweetinviContainer();

            container.BeforeRegistrationCompletes += (sender, args) =>
            {
                container.RegisterInstance(typeof(ITaskDelayer), taskDelayer);
            };
            container.Initialize();

            var client = new TwitterClient(EndToEndTestConfig.TweetinviTest.Credentials, new TwitterClientParameters
            {
                Container = container
            });

            TweetinviEvents.SubscribeToClientEvents(client);

            client.ClientSettings.RateLimitTrackerMode = RateLimitTrackerMode.TrackAndAwait;

            // act - assert
            var rateLimits = await client.RateLimits.GetEndpointRateLimit(Resources.Timeline_GetHomeTimeline);

            var rateLimitsRemaining = rateLimits.Remaining;

            await client.RateLimits.WaitForQueryRateLimit(Resources.Timeline_GetHomeTimeline);

            for (var i = 0; i < rateLimitsRemaining; ++i)
            {
                var timelineIterator = client.Timelines.GetHomeTimelineIterator();
                await timelineIterator.NextPage();
            }

            A.CallTo(() => taskDelayer.Delay(It.IsAny <TimeSpan>())).MustNotHaveHappened();

            await client.RateLimits.WaitForQueryRateLimit(Resources.Timeline_GetHomeTimeline);

            A.CallTo(() => taskDelayer.Delay(It.IsAny <TimeSpan>())).MustHaveHappenedOnceExactly();

            try
            {
                var timelineIterator = client.Timelines.GetHomeTimelineIterator();
                await timelineIterator.NextPage();
            }
            // ReSharper disable once CC0004
            catch (Exception)
            {
                // we expect to throw as we are mocking the task delayer
                A.CallTo(() => taskDelayer.Delay(It.IsAny <TimeSpan>())).MustHaveHappenedTwiceExactly();
                return;
            }

            throw new InvalidOperationException("Should have failed ealier");
        }
        public async Task GetRateLimits_FromCacheOrTwitterApi_GetsFirstFromTwitterApiThenFromCache()
        {
            var fakeRateLimitCache      = A.Fake <IRateLimitCache>();
            var twitterApiRateLimitsDTO = A.Fake <CredentialsRateLimitsDTO>();
            var twitterApiRateLimits    = new CredentialsRateLimits(twitterApiRateLimitsDTO);
            var cacheRateLimits         = A.Fake <ICredentialsRateLimits>();
            var twitterAccessor         = A.Fake <ITwitterAccessor>();


            var container = new TweetinviContainer();

            container.BeforeRegistrationCompletes += (sender, args) => { container.RegisterInstance(typeof(ITwitterAccessor), twitterAccessor); };
            container.Initialize();

            var fakeCredentials = new TwitterCredentials("consumerKey", "consumerSecret", "accessToken", "accessTokenSecret");
            var client          = new TwitterClient(fakeCredentials, new TwitterClientParameters
            {
                Container      = container,
                RateLimitCache = fakeRateLimitCache
            });

            A.CallTo(() => fakeRateLimitCache.GetCredentialsRateLimits(client.Credentials)).Returns(Task.FromResult <ICredentialsRateLimits>(null));
            A.CallTo(() => fakeRateLimitCache.RefreshEntry(client.Credentials, A <ICredentialsRateLimits> .That.Matches(x => x.CredentialsRateLimitsDTO == twitterApiRateLimitsDTO)))
            .ReturnsLazily(() =>
            {
                // we use sequence here as `RefreshCredentialsRateLimits` is calling GetCredentialsRateLimits right after having put it in the cache
                A.CallTo(() => fakeRateLimitCache.GetCredentialsRateLimits(client.Credentials))
                .ReturnsNextFromSequence(twitterApiRateLimits, cacheRateLimits);
                return(Task.CompletedTask);
            });

            A.CallTo(() => twitterAccessor.ExecuteRequest <CredentialsRateLimitsDTO>(It.IsAny <ITwitterRequest>()))
            .Returns(new TwitterResult <CredentialsRateLimitsDTO> {
                DataTransferObject = twitterApiRateLimitsDTO
            });

            // act
            var rateLimits = await client.RateLimits.GetRateLimits(RateLimitsSource.CacheOrTwitterApi);

            var rateLimitsThatShouldComeCache = await client.RateLimits.GetRateLimits(RateLimitsSource.CacheOrTwitterApi);

            // arrange
            Assert.Same(rateLimits.CredentialsRateLimitsDTO, twitterApiRateLimitsDTO);
            Assert.Same(rateLimitsThatShouldComeCache, cacheRateLimits);
        }