Skip to content

Commit

Permalink
Merge pull request #126 from NicolasConstant/develop
Browse files Browse the repository at this point in the history
0.19.1 PR
  • Loading branch information
NicolasConstant authored Nov 23, 2021
2 parents a21b493 + 5ef8af4 commit e21eacd
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using BirdsiteLive.Common.Settings;
Expand Down Expand Up @@ -57,6 +58,7 @@ private async Task AnalyseFailingUserAsync(SyncTwitterUser user)
{
var dbUser = await _twitterUserDal.GetTwitterUserAsync(user.Acct);
dbUser.FetchingErrorCount++;
dbUser.LastSync = DateTime.UtcNow;

if (dbUser.FetchingErrorCount > _instanceSettings.FailingTwitterUserCleanUpThreshold)
{
Expand Down
2 changes: 1 addition & 1 deletion src/BirdsiteLive/BirdsiteLive.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
<UserSecretsId>d21486de-a812-47eb-a419-05682bb68856</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<Version>0.19.0</Version>
<Version>0.19.1</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public async Task<int> GetFailingTwitterUsersCountAsync()

public async Task<SyncTwitterUser[]> GetAllTwitterUsersAsync(int maxNumber)
{
var query = $"SELECT * FROM {_settings.TwitterUserTableName} ORDER BY lastSync ASC LIMIT @maxNumber";
var query = $"SELECT * FROM {_settings.TwitterUserTableName} ORDER BY lastSync ASC NULLS FIRST LIMIT @maxNumber";

using (var dbConnection = Connection)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using System.Xml;
using BirdsiteLive.DAL.Postgres.DataAccessLayers;
Expand Down Expand Up @@ -231,6 +232,39 @@ public async Task GetAllTwitterUsers_Top()
Assert.IsFalse(result[0].LastTweetSynchronizedForAllFollowersId == default);
}

[TestMethod]
public async Task GetAllTwitterUsers_Top_NotInit()
{
// Create accounts
var dal = new TwitterUserPostgresDal(_settings);
for (var i = 0; i < 1000; i++)
{
var acct = $"myid{i}";
var lastTweetId = i+10;

await dal.CreateTwitterUserAsync(acct, lastTweetId);
}

// Update accounts
var now = DateTime.UtcNow;
var allUsers = await dal.GetAllTwitterUsersAsync();
foreach (var acc in allUsers)
{
var lastSync = now.AddDays(acc.LastTweetPostedId);
acc.LastSync = lastSync;
await dal.UpdateTwitterUserAsync(acc);
}

// Create a not init account
await dal.CreateTwitterUserAsync("not_init", -1);

var result = await dal.GetAllTwitterUsersAsync(10);

Assert.IsTrue(result.Any(x => x.Acct == "myid0"));
Assert.IsTrue(result.Any(x => x.Acct == "myid8"));
Assert.IsTrue(result.Any(x => x.Acct == "not_init"));
}

[TestMethod]
public async Task GetAllTwitterUsers_Limited()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,5 +329,64 @@ public async Task ProcessAsync_Protected_Test()
removeTwitterAccountActionMock.VerifyAll();
#endregion
}

[TestMethod]
public async Task ProcessAsync_Unfound_NotInit_Test()
{
#region Stubs
var userId1 = 1;
var acct1 = "user1";

var users = new List<SyncTwitterUser>
{
new SyncTwitterUser
{
Id = userId1,
Acct = acct1,
LastSync = default,
LastTweetPostedId = -1,
LastTweetSynchronizedForAllFollowersId = -1
}
};

var settings = new InstanceSettings
{
FailingTwitterUserCleanUpThreshold = 300
};
#endregion

#region Mocks
var twitterUserServiceMock = new Mock<ICachedTwitterUserService>(MockBehavior.Strict);
twitterUserServiceMock
.Setup(x => x.GetUser(It.Is<string>(y => y == acct1)))
.Returns((TwitterUser)null);

twitterUserServiceMock
.Setup(x => x.PurgeUser(It.Is<string>(y => y == acct1)));

var twitterUserDalMock = new Mock<ITwitterUserDal>(MockBehavior.Strict);
twitterUserDalMock
.Setup(x => x.GetTwitterUserAsync(It.Is<string>(y => y == acct1)))
.ReturnsAsync(users.First());

twitterUserDalMock
.Setup(x => x.UpdateTwitterUserAsync(
It.Is<SyncTwitterUser>(y => y.Id == userId1 && y.FetchingErrorCount == 1 && y.LastSync != default)))
.Returns(Task.CompletedTask);

var removeTwitterAccountActionMock = new Mock<IRemoveTwitterAccountAction>(MockBehavior.Strict);
#endregion

var processor = new RefreshTwitterUserStatusProcessor(twitterUserServiceMock.Object, twitterUserDalMock.Object, removeTwitterAccountActionMock.Object, settings);
var result = await processor.ProcessAsync(users.ToArray(), CancellationToken.None);

#region Validations
Assert.AreEqual(0, result.Length);

twitterUserServiceMock.VerifyAll();
twitterUserDalMock.VerifyAll();
removeTwitterAccountActionMock.VerifyAll();
#endregion
}
}
}

0 comments on commit e21eacd

Please sign in to comment.