public async Task ConcurrentPostTopicTest() { int numTopics = 2; var managersContext = new ManagersContext(); var principalsContext = await PrincipalsContext.ConstructPrincipalsContext(managersContext, TestConstants.AppKey); var usersController = new UsersController(managersContext, principalsContext); var topicController = new TopicsController(managersContext, principalsContext); // Create a user var postUserResponse = await usersController.PostUser(); // Create a func out of the PostTopic method Func <Task <IHttpActionResult> > postTopicFunc = () => topicController.PostTopic(PublisherType.User); // Fire 2 calls in parallel var actionResultList = await ConcurrentCalls <IHttpActionResult> .FireInParallel(postTopicFunc, numTopics); // Delete the topics created for (int i = 0; i < numTopics; i += 1) { var topicHandle = (actionResultList[i] as CreatedNegotiatedContentResult <PostTopicResponse>).Content.TopicHandle; await topicController.DeleteTopic(topicHandle); } // Delete the user created await usersController.DeleteUser(); }
public async Task ConcurrentLikeCommentTest() { var numTests = 100; // Initialize all clients SocialPlusClient[] client = new SocialPlusClient[numTests]; var taskArray = new Task <HttpOperationResponse <object> > [numTests]; for (int i = 0; i < numTests; i += 1) { client[i] = new SocialPlusClient(TestConstants.ServerApiBaseUrl); } // Create user, topic, comment using client_0 var user = await TestUtilities.PostGenericUser(client[0]); var auth = AuthHelper.CreateSocialPlusAuth(user.SessionToken); var topicResponse = await TestUtilities.PostGenericTopic(client[0], auth); var commentResponse = await TestUtilities.PostGenericComment(client[0], auth, topicResponse.TopicHandle); // Initialize each PostLike call var postLikeFuncList = new Func <Task <HttpOperationResponse <object> > > [numTests]; for (int i = 0; i < numTests; i += 1) { postLikeFuncList[i] = () => client[i].CommentLikes.PostLikeWithHttpMessagesAsync(commentHandle: commentResponse.CommentHandle, authorization: auth); } var postLikeOperationResponse = await ConcurrentCalls <HttpOperationResponse <object> > .FireInParallel(postLikeFuncList); await TestUtilities.DeleteComment(client[0], commentResponse.CommentHandle, auth); await TestUtilities.DeleteTopic(client[0], topicResponse.TopicHandle, auth); await TestUtilities.DeleteUser(client[0], auth); // Check that post like operation responses were successful for (int i = 0; i < numTests; i += 1) { Assert.IsTrue( postLikeOperationResponse[i].Response.IsSuccessStatusCode, string.Format($"Like with index {i} failed with status code {postLikeOperationResponse[i].Response.StatusCode}")); } }
public async Task EstimatePerMinuteRateLimitTest() { int estimate = 0; // Set up initial login etc SocialPlusClient client = new SocialPlusClient(TestConstants.ServerApiBaseUrl); // Start a clock var watch = Stopwatch.StartNew(); // Fire 2000 calls in parallel Func <Task <GetBuildInfoResponse> > getBuildInfoFunc = () => client.Config.GetBuildInfoAsync(); await ConcurrentCalls <GetBuildInfoResponse> .FireInParallel(getBuildInfoFunc, 2000); estimate += 2000; // Continue to fire calls in parallel, 1K at a time, up to 8K additional calls, or 1 minute whatever takes longer try { // up to 8 more parallel batches for (int i = 0; i < 8; i += 1) { watch.Stop(); if (watch.ElapsedMilliseconds > 1000 * 60) { Console.WriteLine("The rate limit estimate is unavailable but is higher than " + estimate + " reqs/minute"); return; } watch.Start(); await ConcurrentCalls <GetBuildInfoResponse> .FireInParallel(getBuildInfoFunc, 1000); estimate += 1000; } } catch (Exception e) { // Check that this is a 429 error, by scanning the exception's message for the string '429' Assert.IsTrue(e.Message.Contains("429")); Console.WriteLine("The rate limit estimate is " + estimate + " reqs/minute"); return; } Console.WriteLine("The rate limit estimate is unavailable but is higher than " + estimate + " reqs/minute"); }