Skip to content

Commit

Permalink
Merge pull request #344 from opentween/ratelimit-status
Browse files Browse the repository at this point in the history
MyCommon.TwitterApiStatusを廃止しアカウントごとにレートリミットを把握できるようにする
  • Loading branch information
upsilon authored May 22, 2024
2 parents 9c81949 + f2ae968 commit a7990b2
Show file tree
Hide file tree
Showing 21 changed files with 529 additions and 670 deletions.
73 changes: 73 additions & 0 deletions OpenTween.Tests/Api/RateLimitCollectionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// OpenTween - Client of Twitter
// Copyright (c) 2013 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
// All rights reserved.
//
// This file is part of OpenTween.
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program. If not, see <http://www.gnu.org/licenses/>, or write to
// the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
// Boston, MA 02110-1301, USA.

using Xunit;

namespace OpenTween.Api
{
public class RateLimitCollectionTest
{
[Fact]
public void SubscribeAccessLimitUpdated_Test()
{
var collection = new RateLimitCollection();
var count = 0;

void Handler(RateLimitCollection sender, RateLimitCollection.AccessLimitUpdatedEventArgs e)
{
Assert.Same(collection, sender);
Assert.Equal("/statuses/home_timeline", e.EndpointName);
count++;
}

using (collection.SubscribeAccessLimitUpdated(Handler))
{
collection["/statuses/home_timeline"] = new(150, 100, new(2013, 1, 1, 0, 0, 0));
Assert.Equal(1, count);
}

collection["/statuses/home_timeline"] = new(150, 99, new(2013, 1, 1, 0, 0, 0));
Assert.Equal(1, count); // 変化しない
}

[Fact]
public void SubscribeAccessLimitUpdated_ClearTest()
{
var collection = new RateLimitCollection();
collection["/statuses/home_timeline"] = new(150, 100, new(2013, 1, 1, 0, 0, 0));

var count = 0;

void Handler(RateLimitCollection sender, RateLimitCollection.AccessLimitUpdatedEventArgs e)
{
Assert.Same(collection, sender);
Assert.Null(e.EndpointName);
count++;
}

using (collection.SubscribeAccessLimitUpdated(Handler))
{
collection.Clear();
Assert.Equal(1, count);
}
}
}
}
279 changes: 0 additions & 279 deletions OpenTween.Tests/Api/TwitterApiStatusTest.cs

This file was deleted.

11 changes: 4 additions & 7 deletions OpenTween.Tests/Connection/TwitterApiConnectionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
using Moq;
using OpenTween.Api;
using OpenTween.Api.DataModel;
using OpenTween.SocialProtocol.Twitter;
using Xunit;

namespace OpenTween.Connection
Expand Down Expand Up @@ -98,9 +99,10 @@ public async Task SendAsync_Test()
[Fact]
public async Task SendAsync_UpdateRateLimitTest()
{
var accountState = new TwitterAccountState();
using var mockHandler = new HttpMessageHandlerMock();
using var http = new HttpClient(mockHandler);
using var apiConnection = new TwitterApiConnection();
using var apiConnection = new TwitterApiConnection(new TwitterCredentialNone(), accountState);
apiConnection.Http = http;

mockHandler.Enqueue(x =>
Expand All @@ -116,15 +118,11 @@ public async Task SendAsync_UpdateRateLimitTest()
{ "X-Rate-Limit-Limit", "150" },
{ "X-Rate-Limit-Remaining", "100" },
{ "X-Rate-Limit-Reset", "1356998400" },
{ "X-Access-Level", "read-write-directmessages" },
},
Content = new StringContent("\"hogehoge\""),
};
});

var apiStatus = new TwitterApiStatus();
MyCommon.TwitterApiInfo = apiStatus;

var request = new GetRequest
{
RequestUri = new("hoge/tetete.json", UriKind.Relative),
Expand All @@ -133,8 +131,7 @@ public async Task SendAsync_UpdateRateLimitTest()

using var response = await apiConnection.SendAsync(request);

Assert.Equal(TwitterApiAccessLevel.ReadWriteAndDirectMessage, apiStatus.AccessLevel);
Assert.Equal(new ApiLimit(150, 100, new DateTimeUtc(2013, 1, 1, 0, 0, 0)), apiStatus.AccessLimit["/hoge/tetete"]);
Assert.Equal(new(150, 100, new DateTimeUtc(2013, 1, 1, 0, 0, 0)), accountState.RateLimits["/hoge/tetete"]);

Assert.Equal(0, mockHandler.QueueCount);
}
Expand Down
Loading

0 comments on commit a7990b2

Please sign in to comment.